1 //===----------------------------------------------------------------------===//
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 SUPPORT_NAIVE_STATIC_VECTOR_H
10 #define SUPPORT_NAIVE_STATIC_VECTOR_H
14 #include "test_iterators.h"
15 #include "test_macros.h"
17 template <class T
, std::size_t N
>
18 struct NaiveStaticVector
{
19 struct CapacityError
{};
22 using difference_type
= short;
23 using size_type
= unsigned short;
24 using iterator
= random_access_iterator
<T
*>;
25 using const_iterator
= random_access_iterator
<const T
*>;
27 explicit NaiveStaticVector() = default;
29 explicit NaiveStaticVector(It first
, It last
) {
34 // Moving-from a NaiveStaticVector leaves the source vector holding moved-from objects.
35 // This is intentional (the "Naive" in the name).
36 // Specifically, moving-out-of a sorted+uniqued NaiveStaticVector<MoveOnly>
37 // will leave it in a non-sorted+uniqued state.
39 NaiveStaticVector(const NaiveStaticVector
&) = default;
40 NaiveStaticVector(NaiveStaticVector
&&) = default; // deliberately don't reset size_
41 NaiveStaticVector
& operator=(const NaiveStaticVector
&) = default;
42 NaiveStaticVector
& operator=(NaiveStaticVector
&&) = default;
44 iterator
begin() { return iterator(data_
); }
45 const_iterator
begin() const { return const_iterator(data_
); }
46 const_iterator
cbegin() const { return const_iterator(data_
); }
47 iterator
end() { return begin() + size(); }
48 const_iterator
end() const { return begin() + size(); }
49 size_type
size() const { return size_
; }
50 bool empty() const { return size_
== 0; }
52 void clear() { size_
= 0; }
55 iterator
insert(const_iterator pos
, It first
, It last
) {
56 iterator result
= pos
- cbegin() + begin();
57 while (first
!= last
) {
58 insert(pos
++, *first
++);
63 iterator
insert(const_iterator pos
, T value
) {
65 throw CapacityError();
67 int i
= pos
- cbegin();
69 std::move_backward(&data_
[i
], &data_
[size_
- 1], &data_
[size_
]);
70 data_
[i
] = std::move(value
);
74 template <class... Args
>
75 iterator
emplace(const_iterator pos
, Args
&&... args
) {
76 return insert(pos
, T(std::forward
<Args
>(args
)...));
79 iterator
erase(const_iterator first
, const_iterator last
) {
80 int i
= first
- cbegin();
81 int j
= last
- cbegin();
82 std::move(&data_
[j
], &data_
[size_
], &data_
[i
]);
83 size_
-= (last
- first
);
87 iterator
erase(const_iterator pos
) { return erase(pos
, std::next(pos
)); }
91 std::size_t size_
= 0;
94 #endif // SUPPORT_NAIVE_STATIC_VECTOR_H