18 template<
typename Key,
typename Value>
29 explicit chain(
const Key &new_key,
const Value &new_value) :
entry(new_key, new_value),
next(nullptr) {};
30 explicit chain(
const Key &new_key, Value &&new_value) :
entry(new_key, std::move(new_value)),
next(nullptr) {};
77 return item !=
nullptr;
130 return item !=
nullptr;
144 for (
auto &[key, value] : init)
153 while (item !=
nullptr)
166 void insert(
const Key &key,
const Value &value);
167 void insert(
const Key &key, Value &&value);
185 const auto bucket = (std::hash<Key>{}(key) %
_capacity);
186 for (
chain *item =
_table[bucket]; item !=
nullptr; item = item->next)
188 if (std::get<0>(item->entry) == key)
189 return iterator(
this, bucket, item);
200 for (
size_t bucket = 0; bucket <
_capacity; bucket++)
220 const auto bucket = (std::hash<Key>{}(key)) %
_capacity;
221 for (
const chain *item =
_table[bucket]; item !=
nullptr; item = item->next)
223 if (std::get<0>(item->entry) == key)
230 std::optional<Value>
get(
const Key &key);
231 std::optional<Value>
remove(
const Key &key);
246 template<
typename Key,
typename Value>
253 const auto bucket = (std::hash<Key>{}(key)) %
_capacity;
256 item->next =
_table[bucket];
261 template<
typename Key,
typename Value>
268 const auto bucket = (std::hash<Key>{}(key)) %
_capacity;
271 item->next =
_table[bucket];
276 template<
typename Key,
typename Value>
283 const auto bucket = (std::hash<Key>{}(key)) %
_capacity;
285 item->next =
_table[bucket];
290 const auto bucket = (std::hash<Key>{}(key)) %
_capacity;
291 for (
chain *item =
_table[bucket]; item !=
nullptr; item = item->next)
293 if (std::get<0>(item->entry) == key)
294 return std::get<1>(item->entry);
301 item->next =
_table[bucket];
304 return std::get<1>(item->entry);
307 template<
typename Key,
typename Value>
313 const auto bucket = (std::hash<Key>{}(key)) %
_capacity;
315 for (
chain *item =
_table[bucket]; item !=
nullptr; item = item->next)
317 if (std::get<0>(item->entry) == key)
318 return std::get<1>(item->entry);
324 template<
typename Key,
typename Value>
330 const auto bucket = (std::hash<Key>{}(key)) %
_capacity;
332 chain *previous =
nullptr;
333 for (
chain *item =
_table[bucket]; item !=
nullptr; item = item->next)
335 if (std::get<0>(item->entry) == key)
337 Value value = std::move(std::get<1>(item->entry));
339 if (previous ==
nullptr)
340 _table[bucket] = item->next;
354 template<
typename Key,
typename Value>
357 const size_t new_capacity = std::max(2 *
_size, 10lu);
359 const auto new_table = kcalloc<chain *>(new_capacity);
360 for (
size_t i = 0; i < new_capacity; i++)
361 new_table[i] =
nullptr;
366 while (item !=
nullptr)
368 const auto &key = std::get<0>(item->entry);
369 const auto bucket = (std::hash<Key>{}(key)) % new_capacity;
371 const auto next = item->next;
372 item->next = new_table[bucket];
373 new_table[bucket] = item;
#define MOS_UNREACHABLE()
const entry_type & operator*() const
bool operator==(const const_iterator &other) const
const_iterator & operator++()
const_iterator(const HashMap *map, size_t bucket, const chain *item)
const entry_type * operator->() const
bool operator==(const iterator &other) const
iterator(HashMap *map, size_t bucket, chain *item)
entry_type * operator->()
void insert(const Key &key, const Value &value)
iterator find(const Key &key)
const_iterator find(const Key &key) const
Value & operator[](const Key &key)
mos::default_allocator< chain * > ChainAllocator
std::tuple< const Key, Value > entry_type
HashMap(std::initializer_list< entry_type > init)
const_iterator end() const
HashMap(const HashMap &)=delete
std::optional< Value > get(const Key &key)
std::optional< Value > remove(const Key &key)
T * create(Args &&...args)
chain(const Key &new_key, Value &&new_value)
chain(const Key &new_key, const Value &new_value)