diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 00000000..49c3f915 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "eacirc-streams"] + path = eacirc-streams + url = https://github.com/crocs-muni/eacirc-streams.git diff --git a/CMakeLists.txt b/CMakeLists.txt index 730ab852..2d4d5544 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,18 @@ cmake_minimum_required(VERSION 3.4) project(eacirc) -set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake) +find_package(Git) + +if (NOT EXISTS eacirc-streams/CMakeLists.txt) + execute_process( + COMMAND ${GIT_EXECUTABLE} submodule update --init --recursive + WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} + ) +endif() + +set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/eacirc-streams/eacirc-core/cmake) + +include(build_stream) # === Set CXX flags === if(CMAKE_COMPILER_IS_GNUCXX OR ${CMAKE_CXX_COMPILER_ID} MATCHES "Clang") @@ -14,19 +25,13 @@ else() endif() # === targets === -add_subdirectory(core) +add_subdirectory(eacirc-streams) + add_subdirectory(eacirc) add_subdirectory(solvers) -add_subdirectory(streams/estream) -add_subdirectory(streams/sha3) -add_subdirectory(streams/block) - -add_subdirectory(external/json) -add_subdirectory(external/pcg-cpp-0.98) - add_custom_target(config SOURCES - .travis.yml - appveyor.yml - config.json - ) + .travis.yml + appveyor.yml + config.json + ) diff --git a/cmake/build_stream.cmake b/cmake/build_stream.cmake deleted file mode 100644 index 283215f4..00000000 --- a/cmake/build_stream.cmake +++ /dev/null @@ -1,10 +0,0 @@ -function(build_stream NAME) - string(TOUPPER ${NAME} UPPERCASE_NAME) - - option(BUILD_${NAME} "Build stream ${NAME}" ON) - - if(BUILD_${NAME}) - target_link_libraries(eacirc ${NAME}) - target_compile_definitions(eacirc PRIVATE -DBUILD_${NAME}) - endif() -endfunction() diff --git a/cmake/detect_version.cmake b/cmake/detect_version.cmake deleted file mode 100644 index 8c4101b9..00000000 --- a/cmake/detect_version.cmake +++ /dev/null @@ -1,17 +0,0 @@ -find_package(Git) - -function(detect_version FILE) - if(GIT_FOUND) - execute_process( - COMMAND ${GIT_EXECUTABLE} describe --tags --dirty - WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} - OUTPUT_VARIABLE VERSION_TAG - OUTPUT_STRIP_TRAILING_WHITESPACE - ) - else(${VERSION_TAG}) - message(WARNING "Git not found") - set(GIT_COMMIT "n/a") - set(GIT_COMMIT_SHORT "n/a") - endif() - configure_file("${CMAKE_CURRENT_SOURCE_DIR}/${FILE}.in" "${CMAKE_CURRENT_SOURCE_DIR}/${FILE}" @ONLY) -endfunction(detect_version) diff --git a/core/CMakeLists.txt b/core/CMakeLists.txt deleted file mode 100644 index bcbbcab5..00000000 --- a/core/CMakeLists.txt +++ /dev/null @@ -1,31 +0,0 @@ -add_library(core STATIC - builtins - cmd - dataset - debug - iterators - json - logger - memory - optional - random - statistics - stream - traits - variant - vec - view - ) - -target_include_directories(core INTERFACE - ${CMAKE_CURRENT_SOURCE_DIR}/.. - ) - -set_target_properties(core PROPERTIES - LINKER_LANGUAGE CXX - ) - -target_link_libraries(core - json - pcg - ) diff --git a/core/builtins.h b/core/builtins.h deleted file mode 100644 index 8f1c704b..00000000 --- a/core/builtins.h +++ /dev/null @@ -1,85 +0,0 @@ -#pragma once - -#include - -namespace core { -namespace builtins { - -int count_trailing_zeros(std::uint64_t x) { -#ifdef __GNUC__ - return __builtin_ctzll(x); -#elif _MSC_VER - return __lzcnt64(x); -#elif __CUDACC__ - return __ffsll(*reinterpret_cast(&x)) - 1; -#else -#warning "CAUTION! Unsupported compiler! Slow trivial implementation is used" - if (x == 0) - return 64; - int n = 1; - if ((x & 0xffffffff) == 0) { - x >>= 32; - n += 32; - } - if ((x & 0xffff) == 0) { - x >>= 16; - n += 16; - } - if ((x & 0xff) == 0) { - x >>= 8; - n += 8; - } - if ((x & 0xf) == 0) { - x >>= 4; - n += 4; - } - if ((x & 0x3) == 0) { - x >>= 2; - n += 2; - } - return n -= x & 0x1; -#endif -} - -int count_trailing_zeros(std::uint32_t x) { -#ifdef __GNUC__ - return __builtin_ctz(x); -#elif _MSC_VER - return __lzcnt(x); -#elif __CUDACC__ - return __ffs(*reinterpret_cast(&x)) - 1; -#else -#warning "CAUTION! Unsupported compiler! Slow trivial implementation is used" - if (x == 0) - return 32; - int n = 1; - if ((x & 0xffff) == 0) { - x >>= 16; - n += 16; - } - if ((x & 0xff) == 0) { - x >>= 8; - n += 8; - } - if ((x & 0xf) == 0) { - x >>= 4; - n += 4; - } - if ((x & 0x3) == 0) { - x >>= 2; - n += 2; - } - return n -= x & 0x1; -#endif -} - -int count_trailing_zeros(std::uint16_t x) { - return count_trailing_zeros(static_cast(x)); -} - -int count_trailing_zeros(std::uint8_t x) { - return count_trailing_zeros(static_cast(x)); -} - -} // namespace builtins -} // namespace core diff --git a/core/cmd.h b/core/cmd.h deleted file mode 100644 index f8493afa..00000000 --- a/core/cmd.h +++ /dev/null @@ -1,88 +0,0 @@ -#pragma once - -#include "variant.h" -#include "view.h" -#include -#include -#include - -template struct cmd { - struct proxy { - template using pointer = T Config::*; - - using value_type = core::variant, pointer>; - - template - proxy(pointer ptr) - : _ptr(ptr) {} - - void parse(Config& cfg, std::string val) const { - switch (_ptr.index()) { - case value_type::template index_of>(): { - auto ptr = _ptr.template as>(); - cfg.*ptr = true; - break; - } - case value_type::template index_of>(): { - auto ptr = _ptr.template as>(); - cfg.*ptr = val; - break; - } - } - } - - private: - value_type _ptr; - }; - - struct opt { - const std::string flag; - const std::string name; - const std::string desc; - const proxy value; - }; - - cmd(std::initializer_list opts) - : _opts(opts) { - for (const auto& opt : _opts) { - if (!_mapping.emplace(opt.flag, opt.value).second) - throw std::invalid_argument("an option \"" + opt.flag + "\" is already defined."); - - if (!_mapping.emplace(opt.name, opt.value).second) - throw std::invalid_argument("an option \"" + opt.name + "\" is already defined."); - } - } - - Config parse(view args) { - Config config{}; - - for (const std::string arg : args.drop(1)) { - auto n = arg.find('='); - auto fst = arg.substr(0, n); - auto snd = (n != std::string::npos) ? arg.substr(n + 1) : ""; - - auto it = _mapping.find(fst); - - if (it == _mapping.end()) - throw std::runtime_error("an unknown cmd option \"" + fst + "\""); - if (arg.back() == '=') - throw std::runtime_error("an empty value for an option \"" + fst + "\" is not allowed"); - - it->second.parse(config, snd); - } - return config; - } - - void print(std::ostream& out) const { - out << "Options:" << std::endl; - for (auto& opt : _opts) { - std::string name = " " + opt.flag + ", " + opt.name + " "; - - out << std::left << std::setw(28) << name << opt.desc << std::endl; - } - } - -private: - std::vector _opts; - std::unordered_map _mapping; -}; diff --git a/core/dataset.h b/core/dataset.h deleted file mode 100644 index 82145975..00000000 --- a/core/dataset.h +++ /dev/null @@ -1,47 +0,0 @@ -#pragma once - -#include "iterators.h" -#include "view.h" -#include - -struct dataset { - using storage = std::vector; - - using pointer = storage::pointer; - using const_pointer = storage::const_pointer; - - using iterator = step_iterator>; - using const_iterator = step_iterator>; - - dataset() - : _tvsize(0) - , _data() {} - - dataset(unsigned tv_size, std::size_t n) - : _tvsize(tv_size) - , _data(tv_size * n) {} - - dataset(dataset&&) = default; - dataset(dataset const&) = default; - - dataset& operator=(dataset&&) = default; - dataset& operator=(dataset const&) = default; - - iterator begin() { return {{_data.begin(), _tvsize}, _tvsize}; } - iterator end() { return {{_data.end(), _tvsize}, _tvsize}; } - - const_iterator begin() const { return {{_data.begin(), _tvsize}, _tvsize}; } - const_iterator end() const { return {{_data.end(), _tvsize}, _tvsize}; } - - std::size_t tvsize() const { return _tvsize; } - std::size_t size() const { return _data.size() / _tvsize; } - - pointer rawdata() { return _data.data(); } - const_pointer rawdata() const { return _data.data(); } - - std::size_t rawsize() const { return _data.size(); } - -private: - unsigned _tvsize; - storage _data; -}; diff --git a/core/debug.h b/core/debug.h deleted file mode 100644 index c89e16bb..00000000 --- a/core/debug.h +++ /dev/null @@ -1,31 +0,0 @@ -#pragma once - -#include -#include - -namespace core { -namespace debug { - -struct assertion_failure : std::logic_error { - assertion_failure(const char* message) - : std::logic_error(message) {} -}; - -} // namespace debug -} // namespace core - -#define STRINGIFY_IMPL(x) #x -#define STRINGIFY(x) STRINGIFY_IMPL(x) - -#define ASSERT_ALLWAYS(expr_) \ - if (!(expr_)) \ - throw ::core::debug::assertion_failure("Assertion failure in function " STRINGIFY( \ - __FUNCTION__) " in " __FILE__ ":" STRINGIFY(__LINE__)); - -#ifdef NDEBUG -#define ASSERT(expr_) -#else -#define ASSERT(expr_) ASSERT_ALLWAYS(expr_) -#endif - -#define ASSERT_UNREACHABLE() ASSERT(false) diff --git a/core/iterators.h b/core/iterators.h deleted file mode 100644 index 923ca3aa..00000000 --- a/core/iterators.h +++ /dev/null @@ -1,249 +0,0 @@ -#pragma once - -#include "view.h" - -namespace _impl { - - template - using enable_if_forward_t = typename std::enable_if< - std::is_base_of::iterator_category>::value, - T>::type; - - template - using enable_if_bidirectional_t = typename std::enable_if< - std::is_base_of::iterator_category>::value, - T>::type; - - template - using enable_if_random_access_t = typename std::enable_if< - std::is_base_of::iterator_category>::value, - T>::type; - -} // namespace _impl - -template struct step_iterator { - using pointer = typename std::iterator_traits::pointer; - using reference = typename std::iterator_traits::reference; - using value_type = typename std::iterator_traits::value_type; - using difference_type = typename std::iterator_traits::difference_type; - using iterator_category = typename std::iterator_traits::iterator_category; - - template > - step_iterator() - : _it() - , _n(0) {} - - step_iterator(Iterator it, difference_type n) - : _it(it) - , _n(n) {} - - step_iterator& operator++() { - std::advance(_it, _n); - return *this; - } - - step_iterator operator++(int) { - auto self = *this; - ++(*this); - return self; - } - - reference operator*() const { return *_it; } - pointer operator->() const { return &*_it; } - - bool operator==(step_iterator const& rhs) const { return std::distance(_it, rhs._it) < _n; } - bool operator!=(step_iterator const& rhs) const { return !(*this == rhs); } - - auto operator--() -> _impl::enable_if_bidirectional_t { - std::advance(_it, -_n); - return *this; - } - - auto operator--(int) -> _impl::enable_if_bidirectional_t { - auto self = *this; - --(*this); - return self; - } - - auto operator+=(difference_type n) - -> _impl::enable_if_random_access_t { - _it += n * _n; - return *this; - } - - auto operator-=(difference_type n) - -> _impl::enable_if_random_access_t { - return *this += -n; - } - - auto operator+(difference_type n) const - -> _impl::enable_if_random_access_t { - auto temp = *this; - return temp += n; - } - - auto operator-(difference_type n) const - -> _impl::enable_if_random_access_t { - auto temp = *this; - return temp -= n; - } - - auto operator[](difference_type n) -> _impl::enable_if_random_access_t { - return *(*this + n); - } - - auto operator-(step_iterator const& rhs) const - -> _impl::enable_if_random_access_t { - ASSERT((_it - rhs._it) % _n == 0); - return (_it - rhs._it) / _n; - } - - auto operator<(step_iterator const& rhs) const - -> _impl::enable_if_random_access_t { - ASSERT(_n == rhs._n); - return _it < rhs._it; - } - - auto operator>(step_iterator const& rhs) const - -> _impl::enable_if_random_access_t { - ASSERT(_n == rhs._n); - return _it > rhs._it; - } - - auto operator>=(step_iterator const& rhs) const - -> _impl::enable_if_random_access_t { - return !(*this < rhs); - } - - auto operator<=(step_iterator const& rhs) const - -> _impl::enable_if_random_access_t { - return !(*this > rhs); - } - - friend auto operator+(difference_type lhs, step_iterator const& rhs) - -> _impl::enable_if_random_access_t { - return rhs + lhs; - } - -private: - Iterator _it; - difference_type _n; -}; - -template struct view_iterator { - using value_type = view; - using pointer = value_type const*; - using reference = value_type const&; - using difference_type = typename std::iterator_traits::difference_type; - using iterator_category = typename std::iterator_traits::iterator_category; - - template > - view_iterator() - : _view() {} - - view_iterator(Iterator beg, Iterator end) - : view_iterator(value_type(beg, end)) {} - - view_iterator(Iterator it, difference_type n) - : view_iterator(value_type(it, n)) {} - - view_iterator(value_type view) - : _view(view) {} - - view_iterator& operator++() { - ++(_view._beg); - ++(_view._end); - return *this; - } - - view_iterator operator++(int) { - auto self = *this; - ++(*this); - return self; - } - - reference operator*() const { return _view; } - pointer operator->() const { return &_view; } - - bool operator==(view_iterator const& rhs) const { return _view._end == rhs._view._end; } - bool operator!=(view_iterator const& rhs) const { return !(*this == rhs); } - - auto operator--() -> _impl::enable_if_bidirectional_t { - --(_view._beg); - --(_view._end); - return *this; - } - - auto operator--(int) -> _impl::enable_if_bidirectional_t { - auto self = *this; - --(*this); - return self; - } - - auto operator+=(difference_type n) - -> _impl::enable_if_random_access_t { - _view._beg += n; - _view._end += n; - return *this; - } - - auto operator-=(difference_type n) - -> _impl::enable_if_random_access_t { - return *this += -n; - } - - auto operator+(difference_type n) const - -> _impl::enable_if_random_access_t { - auto temp = *this; - return temp += n; - } - - auto operator-(difference_type n) const - -> _impl::enable_if_random_access_t { - auto temp = *this; - return temp -= n; - } - - auto operator[](difference_type n) -> _impl::enable_if_random_access_t { - return *(*this + n); - } - - auto operator-(view_iterator const& rhs) const - -> _impl::enable_if_random_access_t { - ASSERT(_view._beg - rhs._view._beg == _view._end - rhs._view._end); - return _view._beg - rhs._view._beg; - } - - auto operator<(view_iterator const& rhs) const - -> _impl::enable_if_random_access_t { - ASSERT(_view._end < rhs._view._end); - return _view._beg < rhs._view._beg; - } - - auto operator>(view_iterator const& rhs) const - -> _impl::enable_if_random_access_t { - ASSERT(_view._end > rhs._view._end); - return _view._beg > rhs._view._beg; - } - - auto operator>=(view_iterator const& rhs) const - -> _impl::enable_if_random_access_t { - return !(*this < rhs); - } - - auto operator<=(view_iterator const& rhs) const - -> _impl::enable_if_random_access_t { - return !(*this > rhs); - } - - friend auto operator+(difference_type lhs, view_iterator const& rhs) - -> _impl::enable_if_random_access_t { - return rhs + lhs; - } - -private: - value_type _view; -}; \ No newline at end of file diff --git a/core/json.h b/core/json.h deleted file mode 100644 index 3fe7c7ee..00000000 --- a/core/json.h +++ /dev/null @@ -1,3 +0,0 @@ -#include - -using ::nlohmann::json; diff --git a/core/logger.cc b/core/logger.cc deleted file mode 100644 index 13d4f79f..00000000 --- a/core/logger.cc +++ /dev/null @@ -1,25 +0,0 @@ -#include "logger.h" -#include -#include - -std::ostream& logger::out() { - return std::cout << logger::time() << " "; -} - -std::string logger::date() { - std::time_t t = std::time(nullptr); - std::tm tm = *std::localtime(&t); - - char str[100]; - std::strftime(str, sizeof(str), "%d %b %Y", &tm); - return str; -} - -std::string logger::time() { - std::time_t t = std::time(nullptr); - std::tm tm = *std::localtime(&t); - - char str[100]; - std::strftime(str, sizeof(str), "%H:%M:%S", &tm); - return str; -} diff --git a/core/logger.h b/core/logger.h deleted file mode 100644 index 58836975..00000000 --- a/core/logger.h +++ /dev/null @@ -1,19 +0,0 @@ -#pragma once - -#include - -struct logger { - static void warning(std::string msg) { logger::warning() << msg << std::endl; } - static void error(std::string msg) { logger::error() << msg << std::endl; } - static void info(std::string msg) { logger::info() << msg << std::endl; } - - static std::ostream& warning() { return logger::out() << "[warning] "; } - static std::ostream& error() { return logger::out() << "[error] "; } - static std::ostream& info() { return logger::out() << "[info] "; } - - static std::string date(); - static std::string time(); - -private: - static std::ostream& out(); -}; diff --git a/core/memory.h b/core/memory.h deleted file mode 100644 index 888cac97..00000000 --- a/core/memory.h +++ /dev/null @@ -1,20 +0,0 @@ -#pragma once - -#include - -namespace std { - - template - auto make_unique(Args&&... args) -> - typename std::enable_if::value, std::unique_ptr>::type { - return std::unique_ptr{new T(std::forward(args)...)}; - } - - template - auto make_unique(std::size_t size) -> - typename std::enable_if::value, std::unique_ptr>::type { - using E = typename std::remove_extent::type; - return std::unique_ptr{new E[size]()}; - } - -} // namespace std diff --git a/core/optional.h b/core/optional.h deleted file mode 100644 index db272c25..00000000 --- a/core/optional.h +++ /dev/null @@ -1,233 +0,0 @@ -#pragma once - -#include -#include -#include - -namespace core { - -struct nullopt_t { - constexpr explicit nullopt_t() = default; -}; - -struct in_place_t { - constexpr explicit in_place_t() = default; -}; - -struct bad_optional_access : std::logic_error { - bad_optional_access() - : logic_error("Optional does not contain value!") {} -}; - -template struct optional { - using value_type = T; - - constexpr optional() noexcept : optional(nullopt_t{}) {} - constexpr optional(nullopt_t) noexcept : _set(false) {} - - optional(optional&& other) - : _set(other._set) { - if (_set) - _construct(other._unsafe_move()); - } - - optional(const optional& other) - : _set(other._set) { - if (_set) - _construct(other._unsafe_ref()); - } - - optional(value_type&& value) noexcept : _set(true) { _construct(std::move(value)); } - - optional(const value_type& value) noexcept : _set(true) { _construct(value); } - - template - explicit optional(in_place_t, Args&&... args) - : _set(true) { - _construct(std::forward(args)...); - } - - ~optional() { reset(); } - - optional& operator=(nullopt_t) { - reset(); - return *this; - } - - optional& operator=(optional&& other) { - if (_set && other._set) - _unsafe_ref() = other._unsafe_move(); - else if (_set) - _destruct(); - else if (other._set) - _construct(other._unsafe_move()); - return *this; - } - - optional& operator=(const optional& other) { - if (_set && other._set) - _unsafe_ref() = other._unsafe_ref(); - else if (_set) - _destruct(); - else if (other._set) - _construct(other._unsafe_ref()); - return *this; - } - - template optional& operator=(U&& value) { - if (_set) - _unsafe_ref() = std::forward(value); - else - _construct(std::forward(value)); - return *this; - } - - void swap(optional& other) { - using std::swap; - - if (_set && other._set) - swap(_unsafe_ref(), other._unsafe_ref()); - else if (_set) { - other._construct(_unsafe_move()); - _destruct(); - } else if (other._set) { - _construct(other._unsafe_move()); - other._destruct(); - } - } - - friend void swap(optional& lhs, optional& rhs) { lhs.swap(rhs); } - -public: - bool has_value() const { return _set; } - - void reset() { - if (_set) - _destruct(); - } - - template void emplace(Args&&... args) { - reset(); - _construct(std::forward(args)...); - } - - template - auto emplace(std::initializer_list ilist, Args&&... args) -> typename std::enable_if< - std::is_constructible&, Args&&...>::value>::type { - reset(); - _construct(ilist, std::forward(args)...); - } - - value_type& value() & { - if (!_set) - throw bad_optional_access{}; - return _unsafe_ref(); - } - - const value_type& value() const& { - if (!_set) - throw bad_optional_access{}; - return _unsafe_ref(); - } - - value_type&& value() && { std::move(value()); } - const value_type&& value() const&& { std::move(value()); } - - template value_type value_or(U&& default_value) const& { - return has_value() ? value() : static_cast(std::forward(default_value)); - } - - template value_type value_or(U&& default_value) && { - return has_value() ? std::move(value()) - : static_cast(std::forward(default_value)); - } - -public: - template - auto apply(Fn&& fn, Args&&... args) & -> optional::type> { - if (has_value()) - return fn(_unsafe_ref(), std::forward(args)...); - return {nullopt_t{}}; - } - - template - auto apply(Fn&& fn, Args&&... args) && -> optional::type> { - if (has_value()) - return fn(_unsafe_move(), std::forward(args)...); - return {nullopt_t{}}; - } - - template - auto apply(Fn&& fn, Args&&... args) const& -> optional::type> { - if (has_value()) - return fn(_unsafe_ref(), std::forward(args)...); - return {nullopt_t{}}; - } - -public: - operator bool() const { return has_value(); } - - value_type* operator->() { return &value(); } - const value_type* operator->() const { return &value(); } - - value_type& operator*() & { return value(); } - const value_type& operator*() const& { return value(); } - - value_type&& operator*() && { return value(); } - const value_type&& operator*() const&& { return value(); } - -public: - friend bool operator==(const optional& lhs, const optional& rhs) { - if (lhs && rhs) - return (*lhs) == (*rhs); - return !(lhs || rhs); - } - - friend bool operator!=(const optional& lhs, const optional& rhs) { - if (lhs && rhs) - return (*lhs) != (*rhs); - return lhs || rhs; - } - - friend bool operator==(const optional& lhs, nullopt_t) { return !lhs; } - friend bool operator==(nullopt_t, const optional& rhs) { return !rhs; } - - friend bool operator!=(const optional& lhs, nullopt_t) { return lhs; } - friend bool operator!=(nullopt_t, const optional& rhs) { return rhs; } - - friend bool operator==(const optional& lhs, const value_type& rhs) { - return lhs ? *lhs == rhs : false; - } - friend bool operator==(const value_type& lhs, const optional& rhs) { - return rhs ? lhs == *rhs : false; - } - - friend bool operator!=(const optional& lhs, const value_type& rhs) { - return lhs ? *lhs != rhs : true; - } - friend bool operator!=(const value_type& lhs, const optional& rhs) { - return rhs ? lhs != *rhs : true; - } - -private: - bool _set; - typename std::aligned_storage::type _storage; - - value_type& _unsafe_ref() { return reinterpret_cast(_storage); } - const value_type& _unsafe_ref() const { return reinterpret_cast(_storage); } - - value_type&& _unsafe_move() { return std::move(_unsafe_ref()); } - const value_type&& _unsafe_move() const { return std::move(_unsafe_ref()); } - - template void _construct(Args&&... args) { - new (&_storage) value_type(std::forward(args)...); - _set = true; - } - - void _destruct() { - _unsafe_ref().~value_type(); - _set = false; - } -}; - -} // namespace core diff --git a/core/random.h b/core/random.h deleted file mode 100644 index 4e51b1f9..00000000 --- a/core/random.h +++ /dev/null @@ -1,67 +0,0 @@ -#pragma once - -#include "variant.h" -#include -#include -#include -#include - -template struct seed_seq_from { - using result_type = std::uint_least32_t; - - template - seed_seq_from(Args&&... args) - : _rng(std::forward(args)...) {} - - seed_seq_from(seed_seq_from const&) = delete; - seed_seq_from& operator=(seed_seq_from const&) = delete; - - template void generate(I beg, I end) { - for (; beg != end; ++beg) - *beg = result_type(_rng()); - } - - constexpr std::size_t size() const { - return (sizeof(typename Generator::result_type) > sizeof(result_type) && - Generator::max() > ~std::size_t(0UL)) - ? ~std::size_t(0UL) - : size_t(Generator::max()); - } - -private: - Generator _rng; -}; - -struct polymorphic_generator { - using result_type = std::uint8_t; - - template polymorphic_generator(const std::string& type, Seeder&& seeder) { - if (type == "mt19937") - _rng.emplace(std::forward(seeder)); - if (type == "pcg32") - _rng.emplace(std::forward(seeder)); - else - throw std::runtime_error("requested random generator named \"" + type + - "\" is not valid polymorphic generator"); - } - - static result_type min() { return std::numeric_limits::min(); } - static result_type max() { return std::numeric_limits::max(); } - - result_type operator()() { - switch (_rng.index()) { - case decltype(_rng)::index_of(): - return std::uniform_int_distribution()(_rng.as()); - case decltype(_rng)::index_of(): - return std::uniform_int_distribution()(_rng.as()); - } - - throw std::logic_error("canot call polymorphic generator with undefined generator"); - } - -private: - core::variant _rng; -}; - -using default_random_generator = pcg32; -using default_seed_source = seed_seq_from; diff --git a/core/statistics.cc b/core/statistics.cc deleted file mode 100644 index 80e550c6..00000000 --- a/core/statistics.cc +++ /dev/null @@ -1,219 +0,0 @@ -#include "statistics.h" -#include -#include -#include - -/** gamma function - * taken from http://www.crbond.com/math.htm (Gamma function in C/C++ for real - * arguments, ported from Zhang and Jin) - * returns 1e308 if argument is a negative integer or 0 or if argument exceeds - * 171. - * @param x argument - * @return Gamma(argument) - */ -static double gamma0(double x) { - /* gamma function. - Algorithms and coefficient values from "Computation of Special - Functions", Zhang and Jin, John Wiley and Sons, 1996. - (C) 2003, C. Bond. All rights reserved. - taken from http://www.crbond.com/math.htm */ - int i, k, m; - double ga, gr, r = 1.0, z; - - static double g[] = {1.0, - 0.5772156649015329, - -0.6558780715202538, - -0.420026350340952e-1, - 0.1665386113822915, - -0.421977345555443e-1, - -0.9621971527877e-2, - 0.7218943246663e-2, - -0.11651675918591e-2, - -0.2152416741149e-3, - 0.1280502823882e-3, - -0.201348547807e-4, - -0.12504934821e-5, - 0.1133027232e-5, - -0.2056338417e-6, - 0.6116095e-8, - 0.50020075e-8, - -0.11812746e-8, - 0.1043427e-9, - 0.77823e-11, - -0.36968e-11, - 0.51e-12, - -0.206e-13, - -0.54e-14, - 0.14e-14}; - - if (x > 171.0) - return 1e308; // This value is an overflow flag. - if (x == int(x)) { - if (x > 0.0) { - ga = 1.0; // use factorial - for (i = 2; i < x; i++) { - ga *= i; - } - } else - ga = 1e308; - } else { - if (std::fabs(x) > 1.0) { - z = std::fabs(x); - m = int(z); - r = 1.0; - for (k = 1; k <= m; k++) { - r *= (z - k); - } - z -= m; - } else - z = x; - gr = g[24]; - for (k = 23; k >= 0; k--) { - gr = gr * z + g[k]; - } - ga = 1.0 / (gr * z); - if (std::fabs(x) > 1.0) { - ga *= r; - if (x < 0.0) { - ga = -M_PI / (x * ga * std::sin(M_PI * x)); - } - } - } - return ga; -} - -/** incomplete gamma function - * taken from http://www.crbond.com/math.htm (Incomplete Gamma function, ported - * from Zhang and Jin) - * @param a - * @param x - * @param gin - * @param gim - * @param gip - * @return - */ -static int incog(double a, double x, double& gin, double& gim, double& gip) { - double xam, r, s, ga, t0; - int k; - - if ((a < 0.0) || (x < 0)) - return 1; - xam = -x + a * std::log(x); - if ((xam > 700) || (a > 170.0)) - return 1; - if (x == 0.0) { - gin = 0.0; - gim = gamma0(a); - gip = 0.0; - return 0; - } - if (x <= 1.0 + a) { - s = 1.0 / a; - r = s; - for (k = 1; k <= 60; k++) { - r *= x / (a + k); - s += r; - if (std::fabs(r / s) < 1e-15) - break; - } - gin = std::exp(xam) * s; - ga = gamma0(a); - gip = gin / ga; - gim = ga - gin; - } else { - t0 = 0.0; - for (k = 60; k >= 1; k--) { - t0 = (k - a) / (1.0 + k / (x + t0)); - } - gim = std::exp(xam) / (x + t0); - ga = gamma0(a); - gin = ga - gim; - gip = 1.0 - gim / ga; - } - return 0; -} - -/** function converting Chi^2 value to corresponding p-value - * taken from - * http://www.codeproject.com/Articles/432194/How-to-Calculate-the-Chi-Squared-P-Value - * note: do not use full implementation from above website, it is not precise - * enough for small inputs - * @param Dof degrees of freedom - * @param Cv Chi^2 value - * @return p-value - */ -static double chisqr(int Dof, double Cv) { - if (Cv < 0 || Dof < 1) { - return 1; - } - double K = (double(Dof)) * 0.5; - double X = Cv * 0.5; - if (Dof == 2) { - return std::exp(-1.0 * X); - } - double gin, gim, gip; - incog(K, X, gin, gim, gip); // compute incomplete gamma function - double PValue = gim; - PValue /= gamma0(K); // divide by gamma function value - return PValue; -} - -namespace core { -namespace statistics { - -double two_sample_chisqr(const std::vector& distribution_a, - const std::vector& distribution_b) { - // using two-smaple Chi^2 test - // (http://www.itl.nist.gov/div898/software/dataplot/refman1/auxillar/chi2samp.htm) - - double k1 = 1; - double k2 = 1; - double chisqr_value = 0; - int dof = 0; - - for (unsigned i = 0; i != distribution_a.size(); ++i) { - auto sum = distribution_a[i] + distribution_b[i]; - if (sum > 5) { - dof++; - chisqr_value += std::pow(k1 * distribution_a[i] - k2 * distribution_b[i], 2) / sum; - } - } - dof--; // last category is fully determined by others - - return chisqr(dof, chisqr_value); -} - -double ks_critical_value(std::size_t number_of_samples, unsigned significance_level) { - if (number_of_samples <= 35) - throw std::runtime_error("Too few samples for KS critical value (<=35)."); - - switch (significance_level) { - case 10: - return 1.224 / std::sqrt(double(number_of_samples)); - case 5: - return 1.358 / std::sqrt(double(number_of_samples)); - case 1: - return 1.628 / sqrt(double(number_of_samples)); - default: - throw std::runtime_error("Significance level must be 1, 5, or 10."); - } -} - -double ks_uniformity_test(std::vector samples) { - std::sort(samples.begin(), samples.end()); - - if (samples.front() < 0 || samples.back() > 1) - throw std::out_of_range("Cannot run K-S test, data out of range."); - - double pvalue = 0; - double n = samples.size(); - - for (std::size_t i = 0; i != samples.size(); ++i) { - double temp = std::max(samples[i] - i / n, (i + 1) / n - samples[i]); - pvalue = std::max(pvalue, temp); - } - return pvalue; -} - -} // namespace statistics -} // namespace core diff --git a/core/statistics.h b/core/statistics.h deleted file mode 100644 index 92914f5a..00000000 --- a/core/statistics.h +++ /dev/null @@ -1,21 +0,0 @@ -#pragma once - -#include -#include - -namespace core { -namespace statistics { - -struct Test { - double critical_value; - double test_statistic; -}; - -double two_sample_chisqr(const std::vector& distribution_a, - const std::vector& distribution_b); - -double ks_uniformity_test(std::vector samples); -double ks_uniformity_critical_value(std::size_t number_of_samples, unsigned significance_level); - -} // namespace statistics -} // namespace core diff --git a/core/stream.h b/core/stream.h deleted file mode 100644 index bb3c6cc0..00000000 --- a/core/stream.h +++ /dev/null @@ -1,25 +0,0 @@ -#pragma once - -#include "json.h" -#include "memory.h" -#include "view.h" -#include "dataset.h" -#include - -using value_type = std::uint8_t; -using vec_view = view::const_iterator>; - -struct stream { - virtual ~stream() = default; - - virtual vec_view next() = 0; - - std::size_t osize() const { return _osize; } - -protected: - stream(std::size_t osize) - : _osize(osize) {} - -private: - const std::size_t _osize; -}; diff --git a/core/traits.h b/core/traits.h deleted file mode 100644 index e3948616..00000000 --- a/core/traits.h +++ /dev/null @@ -1,83 +0,0 @@ -#pragma once - -#include - -namespace core { - -/** - * @brief negation - */ - -template struct negation : std::integral_constant {}; - -/** - * @brief conjunction<...B> - */ - -template struct conjunction : std::true_type {}; -template -struct conjunction - : std::conditional, std::false_type>::type {}; - -/** - * @brief disjunction<...B> - */ - -template struct disjunction : std::false_type {}; -template -struct disjunction - : std::conditional>::type {}; - -/** - * @brief is_specialization - */ - -template class> struct is_specialization : std::false_type {}; - -template