16 template<
typename Pair> [[nodiscard]]
auto&
operator()(Pair&& p)
const {
return p.first; }
29 typename Hasher = std::hash<Key>,
30 typename Equal = std::equal_to<>>
31class hash_map :
public hash_set<std::pair<Key, Value>, hash_set_impl::ExtractFirst, Hasher, Equal>
42 Hasher hasher_ = Hasher(),
43 Equal equal_ = Equal())
48 explicit(
false)
hash_map(std::initializer_list<std::pair<Key, Value>> list)
56 auto it = this->
find(key);
57 if (it == this->
end()) {
64 template<
typename K,
typename... Args>
65 std::pair<iterator, bool>
try_emplace(K&& key, Args&& ...args)
67 auto hash = unsigned(this->
hasher(key));
72 primary = this->
table[tableIdx];
74 auto& elem = this->
pool.get(elemIdx);
75 if ((elem.hash == hash) && this->equal(this->extract(elem.value), key)) {
77 return std::pair(
iterator(
this, elemIdx),
false);
79 elemIdx = elem.nextIdx;
83 if (this->
elemCount >= ((this->allocMask + 1) / 4 * 3)) {
86 primary = this->
table[tableIdx];
90 auto poolIdx = this->
pool.emplace(std::forward<K>(key), std::forward<Args>(args)...);
91 auto& poolElem = this->
pool.get(poolIdx);
93 poolElem.nextIdx = primary;
94 this->
table[tableIdx] = poolIdx;
95 return std::pair(
iterator(
this, poolIdx),
true);
98 template<
typename K,
typename V>
101 auto result =
try_emplace(std::forward<K>(key), std::forward<V>(value));
102 if (!result.second) {
104 result.first->second = std::forward<V>(value);
112 return this->
find(k) != this->
end();
117template<
typename Key,
typename Value,
typename Hasher,
typename Equal,
typename Key2>
120 auto it = map.
find(key);
121 return (it != map.
end()) ? &it->second :
nullptr;
124template<
typename Key,
typename Value,
typename Hasher,
typename Equal,
typename Key2>
127 auto it = map.
find(key);
128 return (it != map.
end()) ? &it->second :
nullptr;
std::pair< iterator, bool > insert_or_assign(K &&key, V &&value)
hash_map(unsigned initialSize=0, Hasher hasher_=Hasher(), Equal equal_=Equal())
typename BaseType::const_iterator const_iterator
std::pair< iterator, bool > try_emplace(K &&key, Args &&...args)
std::pair< Key, Value > value_type
typename BaseType::iterator iterator
Value & operator[](K &&key)
bool contains(const K &k) const
Iter< const hash_set, const Value > const_iterator
iterator find(const K &key)
std::pair< iterator, bool > insert(V &&value)
Iter< hash_set, Value > iterator
static constexpr auto invalidIndex
hash_set_impl::Pool< Value > pool
const Value * lookup(const hash_map< Key, Value, Hasher, Equal > &map, const Key2 &key)