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
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
|
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Assertions.h>
#include <AK/Find.h>
#include <AK/Forward.h>
#include <AK/Iterator.h>
#include <AK/Optional.h>
#include <AK/Span.h>
#include <AK/StdLibExtras.h>
#include <AK/Traits.h>
#include <AK/TypedTransfer.h>
#include <AK/kmalloc.h>
// NOTE: We can't include <initializer_list> during the toolchain bootstrap,
// since it's part of libstdc++, and libstdc++ depends on LibC.
// For this reason, we don't support Vector(initializer_list) in LibC.
#ifndef SERENITY_LIBC_BUILD
# include <initializer_list>
#endif
#ifndef __serenity__
# include <new>
#endif
namespace AK {
namespace Detail {
template<typename StorageType, bool>
struct CanBePlacedInsideVectorHelper;
template<typename StorageType>
struct CanBePlacedInsideVectorHelper<StorageType, true> {
template<typename U>
static constexpr bool value = requires(U&& u) { StorageType { &u }; };
};
template<typename StorageType>
struct CanBePlacedInsideVectorHelper<StorageType, false> {
template<typename U>
static constexpr bool value = requires(U&& u) { StorageType(forward<U>(u)); };
};
}
template<typename T, size_t inline_capacity>
requires(!IsRvalueReference<T>) class Vector {
private:
static constexpr bool contains_reference = IsLvalueReference<T>;
using StorageType = Conditional<contains_reference, RawPtr<RemoveReference<T>>, T>;
template<typename U>
static constexpr bool CanBePlacedInsideVector = Detail::CanBePlacedInsideVectorHelper<StorageType, contains_reference>::template value<U>;
public:
using ValueType = T;
Vector()
: m_capacity(inline_capacity)
{
}
#ifndef SERENITY_LIBC_BUILD
Vector(std::initializer_list<T> list) requires(!IsLvalueReference<T>)
{
ensure_capacity(list.size());
for (auto& item : list)
unchecked_append(item);
}
#endif
Vector(Vector&& other)
: m_size(other.m_size)
, m_capacity(other.m_capacity)
, m_outline_buffer(other.m_outline_buffer)
{
if constexpr (inline_capacity > 0) {
if (!m_outline_buffer) {
for (size_t i = 0; i < m_size; ++i) {
new (&inline_buffer()[i]) StorageType(move(other.inline_buffer()[i]));
other.inline_buffer()[i].~StorageType();
}
}
}
other.m_outline_buffer = nullptr;
other.m_size = 0;
other.reset_capacity();
}
Vector(Vector const& other)
{
ensure_capacity(other.size());
TypedTransfer<StorageType>::copy(data(), other.data(), other.size());
m_size = other.size();
}
template<size_t other_inline_capacity>
Vector(Vector<T, other_inline_capacity> const& other)
{
ensure_capacity(other.size());
TypedTransfer<StorageType>::copy(data(), other.data(), other.size());
m_size = other.size();
}
~Vector()
{
clear();
}
Span<StorageType> span() { return { data(), size() }; }
Span<StorageType const> span() const { return { data(), size() }; }
operator Span<StorageType>() { return span(); }
operator Span<StorageType const>() const { return span(); }
bool is_empty() const { return size() == 0; }
ALWAYS_INLINE size_t size() const { return m_size; }
size_t capacity() const { return m_capacity; }
StorageType* data()
{
if constexpr (inline_capacity > 0)
return m_outline_buffer ? m_outline_buffer : inline_buffer();
return m_outline_buffer;
}
StorageType const* data() const
{
if constexpr (inline_capacity > 0)
return m_outline_buffer ? m_outline_buffer : inline_buffer();
return m_outline_buffer;
}
ALWAYS_INLINE T const& at(size_t i) const
{
VERIFY(i < m_size);
if constexpr (contains_reference)
return *data()[i];
else
return data()[i];
}
ALWAYS_INLINE T& at(size_t i)
{
VERIFY(i < m_size);
if constexpr (contains_reference)
return *data()[i];
else
return data()[i];
}
ALWAYS_INLINE T const& operator[](size_t i) const { return at(i); }
ALWAYS_INLINE T& operator[](size_t i) { return at(i); }
T const& first() const { return at(0); }
T& first() { return at(0); }
T const& last() const { return at(size() - 1); }
T& last() { return at(size() - 1); }
template<typename Callback>
Optional<T> first_matching(Callback callback)
{
for (size_t i = 0; i < size(); ++i) {
if (callback(at(i))) {
return at(i);
}
}
return {};
}
template<typename Callback>
Optional<T> last_matching(Callback callback)
{
for (ssize_t i = size() - 1; i >= 0; --i) {
if (callback(at(i))) {
return at(i);
}
}
return {};
}
template<typename V>
bool operator==(V const& other) const
{
if (m_size != other.size())
return false;
return TypedTransfer<StorageType>::compare(data(), other.data(), size());
}
bool contains_slow(T const& value) const
{
for (size_t i = 0; i < size(); ++i) {
if (Traits<T>::equals(at(i), value))
return true;
}
return false;
}
bool contains_in_range(T const& value, size_t const start, size_t const end) const
{
VERIFY(start <= end);
VERIFY(end < size());
for (size_t i = start; i <= end; ++i) {
if (Traits<T>::equals(at(i), value))
return true;
}
return false;
}
template<typename U = T>
void insert(size_t index, U&& value) requires(CanBePlacedInsideVector<U>)
{
auto did_allocate = try_insert<U>(index, forward<U>(value));
VERIFY(did_allocate);
}
template<typename C, typename U = T>
void insert_before_matching(U&& value, C callback, size_t first_index = 0, size_t* inserted_index = nullptr) requires(CanBePlacedInsideVector<U>)
{
auto did_allocate = try_insert_before_matching(forward<U>(value), callback, first_index, inserted_index);
VERIFY(did_allocate);
}
void extend(Vector&& other)
{
auto did_allocate = try_extend(move(other));
VERIFY(did_allocate);
}
void extend(Vector const& other)
{
auto did_allocate = try_extend(other);
VERIFY(did_allocate);
}
ALWAYS_INLINE void append(T&& value)
{
bool did_allocate;
if constexpr (contains_reference)
did_allocate = try_append(value);
else
did_allocate = try_append(move(value));
VERIFY(did_allocate);
}
ALWAYS_INLINE void append(T const& value) requires(!contains_reference)
{
auto did_allocate = try_append(T(value));
VERIFY(did_allocate);
}
void append(StorageType const* values, size_t count)
{
auto did_allocate = try_append(values, count);
VERIFY(did_allocate);
}
template<typename U = T>
ALWAYS_INLINE void unchecked_append(U&& value) requires(CanBePlacedInsideVector<U>)
{
VERIFY((size() + 1) <= capacity());
if constexpr (contains_reference)
new (slot(m_size)) StorageType(&value);
else
new (slot(m_size)) StorageType(forward<U>(value));
++m_size;
}
template<class... Args>
void empend(Args&&... args) requires(!contains_reference)
{
auto did_allocate = try_empend(forward<Args>(args)...);
VERIFY(did_allocate);
}
template<typename U = T>
void prepend(U&& value) requires(CanBePlacedInsideVector<U>)
{
auto did_allocate = try_insert(0, forward<U>(value));
VERIFY(did_allocate);
}
void prepend(Vector&& other)
{
auto did_allocate = try_prepend(move(other));
VERIFY(did_allocate);
}
void prepend(StorageType const* values, size_t count)
{
auto did_allocate = try_prepend(values, count);
VERIFY(did_allocate);
}
// FIXME: What about assigning from a vector with lower inline capacity?
Vector& operator=(Vector&& other)
{
if (this != &other) {
clear();
m_size = other.m_size;
m_capacity = other.m_capacity;
m_outline_buffer = other.m_outline_buffer;
if constexpr (inline_capacity > 0) {
if (!m_outline_buffer) {
for (size_t i = 0; i < m_size; ++i) {
new (&inline_buffer()[i]) StorageType(move(other.inline_buffer()[i]));
other.inline_buffer()[i].~StorageType();
}
}
}
other.m_outline_buffer = nullptr;
other.m_size = 0;
other.reset_capacity();
}
return *this;
}
Vector& operator=(Vector const& other)
{
if (this != &other) {
clear();
ensure_capacity(other.size());
TypedTransfer<StorageType>::copy(data(), other.data(), other.size());
m_size = other.size();
}
return *this;
}
template<size_t other_inline_capacity>
Vector& operator=(Vector<T, other_inline_capacity> const& other)
{
clear();
ensure_capacity(other.size());
TypedTransfer<StorageType>::copy(data(), other.data(), other.size());
m_size = other.size();
return *this;
}
void clear()
{
clear_with_capacity();
if (m_outline_buffer) {
kfree(m_outline_buffer);
m_outline_buffer = nullptr;
}
reset_capacity();
}
void clear_with_capacity()
{
for (size_t i = 0; i < m_size; ++i)
data()[i].~StorageType();
m_size = 0;
}
void remove(size_t index)
{
VERIFY(index < m_size);
if constexpr (Traits<StorageType>::is_trivial()) {
TypedTransfer<StorageType>::copy(slot(index), slot(index + 1), m_size - index - 1);
} else {
at(index).~StorageType();
for (size_t i = index + 1; i < m_size; ++i) {
new (slot(i - 1)) StorageType(move(at(i)));
at(i).~StorageType();
}
}
--m_size;
}
void remove(size_t index, size_t count)
{
if (count == 0)
return;
VERIFY(index + count > index);
VERIFY(index + count <= m_size);
if constexpr (Traits<StorageType>::is_trivial()) {
TypedTransfer<StorageType>::copy(slot(index), slot(index + count), m_size - index - count);
} else {
for (size_t i = index; i < index + count; i++)
at(i).~StorageType();
for (size_t i = index + count; i < m_size; ++i) {
new (slot(i - count)) StorageType(move(at(i)));
at(i).~StorageType();
}
}
m_size -= count;
}
template<typename Callback>
bool remove_first_matching(Callback callback)
{
for (size_t i = 0; i < size(); ++i) {
if (callback(at(i))) {
remove(i);
return true;
}
}
return false;
}
template<typename Callback>
void remove_all_matching(Callback callback)
{
for (size_t i = 0; i < size();) {
if (callback(at(i))) {
remove(i);
} else {
++i;
}
}
}
ALWAYS_INLINE T take_last()
{
VERIFY(!is_empty());
auto value = move(raw_last());
if constexpr (!contains_reference)
last().~T();
--m_size;
if constexpr (contains_reference)
return *value;
else
return value;
}
T take_first()
{
VERIFY(!is_empty());
auto value = move(raw_first());
remove(0);
if constexpr (contains_reference)
return *value;
else
return value;
}
T take(size_t index)
{
auto value = move(raw_at(index));
remove(index);
if constexpr (contains_reference)
return *value;
else
return value;
}
T unstable_take(size_t index)
{
VERIFY(index < m_size);
swap(raw_at(index), raw_at(m_size - 1));
return take_last();
}
template<typename U = T>
[[nodiscard]] bool try_insert(size_t index, U&& value) requires(CanBePlacedInsideVector<U>)
{
if (index > size())
return false;
if (index == size())
return try_append(forward<U>(value));
if (!try_grow_capacity(size() + 1))
return false;
++m_size;
if constexpr (Traits<StorageType>::is_trivial()) {
TypedTransfer<StorageType>::move(slot(index + 1), slot(index), m_size - index - 1);
} else {
for (size_t i = size() - 1; i > index; --i) {
new (slot(i)) StorageType(move(at(i - 1)));
at(i - 1).~StorageType();
}
}
if constexpr (contains_reference)
new (slot(index)) StorageType(&value);
else
new (slot(index)) StorageType(forward<U>(value));
return true;
}
template<typename C, typename U = T>
[[nodiscard]] bool try_insert_before_matching(U&& value, C callback, size_t first_index = 0, size_t* inserted_index = nullptr) requires(CanBePlacedInsideVector<U>)
{
for (size_t i = first_index; i < size(); ++i) {
if (callback(at(i))) {
if (!try_insert(i, forward<U>(value)))
return false;
if (inserted_index)
*inserted_index = i;
return true;
}
}
if (!try_append(forward<U>(value)))
return false;
if (inserted_index)
*inserted_index = size() - 1;
return true;
}
[[nodiscard]] bool try_extend(Vector&& other)
{
if (is_empty()) {
*this = move(other);
return true;
}
auto other_size = other.size();
Vector tmp = move(other);
if (!try_grow_capacity(size() + other_size))
return false;
TypedTransfer<StorageType>::move(data() + m_size, tmp.data(), other_size);
m_size += other_size;
return true;
}
[[nodiscard]] bool try_extend(Vector const& other)
{
if (!try_grow_capacity(size() + other.size()))
return false;
TypedTransfer<StorageType>::copy(data() + m_size, other.data(), other.size());
m_size += other.m_size;
return true;
}
[[nodiscard]] ALWAYS_INLINE bool try_append(T&& value)
{
if (!try_grow_capacity(size() + 1))
return false;
if constexpr (contains_reference)
new (slot(m_size)) StorageType(&value);
else
new (slot(m_size)) StorageType(move(value));
++m_size;
return true;
}
[[nodiscard]] ALWAYS_INLINE bool try_append(T const& value) requires(!contains_reference)
{
return try_append(T(value));
}
[[nodiscard]] bool try_append(StorageType const* values, size_t count)
{
if (!count)
return true;
if (!try_grow_capacity(size() + count))
return false;
TypedTransfer<StorageType>::copy(slot(m_size), values, count);
m_size += count;
return true;
}
template<class... Args>
[[nodiscard]] bool try_empend(Args&&... args) requires(!contains_reference)
{
if (!try_grow_capacity(m_size + 1))
return false;
new (slot(m_size)) StorageType { forward<Args>(args)... };
++m_size;
return true;
}
template<typename U = T>
[[nodiscard]] bool try_prepend(U&& value) requires(CanBePlacedInsideVector<U>)
{
return try_insert(0, forward<U>(value));
}
[[nodiscard]] bool try_prepend(Vector&& other)
{
if (other.is_empty())
return true;
if (is_empty()) {
*this = move(other);
return true;
}
auto other_size = other.size();
if (!try_grow_capacity(size() + other_size))
return false;
for (size_t i = size() + other_size - 1; i >= other.size(); --i) {
new (slot(i)) StorageType(move(at(i - other_size)));
at(i - other_size).~StorageType();
}
Vector tmp = move(other);
TypedTransfer<StorageType>::move(slot(0), tmp.data(), tmp.size());
m_size += other_size;
return true;
}
[[nodiscard]] bool try_prepend(StorageType const* values, size_t count)
{
if (!count)
return true;
if (!try_grow_capacity(size() + count))
return false;
TypedTransfer<StorageType>::move(slot(count), slot(0), m_size);
TypedTransfer<StorageType>::copy(slot(0), values, count);
m_size += count;
return true;
}
[[nodiscard]] bool try_grow_capacity(size_t needed_capacity)
{
if (m_capacity >= needed_capacity)
return true;
return try_ensure_capacity(padded_capacity(needed_capacity));
}
[[nodiscard]] bool try_ensure_capacity(size_t needed_capacity)
{
if (m_capacity >= needed_capacity)
return true;
size_t new_capacity = kmalloc_good_size(needed_capacity * sizeof(StorageType)) / sizeof(StorageType);
auto* new_buffer = (StorageType*)kmalloc(new_capacity * sizeof(StorageType));
if (new_buffer == nullptr)
return false;
if constexpr (Traits<StorageType>::is_trivial()) {
TypedTransfer<StorageType>::copy(new_buffer, data(), m_size);
} else {
for (size_t i = 0; i < m_size; ++i) {
new (&new_buffer[i]) StorageType(move(at(i)));
at(i).~StorageType();
}
}
if (m_outline_buffer)
kfree(m_outline_buffer);
m_outline_buffer = new_buffer;
m_capacity = new_capacity;
return true;
}
[[nodiscard]] bool try_resize(size_t new_size, bool keep_capacity = false) requires(!contains_reference)
{
if (new_size <= size()) {
shrink(new_size, keep_capacity);
return true;
}
if (!try_ensure_capacity(new_size))
return false;
for (size_t i = size(); i < new_size; ++i)
new (slot(i)) StorageType {};
m_size = new_size;
return true;
}
[[nodiscard]] bool try_resize_and_keep_capacity(size_t new_size) requires(!contains_reference)
{
return try_resize(new_size, true);
}
void grow_capacity(size_t needed_capacity)
{
auto did_allocate = try_grow_capacity(needed_capacity);
VERIFY(did_allocate);
}
void ensure_capacity(size_t needed_capacity)
{
auto did_allocate = try_ensure_capacity(needed_capacity);
VERIFY(did_allocate);
}
void shrink(size_t new_size, bool keep_capacity = false)
{
VERIFY(new_size <= size());
if (new_size == size())
return;
if (!new_size) {
if (keep_capacity)
clear_with_capacity();
else
clear();
return;
}
for (size_t i = new_size; i < size(); ++i)
at(i).~StorageType();
m_size = new_size;
}
void resize(size_t new_size, bool keep_capacity = false) requires(!contains_reference)
{
auto did_allocate = try_resize(new_size, keep_capacity);
VERIFY(did_allocate);
}
void resize_and_keep_capacity(size_t new_size) requires(!contains_reference)
{
auto did_allocate = try_resize_and_keep_capacity(new_size);
VERIFY(did_allocate);
}
using ConstIterator = SimpleIterator<Vector const, T const>;
using Iterator = SimpleIterator<Vector, T>;
ConstIterator begin() const { return ConstIterator::begin(*this); }
Iterator begin() { return Iterator::begin(*this); }
ConstIterator end() const { return ConstIterator::end(*this); }
Iterator end() { return Iterator::end(*this); }
template<typename TUnaryPredicate>
ConstIterator find_if(TUnaryPredicate&& finder) const
{
return AK::find_if(begin(), end(), forward<TUnaryPredicate>(finder));
}
template<typename TUnaryPredicate>
Iterator find_if(TUnaryPredicate&& finder)
{
return AK::find_if(begin(), end(), forward<TUnaryPredicate>(finder));
}
ConstIterator find(T const& value) const
{
return AK::find(begin(), end(), value);
}
Iterator find(T const& value)
{
return AK::find(begin(), end(), value);
}
Optional<size_t> find_first_index(T const& value) const
{
if (auto const index = AK::find_index(begin(), end(), value);
index < size()) {
return index;
}
return {};
}
private:
void reset_capacity()
{
m_capacity = inline_capacity;
}
static size_t padded_capacity(size_t capacity)
{
return max(static_cast<size_t>(4), capacity + (capacity / 4) + 4);
}
StorageType* slot(size_t i) { return &data()[i]; }
StorageType const* slot(size_t i) const { return &data()[i]; }
StorageType* inline_buffer()
{
static_assert(inline_capacity > 0);
return reinterpret_cast<StorageType*>(m_inline_buffer_storage);
}
StorageType const* inline_buffer() const
{
static_assert(inline_capacity > 0);
return reinterpret_cast<StorageType const*>(m_inline_buffer_storage);
}
StorageType& raw_last() { return raw_at(size() - 1); }
StorageType& raw_first() { return raw_at(0); }
StorageType& raw_at(size_t index) { return *slot(index); }
size_t m_size { 0 };
size_t m_capacity { 0 };
alignas(StorageType) unsigned char m_inline_buffer_storage[sizeof(StorageType) * inline_capacity];
StorageType* m_outline_buffer { nullptr };
};
}
using AK::Vector;
|