aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/small_containers/compact_flat_map-inl.h
blob: 459d72fa2c126320d445ac3cc02245b01b0ffb8a (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
234
235
236
237
238
239
240
241
242
243
244
#ifndef COMPACT_FLAT_MAP_INL_H_
#error "Direct inclusion of this file is not allowed, include compact_flat_map.h"
// For the sake of sane code completion.
#include "compact_flat_map.h"
#endif

namespace NYT {

///////////////////////////////////////////////////////////////////////////////

template <class K, class V, size_t N>
template <class TInputIterator>
TCompactFlatMap<K, V, N>::TCompactFlatMap(TInputIterator begin, TInputIterator end)
{
    insert(begin, end);
}

template <class K, class V, size_t N>
TCompactFlatMap<K, V, N>::TCompactFlatMap(std::initializer_list<value_type> values)
    : TCompactFlatMap<K, V, N>(values.begin(), values.end())
{ }

template <class K, class V, size_t N>
bool TCompactFlatMap<K, V, N>::operator==(const TCompactFlatMap& rhs) const
{
    return Storage_ == rhs.Storage_;
}

template <class K, class V, size_t N>
bool TCompactFlatMap<K, V, N>::operator!=(const TCompactFlatMap& rhs) const
{
    return !(*this == rhs);
}

template <class K, class V, size_t N>
typename TCompactFlatMap<K, V, N>::iterator TCompactFlatMap<K, V, N>::begin()
{
    return Storage_.begin();
}

template <class K, class V, size_t N>
typename TCompactFlatMap<K, V, N>::const_iterator TCompactFlatMap<K, V, N>::begin() const
{
    return Storage_.begin();
}

template <class K, class V, size_t N>
typename TCompactFlatMap<K, V, N>::const_iterator TCompactFlatMap<K, V, N>::cbegin() const
{
    return Storage_.begin();
}

template <class K, class V, size_t N>
typename TCompactFlatMap<K, V, N>::iterator TCompactFlatMap<K, V, N>::end()
{
    return Storage_.end();
}

template <class K, class V, size_t N>
typename TCompactFlatMap<K, V, N>::const_iterator TCompactFlatMap<K, V, N>::end() const
{
    return Storage_.end();
}

template <class K, class V, size_t N>
typename TCompactFlatMap<K, V, N>::const_iterator TCompactFlatMap<K, V, N>::cend() const
{
    return Storage_.end();
}

template <class K, class V, size_t N>
void TCompactFlatMap<K, V, N>::reserve(size_type n)
{
    Storage_.reserve(n);
}

template <class K, class V, size_t N>
typename TCompactFlatMap<K, V, N>::size_type TCompactFlatMap<K, V, N>::size() const
{
    return Storage_.size();
}

template <class K, class V, size_t N>
int TCompactFlatMap<K, V, N>::ssize() const
{
    return static_cast<int>(Storage_.size());
}

template <class K, class V, size_t N>
bool TCompactFlatMap<K, V, N>::empty() const
{
    return Storage_.empty();
}

template <class K, class V, size_t N>
void TCompactFlatMap<K, V, N>::clear()
{
    Storage_.clear();
}

template <class K, class V, size_t N>
void TCompactFlatMap<K, V, N>::shrink_to_small()
{
    Storage_.shrink_to_small();
}

template <class K, class V, size_t N>
typename TCompactFlatMap<K, V, N>::iterator TCompactFlatMap<K, V, N>::find(const K& k)
{
    auto [rangeBegin, rangeEnd] = equal_range(k);
    return rangeBegin == rangeEnd ? end() : rangeBegin;
}

template <class K, class V, size_t N>
typename TCompactFlatMap<K, V, N>::const_iterator TCompactFlatMap<K, V, N>::find(const K& k) const
{
    auto [rangeBegin, rangeEnd] = equal_range(k);
    return rangeBegin == rangeEnd ? end() : rangeBegin;
}

template <class K, class V, size_t N>
bool TCompactFlatMap<K, V, N>::contains(const K& k) const
{
    return find(k) != end();
}

template <class K, class V, size_t N>
auto TCompactFlatMap<K, V, N>::insert(const value_type& value) -> std::pair<iterator, bool>
{
    return do_insert(value);
}

template <class K, class V, size_t N>
auto TCompactFlatMap<K, V, N>::insert(value_type&& value) -> std::pair<iterator, bool>
{
    return do_insert(std::move(value));
}

template <class K, class V, size_t N>
template <class TArg>
auto TCompactFlatMap<K, V, N>::do_insert(TArg&& value) -> std::pair<iterator, bool>
{
    auto [rangeBegin, rangeEnd] = equal_range(value.first);
    if (rangeBegin != rangeEnd) {
        return {rangeBegin, false};
    } else {
        auto it = Storage_.insert(rangeBegin, std::forward<TArg>(value));
        return {it, true};
    }
}

template <class K, class V, size_t N>
template <class TInputIterator>
void TCompactFlatMap<K, V, N>::insert(TInputIterator begin, TInputIterator end)
{
    for (auto it = begin; it != end; ++it) {
        insert(*it);
    }
}

template <class K, class V, size_t N>
template <class... TArgs>
auto TCompactFlatMap<K, V, N>::emplace(TArgs&&... args) -> std::pair<iterator, bool>
{
    return insert(value_type(std::forward<TArgs>(args)...));
}

template <class K, class V, size_t N>
V& TCompactFlatMap<K, V, N>::operator[](const K& k)
{
    auto [it, inserted] = insert({k, V()});
    return it->second;
}

template <class K, class V, size_t N>
void TCompactFlatMap<K, V, N>::erase(const K& k)
{
    auto [rangeBegin, rangeEnd] = equal_range(k);
    erase(rangeBegin, rangeEnd);
}

template <class K, class V, size_t N>
void TCompactFlatMap<K, V, N>::erase(iterator pos)
{
    Storage_.erase(pos);

    // Try to keep the storage inline. This is why erase doesn't return an iterator.
    Storage_.shrink_to_small();
}

template <class K, class V, size_t N>
void TCompactFlatMap<K, V, N>::erase(iterator b, iterator e)
{
    Storage_.erase(b, e);

    // Try to keep the storage inline. This is why erase doesn't return an iterator.
    Storage_.shrink_to_small();
}

template <class K, class V, size_t N>
std::pair<typename TCompactFlatMap<K, V, N>::iterator, typename TCompactFlatMap<K, V, N>::iterator>
TCompactFlatMap<K, V, N>::equal_range(const K& k)
{
    auto result = std::equal_range(Storage_.begin(), Storage_.end(), k, TKeyComparer());
    YT_ASSERT(std::distance(result.first, result.second) <= 1);
    return result;
}

template <class K, class V, size_t N>
std::pair<typename TCompactFlatMap<K, V, N>::const_iterator, typename TCompactFlatMap<K, V, N>::const_iterator>
TCompactFlatMap<K, V, N>::equal_range(const K& k) const
{
    auto result = std::equal_range(Storage_.begin(), Storage_.end(), k, TKeyComparer());
    YT_ASSERT(std::distance(result.first, result.second) <= 1);
    return result;
}

template <class K, class V, size_t N>
typename TCompactFlatMap<K, V, N>::const_iterator TCompactFlatMap<K, V, N>::lower_bound(const K& k) const
{
    return std::lower_bound(Storage_.begin(), Storage_.end(), k, TKeyComparer());
}

template <class K, class V, size_t N>
typename TCompactFlatMap<K, V, N>::iterator TCompactFlatMap<K, V, N>::lower_bound(const K& k)
{
    return std::lower_bound(Storage_.begin(), Storage_.end(), k, TKeyComparer());
}

template <class K, class V, size_t N>
typename TCompactFlatMap<K, V, N>::const_iterator TCompactFlatMap<K, V, N>::upper_bound(const K& k) const
{
    return std::upper_bound(Storage_.begin(), Storage_.end(), k, TKeyComparer());
}

template <class K, class V, size_t N>
typename TCompactFlatMap<K, V, N>::iterator TCompactFlatMap<K, V, N>::upper_bound(const K& k)
{
    return std::upper_bound(Storage_.begin(), Storage_.end(), k, TKeyComparer());
}

////////////////////////////////////////////////////////////////////////////////

} // namespace NYT