Skip to content

Commit 23a4097

Browse files
committed
FixedVector is constexpr
1 parent 841f49e commit 23a4097

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed

util/fixed_vector.hh

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,28 @@ class FixedVector {
1212

1313
public:
1414
template<typename... Ts>
15-
FixedVector(const Ts... t)
15+
constexpr FixedVector(const Ts... t)
1616
: data{t...}
1717
, back_idx{sizeof...(t)} {
1818
}
1919

2020
// Unchecked!
21-
T &operator[](size_t idx) {
21+
constexpr T &operator[](size_t idx) {
2222
return data[idx];
2323
}
2424

25-
const T &operator[](size_t idx) const {
25+
constexpr const T &operator[](size_t idx) const {
2626
return data[idx];
2727
}
2828

29-
T back() const {
29+
constexpr T back() const {
3030
if (back_idx > 0)
3131
return data[back_idx - 1];
3232
else
3333
return T{};
3434
}
3535

36-
bool push_back(T el) {
36+
constexpr bool push_back(T el) {
3737
if (back_idx >= MaxElements)
3838
return false;
3939

@@ -43,7 +43,7 @@ public:
4343
}
4444

4545
// Resizes
46-
bool resize(size_t num, T el = T{}) {
46+
constexpr bool resize(size_t num, T el = T{}) {
4747
const auto new_back_idx = std::min(num, MaxElements);
4848

4949
if (new_back_idx > back_idx) {
@@ -56,13 +56,13 @@ public:
5656
return (num == new_back_idx);
5757
}
5858

59-
bool resize_for_overwrite(size_t num) {
59+
constexpr bool resize_for_overwrite(size_t num) {
6060
back_idx = std::min(num, MaxElements);
6161
return (num == back_idx);
6262
}
6363

6464
// returns MaxElements for failure
65-
size_t push_back_for_overwrite() {
65+
constexpr size_t push_back_for_overwrite() {
6666
if (back_idx >= MaxElements)
6767
return MaxElements;
6868

@@ -72,31 +72,31 @@ public:
7272
return idx;
7373
}
7474

75-
T pop_back() {
75+
constexpr T pop_back() {
7676
if (back_idx == 0)
7777
return T{};
7878

7979
back_idx--;
8080
return data[back_idx];
8181
}
8282

83-
size_t size() const {
83+
constexpr size_t size() const {
8484
return back_idx;
8585
}
8686

8787
constexpr size_t max_size() const {
8888
return MaxElements;
8989
}
9090

91-
size_t available() {
91+
constexpr size_t available() {
9292
return max_size() - size();
9393
}
9494

95-
void clear() {
95+
constexpr void clear() {
9696
back_idx = 0;
9797
}
9898

99-
void insert(unsigned index, T d) {
99+
constexpr void insert(unsigned index, T d) {
100100
if (back_idx >= max_size() || index > back_idx)
101101
return;
102102

@@ -111,7 +111,7 @@ public:
111111
back_idx++;
112112
}
113113

114-
void erase(const unsigned index) {
114+
constexpr void erase(const unsigned index) {
115115
if (back_idx == 0 || index >= back_idx)
116116
return;
117117

@@ -124,7 +124,7 @@ public:
124124
back_idx -= 1;
125125
}
126126

127-
void erase(T *first, T *last) {
127+
constexpr void erase(T *first, T *last) {
128128
const auto count = std::distance(first, last);
129129

130130
if (back_idx == 0 || count <= 0) {
@@ -138,27 +138,27 @@ public:
138138

139139
using iterator = typename decltype(data)::iterator;
140140

141-
auto begin() const {
141+
constexpr auto begin() const {
142142
return data.begin();
143143
}
144144

145-
auto end() const {
145+
constexpr auto end() const {
146146
return begin() + back_idx;
147147
}
148148

149-
auto begin() {
149+
constexpr auto begin() {
150150
return data.begin();
151151
}
152152

153-
auto end() {
153+
constexpr auto end() {
154154
return begin() + back_idx;
155155
}
156156

157-
auto span() {
157+
constexpr auto span() {
158158
return std::span{begin(), back_idx};
159159
}
160160

161-
auto span() const {
161+
constexpr auto span() const {
162162
return std::span{begin(), back_idx};
163163
}
164164
};

0 commit comments

Comments
 (0)