From 740fab84bea7780e9cda26a0d2e877fa8f24f0b3 Mon Sep 17 00:00:00 2001 From: lisyarus Date: Wed, 7 Feb 2024 22:01:19 +0300 Subject: [PATCH] Add copying and constructing from initializer_list for util::hash_table --- libs/util/include/psemek/util/hash_table.hpp | 59 ++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/libs/util/include/psemek/util/hash_table.hpp b/libs/util/include/psemek/util/hash_table.hpp index 63e9149d..5fef85b0 100644 --- a/libs/util/include/psemek/util/hash_table.hpp +++ b/libs/util/include/psemek/util/hash_table.hpp @@ -6,6 +6,7 @@ #include #include +#include namespace psemek::util { @@ -344,6 +345,35 @@ namespace psemek::util : impl_(hash, equal) {} + hash_set(std::initializer_list init, Hash const & hash = {}, Equal const & equal = {}) + : impl_(hash, equal) + { + for (auto & value : init) + insert(std::move(value)); + } + + hash_set(hash_set && other) = default; + + hash_set(hash_set const & other) requires std::is_copy_constructible_v + : impl_(other.impl_.hash(), other.impl_.equal()) + { + for (auto const & value : other) + insert(value); + } + + hash_set & operator = (hash_set && other) = default; + + hash_set & operator = (hash_set const & other) requires std::is_copy_constructible_v + { + if (this != &other) + { + clear(); + for (auto const & value : other) + insert(value); + } + return *this; + } + std::pair insert(T const & value) { auto result = impl_.insert(value); @@ -406,6 +436,35 @@ namespace psemek::util : impl_(hash, equal) {} + hash_map(std::initializer_list> init, Hash const & hash = {}, KeyEqual const & equal = {}) + : impl_(hash, equal) + { + for (auto & pair : init) + insert(std::move(pair)); + } + + hash_map(hash_map && other) = default; + + hash_map(hash_map const & other) requires std::is_copy_constructible_v> + : impl_(other.impl_.hash(), other.impl_.equal()) + { + for (auto const & pair : other) + insert({pair.first, pair.second}); + } + + hash_map & operator = (hash_map && other) = default; + + hash_map & operator = (hash_map const & other) requires std::is_copy_constructible_v> + { + if (this != &other) + { + clear(); + for (auto const & pair : other) + insert({pair.first, pair.second}); + } + return *this; + } + std::pair insert(std::pair const & value) { return impl_.insert(value);