1#pragma once
2
3#include <Common/HashTable/HashMap.h>
4#include <Common/HashTable/ClearableHashSet.h>
5
6
7template <typename Key, typename Mapped, typename Hash>
8struct ClearableHashMapCell : public ClearableHashTableCell<Key, HashMapCell<Key, Mapped, Hash, ClearableHashSetState>>
9{
10 using Base = ClearableHashTableCell<Key, HashMapCell<Key, Mapped, Hash, ClearableHashSetState>>;
11 using Base::Base;
12
13 ClearableHashMapCell(const typename Base::value_type & value_, const typename Base::State & state)
14 : Base::BaseCell(value_, state), Base::version(state.version) {}
15};
16
17template
18<
19 typename Key,
20 typename Mapped,
21 typename Hash = DefaultHash<Key>,
22 typename Grower = HashTableGrower<>,
23 typename Allocator = HashTableAllocator
24>
25class ClearableHashMap : public HashTable<Key, ClearableHashMapCell<Key, Mapped, Hash>, Hash, Grower, Allocator>
26{
27public:
28 Mapped & operator[](const Key & x)
29 {
30 typename ClearableHashMap::LookupResult it;
31 bool inserted;
32 this->emplace(x, it, inserted);
33
34 if (inserted)
35 new (&it->getMapped()) Mapped();
36
37 return it->getMapped();
38 }
39
40 void clear()
41 {
42 ++this->version;
43 this->m_size = 0;
44 }
45};
46