summaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/compact_containers/compact_flat_set-inl.h
blob: 07f7d5fae10f69f95344cb2994796902f639b5e9 (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
245
246
247
248
249
250
251
#ifndef COMPACT_FLAT_SET_INL_H_
#error "Direct inclusion of this file is not allowed, include compact_flat_set.h"
// For the sake of sane code completion.
#include "compact_flat_set.h"
#endif

namespace NYT {

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

template <class TValue, size_t N>
template <class TInputIterator>
TCompactFlatSet<TValue, N>::TCompactFlatSet(TInputIterator begin, TInputIterator end)
{
    insert(begin, end);
}

template <class TValue, size_t N>
TCompactFlatSet<TValue, N>::TCompactFlatSet(std::initializer_list<TValue> values)
    : TCompactFlatSet<TValue, N>(values.begin(), values.end())
{ }

template <class TValue, size_t N>
typename TCompactFlatSet<TValue, N>::iterator TCompactFlatSet<TValue, N>::begin()
{
    return Storage_.begin();
}

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

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

template <class TValue, size_t N>
typename TCompactFlatSet<TValue, N>::iterator TCompactFlatSet<TValue, N>::end()
{
    return Storage_.end();
}

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

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

template <class TValue, size_t N>
typename TCompactFlatSet<TValue, N>::reverse_iterator TCompactFlatSet<TValue, N>::rbegin()
{
    return Storage_.rbegin();
}

template <class TValue, size_t N>
typename TCompactFlatSet<TValue, N>::const_reverse_iterator TCompactFlatSet<TValue, N>::rbegin() const
{
    return Storage_.rbegin();
}

template <class TValue, size_t N>
typename TCompactFlatSet<TValue, N>::reverse_iterator TCompactFlatSet<TValue, N>::rend()
{
    return Storage_.rend();
}

template <class TValue, size_t N>
typename TCompactFlatSet<TValue, N>::const_reverse_iterator TCompactFlatSet<TValue, N>::rend() const
{
    return Storage_.rend();
}

template <class TValue, size_t N>
void TCompactFlatSet<TValue, N>::reserve(size_type size)
{
    Storage_.reserve(size);
}

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

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

template <class TValue, size_t N>
bool TCompactFlatSet<TValue, N>::empty() const
{
    return Storage_.empty();
}

template <class TValue, size_t N>
void TCompactFlatSet<TValue, N>::clear()
{
    Storage_.clear();
}

template <class TValue, size_t N>
void TCompactFlatSet<TValue, N>::shrink_to_small()
{
    Storage_.shrink_to_small();
}

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

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

template <class TValue, size_t N>
typename TCompactFlatSet<TValue, N>::size_type TCompactFlatSet<TValue, N>::count(const TValue& value) const
{
    auto [rangeBegin, rangeEnd] = equal_range(value);
    return rangeEnd - rangeBegin;
}


template <class TValue, size_t N>
typename TCompactFlatSet<TValue, N>::const_iterator TCompactFlatSet<TValue, N>::lower_bound(const TValue& value) const
{
    return std::ranges::lower_bound(Storage_, value);
}

template <class TValue, size_t N>
typename TCompactFlatSet<TValue, N>::iterator TCompactFlatSet<TValue, N>::lower_bound(const TValue& value)
{
    return std::ranges::lower_bound(Storage_, value);
}

template <class TValue, size_t N>
typename TCompactFlatSet<TValue, N>::const_iterator TCompactFlatSet<TValue, N>::upper_bound(const TValue& value) const
{
    return std::ranges::upper_bound(Storage_, value);
}

template <class TValue, size_t N>
typename TCompactFlatSet<TValue, N>::iterator TCompactFlatSet<TValue, N>::upper_bound(const TValue& value)
{
    return std::ranges::upper_bound(Storage_, value);
}

template <class TValue, size_t N>
std::pair<typename TCompactFlatSet<TValue, N>::iterator, typename TCompactFlatSet<TValue, N>::iterator>
TCompactFlatSet<TValue, N>::equal_range(const TValue& value)
{
    auto result = std::ranges::equal_range(Storage_.begin(), Storage_.end(), value);
    YT_ASSERT(result.size() <= 1);
    return result;
}

template <class TValue, size_t N>
std::pair<typename TCompactFlatSet<TValue, N>::const_iterator, typename TCompactFlatSet<TValue, N>::const_iterator>
TCompactFlatSet<TValue, N>::equal_range(const TValue& value) const
{
    auto result = std::ranges::equal_range(Storage_.begin(), Storage_.end(), value);
    YT_ASSERT(result.size() <= 1);
    return result;
}

template <class TValue, size_t N>
bool TCompactFlatSet<TValue, N>::contains(const TValue& value) const
{
    return find(value) != end();
}

template <class TValue, size_t N>
auto TCompactFlatSet<TValue, N>::insert(const TValue& value) -> std::pair<iterator, bool>
{
    return DoInsert(value);
}

template <class TValue, size_t N>
auto TCompactFlatSet<TValue, N>::insert(TValue&& value) -> std::pair<iterator, bool>
{
    return DoInsert(std::move(value));
}

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

template <class TValue, size_t N>
bool TCompactFlatSet<TValue, N>::erase(const TValue& value)
{
    auto [rangeBegin, rangeEnd] = equal_range(value);
    erase(rangeBegin, rangeEnd);
    return rangeBegin != rangeEnd;
}

template <class TValue, size_t N>
void TCompactFlatSet<TValue, 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 TValue, size_t N>
void TCompactFlatSet<TValue, N>::erase(iterator begin, iterator end)
{
    Storage_.erase(begin, end);

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

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

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

} // namespace NYT