aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/containers/compact_vector/compact_vector.h
blob: 984ec6ac00dbeae91e4acbb6121f56bbb5115644 (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
#pragma once

#include <util/generic/bitops.h>
#include <util/generic/yexception.h>
#include <util/system/sys_alloc.h>

// vector that is 8 bytes when empty (TVector is 24 bytes)

template <typename T>
class TCompactVector {
private:
    typedef TCompactVector<T> TThis;

    // XXX: make header independent on T and introduce nullptr
    struct THeader {
        size_t Size;
        size_t Capacity;
    };

    T* Ptr;

    THeader* Header() {
        return ((THeader*)Ptr) - 1;
    }

    const THeader* Header() const {
        return ((THeader*)Ptr) - 1;
    }

    void destruct_at(size_t pos) {
        (*this)[pos].~T();
    }

public:
    using value_type = T;

    using TIterator = T*;
    using TConstIterator = const T*;

    using iterator = TIterator ;
    using const_iterator = TConstIterator;

    using reverse_iterator = std::reverse_iterator<iterator>;
    using const_reverse_iterator = std::reverse_iterator<const_iterator>;

    TCompactVector()
        : Ptr(nullptr)
    {
    }

    TCompactVector(const TThis& that)
        : Ptr(nullptr)
    {
        Reserve(that.Size());
        for (TConstIterator i = that.Begin(); i != that.End(); ++i) {
            PushBack(*i);
        }
    }

    TCompactVector(TThis&& that) noexcept
        : Ptr(nullptr)
    {
        Swap(that);
    }

    TCompactVector(std::initializer_list<T> init)
        : Ptr(nullptr)
    {
        Reserve(init.size());
        for (const T& val : init) {
            PushBack(val);
        }
    }

    template <class InputIterator>
    TCompactVector(InputIterator begin, InputIterator end)
        : Ptr(nullptr)
    {
        Reserve(std::distance(begin, end));

        for (auto it = begin; it != end; ++it) {
            push_back(*it);
        }
    }

    ~TCompactVector() {
        for (size_t i = 0; i < Size(); ++i) {
            try {
                destruct_at(i);
            } catch (...) {
            }
        }
        if (Ptr)
            y_deallocate(Header());
    }

    TThis& operator = (TThis&& that) noexcept {
        Swap(that);
        return *this;
    }

    TThis& operator = (const TThis& that) {
        if (Y_LIKELY(this != &that)) {
            TThis tmp(that);
            Swap(tmp);
        }
        return *this;
    }

    TThis& operator = (std::initializer_list<T> init) {
        TThis data(init);
        Swap(data);
        return *this;
    }

    TIterator Begin() {
        return Ptr;
    }

    TIterator End() {
        return Ptr + Size();
    }

    TConstIterator Begin() const {
        return Ptr;
    }

    TConstIterator End() const {
        return Ptr + Size();
    }

    iterator begin() {
        return Begin();
    }

    const_iterator begin() const {
        return Begin();
    }

    iterator end() {
        return End();
    }

    const_iterator end() const {
        return End();
    }

    reverse_iterator rbegin() {
        return std::make_reverse_iterator(end());
    }

    const_reverse_iterator rbegin() const {
        return std::make_reverse_iterator(end());
    }

    reverse_iterator rend() {
        return std::make_reverse_iterator(begin());
    }

    const_reverse_iterator rend() const {
        return std::make_reverse_iterator(begin());
    }

    void Swap(TThis& that) noexcept {
        DoSwap(Ptr, that.Ptr);
    }

    void Reserve(size_t newCapacity) {
        if (newCapacity <= Capacity()) {
        } else if (Ptr == nullptr) {
            constexpr size_t maxBlockSize = static_cast<size_t>(1) << (sizeof(size_t) * 8 - 1);
            constexpr size_t maxCapacity = (maxBlockSize - sizeof(THeader)) / sizeof(T);
            Y_ENSURE(newCapacity <= maxCapacity);

            const size_t requiredMemSize = sizeof(THeader) + newCapacity * sizeof(T);
            // most allocators operates pow-of-two memory blocks,
            // so we try to allocate such memory block to fully utilize its capacity
            const size_t memSizePowOf2 = FastClp2(requiredMemSize);
            const size_t realNewCapacity = (memSizePowOf2 - sizeof(THeader)) / sizeof(T);
            Y_ASSERT(realNewCapacity >= newCapacity);

            void* mem = ::y_allocate(memSizePowOf2);
            Ptr = (T*)(((THeader*)mem) + 1);
            Header()->Size = 0;
            Header()->Capacity = realNewCapacity;
        } else {
            TThis copy;
            copy.Reserve(newCapacity);
            for (TConstIterator it = Begin(); it != End(); ++it) {
                copy.PushBack(*it);
            }
            Swap(copy);
        }
    }

    void reserve(size_t newCapacity) {
        Reserve(newCapacity);
    }

    size_t Size() const {
        return Ptr ? Header()->Size : 0;
    }

    size_t size() const {
        return Size();
    }

    bool Empty() const {
        return Size() == 0;
    }

    bool empty() const {
        return Empty();
    }

    size_t Capacity() const {
        return Ptr ? Header()->Capacity : 0;
    }

    void PushBack(const T& elem) {
        Reserve(Size() + 1);
        new (Ptr + Size()) T(elem);
        ++(Header()->Size);
    }

    void push_back(const T& elem) {
        PushBack(elem);
    }

    T& Back() {
        return *(End() - 1);
    }

    const T& Back() const {
        return *(End() - 1);
    }

    T& back() {
        return Back();
    }

    const T& back() const {
        return Back();
    }

    TIterator Insert(TIterator pos, const T& elem) {
        Y_ASSERT(pos >= Begin());
        Y_ASSERT(pos <= End());

        size_t posn = pos - Begin();
        if (pos == End()) {
            PushBack(elem);
        } else {
            Y_ASSERT(Size() > 0);

            Reserve(Size() + 1);

            PushBack(*(End() - 1));

            for (size_t i = Size() - 2; i + 1 > posn; --i) {
                (*this)[i + 1] = (*this)[i];
            }

            (*this)[posn] = elem;
        }
        return Begin() + posn;
    }

    iterator insert(iterator pos, const T& elem) {
        return Insert(pos, elem);
    }

    void Clear() {
        TThis clean;
        Swap(clean);
    }

    void clear() {
        Clear();
    }

    void erase(iterator position) {
        Y_ENSURE(position >= begin() && position < end());
        std::move(position + 1, end(), position);
        destruct_at(Size() - 1);
        Header()->Size -= 1;
    }

    T& operator[](size_t index) {
        Y_ASSERT(index < Size());
        return Ptr[index];
    }

    const T& operator[](size_t index) const {
        Y_ASSERT(index < Size());
        return Ptr[index];
    }
};