forked from glynos/url
-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathstatic_vector.hpp
More file actions
214 lines (184 loc) · 4.66 KB
/
static_vector.hpp
File metadata and controls
214 lines (184 loc) · 4.66 KB
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
// Copyright 2020 Glyn Matthews.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#ifndef SKYR_CONTAINERS_STATIC_VECTOR_HPP
#define SKYR_CONTAINERS_STATIC_VECTOR_HPP
#include <array>
#include <cassert>
#include <cstdlib>
#include <new>
#include <optional>
#include <type_traits>
namespace skyr {
///
/// \tparam T
/// \tparam Capacity
template <class T, std::size_t Capacity>
class static_vector {
private:
alignas(T) std::array<std::byte, sizeof(T) * Capacity> storage_{};
std::size_t size_ = 0;
auto data_ptr() noexcept -> T* {
return std::launder(reinterpret_cast<T*>(storage_.data()));
}
auto data_ptr() const noexcept -> const T* {
return std::launder(reinterpret_cast<const T*>(storage_.data()));
}
public:
///
using value_type = T;
///
using const_reference = const T&;
///
using reference = T&;
///
using const_pointer = const T*;
///
using pointer = T*;
///
using size_type = std::size_t;
///
using difference_type = std::ptrdiff_t;
///
using const_iterator = const T*;
///
using iterator = T*;
/// Constructor
constexpr static_vector() = default;
constexpr static_vector(const static_vector&) = default;
constexpr static_vector(static_vector&&) noexcept = default;
constexpr auto operator=(const static_vector&) -> static_vector& = default;
constexpr auto operator=(static_vector&&) noexcept -> static_vector& = default;
constexpr ~static_vector() {
clear();
}
/// Gets the first const element in the vector
/// \return a const T &
/// \pre `size_ > 0`
constexpr auto front() const noexcept -> const_reference {
assert(size() > 0);
return data_ptr()[0];
}
/// Gets the first element in the vector
/// \return a T &
/// \pre `size() > 0`
constexpr auto front() noexcept -> reference {
assert(size_ > 0);
return data_ptr()[0];
}
///
/// \return
/// \pre `size() > 0`
constexpr auto back() const noexcept -> const_reference {
assert(size_ > 0);
return data_ptr()[size_ - 1];
}
///
/// \return
/// \pre `size() > 0`
constexpr auto back() noexcept -> reference {
assert(size_ > 0);
return data_ptr()[size_ - 1];
}
///
/// \param value
/// \return
/// \pre `size() < capacity()`
/// \post `size() > 0 && size() <= capacity()`
constexpr auto push_back(const_reference value) noexcept -> reference {
assert(size_ < Capacity);
new (&data_ptr()[size_]) T(value);
++size_;
return data_ptr()[size_ - 1];
}
///
/// \tparam Args
/// \param args
/// \return
/// \pre `size() < capacity()`
/// \post `size() > 0 && size() <= capacity()`
template <class... Args>
constexpr auto emplace_back(Args&&... args) noexcept(std::is_nothrow_constructible_v<T, Args...>) -> reference {
assert(size_ < Capacity);
new (&data_ptr()[size_]) T(std::forward<Args>(args)...);
++size_;
return back();
}
///
/// \pre `size() > 0`
constexpr void pop_back() noexcept {
assert(size_ > 0);
back().~value_type();
--size_;
}
///
/// \return
[[nodiscard]] constexpr auto data() noexcept -> pointer {
return data_ptr();
}
///
/// \return
[[nodiscard]] constexpr auto data() const noexcept -> const_pointer {
return data_ptr();
}
///
/// \return
[[nodiscard]] constexpr auto size() const noexcept -> size_type {
return size_;
}
///
/// \return
[[nodiscard]] constexpr auto max_size() const noexcept -> size_type {
return Capacity;
}
///
/// \return
[[nodiscard]] constexpr auto capacity() const noexcept -> size_type {
return Capacity;
}
///
/// \return `true` if there are no elements
[[nodiscard]] constexpr auto empty() const noexcept -> bool {
return size_ == 0;
}
///
/// \post size() == 0
constexpr void clear() noexcept {
while (size_) {
pop_back();
}
}
///
/// \return
[[nodiscard]] constexpr auto begin() noexcept -> iterator {
return data_ptr();
}
///
/// \return
[[nodiscard]] constexpr auto end() noexcept -> iterator {
return data_ptr() + size_;
}
///
/// \return
[[nodiscard]] constexpr auto cbegin() const noexcept -> const_iterator {
return data_ptr();
}
///
/// \return
[[nodiscard]] constexpr auto cend() const noexcept -> const_iterator {
return data_ptr() + size_;
}
///
/// \return
[[nodiscard]] constexpr auto begin() const noexcept -> const_iterator {
return cbegin();
}
///
/// \return
[[nodiscard]] constexpr auto end() const noexcept -> const_iterator {
return cend();
}
};
} // namespace skyr
#endif // SKYR_CONTAINERS_STATIC_VECTOR_HPP