From 6cb54740b450aa7ff1c91b790c93553d8a61d6b1 Mon Sep 17 00:00:00 2001 From: "Timur A. Fatkhullin" Date: Wed, 17 Jun 2026 13:09:06 +0300 Subject: [PATCH] ... --- CMakeLists.txt | 14 +-- include/snipplib/containers/snplib_hmap.h | 105 ++++++++++++++++++++++ include/snipplib/hmap/snplib_hmap.h | 67 -------------- 3 files changed, 112 insertions(+), 74 deletions(-) create mode 100644 include/snipplib/containers/snplib_hmap.h delete mode 100644 include/snipplib/hmap/snplib_hmap.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 4bb84a1..bb57d0d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,11 +3,12 @@ project(snipplib VERSION 0.1.0 LANGUAGES CXX) option(BUILD_EXAMPLES "Build examples" ON) - -set(LIB_HEADERS include/snipplib/concepts/snplib_concepts.h - include/snipplib/utils/snplib_hash.h - include/snipplib/utils/snplib_utils.h - include/snipplib/hmap/snplib_hmap.h) +set(LIB_HEADERS + include/snipplib/concepts/snplib_concepts.h + include/snipplib/utils/snplib_hash.h + include/snipplib/utils/snplib_utils.h + include/snipplib/containers/snplib_hmap.h +) add_library(${PROJECT_NAME} INTERFACE ${LIB_HEADERS}) target_compile_features(${PROJECT_NAME} INTERFACE cxx_std_23) @@ -18,7 +19,6 @@ target_include_directories( "$" ) - set(EXAM_STRING str_exam) add_executable(${EXAM_STRING} examples/str_exam.cpp) -target_link_libraries(${EXAM_STRING} ${PROJECT_NAME}) \ No newline at end of file +target_link_libraries(${EXAM_STRING} ${PROJECT_NAME}) diff --git a/include/snipplib/containers/snplib_hmap.h b/include/snipplib/containers/snplib_hmap.h new file mode 100644 index 0000000..975277e --- /dev/null +++ b/include/snipplib/containers/snplib_hmap.h @@ -0,0 +1,105 @@ +#pragma once + +#include +#include +#include +#include + +#include "../concepts/snplib_concepts.h" +#include "../utils/snplib_hash.h" + +namespace snplib +{ + + +template +class HeterogenMap +{ +protected: + // actuall map key type + typedef std::pair key_t; + + struct KeyHash { + static size_t operator()(key_t key) + { + size_t hash = 0; + + snplib_hash_combine(hash, key.first, key.second); + + return hash; + } + }; + + template + inline static std::unordered_map _umap{}; + + template + inline static std::unordered_map, KeyHash> _getter{}; + + template + inline static std::unordered_map, KeyHash> _setter{}; + + std::vector> _clearFunc{}; + +public: + enum Error { ERROR_OK, ERROR_NO_ELEM }; + + HeterogenMap() = default; + + ~HeterogenMap() + { + for (auto& func : _clearFunc) { + func(); + } + } + + template + size_t push(KeyT const& key, VT&& value) + { + auto kk = std::make_pair(this, key); + auto it = _umap>.emplace(kk, std::forward(value)); + + if (it.second) { + _getter>.emplace(kk, [it = it.first]() { return it->second; }); + + _clearFunc.emplace_back([it = it.first]() { _umap>.erase(it); }); + } // here the element already exists (just update its value) + + return _clearFunc.size(); + } + + template + size_t push(KeyT const& key, VT&& value, std::tuple) + { + auto kk = std::make_pair(this, key); + + auto add_cnv_func = [kk]() { + using u_t = std::tuple_element_t>; + if constexpr (!std::same_as) { + // _umap.emplace(kk, ) + } + }; + + [kk](std::index_sequence) {}(std::make_index_sequence()); + + return push(key, std::forward(value)); + } + + template + size_t emplace(KeyT const& key, CtorArgTs&&... args) + { + return push(key, VT(std::forward(args)...)); + } + + template + std::expected get(KeyT const& key) const + { + if (auto it = _umap.find(std::make_pair(this, key)); it != _umap.end()) { + return it->second; + } + + return std::unexpected(Error::ERROR_NO_ELEM); + } +}; + +} // end of namespace snplib \ No newline at end of file diff --git a/include/snipplib/hmap/snplib_hmap.h b/include/snipplib/hmap/snplib_hmap.h deleted file mode 100644 index 856c0f1..0000000 --- a/include/snipplib/hmap/snplib_hmap.h +++ /dev/null @@ -1,67 +0,0 @@ -#pragma once - -#include "../concepts/snplib_concepts.h" -#include "../utils/snplib_hash.h" - -namespace snplib { - - -template -class HeterogenMap { -protected: - // actuall map key type - typedef std::pair key_t; - - struct KeyHash { - static size_t operator()(key_t key) { - size_t hash = 0; - - snplib_hash_combine(hash, key.first, , key.second); - - return hash; - } - } - - template - inline static std::unordered_map _umap{}; - - std::vector> _clearFunc{}; - -public: - enum Error : {ERROR_OK, ERROR_NO_ELEM}; - - HeterogenMap() = default; - - ~HeterogenMap() { - for (auto &func: _clearFunc) { - func(); - } - } - - template - size_t push(KeyT const& key, VT&& value) { - auto it = _umap>.emplace(std::make_pair(this, key), std::forward(value)); - - if (it.second){ - _clearFunc.empace_back([it=it.first]() { _umap>.erase(it); }); - } // here the element already exists (just update its value) - - return _clearFunc.size(); - } - - template - size_t emplace(KeyT const&, CtorArgTs&& ...args) { - return push(key, VT(std::forward(args)...)); - } - - template - std::expected get(KeyT const& key) const { - if (auto it = _umap.find(std::make_pair(this, key)); it != _umap.end()) { - return it->second; - } - - return std::unexpected(Error::ERROR_NO_ELEM); - } -}; - -} // end of namespace snplib \ No newline at end of file