10
10
#define LLVM_LIBC_SRC___SUPPORT_FIXEDVECTOR_H
11
11
12
12
#include " src/__support/CPP/array.h"
13
-
14
13
#include " src/__support/CPP/iterator.h"
14
+ #include " src/__support/libc_assert.h"
15
15
#include " src/__support/macros/config.h"
16
+ #include " src/string/memory_utils/inline_memset.h"
16
17
17
18
namespace LIBC_NAMESPACE_DECL {
18
19
@@ -23,55 +24,76 @@ template <typename T, size_t CAPACITY> class FixedVector {
23
24
size_t item_count = 0 ;
24
25
25
26
public:
26
- constexpr FixedVector () = default;
27
+ LIBC_INLINE constexpr FixedVector () = default;
27
28
28
29
using iterator = typename cpp::array<T, CAPACITY>::iterator;
29
- constexpr FixedVector (iterator begin, iterator end) : store{}, item_count{} {
30
+ LIBC_INLINE constexpr FixedVector (iterator begin, iterator end)
31
+ : store{}, item_count{} {
32
+ LIBC_ASSERT (begin + CAPACITY >= end);
30
33
for (; begin != end; ++begin)
31
34
push_back (*begin);
32
35
}
33
36
34
37
using const_iterator = typename cpp::array<T, CAPACITY>::const_iterator;
35
- constexpr FixedVector (const_iterator begin, const_iterator end)
38
+ LIBC_INLINE constexpr FixedVector (const_iterator begin, const_iterator end)
36
39
: store{}, item_count{} {
40
+ LIBC_ASSERT (begin + CAPACITY >= end);
37
41
for (; begin != end; ++begin)
38
42
push_back (*begin);
39
43
}
40
44
41
- constexpr FixedVector (size_t count, const T &value) : store{}, item_count{} {
45
+ LIBC_INLINE constexpr FixedVector (size_t count, const T &value)
46
+ : store{}, item_count{} {
47
+ LIBC_ASSERT (count <= CAPACITY);
42
48
for (size_t i = 0 ; i < count; ++i)
43
49
push_back (value);
44
50
}
45
51
46
- constexpr bool push_back (const T &obj) {
52
+ LIBC_INLINE constexpr bool push_back (const T &obj) {
47
53
if (item_count == CAPACITY)
48
54
return false ;
49
55
store[item_count] = obj;
50
56
++item_count;
51
57
return true ;
52
58
}
53
59
54
- constexpr const T &back () const { return store[item_count - 1 ]; }
60
+ LIBC_INLINE constexpr const T &back () const {
61
+ LIBC_ASSERT (!empty ());
62
+ return store[item_count - 1 ];
63
+ }
55
64
56
- constexpr T &back () { return store[item_count - 1 ]; }
65
+ LIBC_INLINE constexpr T &back () {
66
+ LIBC_ASSERT (!empty ());
67
+ return store[item_count - 1 ];
68
+ }
57
69
58
- constexpr bool pop_back () {
70
+ LIBC_INLINE constexpr bool pop_back () {
59
71
if (item_count == 0 )
60
72
return false ;
73
+ inline_memset (&store[item_count - 1 ], 0 , sizeof (T));
61
74
--item_count;
62
75
return true ;
63
76
}
64
77
65
- constexpr T &operator [](size_t idx) { return store[idx]; }
78
+ LIBC_INLINE constexpr T &operator [](size_t idx) {
79
+ LIBC_ASSERT (idx < item_count);
80
+ return store[idx];
81
+ }
66
82
67
- constexpr const T &operator [](size_t idx) const { return store[idx]; }
83
+ LIBC_INLINE constexpr const T &operator [](size_t idx) const {
84
+ LIBC_ASSERT (idx < item_count);
85
+ return store[idx];
86
+ }
68
87
69
- constexpr bool empty () const { return item_count == 0 ; }
88
+ LIBC_INLINE constexpr bool empty () const { return item_count == 0 ; }
70
89
71
- constexpr size_t size () const { return item_count; }
90
+ LIBC_INLINE constexpr size_t size () const { return item_count; }
72
91
73
92
// Empties the store for all practical purposes.
74
- constexpr void reset () { item_count = 0 ; }
93
+ LIBC_INLINE constexpr void reset () {
94
+ inline_memset (store.data (), 0 , sizeof (T) * item_count);
95
+ item_count = 0 ;
96
+ }
75
97
76
98
// This static method does not free up the resources held by |store|,
77
99
// say by calling `free` or something similar. It just does the equivalent
@@ -81,7 +103,9 @@ template <typename T, size_t CAPACITY> class FixedVector {
81
103
// dynamically allocated storate. So, the `destroy` method like this
82
104
// matches the `destroy` API of those other data structures so that users
83
105
// can easily swap one data structure for the other.
84
- static void destroy (FixedVector<T, CAPACITY> *store) { store->reset (); }
106
+ LIBC_INLINE static void destroy (FixedVector<T, CAPACITY> *store) {
107
+ store->reset ();
108
+ }
85
109
86
110
using reverse_iterator = typename cpp::array<T, CAPACITY>::reverse_iterator;
87
111
LIBC_INLINE constexpr reverse_iterator rbegin () {
0 commit comments