aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/small_containers/compact_set-inl.h
blob: e93d11f126fce88f75cfdb9822442d6f17b76daf (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
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
329
330
331
332
333
334
#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

#include <library/cpp/yt/assert/assert.h>

namespace NYT {

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

template <typename T, size_t N,  typename C, typename A>
class TCompactSet<T, N, C, A>::const_iterator
{
private:
    friend class TCompactSet<T, N, C, A>;

    union
    {
        TVectorConstIterator VIter;
        TSetConstIterator SIter;
    };

    bool Small;

    const_iterator(TVectorConstIterator it)
        : VIter(it)
        , Small(true)
    { }

    const_iterator(TSetConstIterator it)
        : SIter(it)
        , Small(false)
    { }

    template <typename TOther>
    void ConstructFrom(TOther&& rhs)
    {
        YT_ASSERT(Small == rhs.Small);

        if (Small) {
            new (&VIter)TVectorConstIterator(std::forward<TOther>(rhs).VIter);
        } else {
            new (&SIter)TSetConstIterator(std::forward<TOther>(rhs).SIter);
        }
    }

    template <typename 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, size_t N,  typename C, typename A>
TCompactSet<T, N, C, A>::TCompactSet(const A& allocator)
    : Set_(allocator)
{ }

template <typename T, size_t N,  typename C, typename A>
bool TCompactSet<T, N, C, A>::empty() const
{
    return Vector_.empty() && Set_.empty();
}

template <typename T, size_t N,  typename C, typename A>
typename TCompactSet<T, N, C, A>::size_type TCompactSet<T, N, C, A>::size() const
{
    return is_small() ? Vector_.size() : Set_.size();
}

template <typename T, size_t N,  typename C, typename A>
const T& TCompactSet<T, N, C, A>::front() const
{
    return is_small() ? Vector_.front() : *Set_.begin();
}

template <typename T, size_t N,  typename C, typename A>
typename TCompactSet<T, N, C, A>::size_type TCompactSet<T, N, C, A>::count(const T& v) const
{
    if (is_small()) {
        return std::binary_search(Vector_.begin(), Vector_.end(), v, C()) ? 1 : 0;
    } else {
        return Set_.count(v);
    }
}

template <typename T, size_t N,  typename C, typename A>
bool TCompactSet<T, N, C, A>::contains(const T& v) const
{
    return count(v) == 1;
}

template <typename T, size_t N,  typename C, typename A>
std::pair<typename TCompactSet<T, N, C, A>::const_iterator, bool> TCompactSet<T, N, C, A>::insert(const T& v)
{
    if (!is_small()) {
        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);
    YT_ASSERT(inserted);
    return {const_iterator(std::move(newIt)), true};
}

template <typename T, size_t N,  typename C, typename A>
template <typename TIter>
void TCompactSet<T, N, C, A>::insert(TIter i, TIter e)
{
    for (; i != e; ++i) {
        insert(*i);
    }
}

template <typename T, size_t N,  typename C, typename A>
bool TCompactSet<T, N, C, A>::erase(const T& v)
{
    if (!is_small()) {
        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, size_t N,  typename C, typename A>
void TCompactSet<T, N, C, A>::clear()
{
    Vector_.clear();
    Set_.clear();
}

template <typename T, size_t N,  typename C, typename A>
typename TCompactSet<T, N, C, A>::const_iterator TCompactSet<T, N, C, A>::begin() const
{
    return is_small() ? const_iterator(Vector_.begin()) : const_iterator(Set_.begin());
}

template <typename T, size_t N,  typename C, typename A>
typename TCompactSet<T, N, C, A>::const_iterator TCompactSet<T, N, C, A>::cbegin() const
{
    return begin();
}

template <typename T, size_t N,  typename C, typename A>
typename TCompactSet<T, N, C, A>::const_iterator TCompactSet<T, N, C, A>::end() const
{
    return is_small() ? const_iterator(Vector_.end()) : const_iterator(Set_.end());
}

template <typename T, size_t N,  typename C, typename A>
typename TCompactSet<T, N, C, A>::const_iterator TCompactSet<T, N, C, A>::cend() const
{
    return end();
}

template <typename T, size_t N,  typename C, typename A>
bool TCompactSet<T, N, C, A>::is_small() const
{
    return Set_.empty();
}

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

} // namespace NYT