text:
/**
 * Copyright (c) 2011-2017 libbitcoin developers (see AUTHORS)
 *
 * This file is part of libbitcoin.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include <boost/test/unit_test.hpp>

#include <vector>
#include <bitcoin/bitcoin.hpp>

using namespace bc;

BOOST_AUTO_TEST_SUITE(collection_tests)

typedef std::vector<uint8_t> collection;

// distinct

BOOST_AUTO_TEST_CASE(collection__distinct__empty__same)
{
    collection parameter;
    const auto& result = distinct(parameter);
    BOOST_REQUIRE(parameter.empty());
    BOOST_REQUIRE(&result == &parameter);
}

BOOST_AUTO_TEST_CASE(collection__distinct__single__match)
{
    const uint8_t expected = 42;
    collection set{ expected };
    const auto& result = distinct(set);
    BOOST_REQUIRE_EQUAL(result.size(), 1u);
    BOOST_REQUIRE_EQUAL(result[0], expected);
}

BOOST_AUTO_TEST_CASE(collection__distinct__distinct_sorted__sorted)
{
    collection set{ 0, 2, 4, 6, 8 };
    const auto& result = distinct(set);
    BOOST_REQUIRE_EQUAL(result.size(), 5u);
    BOOST_REQUIRE_EQUAL(result[0], 0u);
    BOOST_REQUIRE_EQUAL(result[1], 2u);
    BOOST_REQUIRE_EQUAL(result[2], 4u);
    BOOST_REQUIRE_EQUAL(result[3], 6u);
    BOOST_REQUIRE_EQUAL(result[4], 8u);
}

BOOST_AUTO_TEST_CASE(collection__distinct__distinct_unsorted__sorted)
{
    collection set{ 2, 0, 8, 6, 4 };
    const auto& result = distinct(set);
    BOOST_REQUIRE_EQUAL(result.size(), 5u);
    BOOST_REQUIRE_EQUAL(result[0], 0u);
    BOOST_REQUIRE_EQUAL(result[1], 2u);
    BOOST_REQUIRE_EQUAL(result[2], 4u);
    BOOST_REQUIRE_EQUAL(result[3], 6u);
    BOOST_REQUIRE_EQUAL(result[4], 8u);
}

BOOST_AUTO_TEST_CASE(collection__distinct__distinct_unsorted_duplicates__sorted_distinct)
{
    collection set{ 2, 0, 0, 8, 6, 4 };
    const auto& result = distinct(set);
    BOOST_REQUIRE_EQUAL(result.size(), 5u);
    BOOST_REQUIRE_EQUAL(result[0], 0u);
    BOOST_REQUIRE_EQUAL(result[1], 2u);
    BOOST_REQUIRE_EQUAL(result[2], 4u);
    BOOST_REQUIRE_EQUAL(result[3], 6u);
    BOOST_REQUIRE_EQUAL(result[4], 8u);
}

// move_append

BOOST_AUTO_TEST_CASE(collection__move_append__both_empty__both_empty)
{
    collection source;
    collection target;
    move_append(target, source);
    BOOST_REQUIRE_EQUAL(source.size(), 0u);
    BOOST_REQUIRE_EQUAL(target.size(), 0u);
}

BOOST_AUTO_TEST_CASE(collection__move_append__source_empty__both_unchanged)
{
    collection source;
    collection target{ 0, 2, 4, 6, 8 };
    const auto expected = target.size();
    move_append(target, source);
    BOOST_REQUIRE_EQUAL(source.size(), 0u);
    BOOST_REQUIRE_EQUAL(target.size(), expected);
    BOOST_REQUIRE_EQUAL(target[0], 0u);
    BOOST_REQUIRE_EQUAL(target[1], 2u);
    BOOST_REQUIRE_EQUAL(target[2], 4u);
    BOOST_REQUIRE_EQUAL(target[3], 6u);
    BOOST_REQUIRE_EQUAL(target[4], 8u);
}

BOOST_AUTO_TEST_CASE(collection__move_append__target_empty__swapped_values)
{
    collection source{ 0, 2, 4, 6, 8 };
    collection target;
    const auto expected = source.size();
    move_append(target, source);
    BOOST_REQUIRE_EQUAL(source.size(), 0u);
    BOOST_REQUIRE_EQUAL(target.size(), expected);
    BOOST_REQUIRE_EQUAL(target[0], 0u);
    BOOST_REQUIRE_EQUAL(target[1], 2u);
    BOOST_REQUIRE_EQUAL(target[2], 4u);
    BOOST_REQUIRE_EQUAL(target[3], 6u);
    BOOST_REQUIRE_EQUAL(target[4], 8u);
}

BOOST_AUTO_TEST_CASE(collection__move_append__neither_empty__moved_in_order)
{
    collection source{ 10, 12, 14, 16, 18 };
    collection target{ 0, 2, 4, 6, 8 };
    const auto expected = source.size() + source.size();
    move_append(target, source);
    BOOST_REQUIRE_EQUAL(source.size(), 0u);
    BOOST_REQUIRE_EQUAL(target.size(), expected);
    BOOST_REQUIRE_EQUAL(target[0], 0u);
    BOOST_REQUIRE_EQUAL(target[1], 2u);
    BOOST_REQUIRE_EQUAL(target[2], 4u);
    BOOST_REQUIRE_EQUAL(target[3], 6u);
    BOOST_REQUIRE_EQUAL(target[4], 8u);
    BOOST_REQUIRE_EQUAL(target[5], 10u);
    BOOST_REQUIRE_EQUAL(target[6], 12u);
    BOOST_REQUIRE_EQUAL(target[7], 14u);
    BOOST_REQUIRE_EQUAL(target[8], 16u);
    BOOST_REQUIRE_EQUAL(target[9], 18u);
}

BOOST_AUTO_TEST_CASE(collection__pop__single__empty_and_returns_expected)
{
    const uint8_t expected = 42u;
    collection stack{ expected };
    const auto value = pop(stack);
    BOOST_REQUIRE(stack.empty());
    BOOST_REQUIRE_EQUAL(value, expected);
}

BOOST_AUTO_TEST_CASE(collection__pop__multiple__popped_and_returns_expected)
{
    const uint8_t expected = 42u;
    collection stack{ 0, 1, 2, 3, expected };
    const auto value = pop(stack);
    BOOST_REQUIRE_EQUAL(stack.size(), 4u);
    BOOST_REQUIRE_EQUAL(stack[0], 0u);
    BOOST_REQUIRE_EQUAL(stack[1], 1u);
    BOOST_REQUIRE_EQUAL(stack[2], 2u);
    BOOST_REQUIRE_EQUAL(stack[3], 3u);
    BOOST_REQUIRE_EQUAL(value, expected);
}

BOOST_AUTO_TEST_SUITE_END()

ext: cpp


text:
#include <sm/logging/StdOutLogger.hpp>
#include <boost/cstdint.hpp>
#include <sstream>
#ifdef WIN32
#include <windows.h>
#endif
#include <sys/time.h>
#include <iostream>
#include <sm/logging/LoggingEvent.hpp>

namespace sm {
    namespace logging {

        StdOutLogger::StdOutLogger(){}
        StdOutLogger::~StdOutLogger(){}

        void StdOutLogger::logImplementation(const LoggingEvent & event)
        {
            formatter.print(event,std::cout);

        }

    } // namespace logging
} // namespace sm

max_stars_repo_name: PushyamiKaveti/kalibr

max_issues_repo_name: PushyamiKaveti/kalibr

max_forks_repo_name: PushyamiKaveti/kalibr


text:
#pragma once

#include <filesystem>
#include <fstream>

#include <boost/asio/buffer.hpp>
#include <boost/beast/core/bind_handler.hpp>
#include <boost/beast/core/buffers_to_string.hpp>

namespace malloy
{
    using boost::asio::buffer;
    using boost::beast::buffers_to_string;
    using boost::beast::bind_front_handler;

    /**
     * Returns an std::string which represents the raw bytes of the file.
     *
     * @param path The path to the file.
     * @return The content of the file as it resides on the disk - byte by byte.
     */
    [[nodiscard]]
    static inline std::string file_contents(const std::filesystem::path& path)
    {
        // Sanity check
        if (!std::filesystem::is_regular_file(path))
            return { };

        // Open the file
        // Note that we have to use binary mode as we want to return a string
        // representing matching the bytes of the file on the file system.
        std::ifstream file(path, std::ios::in | std::ios::binary);
        if (!file.is_open())
            return { };

        // Read contents
        const std::size_t& size = std::filesystem::file_size(path);
        std::string content(size, '\0');
        file.read(content.data(), size);

        // Close the file
        file.close();

        return content;
    }

    /**
     * Convert a hex value to a decimal value.
     *
     * @param c The hexadecimal input.
     * @return The decimal output.
     */
    [[nodiscard]]
    static inline uint8_t hex2dec(uint8_t c)
    {
        if (c >= '0' && c <= '9') {
            c -= '0';
        }
        else if (c >= 'a' && c <= 'f') {
            c -= 'a' - 10;
        }
        else if (c >= 'A' && c <= 'F') {
            c -= 'A' - 10;
        }
        return c;
    }

    /**
     * Splits a string.
     *
     * @note Passing an empty string will result in the return container containing an empty string.
     * @note Passing an empty string as the delimiter returns { str }.
     * @note Leading and trailing delimiters will be ignored.
     *
     * @param str The string to split.
     * @param delimiter The delimiter.
     * @return The string split into the corresponding parts.
     */
    [[nodiscard]]
    inline
    std::vector<std::string_view>
    split(std::string_view str, std::string_view delimiter)
    {
        // Sanity check str
        if (str.empty())
            return { };

        // Sanity check delimiter
        if (delimiter.empty())
            return { str };

        // Split
        std::vector<std::string_view> parts;
        std::string_view::size_type pos = 0;
        while (pos != std::string_view::npos) {
            // Look for substring
            const auto pos_found = str.find(delimiter, pos);

            // Drop leading delimiters
            if (pos_found == 0) {
                pos += delimiter.size();
                continue;
            }

            // Capture string
            parts.emplace_back(str.substr(pos, pos_found-pos));

            // Drop trailing delimiters
            if (pos_found + delimiter.size() >= str.size())
                break;

            // Move on
            if (pos_found == std::string_view::npos)
                break;
            pos = pos_found + delimiter.size();
        }

        return parts;
    }

    /**
     * Decodes an URL.
     *
     * @details This function replaces %<hex> with the corresponding characters.
     *          See https://en.wikipedia.org/wiki/Percent-encoding
     *
     * @note As the replaced characters are "shorter" than the original input we can perform the
     *       replacement in-place as long as we're somewhat careful not to fuck up.
     *
     * @param str The string to decode.
     */
    static inline void url_decode(std::string& str)
    {
        size_t w = 0;
        for (size_t r = 0 ; r < str.size() ; ++r) {
            uint8_t v = str[r];
            if (str[r] == '%') {
                v = hex2dec(str[++r]) << 4;
                v |= hex2dec(str[++r]);
            }
            str[w++] = v;
        }
        str.resize(w);
    }

    /**
     * Attempts to return a suitable MIME-TYPE for a provided file path
     *
     * @param path The file path.
     * @return The corresponding MIME-TYPE.
     */
    static inline std::string_view mime_type(const std::filesystem::path& path)
    {
        // Extract file extension
        const std::filesystem::path& ext = path.extension();

        if (ext == ".htm")  return "text/html";
        if (ext == ".html") return "text/html";
        if (ext == ".php")  return "text/html";
        if (ext == ".css")  return "text/css";
        if (ext == ".txt")  return "text/plain";
        if (ext == ".js")   return "application/javascript";
        if (ext == ".json") return "application/json";
        if (ext == ".xml")  return "application/xml";
        if (ext == ".swf")  return "application/x-shockwave-flash";
        if (ext == ".flv")  return "video/x-flv";
        if (ext == ".png")  return "image/png";
        if (ext == ".jpe")  return "image/jpeg";
        if (ext == ".jpeg") return "image/jpeg";
        if (ext == ".jpg")  return "image/jpeg";
        if (ext == ".gif")  return "image/gif";
        if (ext == ".bmp")  return "image/bmp";
        if (ext == ".ico")  return "image/vnd.microsoft.icon";
        if (ext == ".tiff") return "image/tiff";
        if (ext == ".tif")  return "image/tiff";
        if (ext == ".svg")  return "image/svg+xml";
        if (ext == ".svgz") return "image/svg+xml";

        return "application/text";
    }

}

ext: hpp


text:
#ifndef ASSOCIATIVE_UTIL_HPP
#define ASSOCIATIVE_UTIL_HPP

#ifndef BOOST_FILESYSTEM_VERSION
#define BOOST_FILESYSTEM_VERSION 3
#elif BOOST_FILESYSTEM_VERSION != 3
#error wrong BOOST_FILESYSTEM_VERSION
#endif

#include <unordered_map>
#include <string>
#include <iterator>
#include <algorithm>

#include <boost/format.hpp>
#include <boost/filesystem.hpp>
#include <boost/optional.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/program_options.hpp>
#include <boost/logic/tribool.hpp>
#include <boost/smart_ptr/shared_ptr.hpp>
#include <boost/smart_ptr/weak_ptr.hpp>
#include <boost/tuple/tuple.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/lambda/lambda.hpp>
#include <boost/interprocess/interprocess_fwd.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>

#include "exception.hpp"

namespace fs = boost::filesystem3;
namespace po = boost::program_options;
namespace ba = boost::algorithm;
namespace lambda = boost::lambda;
namespace bl = boost::logic;
namespace bt = boost::tuples;
namespace bi = boost::interprocess;
namespace bpt = boost::posix_time;

namespace associative
{

	template<typename T>
	class WeakPtr
	{
	private:
		boost::weak_ptr<T> ptr;

	public:
		WeakPtr(const boost::weak_ptr<T>& ptr)
		: ptr(ptr)
		{
		}

		WeakPtr(const boost::shared_ptr<T>& ptr)
		: ptr(ptr)
		{
		}

		bool isDefined()
		{
			return !ptr.expired();
		}

		T& operator*()
		{
			return *ptr.lock();
		}

		T* operator->()
		{
			return get();
		}

		T* get()
		{
			return ptr.lock().get();
		}
	};

	template<typename T>
	class Buffer
	{
	private:
		std::unordered_map<std::string, T> buffer;

	public:
		template<typename Func>
		inline T getOrElse(const std::string& key, const Func& func)
		{
			if (containsKey(buffer, key))
				return buffer[key];
			else
				return buffer[key] = func();
		}

		inline boost::optional<T> option(const std::string& key)
		{
			if (containsKey(buffer, key))
				return boost::make_optional(buffer[key]);
			else
				return boost::none;
		}

		inline void set(const std::string& key, const T& value)
		{
			buffer[key] = value;
		}

		inline void reset(const std::string& key)
		{
			if (containsKey(buffer, key))
				buffer.erase(key);
		}

		std::set<std::string> getKeys()
		{
			std::set<std::string> keys;
			for (auto iter = buffer.begin(); iter != buffer.end(); ++iter)
				keys.insert(iter->first);
			return keys;
		}

		inline void clear()
		{
			buffer.clear();
		}

	};

	std::vector<std::string> parseArguments(const std::string& str);

	template<typename To>
	class LexicalCast
	{
	public:
		template<typename From>
		To operator()(const From& from) const
		{
			return boost::lexical_cast<To, From>(from);
		}

		To operator()(const To& to) const
		{
			return to;
		}
	};

	template<typename Coll, typename Key>
	bool containsKey(const Coll& coll, const Key& key)
	{
		return coll.find(key) != coll.end();
	}

	template<typename T, typename _CharT = char>
	std::basic_string<_CharT> toString(const T& value)
	{
		return boost::lexical_cast<std::basic_string<_CharT> >(value);
	}

	template<typename T, typename Iter, typename Func, typename Head, typename... Tail>
	inline void _mapAll(const Func& func, Iter output, const Head& head, const Tail&... tail)
	{
		*output = func(head);
		_mapAll<T>(func, ++output, tail...);
	}

	template<typename T, typename Iter, typename Func>
	inline void _mapAll(const Func&, Iter)
	{
		// intentionally left blank
	}

	template<typename T, typename Coll = std::vector<T>, typename Func, typename... Args>
	Coll mapAll(const Func& func, const Args&... args)
	{
		Coll coll;
		auto inserter = std::back_inserter(coll);
		_mapAll<T>(func, inserter, args...);
		return coll;
	}

	template<typename T = std::string, typename Coll = std::vector<T>, typename... Args>
	Coll convertAll(const Args&... args)
	{
		return mapAll<T, Coll>(LexicalCast<T>(), args...);
	}

	template<typename Coll, typename Func>
	void forEach(Coll& coll, const Func& func)
	{
		std::for_each(coll.begin(), coll.end(), func);
	}

	template<typename Coll, typename Func>
	void forEach(const Coll& coll, const Func& func)
	{
		std::for_each(coll.begin(), coll.end(), func);
	}

	template<typename Coll, typename OIter, typename Func>
	void filter(const Coll& coll, OIter copyTo, const Func& predicate)
	{
		for (auto iter = coll.begin(); iter != coll.end(); ++iter)
			if (predicate(*iter))
			{
				*copyTo = *iter;
				++copyTo;
			}
	}

	template<typename T>
	class Hash
	{
	public:
		size_t operator()(const T& t) const;
	};

	template<typename P>
	class Hash<boost::shared_ptr<P> >
	{
	public:
		std::size_t operator()(const boost::shared_ptr<P>& p) const
		{
			return std::hash<P*>()(p.get());
		}
	};

	std::string fnv1a(const std::string& str);

}

#endif

max_stars_repo_name: larsrh/associative

max_issues_repo_name: larsrh/associative

max_forks_repo_name: larsrh/associative


text:
#include "SMPSInput.hpp"
#include "CbcLagrangeSolver.hpp"
#include "CbcRecourseSolver.hpp"
#include "combineScenarios.hpp"
#include "lagrangeRootNode.hpp"
#include <boost/scoped_ptr.hpp>

using namespace std;
using boost::scoped_ptr;

int main(int argc, char **argv) {

	MPI_Init(&argc, &argv);

	int mype;
	MPI_Comm_rank(MPI_COMM_WORLD,&mype);

	if (argc != 3) {
		if (mype == 0) printf("Usage: %s [SMPS root name] [number per subproblem]\n",argv[0]);
		return 1;
	}

	string smpsrootname(argv[1]);
	int nper = atoi(argv[2]);

	SMPSInput input(smpsrootname+".cor",smpsrootname+".tim",smpsrootname+".sto");
	combinedInput in = combineScenarios(input,nper,true);

	lagrangeRootNode<CbcLagrangeSolver,CbcRecourseSolver>(in);

	MPI_Finalize();

	return 0;

}

max_stars_repo_name: jalving/PIPS

max_issues_repo_name: jalving/PIPS

max_forks_repo_name: jalving/PIPS


text:
/*! \file
    \brief A storage.

    Copyright (C) 2019-2022 kaoru  https://www.tetengo.org/
*/

#include <cstdint>
#include <filesystem>
#include <iterator>
#include <memory>
#include <optional>
#include <string>
#include <string_view>
#include <unordered_map>
#include <utility>
#include <variant>
#include <vector>

#include <stddef.h>

#include <boost/preprocessor.hpp>
#include <boost/scope_exit.hpp>
#include <boost/test/unit_test.hpp>

#include <tetengo/property/storage.h>
#include <tetengo/property/storage.hpp>

namespace
{
    class concrete_storage : public tetengo::property::storage
    {
    public:
        concrete_storage() : tetengo::property::storage{ value_map_type{} } {}

        virtual ~concrete_storage() = default;

        bool call_value_map() const
        {
            const auto& map = value_map();
            const auto  key = std::filesystem::path{ "hoge" } / "fuga";
            const auto  found = map.find(key.string());
            return found != std::end(map) && std::get<std::uint32_t>(found->second) == 42;
        }

    private:
        virtual void save_impl() const override {}
    };

    class concrete_storage_loader : public tetengo::property::storage_loader
    {
    public:
        virtual ~concrete_storage_loader() = default;

    private:
        virtual std::unique_ptr<tetengo::property::storage>
        load_impl(const std::filesystem::path& /*path*/) const override
        {
            return std::make_unique<concrete_storage>();
        }
    };

}

BOOST_AUTO_TEST_SUITE(test_tetengo)
BOOST_AUTO_TEST_SUITE(property)
BOOST_AUTO_TEST_SUITE(storage)

BOOST_AUTO_TEST_CASE(get_bool)
{
    BOOST_TEST_PASSPOINT();

    {
        const concrete_storage storage{};
        const auto             key = std::filesystem::path{ "hoge" } / "fuga";
        BOOST_TEST(!storage.get_bool(key));
    }

    {
        const auto* const p_loader = tetengo_property_storageLoader_createMemoryStorageLoader();
        BOOST_SCOPE_EXIT(p_loader)
        {
            tetengo_property_storageLoader_destroy(p_loader);
        }
        BOOST_SCOPE_EXIT_END;

        const auto* const p_storage = tetengo_property_storageLoader_load(p_loader, "test_tetengo.property.storage");
        BOOST_SCOPE_EXIT(p_storage)
        {
            tetengo_property_storage_destroy(p_storage);
        }
        BOOST_SCOPE_EXIT_END;
        BOOST_TEST_REQUIRE(p_storage);

        const auto key = std::filesystem::path{ "hoge" } / "fuga";
        auto       value = false;
        BOOST_TEST(!tetengo_property_storage_getBool(p_storage, key.string().c_str(), &value));
    }
    {
        const auto key = std::filesystem::path{ "hoge" } / "fuga";
        auto       value = false;
        BOOST_TEST(!tetengo_property_storage_getBool(nullptr, key.string().c_str(), &value));
    }
    {
        const auto* const p_loader = tetengo_property_storageLoader_createMemoryStorageLoader();
        BOOST_SCOPE_EXIT(p_loader)
        {
            tetengo_property_storageLoader_destroy(p_loader);
        }
        BOOST_SCOPE_EXIT_END;

        const auto* const p_storage = tetengo_property_storageLoader_load(p_loader, "test_tetengo.property.storage");
        BOOST_SCOPE_EXIT(p_storage)
        {
            tetengo_property_storage_destroy(p_storage);
        }
        BOOST_SCOPE_EXIT_END;
        BOOST_TEST_REQUIRE(p_storage);

        auto value = false;
        BOOST_TEST(!tetengo_property_storage_getBool(p_storage, nullptr, &value));
    }
    {
        const auto* const p_loader = tetengo_property_storageLoader_createMemoryStorageLoader();
        BOOST_SCOPE_EXIT(p_loader)
        {
            tetengo_property_storageLoader_destroy(p_loader);
        }
        BOOST_SCOPE_EXIT_END;

        const auto* const p_storage = tetengo_property_storageLoader_load(p_loader, "test_tetengo.property.storage");
        BOOST_SCOPE_EXIT(p_storage)
        {
            tetengo_property_storage_destroy(p_storage);
        }
        BOOST_SCOPE_EXIT_END;
        BOOST_TEST_REQUIRE(p_storage);

        const auto key = std::filesystem::path{ "hoge" } / "fuga";
        BOOST_TEST(!tetengo_property_storage_getBool(p_storage, key.string().c_str(), nullptr));
    }
}

BOOST_AUTO_TEST_CASE(set_bool)
{
    BOOST_TEST_PASSPOINT();

    {
        concrete_storage storage{};
        const auto       key = std::filesystem::path{ "hoge" } / "fuga";
        storage.set_bool(key, false);
        const auto o_value = storage.get_bool(key);
        BOOST_REQUIRE(o_value);
        BOOST_TEST(!*o_value);

        BOOST_CHECK(!storage.get_string(key));
    }
    {
        concrete_storage storage{};
        const auto       key = std::filesystem::path{ "hoge" } / "fuga";
        storage.set_bool(key, true);
        const auto o_value = storage.get_bool(key);
        BOOST_REQUIRE(o_value);
        BOOST_TEST(*o_value);

        BOOST_CHECK(!storage.get_string(key));
    }

    {
        const auto* const p_loader = tetengo_property_storageLoader_createMemoryStorageLoader();
        BOOST_SCOPE_EXIT(p_loader)
        {
            tetengo_property_storageLoader_destroy(p_loader);
        }
        BOOST_SCOPE_EXIT_END;

        auto* const p_storage = tetengo_property_storageLoader_load(p_loader, "test_tetengo.property.storage");
        BOOST_SCOPE_EXIT(p_storage)
        {
            tetengo_property_storage_destroy(p_storage);
        }
        BOOST_SCOPE_EXIT_END;
        BOOST_TEST_REQUIRE(p_storage);

        const auto key = std::filesystem::path{ "hoge" } / "fuga";
        tetengo_property_storage_setBool(p_storage, key.string().c_str(), false);
        auto value = false;
        BOOST_TEST(tetengo_property_storage_getBool(p_storage, key.string().c_str(), &value));
        BOOST_TEST(!value);
    }
    {
        const auto* const p_loader = tetengo_property_storageLoader_createMemoryStorageLoader();
        BOOST_SCOPE_EXIT(p_loader)
        {
            tetengo_property_storageLoader_destroy(p_loader);
        }
        BOOST_SCOPE_EXIT_END;

        auto* const p_storage = tetengo_property_storageLoader_load(p_loader, "test_tetengo.property.storage");
        BOOST_SCOPE_EXIT(p_storage)
        {
            tetengo_property_storage_destroy(p_storage);
        }
        BOOST_SCOPE_EXIT_END;
        BOOST_TEST_REQUIRE(p_storage);

        const auto key = std::filesystem::path{ "hoge" } / "fuga";
        tetengo_property_storage_setBool(p_storage, key.string().c_str(), true);
        auto value = false;
        BOOST_TEST(tetengo_property_storage_getBool(p_storage, key.string().c_str(), &value));
        BOOST_TEST(value);
    }
    {
        const auto* const p_loader = tetengo_property_storageLoader_createMemoryStorageLoader();
        BOOST_SCOPE_EXIT(p_loader)
        {
            tetengo_property_storageLoader_destroy(p_loader);
        }
        BOOST_SCOPE_EXIT_END;

        auto* const p_storage = tetengo_property_storageLoader_load(p_loader, "test_tetengo.property.storage");
        BOOST_SCOPE_EXIT(p_storage)
        {
            tetengo_property_storage_destroy(p_storage);
        }
        BOOST_SCOPE_EXIT_END;
        BOOST_TEST_REQUIRE(p_storage);

        const auto key = std::filesystem::path{ "hoge" } / "fuga";
        tetengo_property_storage_setBool(nullptr, key.string().c_str(), true);
        auto value = false;
        BOOST_TEST(!tetengo_property_storage_getBool(p_storage, key.string().c_str(), &value));
    }
    {
        const auto* const p_loader = tetengo_property_storageLoader_createMemoryStorageLoader();
        BOOST_SCOPE_EXIT(p_loader)
        {
            tetengo_property_storageLoader_destroy(p_loader);
        }
        BOOST_SCOPE_EXIT_END;

        auto* const p_storage = tetengo_property_storageLoader_load(p_loader, "test_tetengo.property.storage");
        BOOST_SCOPE_EXIT(p_storage)
        {
            tetengo_property_storage_destroy(p_storage);
        }
        BOOST_SCOPE_EXIT_END;
        BOOST_TEST_REQUIRE(p_storage);

        const auto key = std::filesystem::path{ "hoge" } / "fuga";
        tetengo_property_storage_setBool(p_storage, nullptr, true);
        auto value = false;
        BOOST_TEST(!tetengo_property_storage_getBool(p_storage, key.string().c_str(), &value));
    }
}

BOOST_AUTO_TEST_CASE(get_uint32)
{
    BOOST_TEST_PASSPOINT();

    {
        const concrete_storage storage{};
        const auto             key = std::filesystem::path{ "hoge" } / "fuga";
        BOOST_TEST(!storage.get_uint32(key));
    }

    {
        const auto* const p_loader = tetengo_property_storageLoader_createMemoryStorageLoader();
        BOOST_SCOPE_EXIT(p_loader)
        {
            tetengo_property_storageLoader_destroy(p_loader);
        }
        BOOST_SCOPE_EXIT_END;

        const auto* const p_storage = tetengo_property_storageLoader_load(p_loader, "test_tetengo.property.storage");
        BOOST_SCOPE_EXIT(p_storage)
        {
            tetengo_property_storage_destroy(p_storage);
        }
        BOOST_SCOPE_EXIT_END;
        BOOST_TEST_REQUIRE(p_storage);

        const auto key = std::filesystem::path{ "hoge" } / "fuga";
        auto       value = static_cast<::uint32_t>(0);
        BOOST_TEST(!tetengo_property_storage_getUint32(p_storage, key.string().c_str(), &value));
    }
    {
        const auto key = std::filesystem::path{ "hoge" } / "fuga";
        auto       value = static_cast<::uint32_t>(0);
        BOOST_TEST(!tetengo_property_storage_getUint32(nullptr, key.string().c_str(), &value));
    }
    {
        const auto* const p_loader = tetengo_property_storageLoader_createMemoryStorageLoader();
        BOOST_SCOPE_EXIT(p_loader)
        {
            tetengo_property_storageLoader_destroy(p_loader);
        }
        BOOST_SCOPE_EXIT_END;

        const auto* const p_storage = tetengo_property_storageLoader_load(p_loader, "test_tetengo.property.storage");
        BOOST_SCOPE_EXIT(p_storage)
        {
            tetengo_property_storage_destroy(p_storage);
        }
        BOOST_SCOPE_EXIT_END;
        BOOST_TEST_REQUIRE(p_storage);

        auto value = static_cast<::uint32_t>(0);
        BOOST_TEST(!tetengo_property_storage_getUint32(p_storage, nullptr, &value));
    }
    {
        const auto* const p_loader = tetengo_property_storageLoader_createMemoryStorageLoader();
        BOOST_SCOPE_EXIT(p_loader)
        {
            tetengo_property_storageLoader_destroy(p_loader);
        }
        BOOST_SCOPE_EXIT_END;

        const auto* const p_storage = tetengo_property_storageLoader_load(p_loader, "test_tetengo.property.storage");
        BOOST_SCOPE_EXIT(p_storage)
        {
            tetengo_property_storage_destroy(p_storage);
        }
        BOOST_SCOPE_EXIT_END;
        BOOST_TEST_REQUIRE(p_storage);

        const auto key = std::filesystem::path{ "hoge" } / "fuga";
        BOOST_TEST(!tetengo_property_storage_getUint32(p_storage, key.string().c_str(), nullptr));
    }
}

BOOST_AUTO_TEST_CASE(set_uint32)
{
    BOOST_TEST_PASSPOINT();

    {
        concrete_storage storage{};
        const auto       key = std::filesystem::path{ "hoge" } / "fuga";
        storage.set_uint32(key, 42);
        const auto o_value = storage.get_uint32(key);
        BOOST_REQUIRE(o_value);
        BOOST_TEST(*o_value == 42U);

        BOOST_CHECK(!storage.get_string(key));
    }

    {
        const auto* const p_loader = tetengo_property_storageLoader_createMemoryStorageLoader();
        BOOST_SCOPE_EXIT(p_loader)
        {
            tetengo_property_storageLoader_destroy(p_loader);
        }
        BOOST_SCOPE_EXIT_END;

        auto* const p_storage = tetengo_property_storageLoader_load(p_loader, "test_tetengo.property.storage");
        BOOST_SCOPE_EXIT(p_storage)
        {
            tetengo_property_storage_destroy(p_storage);
        }
        BOOST_SCOPE_EXIT_END;
        BOOST_TEST_REQUIRE(p_storage);

        const auto key = std::filesystem::path{ "hoge" } / "fuga";
        tetengo_property_storage_setUint32(p_storage, key.string().c_str(), 42);
        auto value = static_cast<::uint32_t>(0);
        BOOST_TEST(tetengo_property_storage_getUint32(p_storage, key.string().c_str(), &value));
        BOOST_TEST(value == 42U);
    }
    {
        const auto* const p_loader = tetengo_property_storageLoader_createMemoryStorageLoader();
        BOOST_SCOPE_EXIT(p_loader)
        {
            tetengo_property_storageLoader_destroy(p_loader);
        }
        BOOST_SCOPE_EXIT_END;

        auto* const p_storage = tetengo_property_storageLoader_load(p_loader, "test_tetengo.property.storage");
        BOOST_SCOPE_EXIT(p_storage)
        {
            tetengo_property_storage_destroy(p_storage);
        }
        BOOST_SCOPE_EXIT_END;
        BOOST_TEST_REQUIRE(p_storage);

        const auto key = std::filesystem::path{ "hoge" } / "fuga";
        tetengo_property_storage_setUint32(nullptr, key.string().c_str(), 1);
        auto value = static_cast<::uint32_t>(0);
        BOOST_TEST(!tetengo_property_storage_getUint32(p_storage, key.string().c_str(), &value));
    }
    {
        const auto* const p_loader = tetengo_property_storageLoader_createMemoryStorageLoader();
        BOOST_SCOPE_EXIT(p_loader)
        {
            tetengo_property_storageLoader_destroy(p_loader);
        }
        BOOST_SCOPE_EXIT_END;

        auto* const p_storage = tetengo_property_storageLoader_load(p_loader, "test_tetengo.property.storage");
        BOOST_SCOPE_EXIT(p_storage)
        {
            tetengo_property_storage_destroy(p_storage);
        }
        BOOST_SCOPE_EXIT_END;
        BOOST_TEST_REQUIRE(p_storage);

        const auto key = std::filesystem::path{ "hoge" } / "fuga";
        tetengo_property_storage_setUint32(p_storage, nullptr, 1);
        auto value = static_cast<::uint32_t>(0);
        BOOST_TEST(!tetengo_property_storage_getUint32(p_storage, key.string().c_str(), &value));
    }
}

BOOST_AUTO_TEST_CASE(get_string)
{
    BOOST_TEST_PASSPOINT();

    {
        const concrete_storage storage{};
        const auto             key = std::filesystem::path{ "hoge" } / "fuga";
        BOOST_TEST(!storage.get_string(key));
    }

    {
        const auto* const p_loader = tetengo_property_storageLoader_createMemoryStorageLoader();
        BOOST_SCOPE_EXIT(p_loader)
        {
            tetengo_property_storageLoader_destroy(p_loader);
        }
        BOOST_SCOPE_EXIT_END;

        const auto* const p_storage = tetengo_property_storageLoader_load(p_loader, "test_tetengo.property.storage");
        BOOST_SCOPE_EXIT(p_storage)
        {
            tetengo_property_storage_destroy(p_storage);
        }
        BOOST_SCOPE_EXIT_END;
        BOOST_TEST_REQUIRE(p_storage);

        const auto key = std::filesystem::path{ "hoge" } / "fuga";
        BOOST_TEST(
            tetengo_property_storage_getString(p_storage, key.string().c_str(), nullptr, 0) == static_cast<size_t>(-1));
    }
    {
        const auto key = std::filesystem::path{ "hoge" } / "fuga";
        BOOST_TEST(
            tetengo_property_storage_getString(nullptr, key.string().c_str(), nullptr, 0) == static_cast<size_t>(-1));
    }
    {
        const auto* const p_loader = tetengo_property_storageLoader_createMemoryStorageLoader();
        BOOST_SCOPE_EXIT(p_loader)
        {
            tetengo_property_storageLoader_destroy(p_loader);
        }
        BOOST_SCOPE_EXIT_END;

        const auto* const p_storage = tetengo_property_storageLoader_load(p_loader, "test_tetengo.property.storage");
        BOOST_SCOPE_EXIT(p_storage)
        {
            tetengo_property_storage_destroy(p_storage);
        }
        BOOST_SCOPE_EXIT_END;
        BOOST_TEST_REQUIRE(p_storage);

        BOOST_TEST(tetengo_property_storage_getString(p_storage, nullptr, nullptr, 0) == static_cast<size_t>(-1));
    }
}

BOOST_AUTO_TEST_CASE(set_string)
{
    BOOST_TEST_PASSPOINT();

    {
        concrete_storage storage{};
        const auto       key = std::filesystem::path{ "hoge" } / "fuga";
        storage.set_string(key, "foo");
        const auto o_value = storage.get_string(key);
        BOOST_REQUIRE(o_value);
        BOOST_TEST(*o_value == "foo");

        BOOST_CHECK(!storage.get_uint32(key));
    }

    {
        const auto* const p_loader = tetengo_property_storageLoader_createMemoryStorageLoader();
        BOOST_SCOPE_EXIT(p_loader)
        {
            tetengo_property_storageLoader_destroy(p_loader);
        }
        BOOST_SCOPE_EXIT_END;

        auto* const p_storage = tetengo_property_storageLoader_load(p_loader, "test_tetengo.property.storage");
        BOOST_SCOPE_EXIT(p_storage)
        {
            tetengo_property_storage_destroy(p_storage);
        }
        BOOST_SCOPE_EXIT_END;
        BOOST_TEST_REQUIRE(p_storage);

        const auto key = std::filesystem::path{ "hoge" } / "fuga";
        tetengo_property_storage_setString(p_storage, key.string().c_str(), "foo");
        const auto length = tetengo_property_storage_getString(p_storage, key.string().c_str(), nullptr, 0);
        BOOST_TEST(length == 3U);
        std::vector<char> value(length + 1, '\0');
        const auto        length_again =
            tetengo_property_storage_getString(p_storage, key.string().c_str(), std::data(value), std::size(value));
        BOOST_TEST(length == length_again);
        BOOST_TEST(std::string_view{ std::data(value) } == "foo");
    }
    {
        const auto* const p_loader = tetengo_property_storageLoader_createMemoryStorageLoader();
        BOOST_SCOPE_EXIT(p_loader)
        {
            tetengo_property_storageLoader_destroy(p_loader);
        }
        BOOST_SCOPE_EXIT_END;

        auto* const p_storage = tetengo_property_storageLoader_load(p_loader, "test_tetengo.property.storage");
        BOOST_SCOPE_EXIT(p_storage)
        {
            tetengo_property_storage_destroy(p_storage);
        }
        BOOST_SCOPE_EXIT_END;
        BOOST_TEST_REQUIRE(p_storage);

        const auto key = std::filesystem::path{ "hoge" } / "fuga";
        tetengo_property_storage_setString(p_storage, key.string().c_str(), "foobar");
        const auto length = tetengo_property_storage_getString(p_storage, key.string().c_str(), nullptr, 0);
        BOOST_TEST(length == 6U);
        std::vector<char> value(5, '\0');
        const auto        length_again =
            tetengo_property_storage_getString(p_storage, key.string().c_str(), std::data(value), std::size(value));
        BOOST_TEST(length == length_again);
        BOOST_TEST(std::string_view{ std::data(value) } == "foob");
    }
    {
        const auto* const p_loader = tetengo_property_storageLoader_createMemoryStorageLoader();
        BOOST_SCOPE_EXIT(p_loader)
        {
            tetengo_property_storageLoader_destroy(p_loader);
        }
        BOOST_SCOPE_EXIT_END;

        auto* const p_storage = tetengo_property_storageLoader_load(p_loader, "test_tetengo.property.storage");
        BOOST_SCOPE_EXIT(p_storage)
        {
            tetengo_property_storage_destroy(p_storage);
        }
        BOOST_SCOPE_EXIT_END;
        BOOST_TEST_REQUIRE(p_storage);

        const auto key = std::filesystem::path{ "hoge" } / "fuga";
        tetengo_property_storage_setString(nullptr, key.string().c_str(), "foo");
        BOOST_TEST(
            tetengo_property_storage_getString(p_storage, key.string().c_str(), nullptr, 0) == static_cast<size_t>(-1));
    }
    {
        const auto* const p_loader = tetengo_property_storageLoader_createMemoryStorageLoader();
        BOOST_SCOPE_EXIT(p_loader)
        {
            tetengo_property_storageLoader_destroy(p_loader);
        }
        BOOST_SCOPE_EXIT_END;

        auto* const p_storage = tetengo_property_storageLoader_load(p_loader, "test_tetengo.property.storage");
        BOOST_SCOPE_EXIT(p_storage)
        {
            tetengo_property_storage_destroy(p_storage);
        }
        BOOST_SCOPE_EXIT_END;
        BOOST_TEST_REQUIRE(p_storage);

        const auto key = std::filesystem::path{ "hoge" } / "fuga";
        tetengo_property_storage_setString(p_storage, nullptr, "foo");
        BOOST_TEST(
            tetengo_property_storage_getString(p_storage, key.string().c_str(), nullptr, 0) == static_cast<size_t>(-1));
    }
}

BOOST_AUTO_TEST_CASE(save)
{
    BOOST_TEST_PASSPOINT();

    {
        const concrete_storage storage{};
        storage.save();
    }

    {
        const auto* const p_loader = tetengo_property_storageLoader_createMemoryStorageLoader();
        BOOST_SCOPE_EXIT(p_loader)
        {
            tetengo_property_storageLoader_destroy(p_loader);
        }
        BOOST_SCOPE_EXIT_END;

        const auto* const p_storage = tetengo_property_storageLoader_load(p_loader, "test_tetengo.property.storage");
        BOOST_SCOPE_EXIT(p_storage)
        {
            tetengo_property_storage_destroy(p_storage);
        }
        BOOST_SCOPE_EXIT_END;
        BOOST_TEST_REQUIRE(p_storage);

        tetengo_property_storage_save(p_storage);
    }
    {
        tetengo_property_storage_save(nullptr);
    }
}

BOOST_AUTO_TEST_CASE(value_map)
{
    BOOST_TEST_PASSPOINT();

    concrete_storage storage{};
    const auto       key = std::filesystem::path{ "hoge" } / "fuga";
    storage.set_uint32(key, 42);
    BOOST_TEST(storage.call_value_map());
}

BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_SUITE(storage_loader)

BOOST_AUTO_TEST_CASE(construction)
{
    BOOST_TEST_PASSPOINT();

    const concrete_storage_loader factory{};
}

BOOST_AUTO_TEST_CASE(load)
{
    BOOST_TEST_PASSPOINT();

    const concrete_storage_loader factory{};

    const auto path = std::filesystem::path{ "foo" } / "bar";
    const auto p_storage = factory.load(path);
    BOOST_CHECK(p_storage);
}

BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_SUITE_END()

max_stars_repo_name: tetengo/tetengo

max_issues_repo_name: tetengo/tetengo

max_forks_repo_name: tetengo/tetengo


text:
#include <iostream>
#include <functional>
#include <map>
#include <deque>
#include <boost/any.hpp>
class Object
{
public:
	std::string tag = "undefined";
	bool isFunction = false;
	int functionID = -1;
	std::map<std::string, std::shared_ptr<Object>> scope;
	std::pair<std::shared_ptr<Object>, Object *> prototype = {nullptr, this};
	std::map<std::string, std::shared_ptr<Object>> member;
	std::deque<std::shared_ptr<Object>> list;
	Object *__scope__ = nullptr;
	std::pair<std::shared_ptr<Object>, Object *> __proto__;
	boost::any value;

	Object() = default;
	~Object() = default;

	virtual std::shared_ptr<Object> apply(std::shared_ptr<Object> __this__ = nullptr, std::shared_ptr<Object> e = nullptr) {
		return nullptr;
	}

	virtual std::shared_ptr<Object> newObj(std::shared_ptr<Object> e = nullptr) {
		std::shared_ptr<Object> ret = std::make_shared<Object>();
		ret->tag = "disc";
		if(this->prototype.first == nullptr) {
			this->prototype.first = std::make_shared<Object>();
			this->prototype.first->tag = "disc";
		}
		ret->__proto__ = this->prototype;
		this->apply(ret, e);
		return ret;
	}

	template<typename T>
	std::shared_ptr<T> newFunc(int funcID) {
		std::shared_ptr<T> ret = std::make_shared<T>();
		ret->tag = "Function";
		ret->isFunction = true;
		ret->functionID = funcID;
		ret->__scope__ = this;
		return ret;
	}

};

std::shared_ptr<Object>& brackets(std::shared_ptr<Object> o, std::string name) {
	if(o == nullptr || o->tag == "undefined") {
		std::cerr<<"RUNTIME ERROR: cannot modify undefined"<<std::endl;
		exit(1);
	}
	if(o->tag != "disc") {
		std::cerr<<"RUNTIME ERROR: only disc has member"<<std::endl;
		exit(1);
	}
	Object *tryfind = o.get();
	if(name != "prototype" && name != "proto") {
		while(tryfind != nullptr) {
			try {
				if(tryfind->member.find(name) != tryfind->member.end()) {
					return tryfind->member.at(name);
				}
				else
				{
					tryfind = tryfind->__proto__.first.get();
					continue;
				}
			}
			catch(...)
			{
				break;
			}
		}

		o->member[name] = std::make_shared<Object>();
		return o->member.at(name);
	}
	else if(name == "prototype") {
		if(o->prototype.first == nullptr) {
			o->prototype.first = std::make_shared<Object>();
			//o->prototype.first->tag = "disc";
		}
		return o->prototype.first;
	}

	return o->__proto__.first;
}

std::shared_ptr<Object>& brackets(std::shared_ptr<Object> o, double i) {
	if(o->tag != "list") {
		std::cerr<<"RUNTIME ERROR: only list has index"<<std::endl;
		exit(1);
	}
	try {
		return o->list.at(i);
	}
	catch(...)
	{
		std::cerr<<"RUNTIME ERROR: out of range"<<std::endl;
		exit(1);
	}
}

std::shared_ptr<Object> listwith() {
	std::shared_ptr<Object> ret = std::make_shared<Object>();
	ret->tag = "list";
	return ret;
}

template<typename T, typename ...Args>
std::shared_ptr<Object> listwith(T t, Args ...args) {
	std::shared_ptr<Object> ret = std::make_shared<Object>();
	ret->tag = "list";
	std::shared_ptr<Object> to;
	objBind(to, t);
	ret = listwith(args...);
	ret->list.push_front(to);
	return ret;
}

template<typename ...Args>
std::shared_ptr<Object> listwith(std::shared_ptr<Object> t, Args ...args) {
	std::shared_ptr<Object> ret = std::make_shared<Object>();
	ret->tag = "list";
	ret = listwith(args...);
	ret->list.push_front(t);
	return ret;
}

template<>
std::shared_ptr<Object> listwith(std::shared_ptr<Object> t) {
	std::shared_ptr<Object> ret = std::make_shared<Object>();
	ret->tag = "list";
	ret->list.push_front(t);
	return ret;
}

template<typename T>
std::shared_ptr<Object> listwith(T t) {
	std::shared_ptr<Object> ret = std::make_shared<Object>();
	ret->tag = "list";
	std::shared_ptr<Object> to;
	objBind(to, t);
	ret->list.push_front(to);
	return ret;
}

static void objBind(std::shared_ptr<Object>& o, double i) {
	std::shared_ptr<Object> ret = std::make_shared<Object>();
	ret->tag = "double";
	ret->value = i;
	o = ret;
}

static void objBind(std::shared_ptr<Object>& o, std::string i) {
	std::shared_ptr<Object> ret = std::make_shared<Object>();
	ret->tag = "string";
	ret->value = i;
	o = ret;
}

static void objBind(std::shared_ptr<Object>& o, bool i) {
	std::shared_ptr<Object> ret = std::make_shared<Object>();
	ret->tag = "bool";
	ret->value = i;
	o = ret;
}

static void objBind(std::shared_ptr<Object>& o, const std::shared_ptr<Object>& n) {
	o = n;
}

static void renewScope(Object *func, std::shared_ptr<Object> var, std::string name, int level = 0, bool isAlloc = false) {
	if(isAlloc) {
		func->scope[name] = var;
		return;
	}
	int i = 0;
	Object *trace = func;
	while(i < level) {
		trace = trace->__scope__;
		if(trace == nullptr) {
			std::cerr<<"RUNTIME ERROR: "<<name<<" in no scope, cannot be renewed "<<std::endl;
			return;
		}
i++;
	}
	trace->scope[name] = var;
}

static std::shared_ptr<Object>& objFetch(Object *func, std::string name, int level = 0) {
	int i = 0;
	Object *trace = func;
	while(i < level) {
		trace = trace->__scope__;
		if(trace == nullptr) {
			std::cerr<<"RUNTIME ERROR: "<<name<<" in no scope, cannot be fetched "<<std::endl;
			exit(1);
		}
i++;
	}
	return trace->scope[name];
}

static std::shared_ptr<Object> objAdd(std::shared_ptr<Object> o, double i) {
	std::shared_ptr<Object> ret = std::make_shared<Object>();
	ret->tag = o->tag;
	if(o->tag != "double") {
		std::cerr<<"RUNTIME ERROR: math operation with no double type"<<std::endl;
		return nullptr;
	}
	ret->value = boost::any_cast<double>(o->value) + i;
	return ret;
}

static std::shared_ptr<Object> objAdd(std::shared_ptr<Object> o, std::string s) {
	std::shared_ptr<Object> ret = std::make_shared<Object>();
	ret->tag = o->tag;
	if(o->tag != "string") {
		std::cerr<<"RUNTIME ERROR: plus operation with no string type"<<std::endl;
		return nullptr;
	}
	ret->value = boost::any_cast<std::string>(o->value) + s;
	return ret;
}

static std::shared_ptr<Object> objAdd(std::shared_ptr<Object> o, std::shared_ptr<Object> r) {
	std::shared_ptr<Object> ret = std::make_shared<Object>();
	ret->tag = o->tag;
	if(o->tag == r->tag && o->tag == "string") {
		ret->value = boost::any_cast<std::string>(o->value) + boost::any_cast<std::string>(r->value);
		return ret;
	}
	else if(o->tag == r->tag && o->tag == "double") {
		ret->value = boost::any_cast<double>(o->value) + boost::any_cast<double>(r->value);
		return ret;
	}
	std::cerr<<"RUNTIME ERROR: plus operation with no illegal type"<<std::endl;
	return nullptr;
}

static std::shared_ptr<Object> objAdd(double i, std::shared_ptr<Object> r) {
	std::shared_ptr<Object> ret = std::make_shared<Object>();
	ret->tag = r->tag;
	if(r->tag != "double") {
		std::cerr<<"RUNTIME ERROR: math operation with no double type"<<std::endl;
		return nullptr;
	}
	ret->value = boost::any_cast<double>(r->value) + i;
	return ret;
}

static std::shared_ptr<Object> objAdd(std::string s, std::shared_ptr<Object> r) {
	std::shared_ptr<Object> ret = std::make_shared<Object>();
	ret->tag = r->tag;
	if(r->tag != "string") {
		std::cerr<<"RUNTIME ERROR: plus operation with no string type"<<std::endl;
		return nullptr;
	}
	ret->value = boost::any_cast<std::string>(r->value) + s;
	return ret;
}

static std::shared_ptr<Object> objAdd(double i, double r) {
	std::shared_ptr<Object> ret = std::make_shared<Object>();
	ret->value = r + i;
	return ret;
}

static std::shared_ptr<Object> objSub(std::shared_ptr<Object> o, double i) {
	std::shared_ptr<Object> ret = std::make_shared<Object>();
	ret->tag = o->tag;
	if(o->tag != "double") {
		std::cerr<<"RUNTIME ERROR: math operation with no double type"<<std::endl;
		return nullptr;
	}
	ret->value = boost::any_cast<double>(o->value) - i;
	return ret;
}

static std::shared_ptr<Object> objSub(std::shared_ptr<Object> o, std::shared_ptr<Object> r) {
	std::shared_ptr<Object> ret = std::make_shared<Object>();
	ret->tag = o->tag;
	if(o->tag == r->tag && o->tag == "double") {
		ret->value = boost::any_cast<double>(o->value) - boost::any_cast<double>(r->value);
		return ret;
	}
	std::cerr<<"RUNTIME ERROR: plus operation with no illegal type"<<std::endl;
	return nullptr;
}

static std::shared_ptr<Object> objSub(double i, std::shared_ptr<Object> r) {
	std::shared_ptr<Object> ret = std::make_shared<Object>();
	ret->tag = r->tag;
	if(r->tag != "double") {
		std::cerr<<"RUNTIME ERROR: math operation with no double type"<<std::endl;
		return nullptr;
	}
	ret->value = i - boost::any_cast<double>(r->value);
	return ret;
}

static std::shared_ptr<Object> objSub(double i, double r) {
	std::shared_ptr<Object> ret = std::make_shared<Object>();
	ret->value = i - r;
	return ret;
}

static std::shared_ptr<Object> objMul(std::shared_ptr<Object> o, double i) {
	std::shared_ptr<Object> ret = std::make_shared<Object>();
	ret->tag = o->tag;
	if(o->tag != "double") {
		std::cerr<<"RUNTIME ERROR: math operation with no double type"<<std::endl;
		return nullptr;
	}
	ret->value = i * boost::any_cast<double>(o->value);
	return ret;
}

static std::shared_ptr<Object> objMul(std::shared_ptr<Object> o, std::shared_ptr<Object> r) {
	std::shared_ptr<Object> ret = std::make_shared<Object>();
	ret->tag = o->tag;
	if(o->tag == r->tag && o->tag == "double") {
		ret->value = boost::any_cast<double>(r->value) * boost::any_cast<double>(o->value);
		return ret;
	}
	std::cerr<<"RUNTIME ERROR: plus operation with no illegal type"<<std::endl;
	return nullptr;
}

static std::shared_ptr<Object> objMul(double i, std::shared_ptr<Object> r) {
	std::shared_ptr<Object> ret = std::make_shared<Object>();
	ret->tag = r->tag;
	if(r->tag != "double") {
		std::cerr<<"RUNTIME ERROR: math operation with no double type"<<std::endl;
		return nullptr;
	}
	ret->value = i * boost::any_cast<double>(r->value);
	return ret;
}

static std::shared_ptr<Object> objMul(double i, double r) {
	std::shared_ptr<Object> ret = std::make_shared<Object>();
	ret->value = i * r;
	return ret;
}

static std::shared_ptr<Object> objDiv(std::shared_ptr<Object> o, double i) {
	std::shared_ptr<Object> ret = std::make_shared<Object>();
	ret->tag = o->tag;
	if(o->tag != "double") {
		std::cerr<<"RUNTIME ERROR: math operation with no double type"<<std::endl;
		return nullptr;
	}
	ret->value = i / boost::any_cast<double>(o->value);
	return ret;
}

static std::shared_ptr<Object> objDiv(std::shared_ptr<Object> o, std::shared_ptr<Object> r) {
	std::shared_ptr<Object> ret = std::make_shared<Object>();
	ret->tag = o->tag;
	if(r->tag == o->tag && o->tag == "double") {
		ret->value = boost::any_cast<double>(o->value) / boost::any_cast<double>(r->value);
		return ret;
	}
	std::cerr<<"RUNTIME ERROR: plus operation with no illegal type"<<std::endl;
	return nullptr;
}

static std::shared_ptr<Object> objDiv(double i, std::shared_ptr<Object> r) {
	std::shared_ptr<Object> ret = std::make_shared<Object>();
	ret->tag = r->tag;
	if(r->tag != "double") {
		std::cerr<<"RUNTIME ERROR: math operation with no double type"<<std::endl;
		return nullptr;
	}
	ret->value = i / boost::any_cast<double>(r->value);
	return ret;
}

static std::shared_ptr<Object> objDiv(double i, double r) {
	std::shared_ptr<Object> ret = std::make_shared<Object>();
	ret->value = i / r;
	return ret;
}

static bool objIsLarger(std::shared_ptr<Object> r, double d) {
	if(r->tag != "double") {
		std::cerr<<"RUNTIME ERROR: cmp operation with no double type"<<std::endl;
		return nullptr;
	}
	return boost::any_cast<double>(r->value) > d;
}

static bool objIsLarger(double d, std::shared_ptr<Object> r) {
	if(r->tag != "double") {
		std::cerr<<"RUNTIME ERROR: cmp operation with no double type"<<std::endl;
		return nullptr;
	}
	return d > boost::any_cast<double>(r->value);
}

static bool objIsLarger(std::shared_ptr<Object> l, std::shared_ptr<Object> r) {
	if(r->tag != "double" || l->tag != "double") {
		std::cerr<<"RUNTIME ERROR: cmp operation with no double type"<<std::endl;
		return nullptr;
	}
	return boost::any_cast<double>(l->value) > boost::any_cast<double>(r->value);
}

static bool objIsLarger(double r, double d) {
	return r > d;
}

static bool objAnd(std::shared_ptr<Object> r, bool d) {
	if(r->tag != "bool") {
		std::cerr<<"RUNTIME ERROR: bool operation with no double type"<<std::endl;
		return nullptr;
	}
	return boost::any_cast<bool>(r->value) && d;
}

static bool objAnd(bool d, std::shared_ptr<Object> r) {
	if(r->tag != "bool") {
		std::cerr<<"RUNTIME ERROR: bool operation with no double type"<<std::endl;
		return nullptr;
	}
	return d && boost::any_cast<bool>(r->value);
}

static bool objAnd(std::shared_ptr<Object> l, std::shared_ptr<Object> r) {
	if(r->tag != "bool" || l->tag != "bool") {
		std::cerr<<"RUNTIME ERROR: bool operation with no double type"<<std::endl;
		return nullptr;
	}
	return boost::any_cast<bool>(l->value) && boost::any_cast<bool>(r->value);
}

static bool objAnd(bool r, bool d) {
	return r && d;
}

static bool objOr(std::shared_ptr<Object> r, bool d) {
	if(r->tag != "bool") {
		std::cerr<<"RUNTIME ERROR: bool operation with no double type"<<std::endl;
		return nullptr;
	}
	return boost::any_cast<bool>(r->value) || d;
}

static bool objOr(bool d, std::shared_ptr<Object> r) {
	if(r->tag != "bool") {
		std::cerr<<"RUNTIME ERROR: bool operation with no double type"<<std::endl;
		return nullptr;
	}
	return d || boost::any_cast<bool>(r->value);
}

static bool objOr(std::shared_ptr<Object> l, std::shared_ptr<Object> r) {
	if(r->tag != "bool" || l->tag != "bool") {
		std::cerr<<"RUNTIME ERROR: bool operation with no double type"<<std::endl;
		return nullptr;
	}
	return boost::any_cast<bool>(l->value) || boost::any_cast<bool>(r->value);
}

static bool objOr(bool r, bool d) {
	return r || d;
}

static bool objNot(bool r) {
	return !r;
}

static bool objNot(std::shared_ptr<Object> r) {
	if(r->tag != "bool") {
		std::cerr<<"RUNTIME ERROR: bool operation with no double type"<<std::endl;
		return nullptr;
	}
	return !boost::any_cast<bool>(r->value);
}

static bool objIsSmaller(std::shared_ptr<Object> r, double d) {
	if(r->tag != "double") {
		std::cerr<<"RUNTIME ERROR: cmp operation with no double type"<<std::endl;
		return nullptr;
	}
	return boost::any_cast<double>(r->value) < d;
}

static bool objIsSmaller(double d, std::shared_ptr<Object> r) {
	if(r->tag != "double") {
		std::cerr<<"RUNTIME ERROR: cmp operation with no double type"<<std::endl;
		return nullptr;
	}
	return d < boost::any_cast<double>(r->value);
}

static bool objIsSmaller(std::shared_ptr<Object> l, std::shared_ptr<Object> r) {
	if(r->tag != "double" || l->tag != "double") {
		std::cerr<<"RUNTIME ERROR: cmp operation with no double type"<<std::endl;
		return nullptr;
	}
	return boost::any_cast<double>(l->value) < boost::any_cast<double>(r->value);
}

static bool objIsSmaller(double r, double d) {
	return r < d;
}

static bool objIsEqual(std::shared_ptr<Object> r, bool b) {
	if(r->tag == "bool") {
		return b == boost::any_cast<bool>(r->value);
	}
	return false;
}

static bool objIsEqual(std::shared_ptr<Object> r, double d) {
	if(r->tag == "double") {
		return d == boost::any_cast<double>(r->value);
	}
	return false;
}

static bool objIsEqual(std::shared_ptr<Object> r, std::string s) {
	if(r->tag == "string") {
		return s == boost::any_cast<std::string>(r->value);
	}
	return false;
}

static bool objIsEqual(bool b, std::shared_ptr<Object> r) {
	if(r->tag == "bool") {
		return b == boost::any_cast<bool>(r->value);
	}
	return false;
}

static bool objIsEqual(double d, std::shared_ptr<Object> r) {
	if(r->tag == "double") {
		return d == boost::any_cast<double>(r->value);
	}
	return false;
}

static bool objIsEqual(std::string s, std::shared_ptr<Object> r) {
	if(r->tag == "string") {
		return s == boost::any_cast<std::string>(r->value);
	}
	return false;
}

static bool objIsEqual(std::shared_ptr<Object> l, std::shared_ptr<Object> r) {
	if(l->tag != r->tag) {
		return false;
	}

	if(l->tag == "undefined") {
		return true;
	}
	else if(l->tag == "bool") {
		return boost::any_cast<bool>(l->value) == boost::any_cast<bool>(r->value);
	}
	else if(l->tag == "double") {
		return boost::any_cast<double>(l->value) == boost::any_cast<double>(r->value);
	}
	else if(l->tag == "string") {
		return boost::any_cast<std::string>(l->value) == boost::any_cast<std::string>(r->value);
	}
	else if(l->tag == "Function") {
		return l->functionID == r->functionID;
	}
	else if(l->tag == "list") {
		if(l->list.size() != r->list.size()) {
			return false;
		}
		bool isequal = true;
		for (int i = 0; i < l->list.size(); ++i)
		{
			isequal = objIsEqual(l->list[i], r->list[i]);
			if(isequal == false) {
				return false;
			}
		}
		return true;
	}
	else if(l->tag == "disc") {
		if(l->__proto__ != r->__proto__) {
			return false;
		}
		else if(l->member.size() != r->member.size()) {
			return false;
		}
		bool isequal = true;
		for (auto const& x: l->member)
		{
			try {
				return objIsEqual(x.second, r->member.at(x.first));
			}
			catch(...)
			{
				return false;
			}
		}
		return true;
	}
	return false;
}

template<typename L, typename R>
static bool objIsEqual(L l, R r) {
	return (l == r);
}

template<typename L, typename R>
static bool objIsInEqual(L l, R r) {
	return !(objIsEqual(l, r));
}

void objPrint(std::string o, bool inLine = true) {
	std::cout<<o;
	if(inLine) {
		std::cout<<std::endl;
	}
}

void objPrint(double o, bool inLine = true) {
	std::cout<<o;
	if(inLine) {
		std::cout<<std::endl;
	}
}

void objPrint(bool o, bool inLine = true) {
	std::cout<<std::boolalpha<<o;
	if(inLine) {
		std::cout<<std::endl;
	}
}

void objPrint(const std::shared_ptr<Object>& o, bool inLine = true) {
	if(o->tag == "undefined") {
		std::cout<<" undefined ";
	}
	else if(o->tag == "disc") {
		std::cout<<" { ";
		for (auto const& x: o->member)
		{
			std::cout<<x.first<<":";
			objPrint(x.second, false);
			std::cout<<"  ";
		}
		std::cout<<" } ";
	}
	else if(o->tag == "list") {
		std::cout<<" [ ";
		for (auto const& x: o->list)
		{
			objPrint(x, false);
		}
		std::cout<<" ] ";
	}
	else if(o->tag == "Function") {
		std::cout<<" Function ";
	}
	else if(o->tag == "string") {
		std::cout<<boost::any_cast<std::string>(o->value)<<" ";
	}
	else if(o->tag == "double") {
		std::cout<<boost::any_cast<double>(o->value)<<" ";
	}
	else if(o->tag == "bool") {
		std::cout<<std::boolalpha<<boost::any_cast<bool>(o->value)<<" ";
	}
	if(inLine) {
		std::cout<<std::endl;
	}
}
std::shared_ptr<Object> apiWrapperPrint(std::shared_ptr<Object> o) {
	objPrint(o);
	return std::make_shared<Object>();
}

std::map<std::string, std::function<std::shared_ptr<Object>(std::shared_ptr<Object>)> > API = {
	{"console.log",apiWrapperPrint}
};
class __IRFUNC1: public Object {
public:
virtual std::shared_ptr<Object> apply(std::shared_ptr<Object> __this__ = nullptr, std::shared_ptr<Object> j = nullptr) {
objBind(brackets(__this__, "name"), j);
std::shared_ptr<Object> __IR_variable1 = std::make_shared<Object>();
objBind(__IR_variable1, objAdd((std::string)"o", j));
renewScope(this, __IR_variable1, "__IR_variable1");
objBind(brackets(__this__, "obj"), __IR_variable1);
return std::make_shared<Object>();
}

virtual std::shared_ptr<Object> newObj(std::shared_ptr<Object> j) {
std::shared_ptr<Object> ret = std::make_shared<Object>();
ret->tag = "disc";
if(this->prototype.first == nullptr) {
this->prototype.first = std::make_shared<Object>();
this->prototype.first->tag = "disc";
}
ret->__proto__ = this->prototype;
this->apply(ret, j);
return ret;
}
};

class __window__: public Object {
public:
virtual std::shared_ptr<Object> apply(std::shared_ptr<Object> __this__ = nullptr) {
std::shared_ptr<Object> a = std::make_shared<Object>();
renewScope(this, a, "a");
objBind(a, newFunc<__IRFUNC1>(1));
renewScope(this, a, "a", false);
std::shared_ptr<Object> t = std::make_shared<Object>();
renewScope(this, t, "t");
objBind(t, (std::string)"i");
renewScope(this, t, "t", false);
std::shared_ptr<Object> b = std::make_shared<Object>();
renewScope(this, b, "b");
objBind(b, a->newObj(t));
renewScope(this, b, "b", false);
API["console.log"](b);
std::shared_ptr<Object> i = std::make_shared<Object>();
renewScope(this, i, "i");
objBind(i, (double)1);
renewScope(this, i, "i", false);
while(({
std::shared_ptr<Object> __IR_variable2 = std::make_shared<Object>();
objBind(__IR_variable2, objIsSmaller(i, (double)5));
renewScope(this, __IR_variable2, "__IR_variable2");
objIsEqual(__IR_variable2, true);
})) {
API["console.log"](i);
std::shared_ptr<Object> __IR_variable3 = std::make_shared<Object>();
objBind(__IR_variable3, objAdd(i, (double)1));
renewScope(this, __IR_variable3, "__IR_variable3");
objBind(i, __IR_variable3);
renewScope(this, i, "i", false);
};
return nullptr;
}
};

int main(int argc, char const *argv[]) {
std::shared_ptr<__window__> window = std::make_shared<__window__>();
window->apply(window);
return 0;
}


text:
#include <boost/python.hpp>
using namespace boost::python;

BOOST_PYTHON_MODULE(configuration)
{
  class_<ConfigurationManager>("ConfigurationManager", init<const char*,const int&, const char*,const int&,std::ostream&>())
    .add_property("AddConfig", &World::AddConfig)
    .add_property("Update", &World::Update);
}

max_stars_repo_name: michele-brambilla/ConfigurationService

max_issues_repo_name: michele-brambilla/ConfigurationService

max_forks_repo_name: michele-brambilla/ConfigurationService


text:
// Boost.Bimap
//
// Copyright (c) 2006-2007 Matias Capeletto
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

//  VC++ 8.0 warns on usage of certain Standard Library and API functions that
//  can be cause buffer overruns or other possible security issues if misused.
//  See http://msdn.microsoft.com/msdnmag/issues/05/05/SafeCandC/default.aspx
//  But the wording of the warning is misleading and unsettling, there are no
//  portable alternative functions, and VC++ 8.0's own libraries use the
//  functions in question. So turn off the warnings.
#define _CRT_SECURE_NO_DEPRECATE
#define _SCL_SECURE_NO_DEPRECATE

#include <boost/config.hpp>

// Boost.Test
#include <boost/test/minimal.hpp>

#include <boost/static_assert.hpp>

// Boost.MPL
#include <boost/type_traits/is_same.hpp>
#include <boost/mpl/placeholders.hpp>
#include <boost/mpl/assert.hpp>
#include <boost/type_traits/add_const.hpp>

// Boost.Bimap
#include <boost/bimap/detail/test/check_metadata.hpp>

// Boost.Bimap.Tags
#include <boost/bimap/tags/tagged.hpp>
#include <boost/bimap/tags/support/default_tagged.hpp>
#include <boost/bimap/tags/support/is_tagged.hpp>
#include <boost/bimap/tags/support/overwrite_tagged.hpp>
#include <boost/bimap/tags/support/tag_of.hpp>
#include <boost/bimap/tags/support/value_type_of.hpp>
#include <boost/bimap/tags/support/apply_to_value_type.hpp>

BOOST_BIMAP_TEST_STATIC_FUNCTION( test_metafunctions )
{
    using namespace boost::bimaps::tags::support;
    using namespace boost::bimaps::tags;
    using namespace boost::mpl::placeholders;
    using namespace boost;

    struct tag      {};
    struct value    {};

    // Test apply_to_value_type metafunction
    // tagged<value,tag> ----(add_const<_>)----> tagged<value const,tag>
    typedef tagged< value, tag > ttype;
    typedef apply_to_value_type< add_const<_>,ttype>::type result;
    typedef is_same<tagged<value const,tag>,result> compare;
    BOOST_MPL_ASSERT_MSG(compare::value,tag,(result));
}

struct tag_a {};
struct tag_b {};
struct data  {};

void test_function()
{

    using namespace boost::bimaps::tags::support;
    using namespace boost::bimaps::tags;
    using boost::is_same;

    typedef tagged< data, tag_a > data_a;
    typedef tagged< data, tag_b > data_b;

    BOOST_CHECK(( is_same< data_a::value_type   , data  >::value ));
    BOOST_CHECK(( is_same< data_a::tag          , tag_a >::value ));

    BOOST_CHECK((
        is_same< overwrite_tagged < data_a, tag_b >::type, data_b >::value
    ));
    BOOST_CHECK((
        is_same< default_tagged   < data_a, tag_b >::type, data_a >::value
    ));
    BOOST_CHECK((
        is_same< default_tagged   < data  , tag_b >::type, data_b >::value
    ));

    BOOST_CHECK(( is_tagged< data   >::value == false ));
    BOOST_CHECK(( is_tagged< data_a >::value == true  ));

    BOOST_CHECK(( is_same< value_type_of<data_a>::type, data  >::value ));
    BOOST_CHECK(( is_same< tag_of       <data_a>::type, tag_a >::value ));

}

int test_main( int, char* [] )
{
    test_function();

    // Test metanfunctions
    BOOST_BIMAP_CALL_TEST_STATIC_FUNCTION( test_metafunctions );

    return 0;
}

max_stars_repo_name: randolphwong/mcsema

max_issues_repo_name: randolphwong/mcsema

max_forks_repo_name: randolphwong/mcsema


text:
#include <boost/test/unit_test.hpp>

#include "sam/cigar.h"

using namespace std;
using namespace gamgee;

BOOST_AUTO_TEST_CASE( cigar_consumes_read_bases ) {
	BOOST_CHECK(Cigar::consumes_read_bases(CigarOperator::M));
	BOOST_CHECK(Cigar::consumes_read_bases(CigarOperator::EQ));
	BOOST_CHECK(Cigar::consumes_read_bases(CigarOperator::X));
	BOOST_CHECK(Cigar::consumes_read_bases(CigarOperator::I));
	BOOST_CHECK(!Cigar::consumes_read_bases(CigarOperator::D));
	BOOST_CHECK(Cigar::consumes_read_bases(CigarOperator::S));
	BOOST_CHECK(!Cigar::consumes_read_bases(CigarOperator::H));
	BOOST_CHECK(!Cigar::consumes_read_bases(CigarOperator::N));
	BOOST_CHECK(!Cigar::consumes_read_bases(CigarOperator::P));
	BOOST_CHECK(!Cigar::consumes_read_bases(CigarOperator::B));
}

BOOST_AUTO_TEST_CASE( cigar_consumes_reference_bases ) {
	BOOST_CHECK(Cigar::consumes_reference_bases(CigarOperator::M));
	BOOST_CHECK(Cigar::consumes_reference_bases(CigarOperator::EQ));
	BOOST_CHECK(Cigar::consumes_reference_bases(CigarOperator::X));
	BOOST_CHECK(!Cigar::consumes_reference_bases(CigarOperator::I));
	BOOST_CHECK(Cigar::consumes_reference_bases(CigarOperator::D));
	BOOST_CHECK(!Cigar::consumes_reference_bases(CigarOperator::S));
	BOOST_CHECK(!Cigar::consumes_reference_bases(CigarOperator::H));
	BOOST_CHECK(Cigar::consumes_reference_bases(CigarOperator::N));
	BOOST_CHECK(!Cigar::consumes_reference_bases(CigarOperator::P));
	BOOST_CHECK(!Cigar::consumes_reference_bases(CigarOperator::B));
}

max_stars_repo_name: audiohacked/gamgee

max_issues_repo_name: audiohacked/gamgee

max_forks_repo_name: audiohacked/gamgee


text:
//==============================================================================
//         Copyright 2003 - 2013   LASMEA UMR 6602 CNRS/Univ. Clermont II
//         Copyright 2009 - 2013   LRI    UMR 8623 CNRS/Univ Paris Sud XI
//
//          Distributed under the Boost Software License, Version 1.0.
//                 See accompanying file LICENSE.txt or copy at
//                     http://www.boost.org/LICENSE_1_0.txt
//==============================================================================
#include <nt2/include/functions/sqrt1pm1.hpp>

#include <boost/dispatch/functor/meta/call.hpp>
#include <nt2/sdk/functor/meta/call.hpp>
#include <nt2/sdk/unit/tests/relation.hpp>
#include <nt2/sdk/unit/tests/type_expr.hpp>
#include <complex>
#include <nt2/sdk/complex/complex.hpp>
#include <nt2/sdk/unit/tests/ulp.hpp>
#include <nt2/sdk/unit/tests/basic.hpp>
#include <nt2/sdk/meta/as_integer.hpp>
#include <nt2/sdk/unit/module.hpp>
#include <boost/simd/sdk/config.hpp>

#include <nt2/include/constants/eps.hpp>
#include <nt2/include/constants/half.hpp>
#include <nt2/include/constants/mone.hpp>
#include <nt2/include/constants/one.hpp>
#include <nt2/include/constants/sqrt_2.hpp>
#include <nt2/include/constants/zero.hpp>
#include <nt2/include/constants/inf.hpp>
#include <nt2/include/constants/minf.hpp>
#include <nt2/include/constants/nan.hpp>
#include <nt2/include/constants/i.hpp>

NT2_TEST_CASE_TPL ( sqrt1pm1_real,  NT2_REAL_TYPES)
{
  using nt2::sqrt1pm1;
  using nt2::tag::sqrt1pm1_;
  typedef typename std::complex<T> cT;
  typedef typename nt2::meta::call<sqrt1pm1_(cT)>::type r_t;

  // return type conformity test
  NT2_TEST_TYPE_IS(r_t, cT);

  // specific values tests
#ifndef BOOST_SIMD_NO_INVALIDS
    NT2_TEST_ULP_EQUAL(sqrt1pm1(nt2::Inf<cT>()),  cT(nt2::Inf<T>()), 0);
    NT2_TEST_ULP_EQUAL(sqrt1pm1(nt2::Minf<cT>()),  cT(-1, nt2::Inf<T>()), 0);
    NT2_TEST_ULP_EQUAL(sqrt1pm1(nt2::Nan<cT>()),  cT(nt2::Nan<T>(), nt2::Nan<T>()), 0);
#endif
    NT2_TEST_ULP_EQUAL(sqrt1pm1(nt2::Mone<cT>()),  cT(nt2::Mone<T>()), 0);
    NT2_TEST_ULP_EQUAL(sqrt1pm1(nt2::One<cT>()),  cT(nt2::Sqrt_2<T>()-nt2::One<T>()), 2);
    NT2_TEST_ULP_EQUAL(sqrt1pm1(cT(nt2::Eps<T>())), cT(nt2::Eps<T>()*nt2::Half<T>()), 2);
    NT2_TEST_ULP_EQUAL(sqrt1pm1(cT(-1, 2)), cT(0, 1), 2);
    NT2_TEST_ULP_EQUAL(sqrt1pm1(nt2::Zero<cT>()),  cT(nt2::Zero<T>()), 0);
}


text:
//==================================================================================================
/*!
  @file

  @copyright 2016 NumScale SAS

  Distributed under the Boost Software License, Version 1.0.
  (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
*/
//==================================================================================================
#ifndef BOOST_SIMD_ARCH_COMMON_SIMD_FUNCTION_LOG1P_HPP_INCLUDED
#define BOOST_SIMD_ARCH_COMMON_SIMD_FUNCTION_LOG1P_HPP_INCLUDED
#include <boost/simd/detail/overload.hpp>

#include <boost/simd/detail/dispatch/function/overload.hpp>
#include <boost/config.hpp>

#include <boost/simd/function/any.hpp>
#include <boost/simd/function/bitwise_cast.hpp>
#include <boost/simd/function/dec.hpp>
#include <boost/simd/function/fma.hpp>
#include <boost/simd/function/ifrexp.hpp>
#include <boost/simd/function/genmask.hpp>
#include <boost/simd/function/horn.hpp>
#include <boost/simd/function/if_else.hpp>
#include <boost/simd/function/if_minus.hpp>
#include <boost/simd/function/if_nan_else.hpp>
#include <boost/simd/function/inc.hpp>
#include <boost/simd/function/is_ngez.hpp>
#include <boost/simd/function/is_nez.hpp>
#include <boost/simd/function/musl.hpp>
#include <boost/simd/function/plain.hpp>
#include <boost/simd/function/sqr.hpp>
#include <boost/simd/function/tofloat.hpp>

#include <boost/simd/constant/half.hpp>
#include <boost/simd/constant/inf.hpp>
#include <boost/simd/constant/minf.hpp>
#include <boost/simd/constant/sqrt_2o_2.hpp>
#include <boost/simd/constant/two.hpp>

#include <boost/simd/detail/constant/log_2hi.hpp>
#include <boost/simd/detail/constant/log_2lo.hpp>

#include <boost/simd/detail/dispatch/meta/as_integer.hpp>

namespace boost { namespace simd { namespace ext
{
  namespace bd = boost::dispatch;

 BOOST_DISPATCH_OVERLOAD_IF ( log1p_
                             , (typename A0, typename X)
                             , (detail::is_native<X>)
                             , bd::cpu_
                             , bs::pack_< bd::single_<A0>, X>
                          )
  {
    BOOST_FORCEINLINE A0 operator()( const A0& a0) BOOST_NOEXCEPT
    {
      return musl_(log1p)(a0);
    }
  };

 BOOST_DISPATCH_OVERLOAD_IF ( log1p_
                             , (typename A0, typename X)
                             , (detail::is_native<X>)
                             , bd::cpu_
                             , bs::pack_< bd::double_<A0>, X>
                          )
  {
    BOOST_FORCEINLINE A0 operator()( const A0& a0) BOOST_NOEXCEPT
    {
      return plain_(log1p)(a0);
    }
  };

  BOOST_DISPATCH_OVERLOAD_IF ( log1p_
                          , (typename A0, typename X)
                          , (detail::is_native<X>)
                          , bd::cpu_
                          , bs::musl_tag
                          , bs::pack_< bd::single_<A0>, X>
                          )
  {
    BOOST_FORCEINLINE A0 operator() (const musl_tag &, const A0& a0) const BOOST_NOEXCEPT
    {
      using uiA0 = bd::as_integer_t<A0, unsigned>;
      using iA0 = bd::as_integer_t<A0,   signed>;
      const A0 uf =  inc(a0);
      auto isnez = is_nez(uf);

      uiA0 iu = bitwise_cast<uiA0>(uf);
      iu += 0x3f800000 - 0x3f3504f3;
      iA0 k = bitwise_cast<iA0>(iu>>23) - 0x7f;
      /* correction term ~ log(1+x)-log(u), avoid underflow in c/u */
      /* reduce x into [sqrt(2)/2, sqrt(2)] */
      iu = (iu&0x007fffff) + 0x3f3504f3;
      A0 f =  dec(bitwise_cast<A0>(iu));
      A0 s = f/(2.0f + f);
      A0 z = sqr(s);
      A0 w = sqr(z);
      A0 t1= w*horn<A0, 0x3eccce13, 0x3e789e26>(w);
      A0 t2= z*horn<A0, 0x3f2aaaaa, 0x3e91e9ee>(w);
      A0 R = t2 + t1;
      A0 hfsq = Half<A0>()*sqr(f);
      A0 dk = tofloat(k);
      A0  c = if_else( k >= 2, oneminus(uf-a0), a0-dec(uf))/uf;
      A0 r = fma(dk, Log_2hi<A0>(), ((fma(s, (hfsq+R), dk*Log_2lo<A0>()+c) - hfsq) + f));
#ifndef BOOST_SIMD_NO_INFINITIES
      A0 zz = if_else(isnez, if_else(a0 == Inf<A0>(), Inf<A0>(), r), Minf<A0>());
#else
      A0 zz = if_else(isnez, r, Minf<A0>());
#endif
      return if_nan_else(is_ngez(uf), zz);
    }
  };

  BOOST_DISPATCH_OVERLOAD_IF ( log1p_
                             , (typename A0, typename X)
                             , (detail::is_native<X>)
                             , bd::cpu_
                             , bs::musl_tag
                             , bs::pack_< bd::double_<A0>, X>
                             )
  {
    BOOST_FORCEINLINE A0 operator() (const musl_tag &, const A0& a0) const BOOST_NOEXCEPT
    {
      using uiA0 = bd::as_integer_t<A0, unsigned>;
      using iA0 = bd::as_integer_t<A0,   signed>;
      const A0 uf =  inc(a0);
      auto isnez = is_nez(uf);

      /* reduce x into [sqrt(2)/2, sqrt(2)] */
      uiA0 hu = bitwise_cast<uiA0>(uf)>>32;
      hu += 0x3ff00000 - 0x3fe6a09e;
      iA0 k = bitwise_cast<iA0>(hu>>20) - 0x3ff;
      /* correction term ~ log(1+x)-log(u), avoid underflow in c/u */
      A0  c =  if_else( k >= 2, oneminus(uf-a0), a0-dec(uf))/uf;
      hu =  (hu&0x000fffff) + 0x3fe6a09e;
      A0 f = bitwise_cast<A0>( bitwise_cast<uiA0>(hu<<32) | (bitwise_and(0xffffffffull, bitwise_cast<uiA0>(uf))));
      f = dec(f);

      A0 hfsq = Half<A0>()*sqr(f);
      A0 s = f/(2.0 + f);
      A0 z = sqr(s);
      A0 w = sqr(z);
      A0 t1= w*horn<A0, 0x3fd999999997fa04ll, 0x3fcc71c51d8e78afll, 0x3fc39a09d078c69fll > (w);
      A0 t2= z*horn<A0, 0x3fe5555555555593ll, 0x3fd2492494229359ll
                      , 0x3fc7466496cb03dell, 0x3fc2f112df3e5244ll> (w);
      A0 R = t2 + t1;
      A0 dk = tofloat(k);
      A0 r = fma(dk, Log_2hi<A0>(), ((fma(s, (hfsq+R), dk*Log_2lo<A0>()+c) - hfsq) + f));

#ifndef BOOST_SIMD_NO_INFINITIES
      A0 zz = if_else(isnez, if_else(a0 == Inf<A0>(), Inf<A0>(), r), Minf<A0>());
#else
      A0 zz = if_else(isnez, r, Minf<A0>());
#endif
      return if_nan_else(is_ngez(uf), zz);
    }
  };
//=================================================================================================================
  BOOST_DISPATCH_OVERLOAD_IF ( log1p_
                          , (typename A0, typename X)
                          , (detail::is_native<X>)
                          , bd::cpu_
                          , bs::plain_tag
                          , bs::pack_< bd::single_<A0>, X>
                          )
  {
    BOOST_FORCEINLINE A0 operator() (const plain_tag &, const A0& a0) const BOOST_NOEXCEPT
    {
      using iA0 = bd::as_integer_t<A0,   signed>;
      const A0 uf =  inc(a0);
      auto isnez = is_nez(uf);

     /* reduce x into [sqrt(2)/2, sqrt(2)] */
      iA0 k;
      A0 x;
      std::tie(x, k) = ifrexp(uf);
      A0  x_lt_sqrthf = genmask(Sqrt_2o_2<A0>() >  x);
      k += bitwise_cast<iA0>(x_lt_sqrthf);
      A0 f = dec(x+bitwise_and(x, x_lt_sqrthf));
      /* correction term ~ log(1+x)-log(u), avoid underflow in c/u */
      A0  c = if_else( k >= 2, oneminus(uf-a0), a0-dec(uf))/uf;

      A0 s = f/(2.0f + f);
      A0 z = sqr(s);
      A0 w = sqr(z);
      A0 t1= w*horn<A0, 0x3eccce13, 0x3e789e26>(w);
      A0 t2= z*horn<A0, 0x3f2aaaaa, 0x3e91e9ee>(w);
      A0 R = t2 + t1;
      A0 hfsq = Half<A0>()*sqr(f);
      A0 dk = tofloat(k);
      A0 r = fma(dk, Log_2hi<A0>(), ((fma(s, (hfsq+R), dk*Log_2lo<A0>()+c) - hfsq) + f));
#ifndef BOOST_SIMD_NO_INFINITIES
      A0 zz = if_else(isnez, if_else(a0 == Inf<A0>(), Inf<A0>(), r), Minf<A0>());
#else
      A0 zz = if_else(isnez, r, Minf<A0>());
#endif
      return if_nan_else(is_ngez(uf), zz);
    }
  };

  BOOST_DISPATCH_OVERLOAD_IF ( log1p_
                             , (typename A0, typename X)
                             , (detail::is_native<X>)
                             , bd::cpu_
                             , bs::plain_tag
                             , bs::pack_< bd::double_<A0>, X>
                             )
  {
    BOOST_FORCEINLINE A0 operator() (const plain_tag &, const A0& a0) const BOOST_NOEXCEPT
    {
      using iA0 = bd::as_integer_t<A0,   signed>;
      const A0 uf =  inc(a0);
      auto isnez = is_nez(uf);

      /* reduce x into [sqrt(2)/2, sqrt(2)] */
      iA0 k;
      A0 x;
      std::tie(x, k) = ifrexp(uf);
      A0  x_lt_sqrthf = genmask(Sqrt_2o_2<A0>() >  x);
      k += bitwise_cast<iA0>(x_lt_sqrthf);
      A0 f = dec(x+bitwise_and(x, x_lt_sqrthf));
      /* correction term ~ log(1+x)-log(u), avoid underflow in c/u */
      A0  c = if_else( k >= 2, oneminus(uf-a0), a0-dec(uf))/uf;

      A0 hfsq = Half<A0>()*sqr(f);
      A0 s = f/(2.0 + f);
      A0 z = sqr(s);
      A0 w = sqr(z);
      A0 t1= w*horn<A0, 0x3fd999999997fa04ll, 0x3fcc71c51d8e78afll, 0x3fc39a09d078c69fll > (w);
      A0 t2= z*horn<A0, 0x3fe5555555555593ll, 0x3fd2492494229359ll
                      , 0x3fc7466496cb03dell, 0x3fc2f112df3e5244ll> (w);
      A0 R = t2 + t1;
      A0 dk = tofloat(k);
      A0 r = fma(dk, Log_2hi<A0>(), ((fma(s, (hfsq+R), dk*Log_2lo<A0>()+c) - hfsq) + f));

#ifndef BOOST_SIMD_NO_INFINITIES
      A0 zz = if_else(isnez, if_else(a0 == Inf<A0>(), Inf<A0>(), r), Minf<A0>());
#else
      A0 zz = if_else(isnez, r, Minf<A0>());
#endif
      return if_nan_else(is_ngez(uf), zz);
    }
  };

} } }

#endif

max_stars_repo_name: nickporubsky/boost-simd-clone

max_issues_repo_name: nickporubsky/boost-simd-clone

max_forks_repo_name: nickporubsky/boost-simd-clone


text:
#include "test.hpp"
#define BOOST_TEST_MODULE test
#include <boost/test/unit_test.hpp>
#include <fstream>
using namespace boost;

BOOST_AUTO_TEST_SUITE(test_fa)

BOOST_AUTO_TEST_CASE(test_clear1) {
    BOOST_CHECK_EQUAL(test::test_clear1(), true);
}

BOOST_AUTO_TEST_CASE(test_clear2) {
    BOOST_CHECK_EQUAL(test::test_clear2(), true);
}

BOOST_AUTO_TEST_CASE(test_clear3) {
    BOOST_CHECK_EQUAL(test::test_clear3(), true);
}

BOOST_AUTO_TEST_CASE(test_clear4) {
    BOOST_CHECK_EQUAL(test::test_clear4(), true);
}

BOOST_AUTO_TEST_CASE(test_clear5) {
    BOOST_CHECK_EQUAL(test::test_clear5(), true);
}

BOOST_AUTO_TEST_CASE(test_clear6) {
    BOOST_CHECK_EQUAL(test::test_clear6(), true);
}

BOOST_AUTO_TEST_CASE(test_clear7) {
    BOOST_CHECK_EQUAL(test::test_clear7(), true);
}

BOOST_AUTO_TEST_CASE(test_clear8) {
    BOOST_CHECK_EQUAL(test::test_clear8(), true);
}

BOOST_AUTO_TEST_CASE(test_minimize1) {
    BOOST_CHECK_EQUAL(test::test_minimize1(), true);
}

BOOST_AUTO_TEST_CASE(test_minimize2) {
    BOOST_CHECK_EQUAL(test::test_minimize2(), true);
}

BOOST_AUTO_TEST_CASE(test_minimize3) {
    BOOST_CHECK_EQUAL(test::test_minimize3(), true);
}

BOOST_AUTO_TEST_CASE(test_minimize4) {
    BOOST_CHECK_EQUAL(test::test_minimize4(), true);
}

BOOST_AUTO_TEST_CASE(test_minimize5) {
    BOOST_CHECK_EQUAL(test::test_minimize5(), true);
}

BOOST_AUTO_TEST_CASE(test_minimize6) {
    BOOST_CHECK_EQUAL(test::test_minimize6(), true);
}

BOOST_AUTO_TEST_CASE(test_minimize7) {
    BOOST_CHECK_EQUAL(test::test_minimize7(), true);
}

BOOST_AUTO_TEST_CASE(test_minimize8) {
    BOOST_CHECK_EQUAL(test::test_minimize8(), true);
}

BOOST_AUTO_TEST_CASE(test_basic_regular_expression1) {
    BOOST_CHECK_EQUAL(test::test_basic_regular_expression1(), true);
}

BOOST_AUTO_TEST_CASE(test_basic_regular_expression2) {
    BOOST_CHECK_EQUAL(test::test_basic_regular_expression2(), true);
}

BOOST_AUTO_TEST_CASE(test_basic_regular_expression3) {
    BOOST_CHECK_EQUAL(test::test_basic_regular_expression3(), true);
}

BOOST_AUTO_TEST_CASE(test_basic_regular_expression4) {
    BOOST_CHECK_EQUAL(test::test_basic_regular_expression4(), true);
}

BOOST_AUTO_TEST_CASE(test_basic_regular_expression5) {
    BOOST_CHECK_EQUAL(test::test_basic_regular_expression5(), true);
}

BOOST_AUTO_TEST_CASE(test_intersect1) {
    BOOST_CHECK_EQUAL(test::test_intersect1(), true);
}

BOOST_AUTO_TEST_CASE(test_intersect2) {
    BOOST_CHECK_EQUAL(test::test_intersect2(), true);
}

BOOST_AUTO_TEST_CASE(test_intersect3) {
    BOOST_CHECK_EQUAL(test::test_intersect3(), true);
}

BOOST_AUTO_TEST_CASE(test_intersect4) {
    BOOST_CHECK_EQUAL(test::test_intersect4(), true);
}

BOOST_AUTO_TEST_CASE(test_intersect5) {
    BOOST_CHECK_EQUAL(test::test_intersect5(), true);
}

BOOST_AUTO_TEST_CASE(test_union1) {
    BOOST_CHECK_EQUAL(test::test_union1(), true);
}

BOOST_AUTO_TEST_CASE(test_union2) {
    BOOST_CHECK_EQUAL(test::test_union2(), true);
}

BOOST_AUTO_TEST_CASE(test_union3) {
    BOOST_CHECK_EQUAL(test::test_union3(), true);
}

BOOST_AUTO_TEST_CASE(test_union4) {
    BOOST_CHECK_EQUAL(test::test_union4(), true);
}

BOOST_AUTO_TEST_CASE(test_union5) {
    BOOST_CHECK_EQUAL(test::test_union5(), true);
}

BOOST_AUTO_TEST_CASE(test_union6) {
    BOOST_CHECK_EQUAL(test::test_union6(), true);
}

BOOST_AUTO_TEST_CASE(test_concat1) {
    BOOST_CHECK_EQUAL(test::test_concat1(), true);
}

BOOST_AUTO_TEST_CASE(test_concat2) {
    BOOST_CHECK_EQUAL(test::test_concat2(), true);
}

BOOST_AUTO_TEST_CASE(test_concat3) {
    BOOST_CHECK_EQUAL(test::test_concat3(), true);
}

BOOST_AUTO_TEST_CASE(test_subset1) {
    BOOST_CHECK_EQUAL(test::test_subset1(), true);
}

BOOST_AUTO_TEST_CASE(test_subset2) {
    BOOST_CHECK_EQUAL(test::test_subset2(), true);
}

BOOST_AUTO_TEST_CASE(test_subset3) {
    BOOST_CHECK_EQUAL(test::test_subset3(), true);
}

BOOST_AUTO_TEST_CASE(test_composite1) {
    BOOST_CHECK_EQUAL(test::test_composite1(), true);
}

BOOST_AUTO_TEST_CASE(test_composite2) {
    BOOST_CHECK_EQUAL(test::test_composite2(), true);
}

BOOST_AUTO_TEST_CASE(test_composite3) {
    BOOST_CHECK_EQUAL(test::test_composite3(), true);
}

BOOST_AUTO_TEST_CASE(test_composite4) {
    BOOST_CHECK_EQUAL(test::test_composite4(), true);
}

BOOST_AUTO_TEST_CASE(test_left_quotient1) {
    BOOST_CHECK_EQUAL(test::test_left_quotient1(), true);
}

BOOST_AUTO_TEST_CASE(test_left_quotient2) {
    BOOST_CHECK_EQUAL(test::test_left_quotient2(), true);
}

BOOST_AUTO_TEST_CASE(test_poststar) {
    BOOST_CHECK_EQUAL(test::test_poststar(), true);
}

BOOST_AUTO_TEST_CASE(test_prestar) {
    BOOST_CHECK_EQUAL(test::test_prestar(), true);
}

BOOST_AUTO_TEST_CASE(test_fomula_automaton) {
    BOOST_CHECK_EQUAL(test::test_fomula_automaton(), true);
}

// test for ll.
BOOST_AUTO_TEST_CASE(test_propositional_logic) {
    BOOST_CHECK_EQUAL(test::test_propositional_logic(), true);
}

BOOST_AUTO_TEST_CASE(test_ltl) {
    BOOST_CHECK_EQUAL(test::test_ltl(), true);
}

BOOST_AUTO_TEST_CASE(test_ctl) {
    BOOST_CHECK_EQUAL(test::test_ctl(), true);
}

BOOST_AUTO_TEST_SUITE_END()

max_stars_repo_name: LoringHe/fml

max_issues_repo_name: Jinlong-He/fml

max_forks_repo_name: Jinlong-He/fml


text:
#ifndef ARRAYDIFF_IO_PIPELINE_HH
#define ARRAYDIFF_IO_PIPELINE_HH

#include <boost/lockfree/spsc_queue.hpp>

#include <atomic>
#include <cassert>
#include <chrono>
#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <experimental/optional>
#include <functional>
#include <iostream>
#include <memory>
#include <random>
#include <thread>
#include <utility>

namespace arraydiff {
namespace io {

using std::atomic;
using std::experimental::optional;
using std::function;
using std::make_shared;
using std::shared_ptr;
using std::vector;

class Datum {
public:
  Datum(const uint8_t* ptr, size_t sz)
    : buffer(ptr), size(sz) {}

  const uint8_t* buffer;
  size_t size;
};

template <typename T = uint8_t>
class MutDatum {
public:
  static MutDatum Alloc(size_t sz) {
    T* buf = (T*)std::malloc(sizeof(T) * sz);
    return MutDatum(buf, sz);
  }

  MutDatum()
    : buffer(NULL), size(0) {}
  MutDatum(T* ptr, size_t sz)
    : buffer(ptr), size(sz) {}

  void dealloc() {
    std::free(this->buffer);
    this->buffer = NULL;
    this->size = 0;
  }

  T* buffer;
  size_t size;
};

template <typename Item>
class RandomAccessData {
public:
  virtual ~RandomAccessData() {}

  virtual size_t size() const = 0;
  virtual Item at(size_t index) const = 0;
};

template <typename Item>
class DataSource {
public:
  virtual ~DataSource() {}

  virtual Item next() = 0;
};

template <typename Item>
class CyclicDataSource : public virtual DataSource<Item> {
public:
  explicit CyclicDataSource(shared_ptr<RandomAccessData<Item>> data)
    : data_(data), counter_(0UL) {}
  virtual ~CyclicDataSource() {}

  virtual Item next() {
    size_t index = counter_;
    assert(index < data_->size());
    ++counter_;
    if (counter_ >= data_->size()) {
      counter_ = 0UL;
    }
    return data_->at(index);
  }

private:
  shared_ptr<RandomAccessData<Item>> data_;
  size_t counter_;
};

template <typename Item>
class PermuteDataSource : public virtual DataSource<Item> {
public:
  explicit PermuteDataSource(shared_ptr<RandomAccessData<Item>> data)
    : data_(data), permuted_(), counter_(0UL)
  {
    for (size_t i = 0; i < data_->size(); ++i) {
      permuted_.push_back(i);
    }
    assert(permuted_.size() == data_->size());
    if (data_->size() > 1) {
      // Use Fisher-Yates shuffle to generate a permutation.
      std::random_device rd;
      std::mt19937_64 rng(rd());
      for (size_t i = 0; i < data_->size() - 1; ++i) {
        std::uniform_int_distribution<size_t> dist_j(i, data_->size() - 1);
        size_t j = dist_j(rng);
        if (i != j) {
          auto tmp_i = permuted_.at(i);
          permuted_.at(i) = permuted_.at(j);
          permuted_.at(j) = tmp_i;
        }
      }
    }
  }

  virtual ~PermuteDataSource() {}

  virtual Item next() {
    size_t index = permuted_.at(counter_);
    assert(index < data_->size());
    ++counter_;
    if (counter_ >= data_->size()) {
      counter_ = 0UL;
    }
    return data_->at(index);
  }

private:
  shared_ptr<RandomAccessData<Item>> data_;
  vector<size_t> permuted_;
  size_t counter_;
};

template <typename Item>
class RandomSamplingDataSource : public virtual DataSource<Item> {
public:
  explicit RandomSamplingDataSource(shared_ptr<RandomAccessData<Item>> data)
    : data_(data), dist_(0UL, data->size() - 1UL)
  {
    std::random_device rd;
    rng_ = std::mt19937_64(rd());
  }
  virtual ~RandomSamplingDataSource() {}

  virtual Item next() {
    size_t index = dist_(rng_);
    assert(index < data_->size());
    return data_->at(index);
  }

private:
  shared_ptr<RandomAccessData<Item>> data_;
  std::uniform_int_distribution<size_t> dist_;
  std::mt19937_64 rng_;
};

template <typename Item>
class RandomPermuteDataSource : public virtual DataSource<Item> {
public:
  explicit RandomPermuteDataSource(shared_ptr<RandomAccessData<Item>> data)
    : data_(data), permuted_(), counter_(-1)
  {
    std::random_device rd;
    rng_ = std::mt19937_64(rd());
    for (size_t i = 0; i < data_->size(); ++i) {
      permuted_.push_back(i);
    }
    assert(permuted_.size() == data_->size());
  }

  virtual ~RandomPermuteDataSource() {}

  virtual Item next() {
    if (counter_ >= data_->size()) {
      std::clog << "DEBUG: RandomPermuteDataSource:"
          << " counter: " << counter_
          << " resetting..." << std::endl;
      // Use Fisher-Yates shuffle to generate a permutation.
      assert(permuted_.size() == data_->size());
      for (size_t i = 0; i < data_->size() - 1; ++i) {
        std::uniform_int_distribution<size_t> dist_j(i, data_->size() - 1);
        size_t j = dist_j(rng_);
        if (i != j) {
          auto tmp_i = permuted_.at(i);
          permuted_.at(i) = permuted_.at(j);
          permuted_.at(j) = tmp_i;
        }
      }
      counter_ = 0;
    }
    size_t index = permuted_.at(counter_);
    assert(index < data_->size());
    if (counter_ < 1000UL) {
      std::clog << "DEBUG: counter: " << counter_ << " index: " << index << std::endl;
    }
    ++counter_;
    return data_->at(index);
  }

private:
  shared_ptr<RandomAccessData<Item>> data_;
  std::mt19937_64 rng_;
  vector<size_t> permuted_;
  size_t counter_;
};

template <typename Item>
class RandomJoinDataSource /*: public virtual DataSource<Item>*/ {
public:
  explicit RandomJoinDataSource(vector<shared_ptr<DataSource<Item>>> srcs);
  //virtual ~RandomJoinDataSource() {}
};

template <typename Item>
using DataPipelineQueue = boost::lockfree::spsc_queue<Item, boost::lockfree::capacity<1024>>;

template <typename SrcItem, typename Item>
class DataPipelineWorker {
public:
  DataPipelineWorker(
      size_t worker_rank,
      size_t num_workers,
      shared_ptr<DataSource<SrcItem>> source,
      function<Item(size_t, size_t, SrcItem, std::mt19937_64*)> transform,
      shared_ptr<DataPipelineQueue<Item>> queue,
      shared_ptr<atomic<bool>> stop)
    : worker_rank_(worker_rank),
      num_workers_(num_workers),
      source_(source),
      transform_(transform),
      queue_(queue),
      stop_(stop) {}

  void run_loop() {
    std::random_device rd;
    std::mt19937_64 rng(rd());
    volatile bool stop_signal = false;
    for (; !stop_signal; stop_signal = stop_->load()) {
      SrcItem src_item = source_->next();
      Item item = (transform_)(worker_rank_, num_workers_, src_item, &rng);
      bool pushed = false;
      if (!stop_signal && queue_->push(item)) {
        pushed = true;
      }
      if (!pushed) {
        for (size_t t1 = 0; t1 < 10UL && !stop_signal; ++t1) {
          for (size_t t2 = 0; t2 < 100UL && !stop_signal; ++t2) {
            stop_signal = stop_->load();
          }
          if (!stop_signal && queue_->push(item)) {
            pushed = true;
            break;
          }
        }
      }
      if (!pushed) {
        while (!stop_signal) {
          std::this_thread::sleep_for(std::chrono::milliseconds(1));
          stop_signal = stop_->load();
          if (!stop_signal && queue_->push(item)) {
            pushed = true;
            break;
          }
        }
      }
    }
  }

private:
  size_t worker_rank_;
  size_t num_workers_;
  shared_ptr<DataSource<SrcItem>> source_;
  function<Item(size_t, size_t, SrcItem, std::mt19937_64*)> transform_;
  shared_ptr<DataPipelineQueue<Item>> queue_;
  shared_ptr<atomic<bool>> stop_;
};

template <typename Item>
class DataPipelineDataSource : public virtual DataSource<optional<Item>> {
public:
  template <typename SrcItem>
  DataPipelineDataSource(
      size_t num_workers,
      function<shared_ptr<DataSource<SrcItem>>(void)> source_gen,
      function<Item(size_t, size_t, SrcItem, std::mt19937_64*)> transform)
    : num_workers_(num_workers), counter_(0)
  {
    assert(num_workers_ >= 1);
    stop_ = make_shared<atomic<bool>>(false);
    for (size_t p = 0; p < num_workers_; ++p) {
      auto source = source_gen();
      auto queue = make_shared<DataPipelineQueue<Item>>();
      auto thread = make_shared<std::thread>([=]() {
        DataPipelineWorker<SrcItem, Item> worker(p, num_workers_, source, transform, queue, stop_);
        worker.run_loop();
      });
      queue_.push_back(queue);
      thread_.push_back(thread);
    }
  }

  virtual ~DataPipelineDataSource() {
    stop_->store(true);
    for (size_t p = 0; p < num_workers_; ++p) {
      thread_[p]->join();
    }
  }

  virtual optional<Item> next() {
    size_t queue_id = counter_ % num_workers_;
    Item item;
    while (!queue_[queue_id]->pop(item));
    ++counter_;
    return item;
  }

private:
  size_t num_workers_;
  //function<DataSource<SrcItem>(void)> source_gen_;
  size_t counter_;
  vector<shared_ptr<std::thread>> thread_;
  vector<shared_ptr<DataPipelineQueue<Item>>> queue_;
  shared_ptr<atomic<bool>> stop_;
};

} // namespace io
} // namespace arraydiff

#endif

ext: hh


text:
// Copyright 2018 The Simons Foundation, Inc. - All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <Eigen/Dense>
#include <algorithm>
#include <cmath>
#include <iostream>
#include "Utils/random_utils.hpp"
#include <vector>
#include "Utils/json_utils.hpp"
#include "abstract_hilbert.hpp"

#ifndef NETKET_CUSTOM_HILBERT_HPP
#define NETKET_CUSTOM_HILBERT_HPP

namespace netket {

/**
  User-Define Hilbert space
*/

class CustomHilbert : public AbstractHilbert {
  std::vector<double> local_;

  int nstates_;

  int size_;

 public:
  explicit CustomHilbert(const json &pars) {
    if (FieldExists(pars["Hilbert"], "QuantumNumbers")) {
      if (!pars["Hilbert"]["QuantumNumbers"].is_array()) {
        throw InvalidInputError("QuantumNumbers is not an array");
      }
      local_ = pars["Hilbert"]["QuantumNumbers"].get<std::vector<double>>();
    } else {
      throw InvalidInputError("QuantumNumbers are not defined");
    }

    if (FieldExists(pars["Hilbert"], "Size")) {
      size_ = pars["Hilbert"]["Size"];
      if (size_ <= 0) {
        throw InvalidInputError("Hilbert Size parameter must be positive");
      }
    } else {
      throw InvalidInputError("Hilbert space extent is not defined");
    }

    nstates_ = local_.size();
  }

  bool IsDiscrete() const override { return true; }

  int LocalSize() const override { return nstates_; }

  int Size() const override { return size_; }

  std::vector<double> LocalStates() const override { return local_; }

  void RandomVals(Eigen::VectorXd &state,
                  netket::default_random_engine &rgen) const override {
    std::uniform_int_distribution<int> distribution(0, nstates_ - 1);

    assert(state.size() == size_);

    // unconstrained random
    for (int i = 0; i < state.size(); i++) {
      state(i) = local_[distribution(rgen)];
    }
  }

  void UpdateConf(Eigen::VectorXd &v, const std::vector<int> &tochange,
                  const std::vector<double> &newconf) const override {
    assert(v.size() == size_);

    int i = 0;
    for (auto sf : tochange) {
      v(sf) = newconf[i];
      i++;
    }
  }
};

}  // namespace netket
#endif


text:
#include <stan/math/prim/scal.hpp>
#include <gtest/gtest.h>
#include <boost/random/mersenne_twister.hpp>
#include <boost/math/distributions.hpp>

TEST(ProbDistributionsPoisson, error_check) {
  using std::log;

  boost::random::mt19937 rng;
  EXPECT_NO_THROW(stan::math::poisson_rng(6, rng));

  EXPECT_THROW(stan::math::poisson_rng(-6, rng), std::domain_error);

  EXPECT_NO_THROW(stan::math::poisson_rng(1e9, rng));

  EXPECT_THROW(stan::math::poisson_rng(pow(2.0, 31), rng), std::domain_error);

  EXPECT_NO_THROW(stan::math::poisson_log_rng(6, rng));

  EXPECT_NO_THROW(stan::math::poisson_log_rng(-6, rng));

  EXPECT_NO_THROW(stan::math::poisson_log_rng(log(1e9), rng));

  EXPECT_THROW(stan::math::poisson_log_rng(log(pow(2.0, 31)), rng),
               std::domain_error);
}

TEST(ProbDistributionsPoisson, chiSquareGoodnessFitTest) {
  boost::random::mt19937 rng;
  int N = 1000;
  int K = boost::math::round(2 * std::pow(N, 0.4));
  boost::math::poisson_distribution<> dist(5);
  boost::math::chi_squared mydist(K - 1);

  int loc[K - 1];
  for (int i = 1; i < K; i++)
    loc[i - 1] = i - 1;

  int count = 0;
  double bin[K];
  double expect[K];
  for (int i = 0; i < K; i++) {
    bin[i] = 0;
    expect[i] = N * pdf(dist, i);
  }
  expect[K - 1] = N * (1 - cdf(dist, K - 1));

  while (count < N) {
    int a = stan::math::poisson_rng(5, rng);
    int i = 0;
    while (i < K - 1 && a > loc[i])
      ++i;
    ++bin[i];
    count++;
  }

  double chi = 0;

  for (int j = 0; j < K; j++)
    chi += ((bin[j] - expect[j]) * (bin[j] - expect[j]) / expect[j]);

  EXPECT_TRUE(chi < quantile(complement(mydist, 1e-6)));
}

TEST(ProbDistributionsPoisson, chiSquareGoodnessFitTest2) {
  using std::log;

  boost::random::mt19937 rng;
  int N = 1000;
  int K = boost::math::round(2 * std::pow(N, 0.4));
  boost::math::poisson_distribution<> dist(5);
  boost::math::chi_squared mydist(K - 1);

  int loc[K - 1];
  for (int i = 1; i < K; i++)
    loc[i - 1] = i - 1;

  int count = 0;
  double bin[K];
  double expect[K];
  for (int i = 0; i < K; i++) {
    bin[i] = 0;
    expect[i] = N * pdf(dist, i);
  }
  expect[K - 1] = N * (1 - cdf(dist, K - 1));

  while (count < N) {
    int a = stan::math::poisson_log_rng(log(5), rng);
    int i = 0;
    while (i < K - 1 && a > loc[i])
      ++i;
    ++bin[i];
    count++;
  }

  double chi = 0;

  for (int j = 0; j < K; j++)
    chi += ((bin[j] - expect[j]) * (bin[j] - expect[j]) / expect[j]);

  EXPECT_TRUE(chi < quantile(complement(mydist, 1e-6)));
}

max_stars_repo_name: sakrejda/math

max_issues_repo_name: sakrejda/math

max_forks_repo_name: sakrejda/math