aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/containers/flat_hash/lib/map.h
blob: 42fa0dda35c379fca1a6620c965dbc606fa8abc4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#pragma once

#include "table.h"
#include "concepts/iterator.h"

#include <util/generic/algorithm.h>
#include <util/generic/mapfindptr.h> 
 
namespace NFlatHash {

namespace NPrivate {

struct TMapKeyGetter {
    template <class T>
    static constexpr auto& Apply(T& t) noexcept { return t.first; };

    template <class T>
    static constexpr const auto& Apply(const T& t) noexcept { return t.first; };
};

}  // namespace NPrivate

template <class Key,
          class T,
          class Hash,
          class KeyEqual,
          class Container,
          class Probing,
          class SizeFitter,
          class Expander>
class TMap : private TTable<Hash,
                            KeyEqual,
                            Container,
                            NPrivate::TMapKeyGetter,
                            Probing,
                            SizeFitter,
                            Expander>, 
             public TMapOps<TMap<Key, 
                                 T, 
                                 Hash, 
                                 KeyEqual, 
                                 Container, 
                                 Probing, 
                                 SizeFitter, 
                                 Expander>> 
{
private:
    using TBase = TTable<Hash,
                         KeyEqual,
                         Container,
                         NPrivate::TMapKeyGetter,
                         Probing,
                         SizeFitter,
                         Expander>;

    static_assert(std::is_same<std::pair<const Key, T>, typename Container::value_type>::value);

public:
    using key_type = Key;
    using mapped_type = T;
    using typename TBase::value_type;
    using typename TBase::size_type;
    using typename TBase::difference_type;
    using typename TBase::hasher;
    using typename TBase::key_equal;
    using typename TBase::reference;
    using typename TBase::const_reference;
    using typename TBase::iterator;
    using typename TBase::const_iterator;
    using typename TBase::allocator_type;
    using typename TBase::pointer;
    using typename TBase::const_pointer;

private:
    static constexpr size_type INIT_SIZE = 8;

public:
    TMap() : TBase(INIT_SIZE) {}

    template <class... Rest>
    TMap(size_type initSize, Rest&&... rest) : TBase(initSize, std::forward<Rest>(rest)...) {}

    template <class I, class... Rest>
    TMap(I first, I last,
         std::enable_if_t<NConcepts::IteratorV<I>, size_type> initSize = INIT_SIZE,
         Rest&&... rest)
        : TBase(initSize, std::forward<Rest>(rest)...)
    {
        insert(first, last);
    }

    template <class... Rest>
    TMap(std::initializer_list<value_type> il, size_type initSize = INIT_SIZE, Rest&&... rest)
        : TBase(initSize, std::forward<Rest>(rest)...)
    {
        insert(il.begin(), il.end());
    }

    TMap(std::initializer_list<value_type> il, size_type initSize = INIT_SIZE)
        : TBase(initSize)
    {
        insert(il.begin(), il.end());
    }

    TMap(const TMap&) = default;
    TMap(TMap&&) = default;

    TMap& operator=(const TMap&) = default;
    TMap& operator=(TMap&&) = default;

    // Iterators
    using TBase::begin;
    using TBase::cbegin;
    using TBase::end;
    using TBase::cend;

    // Capacity
    using TBase::empty;
    using TBase::size;

    // Modifiers
    using TBase::clear;
    using TBase::insert;
    using TBase::emplace;
    using TBase::emplace_hint;
    using TBase::erase;
    using TBase::swap;

    template <class V>
    std::pair<iterator, bool> insert_or_assign(const key_type& k, V&& v) {
        return InsertOrAssignImpl(k, std::forward<V>(v));
    }
    template <class V>
    std::pair<iterator, bool> insert_or_assign(key_type&& k, V&& v) {
        return InsertOrAssignImpl(std::move(k), std::forward<V>(v));
    }

    template <class V>
    iterator insert_or_assign(const_iterator, const key_type& k, V&& v) { // TODO(tender-bum)
        return insert_or_assign(k, std::forward<V>(v)).first;
    }
    template <class V>
    iterator insert_or_assign(const_iterator, key_type&& k, V&& v) { // TODO(tender-bum)
        return insert_or_assign(std::move(k), std::forward<V>(v)).first;
    }

    template <class... Args>
    std::pair<iterator, bool> try_emplace(const key_type& key, Args&&... args) {
        return TryEmplaceImpl(key, std::forward<Args>(args)...);
    }
    template <class... Args>
    std::pair<iterator, bool> try_emplace(key_type&& key, Args&&... args) {
        return TryEmplaceImpl(std::move(key), std::forward<Args>(args)...);
    }

    template <class... Args>
    iterator try_emplace(const_iterator, const key_type& key, Args&&... args) { // TODO(tender-bum)
        return try_emplace(key, std::forward<Args>(args)...).first;
    }
    template <class... Args>
    iterator try_emplace(const_iterator, key_type&& key, Args&&... args) { // TODO(tender-bum)
        return try_emplace(std::move(key), std::forward<Args>(args)...).first;
    }

    // Lookup
    using TBase::count;
    using TBase::find;
    using TBase::contains;

    template <class K>
    mapped_type& at(const K& key) {
        auto it = find(key);
        if (it == end()) {
            throw std::out_of_range{ "no such key in map" };
        }
        return it->second;
    }

    template <class K>
    const mapped_type& at(const K& key) const { return const_cast<TMap*>(this)->at(key); }

    template <class K>
    Y_FORCE_INLINE mapped_type& operator[](K&& key) {
        return TBase::TryCreate(key, [&](size_type idx) {
            TBase::Buckets_.InitNode(idx, std::forward<K>(key), mapped_type{});
        }).first->second;
    }

    // Bucket interface
    using TBase::bucket_count;
    using TBase::bucket_size;

    // Hash policy
    using TBase::load_factor;
    using TBase::rehash;
    using TBase::reserve;

    // Observers
    using TBase::hash_function;
    using TBase::key_eq;

    friend bool operator==(const TMap& lhs, const TMap& rhs) {
        return lhs.size() == rhs.size() && AllOf(lhs, [&rhs](const auto& v) {
            auto it = rhs.find(v.first);
            return it != rhs.end() && *it == v;
        });
    }

    friend bool operator!=(const TMap& lhs, const TMap& rhs) { return !(lhs == rhs); }

private:
    template <class K, class... Args>
    std::pair<iterator, bool> TryEmplaceImpl(K&& key, Args&&... args) {
        return TBase::TryCreate(key, [&](size_type idx) {
            TBase::Buckets_.InitNode(
                idx,
                std::piecewise_construct,
                std::forward_as_tuple(std::forward<K>(key)),
                std::forward_as_tuple(std::forward<Args>(args)...));
        });
    }

    template <class K, class V>
    std::pair<iterator, bool> InsertOrAssignImpl(K&& key, V&& v) {
        auto p = try_emplace(std::forward<K>(key), std::forward<V>(v));
        if (!p.second) {
            p.first->second = std::forward<V>(v);
        }
        return p;
    }
};

}  // namespace NFlatHash