-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvec.hh
598 lines (484 loc) · 17.3 KB
/
vec.hh
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
// Copyright (c) 2022 Mikael Simonsson <https://mikaelsimonsson.com>.
// SPDX-License-Identifier: BSL-1.0
// # Vector with optional small-capacity
// * Optional small-capacity (on stack).
// * Trivially-relocatable optimization.
#pragma once
#include "snn-core/array_view.hh"
#include "snn-core/exception.hh"
#include "snn-core/contiguous_interface.hh"
#include "snn-core/detail/vec/common.hh"
#include "snn-core/generic/error.hh"
#include "snn-core/math/common.hh"
#include "snn-core/mem/copy_construct.hh"
#include "snn-core/mem/construct.hh"
#include "snn-core/mem/destruct.hh"
#include "snn-core/mem/destruct_n.hh"
#include "snn-core/mem/relocate_left.hh"
#include "snn-core/mem/relocate_right.hh"
#include "snn-core/range/contiguous.hh"
namespace snn
{
// ## Classes
// ### vec
template <typename T, usize SmallCapacity = 0>
requires(std::is_move_constructible_v<T> && std::is_same_v<std::remove_cvref_t<T>, T>)
class vec final : public contiguous_interface<vec<T, SmallCapacity>>
{
private:
using buffer_type = detail::vec::buffer<T, SmallCapacity>;
public:
// #### Types
using value_type = T;
using iterator = T*;
using const_iterator = const T*;
using pointer = T*;
using const_pointer = const T*;
using reference = T&;
using const_reference = const T&;
using trivially_relocatable_type = trivially_relocatable_if_t<vec, buffer_type>;
// #### Default constructor
constexpr vec() noexcept = default;
// #### Explicit constructors
constexpr explicit vec(container::reserve_t, const usize capacity)
: vec{}
{
reserve(capacity);
}
constexpr explicit vec(meta::iterators_t, const const_iterator first,
const const_iterator last)
: vec{}
{
snn_should(first == last || (first != nullptr && last != nullptr && last > first));
const auto count = static_cast<usize>(last - first);
if (count > 0)
{
reserve(count);
mem::copy_construct(not_null{first}, not_null{last}, not_null{buf_.begin()},
promise::no_overlap);
buf_.set_count(count, promise::has_capacity);
}
}
// #### Converting constructors
constexpr vec(initializer_list<T> init)
: vec{}
{
if (init.size() > 0)
{
reserve(init.size());
mem::copy_construct(not_null{init.begin()}, not_null{init.end()},
not_null{buf_.begin()}, promise::no_overlap);
buf_.set_count(init.size(), promise::has_capacity);
}
}
// #### Copy/move-assignment/constructor
constexpr vec(const vec& other)
: vec{}
{
append(other);
}
constexpr vec& operator=(const vec& other)
{
if (this != &other)
{
clear();
append(other);
}
return *this;
}
constexpr vec(vec&& other) noexcept
: buf_{std::move(other.buf_)}
{
}
constexpr vec& operator=(vec&& other) noexcept
{
swap(other);
return *this;
}
// #### Destructor
~vec() = default; // "Rule of five".
// #### Explicit conversion operators
constexpr explicit operator bool() const noexcept
{
return !is_empty();
}
// #### Iterators
[[nodiscard]] constexpr iterator begin() noexcept
{
return buf_.begin();
}
[[nodiscard]] constexpr iterator end() noexcept
{
return buf_.end();
}
[[nodiscard]] constexpr const_iterator begin() const noexcept
{
return buf_.cbegin();
}
[[nodiscard]] constexpr const_iterator end() const noexcept
{
return buf_.cend();
}
[[nodiscard]] constexpr const_iterator cbegin() const noexcept
{
return buf_.cbegin();
}
[[nodiscard]] constexpr const_iterator cend() const noexcept
{
return buf_.cend();
}
// #### Single element access
SNN_DIAGNOSTIC_PUSH
SNN_DIAGNOSTIC_IGNORE_UNSAFE_BUFFER_USAGE
template <value_type_or<reference> R = reference>
[[nodiscard]] constexpr optional<R> at(const usize pos)
noexcept(std::is_nothrow_copy_constructible_v<R>)
{
if (pos < buf_.count())
{
return buf_.begin()[pos];
}
return nullopt;
}
template <value_type_or<const_reference> R = const_reference>
[[nodiscard]] constexpr optional<R> at(const usize pos) const
noexcept(std::is_nothrow_copy_constructible_v<R>)
{
if (pos < buf_.count())
{
return buf_.cbegin()[pos];
}
return nullopt;
}
[[nodiscard]] constexpr reference at(const usize pos, promise::within_bounds_t) noexcept
{
// Don't use view(), it makes it harder for the optimizer to remove the assert.
snn_assert(pos < buf_.count());
return buf_.begin()[pos];
}
[[nodiscard]] constexpr const_reference at(const usize pos,
promise::within_bounds_t) const noexcept
{
// Don't use view(), it makes it harder for the optimizer to remove the assert.
snn_assert(pos < buf_.count());
return buf_.cbegin()[pos];
}
template <value_type_or<reference> R = reference>
[[nodiscard]] constexpr optional<R> back() noexcept(std::is_nothrow_copy_constructible_v<R>)
{
const usize c = buf_.count();
if (c > 0)
{
return buf_.begin()[c - 1];
}
return nullopt;
}
template <value_type_or<const_reference> R = const_reference>
[[nodiscard]] constexpr optional<R> back() const
noexcept(std::is_nothrow_copy_constructible_v<R>)
{
const usize c = buf_.count();
if (c > 0)
{
return buf_.cbegin()[c - 1];
}
return nullopt;
}
[[nodiscard]] constexpr reference back(promise::not_empty_t) noexcept
{
// Don't use view(), it makes it harder for the optimizer to remove the assert.
snn_assert(buf_.count() > 0);
return buf_.begin()[buf_.count() - 1];
}
[[nodiscard]] constexpr const_reference back(promise::not_empty_t) const noexcept
{
// Don't use view(), it makes it harder for the optimizer to remove the assert.
snn_assert(buf_.count() > 0);
return buf_.cbegin()[buf_.count() - 1];
}
template <value_type_or<reference> R = reference>
[[nodiscard]] constexpr optional<R> front()
noexcept(std::is_nothrow_copy_constructible_v<R>)
{
if (buf_.count() > 0)
{
return buf_.begin()[0];
}
return nullopt;
}
template <value_type_or<const_reference> R = const_reference>
[[nodiscard]] constexpr optional<R> front() const
noexcept(std::is_nothrow_copy_constructible_v<R>)
{
if (buf_.count() > 0)
{
return buf_.cbegin()[0];
}
return nullopt;
}
[[nodiscard]] constexpr reference front(promise::not_empty_t) noexcept
{
// Don't use view(), it makes it harder for the optimizer to remove the assert.
snn_assert(buf_.count() > 0);
return buf_.begin()[0];
}
[[nodiscard]] constexpr const_reference front(promise::not_empty_t) const noexcept
{
// Don't use view(), it makes it harder for the optimizer to remove the assert.
snn_assert(buf_.count() > 0);
return buf_.cbegin()[0];
}
SNN_DIAGNOSTIC_POP
// #### Append value
constexpr void append(T value)
{
if (buf_.count() == buf_.capacity()) [[unlikely]]
{
buf_.grow(not_zero{check_capacity_(recommend_capacity_())}, promise::is_valid);
}
mem::construct(not_null{buf_.end()}, std::move(value));
buf_.increment_count(promise::has_capacity);
}
template <typename... Args>
constexpr void append_inplace(Args&&... args)
{
if (buf_.count() < buf_.capacity()) [[likely]]
{
mem::construct(not_null{buf_.end()}, std::forward<Args>(args)...);
buf_.increment_count(promise::has_capacity);
}
else
{
// Arguments could come from within self, use slow grow path.
buf_.grow_append_inplace(not_zero{check_capacity_(recommend_capacity_())},
promise::is_valid, std::forward<Args>(args)...);
}
}
// #### Append another vec
constexpr void append(const same_as<vec> auto& other)
{
// Note: Other could be *this.
if (other.count() > 0)
{
reserve_append(other.count());
mem::copy_construct(not_null{other.cbegin()}, not_null{other.cend()},
not_null{buf_.end()}, promise::no_overlap);
buf_.set_count(buf_.count() + other.count(), promise::has_capacity);
}
}
// #### Count/Size
[[nodiscard]] constexpr snn::byte_size byte_size() const noexcept
{
return snn::byte_size{sizeof(T) * buf_.count()};
}
[[nodiscard]] constexpr usize count() const noexcept
{
return buf_.count();
}
[[nodiscard]] constexpr bool is_empty() const noexcept
{
return buf_.count() == 0;
}
[[nodiscard]] constexpr usize size() const noexcept
requires(sizeof(T) == 1) // Use `count()` or `byte_size()` instead.
{
return buf_.count();
}
// #### Capacity
[[nodiscard]] constexpr usize capacity() const noexcept
{
return buf_.capacity();
}
[[nodiscard]] static constexpr usize default_capacity() noexcept
{
return SmallCapacity;
}
constexpr void reserve(const usize capacity)
{
if (capacity > buf_.capacity())
{
buf_.grow(not_zero{check_capacity_(math::max(capacity, recommend_capacity_()))},
promise::is_valid);
}
}
constexpr void reserve_append(const usize append_count)
{
reserve(math::add_with_saturation(buf_.count(), append_count));
}
// #### Raw data access
[[nodiscard]] constexpr const_pointer data() const noexcept
{
return buf_.cbegin();
}
// pointer data() noexcept
// {
// // Not implemented.
// // Use writable(), which is more readable and easier to audit.
// }
[[nodiscard]] constexpr pointer writable() noexcept
{
return buf_.begin();
}
// #### Range
[[nodiscard]] constexpr auto range() noexcept
{
return view().range();
}
[[nodiscard]] constexpr auto range() const noexcept
{
return view().range();
}
// #### View
[[nodiscard]] constexpr auto view() noexcept
{
return array_view<T>{buf_.begin(), buf_.count()};
}
[[nodiscard]] constexpr auto view() const noexcept
{
return array_view<const T>{buf_.cbegin(), buf_.count()};
}
[[nodiscard]] constexpr auto view(const usize pos,
const usize count = constant::npos) noexcept
{
return view().view(pos, count);
}
[[nodiscard]] constexpr auto view(const usize pos,
const usize count = constant::npos) const noexcept
{
return view().view(pos, count);
}
// #### Modifiers
constexpr void clear() noexcept
{
buf_.clear();
}
constexpr void insert_at(const usize pos, T value)
{
if (pos < buf_.count())
{
if (buf_.count() == buf_.capacity()) [[unlikely]]
{
buf_.grow(not_zero{check_capacity_(recommend_capacity_())}, promise::is_valid);
}
SNN_DIAGNOSTIC_PUSH
SNN_DIAGNOSTIC_IGNORE_UNSAFE_BUFFER_USAGE
mem::relocate_right(not_null{buf_.begin() + pos}, not_null{buf_.end()},
not_zero<usize>{1});
mem::construct(not_null{buf_.begin() + pos}, std::move(value)); // Must not throw.
SNN_DIAGNOSTIC_POP
buf_.increment_count(promise::has_capacity);
}
else if (pos == buf_.count())
{
append(std::move(value));
}
else
{
throw_or_abort(generic::error::invalid_position_for_replace_or_insert);
}
}
constexpr void drop_at(const usize pos, const usize count) noexcept
{
if (pos < buf_.count() && count > 0)
{
drop_at_(pos, count);
}
}
constexpr void drop_back(promise::not_empty_t) noexcept
{
snn_assert(buf_.count() > 0);
buf_.decrement_count(promise::not_empty);
mem::destruct(not_null{buf_.end()});
}
constexpr void drop_back_n(const usize count) noexcept
{
drop_back_n_(math::min(count, buf_.count()));
}
constexpr void truncate(const usize count) noexcept
{
if (count < buf_.count())
{
drop_back_n_(buf_.count() - count);
}
}
// #### Search
template <typename V>
[[nodiscard]] constexpr usize count(V&& value) const
{
return view().count(std::forward<V>(value));
}
// #### Swap
constexpr void swap(vec& other) noexcept
{
// This works even if this == &other.
buf_.swap(other.buf_);
}
// #### Stream append (insertion operator)
constexpr vec& operator<<(T value)
{
append(std::move(value));
return *this;
}
constexpr vec& operator<<(const same_as<vec> auto& other)
{
append(other);
return *this;
}
// #### Comparison
constexpr bool operator==(const vec& other) const noexcept
{
return view() == other.view();
}
// #### Contiguous interface
// This class inherits from `contiguous_interface`, see
// [contiguous\_interface.hh](contiguous_interface.hh) for additional functionality.
private:
buffer_type buf_;
static constexpr usize check_capacity_(const usize capacity)
{
if (capacity <= detail::vec::max_capacity<T>)
{
return capacity;
}
throw_or_abort(generic::error::capacity_would_exceed_max_capacity);
}
constexpr usize recommend_capacity_() const noexcept
{
// Always return a capacity greater than the current count.
// This can't overflow `usize`, count is always `<= constant::limit<iptrdiff>::max`.
return buf_.count() + ((buf_.count() + 2) / 2); // ~1.5x
}
constexpr void drop_at_(const usize pos, const usize count) noexcept
{
snn_should(pos < buf_.count() && count > 0);
const usize remaining_count = buf_.count() - pos;
if (count >= remaining_count)
{
drop_back_n_(remaining_count);
}
else
{
SNN_DIAGNOSTIC_PUSH
SNN_DIAGNOSTIC_IGNORE_UNSAFE_BUFFER_USAGE
mem::destruct_n(buf_.begin() + pos, count);
mem::relocate_left(not_null{buf_.begin() + pos + count}, not_null{buf_.end()},
not_zero{count});
SNN_DIAGNOSTIC_POP
buf_.set_count(buf_.count() - count, promise::has_capacity);
}
}
constexpr void drop_back_n_(usize count) noexcept
{
snn_should(count <= buf_.count());
buf_.set_count(buf_.count() - count, promise::has_capacity);
mem::destruct_n(buf_.end(), count);
}
};
// ## Functions
// ### swap
template <typename T, usize SmallCapacity>
constexpr void swap(vec<T, SmallCapacity>& a, vec<T, SmallCapacity>& b) noexcept
{
a.swap(b);
}
}