Add copying and constructing from initializer_list for util::hash_table

This commit is contained in:
Nikita Lisitsa 2024-02-07 22:01:19 +03:00
parent 929e8091dc
commit 740fab84be

View file

@ -6,6 +6,7 @@
#include <memory>
#include <optional>
#include <initializer_list>
namespace psemek::util
{
@ -344,6 +345,35 @@ namespace psemek::util
: impl_(hash, equal)
{}
hash_set(std::initializer_list<T> 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<T>
: 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<T>
{
if (this != &other)
{
clear();
for (auto const & value : other)
insert(value);
}
return *this;
}
std::pair<iterator, bool> insert(T const & value)
{
auto result = impl_.insert(value);
@ -406,6 +436,35 @@ namespace psemek::util
: impl_(hash, equal)
{}
hash_map(std::initializer_list<std::pair<Key, Value>> 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<std::pair<Key, Value>>
: 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<std::pair<Key, Value>>
{
if (this != &other)
{
clear();
for (auto const & pair : other)
insert({pair.first, pair.second});
}
return *this;
}
std::pair<iterator, bool> insert(std::pair<Key, Value> const & value)
{
return impl_.insert(value);