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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
|
#ifndef COMPACT_SET_INL_H_
#error "Direct inclusion of this file is not allowed, include compact_set.h"
// For the sake of sane code completion.
#include "compact_set.h"
#endif
namespace NYT {
////////////////////////////////////////////////////////////////////////////////
template <typename T, unsigned N, typename C>
class TCompactSet<T, N, C>::const_iterator
{
private:
friend class TCompactSet<T, N, C>;
union
{
TVectorConstIterator VIter;
TSetConstIterator SIter;
};
bool Small;
const_iterator(TVectorConstIterator it)
: VIter(it)
, Small(true)
{ }
const_iterator(TSetConstIterator it)
: SIter(it)
, Small(false)
{ }
template <class TOther>
void ConstructFrom(TOther&& rhs)
{
Y_VERIFY_DEBUG(Small == rhs.Small);
if (Small) {
new (&VIter)TVectorConstIterator(std::forward<TOther>(rhs).VIter);
} else {
new (&SIter)TSetConstIterator(std::forward<TOther>(rhs).SIter);
}
}
template <class TOther>
const_iterator& AssignFrom(TOther&& rhs)
{
if (this == &rhs) {
return *this;
}
if (Small && rhs.Small) {
VIter = std::forward<TOther>(rhs).VIter;
} else if (!Small && !rhs.Small) {
SIter = std::forward<TOther>(rhs).SIter;
} else {
if (Small) {
VIter.~TVectorConstIterator();
} else {
SIter.~TSetConstIterator();
}
if (rhs.Small) {
new (&VIter)TVectorConstIterator(std::forward<TOther>(rhs).VIter);
} else {
new (&SIter)TSetConstIterator(std::forward<TOther>(rhs).SIter);
}
}
Small = rhs.Small;
return *this;
}
public:
static_assert(std::is_same_v<
typename std::iterator_traits<TVectorConstIterator>::difference_type,
typename std::iterator_traits<TSetConstIterator>::difference_type>);
static_assert(std::is_same_v<
typename std::iterator_traits<TVectorConstIterator>::value_type,
typename std::iterator_traits<TSetConstIterator>::value_type>);
static_assert(std::is_same_v<
typename std::iterator_traits<TVectorConstIterator>::pointer,
typename std::iterator_traits<TSetConstIterator>::pointer>);
static_assert(std::is_same_v<
typename std::iterator_traits<TVectorConstIterator>::reference,
typename std::iterator_traits<TSetConstIterator>::reference>);
using difference_type = typename std::iterator_traits<TVectorConstIterator>::difference_type;
using value_type = typename std::iterator_traits<TVectorConstIterator>::value_type;
using pointer = typename std::iterator_traits<TVectorConstIterator>::pointer;
using reference = typename std::iterator_traits<TVectorConstIterator>::reference;
using iterator_category = std::bidirectional_iterator_tag;
const_iterator(const const_iterator& rhs)
: Small(rhs.Small)
{
ConstructFrom(rhs);
}
const_iterator(const_iterator&& rhs)
: Small(rhs.Small)
{
ConstructFrom(std::move(rhs));
}
~const_iterator()
{
if (Small) {
VIter.~TVectorConstIterator();
} else {
SIter.~TSetConstIterator();
}
}
const_iterator& operator=(const const_iterator& rhs)
{
return AssignFrom(rhs);
}
const_iterator& operator=(const_iterator&& rhs)
{
return AssignFrom(std::move(rhs));
}
const_iterator& operator++()
{
if (Small) {
++VIter;
} else {
++SIter;
}
return *this;
}
const_iterator operator++(int)
{
auto result = *this;
if (Small) {
++VIter;
} else {
++SIter;
}
return result;
}
const_iterator& operator--()
{
if (Small) {
--VIter;
} else {
--SIter;
}
return *this;
}
const_iterator operator--(int)
{
auto result = *this;
if (Small) {
--VIter;
} else {
--SIter;
}
return result;
}
bool operator==(const const_iterator& rhs) const
{
if (Small != rhs.Small) {
return false;
}
return Small ? (VIter == rhs.VIter) : (SIter == rhs.SIter);
}
bool operator!=(const const_iterator& rhs) const
{
return !(*this == rhs);
}
const T& operator*() const
{
return Small ? *VIter : *SIter;
}
const T* operator->() const
{
return &operator*();
}
};
////////////////////////////////////////////////////////////////////////////////
template <typename T, unsigned N, typename C>
bool TCompactSet<T, N, C>::empty() const
{
return Vector.empty() && Set.empty();
}
template <typename T, unsigned N, typename C>
typename TCompactSet<T, N, C>::size_type TCompactSet<T, N, C>::size() const
{
return IsSmall() ? Vector.size() : Set.size();
}
template <typename T, unsigned N, typename C>
const T& TCompactSet<T, N, C>::front() const
{
return IsSmall() ? Vector.front() : *Set.begin();
}
template <typename T, unsigned N, typename C>
typename TCompactSet<T, N, C>::size_type TCompactSet<T, N, C>::count(const T& v) const
{
if (IsSmall()) {
return std::binary_search(Vector.begin(), Vector.end(), v, C()) ? 1 : 0;
} else {
return Set.count(v);
}
}
template <typename T, unsigned N, typename C>
bool TCompactSet<T, N, C>::contains(const T& v) const
{
return count(v) == 1;
}
template <typename T, unsigned N, typename C>
std::pair<typename TCompactSet<T, N, C>::const_iterator, bool> TCompactSet<T, N, C>::insert(const T& v)
{
if (!IsSmall()) {
auto [it, inserted] = Set.insert(v);
return {const_iterator(std::move(it)), inserted};
}
auto it = std::lower_bound(Vector.begin(), Vector.end(), v, C());
if (it != Vector.end() && !C()(v, *it)) {
return {const_iterator(std::move(it)), false}; // Don't reinsert if it already exists.
}
if (Vector.size() < N) {
auto newIt = Vector.insert(it, v);
return {const_iterator(std::move(newIt)), true};
}
Set.insert(Vector.begin(), Vector.end());
Vector.clear();
auto [newIt, inserted] = Set.insert(v);
Y_VERIFY_DEBUG(inserted);
return {const_iterator(std::move(newIt)), true};
}
template <typename T, unsigned N, typename C>
template <typename TIter>
void TCompactSet<T, N, C>::insert(TIter i, TIter e)
{
for (; i != e; ++i) {
insert(*i);
}
}
template <typename T, unsigned N, typename C>
bool TCompactSet<T, N, C>::erase(const T& v)
{
if (!IsSmall()) {
return Set.erase(v);
}
auto [rangeBegin, rangeEnd] = std::equal_range(Vector.begin(), Vector.end(), v, C());
if (rangeBegin != rangeEnd) {
Vector.erase(rangeBegin, rangeEnd);
return true;
} else {
return false;
}
}
template <typename T, unsigned N, typename C>
void TCompactSet<T, N, C>::clear()
{
Vector.clear();
Set.clear();
}
template <typename T, unsigned N, typename C>
typename TCompactSet<T, N, C>::const_iterator TCompactSet<T, N, C>::begin() const
{
return IsSmall() ? const_iterator(Vector.begin()) : const_iterator(Set.begin());
}
template <typename T, unsigned N, typename C>
typename TCompactSet<T, N, C>::const_iterator TCompactSet<T, N, C>::cbegin() const
{
return begin();
}
template <typename T, unsigned N, typename C>
typename TCompactSet<T, N, C>::const_iterator TCompactSet<T, N, C>::end() const
{
return IsSmall() ? const_iterator(Vector.end()) : const_iterator(Set.end());
}
template <typename T, unsigned N, typename C>
typename TCompactSet<T, N, C>::const_iterator TCompactSet<T, N, C>::cend() const
{
return end();
}
template <typename T, unsigned N, typename C>
bool TCompactSet<T, N, C>::IsSmall() const
{
return Set.empty();
}
////////////////////////////////////////////////////////////////////////////////
} // namespace NYT
|