From d35d356784d53f8b7a1adfb8395d9345f1f5652f Mon Sep 17 00:00:00 2001 From: "Timur A. Fatkhullin" Date: Wed, 2 Sep 2026 21:18:07 +0300 Subject: [PATCH] working hmap class --- examples/hmap_example.cpp | 31 +++++++--------- include/snipplib/containers/snplib_hmap.h | 45 ++++++++++++++++------- 2 files changed, 46 insertions(+), 30 deletions(-) diff --git a/examples/hmap_example.cpp b/examples/hmap_example.cpp index fc1dcec..fc7249b 100644 --- a/examples/hmap_example.cpp +++ b/examples/hmap_example.cpp @@ -182,23 +182,8 @@ int main() std::print("push element with key '{}' and value {}\t...", k2, s1); ok = hm.push( - k2, s1, - [](auto const& v) -> decltype(hm)::hmap_expected_t { - return v.c_str(); - // if (v) { - // return v.c_str(); - // } else { - // return std::unexpected(v.error()); - // } - }, - [](auto const& v) -> decltype(hm)::hmap_expected_t { - return std::string(v.value()); - // if (v) { - // return std::string(v.value()); - // } else { - // return std::unexpected(v.error()); - // } - }); + k2, s1, [](auto const& v) -> hmap_expected_t { return v.c_str(); }, + [](auto const& v) -> hmap_expected_t { return std::string(v); }); if (!ok) { std::println("cannot insert the value {}", s1); } else { @@ -213,6 +198,8 @@ int main() std::println("OK"); } + std::println("in total {} elelents were pushed", hm.size()); + std::println("\n"); std::print("get '{}'-element as double: ", k1); @@ -223,5 +210,15 @@ int main() std::println("FAIL"); } + std::println("\n"); + + std::print("get '{}'-element as const char*: ", k2); + auto sp2 = hm.get(k2); + if (sp2) { + std::println("{}", sp2.value()); + } else { + std::println("FAIL"); + } + return 0; } \ No newline at end of file diff --git a/include/snipplib/containers/snplib_hmap.h b/include/snipplib/containers/snplib_hmap.h index 121d249..1021f91 100644 --- a/include/snipplib/containers/snplib_hmap.h +++ b/include/snipplib/containers/snplib_hmap.h @@ -116,6 +116,7 @@ public: return does_contain; } + void clear() { for (auto& func : _clearFunc) { @@ -413,15 +414,17 @@ inline std::error_code make_error_code(hmap_error_e ec) } +typedef std::error_code hmap_error_t; + +template +using hmap_expected_t = std::expected; + + + template class hmap { public: - typedef std::error_code error_t; - - template - using hmap_expected_t = std::expected; - template using hmap_user_conv_from_t = std::function(hmap_expected_t const&)>; @@ -439,7 +442,7 @@ protected: template inline static std::unordered_map>> + std::unordered_map>> _setter{}; @@ -555,6 +558,16 @@ public: return does_contain; } + bool erase(KeyT const& key) + { + bool ok = false; + for (auto& func : _eraseFunc) { + ok |= func(key, this); + } + + return ok; + } + void clear() { for (auto& func : _clearFunc) { @@ -574,6 +587,7 @@ public: return N; } + template bool push(KeyT const& key, VT&& value) { @@ -632,7 +646,9 @@ public: // NOTE: do not use here iterator since after insertion // an rehashing may occur and all iterators will be invalidated! - _getter[this].emplace(key, [key](const hmap* obj) { return _values[obj][key]; }); + _getter[this].emplace(key, + [key](const hmap* obj) -> hmap_expected_t { return _values[obj][key]; }); + _setter[this].emplace(key, [key](const v_t& v, const hmap* obj) { _values[obj][key] = v; return hmap_error_e::ERROR_OK; @@ -643,8 +659,8 @@ public: // FTs - conversional functions: - // convert-from: std::function(hmap_expected_t const&)> (from inner type to user) - // convert-to: std::function(hmap_expected_t const&)> (from user type to inner) + // convert-from: std::function(VT const&)> (from inner type to user) + // convert-to: std::function(UT const&)> (from user type to inner) // template requires(sizeof...(FTs) > 1) @@ -669,14 +685,14 @@ public: _getter[this].emplace( kk, - [kk, from_cnv_func_arg = - std::forward(from_cnv_func)](const hmap* obj) mutable -> u_t { + [kk, from_cnv_func_arg = std::forward(from_cnv_func)]( + const hmap* obj) mutable -> hmap_expected_t { return std::forward(from_cnv_func_arg)(_values[obj][kk]); }); _setter[this].emplace(kk, [kk, to_cnv_func_arg = std::forward(to_cnv_func)]( - const u_t& v, const hmap* obj) mutable -> hmap::error_t { + const u_t& v, const hmap* obj) mutable -> hmap_error_t { auto val = std::forward(to_cnv_func_arg)(v); if (val) { _values[obj][kk] = val.value(); @@ -708,6 +724,9 @@ public: return ok; } + // + // overloaded method with trivial conversion functions constructed from + // element types of the input std::tuple template requires(sizeof...(Ts) > 0) auto push(KeyT const& key, VT&& value, std::tuple) @@ -735,7 +754,7 @@ public: } template - hmap::error_t set(KeyT const& key, UT&& value) + hmap_error_t set(KeyT const& key, UT&& value) { using u_t = std::decay_t;