summaryrefslogtreecommitdiff
path: root/asl/containers/chunked_buffer.hpp
blob: 3af7aa269531c3cd615019dd38659cb04e1fffdc (plain)
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
// Copyright 2025 Steven Le Rouzic
//
// SPDX-License-Identifier: BSD-3-Clause

#pragma once

#include "asl/base/utility.hpp"
#include "asl/base/assert.hpp"
#include "asl/base/numeric.hpp"
#include "asl/containers/buffer.hpp"
#include "asl/memory/allocator.hpp"
#include "asl/types/array.hpp"
#include "asl/types/maybe_uninit.hpp"

namespace asl
{

template<
    is_object T,
    isize_t kChunkSize,
    allocator Allocator = DefaultAllocator>
class chunked_buffer
{
    static_assert(kChunkSize > 0 && is_pow2(kChunkSize));

    using Chunk = array<maybe_uninit<T>, kChunkSize>;

    static constexpr isize_t chunk_index(isize_t i)
    {
        static constexpr int kChunkSizeLog2 = countr_zero(uint64_t{kChunkSize});
        return i >> kChunkSizeLog2;
    }

    static constexpr isize_t index_in_chunk(isize_t i)
    {
        static constexpr isize_t kMask = kChunkSize - 1;
        return i & kMask;
    }

    struct PerChunkIterator
    {
        isize_t from_chunk;
        isize_t to_chunk;

        isize_t from_index_in_chunk;
        isize_t to_index_in_chunk;

        [[nodiscard]] constexpr bool has_more() const
        {
            return from_chunk <= to_chunk;
        }

        void advance()
        {
            from_chunk += 1;
            from_index_in_chunk = 0;
        }

        [[nodiscard]] constexpr isize_t chunk() const { return from_chunk; }

        span<maybe_uninit<T>> make_span(Chunk& chunk) const
        {
            isize_t from = from_index_in_chunk;
            isize_t to = (from_chunk == to_chunk) ? to_index_in_chunk : kChunkSize - 1;
            return chunk.as_span().subspan(from, to - from + 1);
        }
    };

    PerChunkIterator make_index_iterator(isize_t from, isize_t to)
    {
        return PerChunkIterator {
            chunk_index(from), chunk_index(to),
            index_in_chunk(from), index_in_chunk(to)
        };
    }

    buffer<Chunk*, Allocator>    m_chunks;
    isize_t                      m_size{};

    void resize_uninit_inner(isize_t new_size)
    {
        ASL_ASSERT(new_size >= 0);

        if constexpr (!trivially_destructible<T>)
        {
            const isize_t old_size = size();
            if (new_size < old_size)
            {
                for (PerChunkIterator it = make_index_iterator(new_size, old_size - 1);
                    it.has_more();
                    it.advance())
                {
                    auto span = it.make_span(*m_chunks[it.chunk()]);
                    for (auto& el: span)
                    {
                        el.destroy_unsafe();
                    }
                }
            }
        }

        reserve_capacity(new_size);
        m_size = new_size;
    }

    template<typename... Args>
    void resize_construct(isize_t new_size, Args&&... args)
        requires constructible_from<T, Args&&...>
    {
        const isize_t old_size = m_size;
        resize_uninit_inner(new_size);

        if (new_size > old_size)
        {
            for (PerChunkIterator it = make_index_iterator(old_size, new_size - 1);
                it.has_more();
                it.advance())
            {
                auto span = it.make_span(*m_chunks[it.chunk()]);
                for (auto& uninit: span)
                {
                    uninit.construct_unsafe(std::forward<Args>(args)...);
                }
            }
        }
    }

    void copy_from(const chunked_buffer& other)
        requires copyable<T>
    {
        const isize_t this_size = size();
        isize_t to_copy_assign = asl::min(other.size(), this_size);

        resize_uninit_inner(other.size());

        for (PerChunkIterator it = make_index_iterator(0, to_copy_assign - 1);
            it.has_more();
            it.advance())
        {
            auto to_span = it.make_span(*m_chunks[it.chunk()]);
            auto from_span = it.make_span(*other.m_chunks[it.chunk()]);

            copy_assign_n(
                reinterpret_cast<T*>(to_span.data()), // NOLINT(*-reinterpret-cast)
                reinterpret_cast<const T*>(from_span.data()), // NOLINT(*-reinterpret-cast)
                to_span.size());
        }

        if (other.size() > this_size)
        {
            for (PerChunkIterator it = make_index_iterator(to_copy_assign, other.size() - 1);
                it.has_more();
                it.advance())
            {
                auto to_span = it.make_span(*m_chunks[it.chunk()]);
                auto from_span = it.make_span(*other.m_chunks[it.chunk()]);

                copy_uninit_n(
                    reinterpret_cast<T*>(to_span.data()), // NOLINT(*-reinterpret-cast)
                    reinterpret_cast<const T*>(from_span.data()), // NOLINT(*-reinterpret-cast)
                    to_span.size());
            }
        }

        ASL_ASSERT(size() == other.size());
    }

public:
    constexpr chunked_buffer()
        requires default_constructible<Allocator>
        = default;

    explicit constexpr chunked_buffer(Allocator allocator)
        : m_chunks{std::move(allocator)}
    {}

    constexpr chunked_buffer(const chunked_buffer& other)
        requires copyable<T> && copy_constructible<Allocator>
        : m_chunks{other.m_chunks.allocator_copy()}
    {
        copy_from(other);
    }

    constexpr chunked_buffer(chunked_buffer&& other)
        : m_chunks{std::move(other.m_chunks)}
        , m_size{asl::exchange(other.m_size, 0)}
    {
        ASL_ASSERT(other.m_chunks.size() == 0);
    }

    constexpr chunked_buffer& operator=(const chunked_buffer& other)
        requires copyable<T>
    {
        if (&other == this) { return *this; }
        copy_from(other);
        return *this;
    }

    constexpr chunked_buffer& operator=(chunked_buffer&& other)
    {
        if (&other == this) { return *this; }
        destroy();
        m_chunks = std::move(other.m_chunks);
        m_size = asl::exchange(other.m_size, 0);
        ASL_ASSERT(other.m_chunks.size() == 0);
        return *this;
    }

    ~chunked_buffer()
    {
        destroy();
    }

    void clear()
    {
        if constexpr (trivially_destructible<T>)
        {
            m_size = 0;
        }
        else if (m_size > 0)
        {
            resize_uninit_inner(0);
            ASL_ASSERT(m_size == 0);
        }
    }

    void destroy()
    {
        clear();
        ASL_ASSERT(size() == 0);

        for (Chunk* chunk:  m_chunks)
        {
            alloc_delete(m_chunks.allocator(), chunk);
        }

        m_chunks.destroy();
    }

    [[nodiscard]] constexpr isize_t size() const { return m_size; }

    [[nodiscard]] constexpr bool is_empty() const { return size() == 0; }

    [[nodiscard]] constexpr isize_t capacity() const
    {
        return m_chunks.size() * kChunkSize;
    }

    constexpr auto&& operator[](this auto&& self, isize_t i)
    {
        ASL_ASSERT(i >= 0 && i < self.m_size);
        return std::forward_like<decltype(self)>(
            (*std::forward<decltype(self)>(self).m_chunks[chunk_index(i)])
                [index_in_chunk(i)].as_init_unsafe()
        );
    }

    constexpr T& push(auto&&... args)
        requires constructible_from<T, decltype(args)&&...>
    {
        const isize_t chunk = chunk_index(m_size);
        const isize_t in_chunk = index_in_chunk(m_size);

        if (m_size == capacity())
        {
            resize_uninit_inner(m_size + 1);
        }
        else
        {
            m_size += 1;
        }

        void* uninit = &(*m_chunks[chunk])[in_chunk];
        return *construct_at<T>(uninit, std::forward<decltype(args)>(args)...);
    }

    void reserve_capacity(isize_t new_capacity)
    {
        new_capacity = round_up_pow2(new_capacity, kChunkSize);
        if (new_capacity <= capacity()) { return; }

        const isize_t required_chunks = new_capacity / kChunkSize;
        const isize_t additional_chunks = required_chunks - m_chunks.size();
        ASL_ASSERT(additional_chunks > 0);

        m_chunks.reserve_capacity(required_chunks);
        for (isize_t i = 0; i < additional_chunks; ++i)
        {
            // @Todo(C++26) _unsafe shouldn't be needed with trivial unions
            auto* chunk = alloc_uninit_unsafe<Chunk>(m_chunks.allocator());
            m_chunks.push(chunk);
        }
    }

    void resize(isize_t new_size)
        requires default_constructible<T>
    {
        if constexpr (trivially_default_constructible<T>)
        {
            resize_zero(new_size);
        }
        else
        {
            resize_construct(new_size);
        }
    }

    void resize(isize_t new_size, const T& value)
        requires copy_constructible<T>
    {
        resize_construct(new_size, value);
    }

    void resize_zero(isize_t new_size)
        requires trivially_default_constructible<T>
    {
        const isize_t old_size = m_size;
        resize_uninit_inner(new_size);

        if (new_size > old_size)
        {
            for (PerChunkIterator it = make_index_iterator(old_size, new_size - 1);
                it.has_more();
                it.advance())
            {
                auto span = it.make_span(*m_chunks[it.chunk()]);
                asl::memzero(span.data(), span.size_bytes());
            }
        }
    }

    void resize_uninit(isize_t new_size)
        requires trivially_default_constructible<T>
    {
        resize_uninit_inner(new_size);
    }

    template<typename Chunk>
    class generic_iterator
    {
        isize_t     m_index;
        span<Chunk> m_chunks;

    public:
        constexpr generic_iterator(isize_t index, span<Chunk> chunks)
            : m_index{index}
            , m_chunks{chunks}
        {}

        constexpr generic_iterator& operator++()
        {
            m_index += 1;
            return *this;
        }

        constexpr generic_iterator operator++(int)
        {
            auto tmp = *this;
            m_index += 1;
            return tmp;
        }

        constexpr bool operator==(this generic_iterator self, generic_iterator other)
        {
            ASL_ASSERT(self.m_chunks.data() == other.m_chunks.data());
            return self.m_index == other.m_index;
        }

        constexpr auto& operator*(this generic_iterator self)
        {
            ASL_ASSERT(self.m_index >= 0);
            return (*self.m_chunks[chunk_index(self.m_index)])[index_in_chunk(self.m_index)].as_init_unsafe();
        }

        constexpr auto* operator->(this generic_iterator self)
        {
            return &*self;
        }
    };

    using iterator       = generic_iterator<Chunk*>;
    using const_iterator = generic_iterator<const Chunk* const>;

    constexpr iterator begin() { return iterator{0, m_chunks}; }
    constexpr iterator end()   { return iterator{m_size, m_chunks}; }

    constexpr const_iterator begin() const
    {
        return const_iterator{0, {m_chunks.data(), m_chunks.size()}};
    }

    constexpr const_iterator end() const
    {
        return const_iterator{m_size, {m_chunks.data(), m_chunks.size()}};
    }
};

} // namespace asl