1 //===-- A data structure for a fixed capacity data store --------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_LIBC_SRC___SUPPORT_FIXEDVECTOR_H
10 #define LLVM_LIBC_SRC___SUPPORT_FIXEDVECTOR_H
12 #include "src/__support/CPP/array.h"
14 #include "src/__support/CPP/iterator.h"
15 #include "src/__support/macros/config.h"
17 namespace LIBC_NAMESPACE_DECL
{
19 // A fixed size data store backed by an underlying cpp::array data structure. It
20 // supports vector like API but is not resizable like a vector.
21 template <typename T
, size_t CAPACITY
> class FixedVector
{
22 cpp::array
<T
, CAPACITY
> store
;
23 size_t item_count
= 0;
26 constexpr FixedVector() = default;
28 using iterator
= typename
cpp::array
<T
, CAPACITY
>::iterator
;
29 constexpr FixedVector(iterator begin
, iterator end
) : store
{}, item_count
{} {
30 for (; begin
!= end
; ++begin
)
34 using const_iterator
= typename
cpp::array
<T
, CAPACITY
>::const_iterator
;
35 constexpr FixedVector(const_iterator begin
, const_iterator end
)
36 : store
{}, item_count
{} {
37 for (; begin
!= end
; ++begin
)
41 constexpr FixedVector(size_t count
, const T
&value
) : store
{}, item_count
{} {
42 for (size_t i
= 0; i
< count
; ++i
)
46 constexpr bool push_back(const T
&obj
) {
47 if (item_count
== CAPACITY
)
49 store
[item_count
] = obj
;
54 constexpr const T
&back() const { return store
[item_count
- 1]; }
56 constexpr T
&back() { return store
[item_count
- 1]; }
58 constexpr bool pop_back() {
65 constexpr T
&operator[](size_t idx
) { return store
[idx
]; }
67 constexpr const T
&operator[](size_t idx
) const { return store
[idx
]; }
69 constexpr bool empty() const { return item_count
== 0; }
71 constexpr size_t size() const { return item_count
; }
73 // Empties the store for all practical purposes.
74 constexpr void reset() { item_count
= 0; }
76 // This static method does not free up the resources held by |store|,
77 // say by calling `free` or something similar. It just does the equivalent
78 // of the `reset` method. Considering that FixedVector is of fixed storage,
79 // a `destroy` method like this should not be required. However, FixedVector
80 // is used in a few places as an alternate for data structures which use
81 // dynamically allocated storate. So, the `destroy` method like this
82 // matches the `destroy` API of those other data structures so that users
83 // can easily swap one data structure for the other.
84 static void destroy(FixedVector
<T
, CAPACITY
> *store
) { store
->reset(); }
86 using reverse_iterator
= typename
cpp::array
<T
, CAPACITY
>::reverse_iterator
;
87 LIBC_INLINE
constexpr reverse_iterator
rbegin() {
88 return reverse_iterator
{&store
[item_count
]};
90 LIBC_INLINE
constexpr reverse_iterator
rend() { return store
.rend(); }
92 LIBC_INLINE
constexpr iterator
begin() { return store
.begin(); }
93 LIBC_INLINE
constexpr iterator
end() { return iterator
{&store
[item_count
]}; }
95 LIBC_INLINE
constexpr const_iterator
begin() const { return store
.begin(); }
96 LIBC_INLINE
constexpr const_iterator
end() const {
97 return const_iterator
{&store
[item_count
]};
101 } // namespace LIBC_NAMESPACE_DECL
103 #endif // LLVM_LIBC_SRC___SUPPORT_FIXEDVECTOR_H