UdonLibrary 1.0.0
機械システム研究部 C++ ライブラリ
読み取り中…
検索中…
一致する文字列を見つけられません
StaticVector.hpp
[詳解]
1//
2// 疑似可変長配列コンテナ
3//
4// Copyright (c) 2022-2024 udonrobo
5//
6
7#pragma once
8
10#include <algorithm>
11#include <initializer_list>
12#include <iterator>
13
14namespace Udon
15{
16
20 template <typename T, size_t Capacity = 128>
22 {
23 public:
24 using value_type = T;
25 using size_type = size_t;
26 using reference = T&;
27 using const_reference = const T&;
28 using pointer = T*;
29 using const_pointer = const T*;
30 using iterator = T*;
31 using const_iterator = const T*;
32 using reverse_iterator = std::reverse_iterator<iterator>;
33 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
34
35 private:
36 value_type m_data[Capacity];
37 size_type m_size;
38
39 public:
41 : m_size()
42 {
43 }
44
45 StaticVector(std::initializer_list<T> init)
46 : m_size(init.size())
47 {
48 std::copy(init.begin(), init.end(), begin());
49 }
50
52 : m_size(n)
53 {
54 std::fill(begin(), end(), value_type());
55 }
56
58 : m_size(n)
59 {
60 std::fill(begin(), end(), value);
61 }
62
63 template <typename InputIterator, typename = typename std::enable_if<std::is_convertible<typename std::iterator_traits<InputIterator>::iterator_category, std::input_iterator_tag>::value>::type>
64 StaticVector(InputIterator first, InputIterator last)
65 : m_size(std::distance(first, last))
66 {
67 std::copy(first, last, begin());
68 }
69
71 : m_size(other.m_size)
72 {
73 std::copy(other.cbegin(), other.cend(), begin());
74 }
75
77 : m_size(other.m_size)
78 {
79 for (size_type i = 0; i < m_size; ++i)
80 {
81 m_data[i] = std::move(other.m_data[i]);
82 }
83 }
84
85 explicit operator bool() const
86 {
87 return m_size > 0;
88 }
89
91 {
92 return m_size;
93 }
94
96 {
97 return Capacity;
98 }
99
100 bool empty() const
101 {
102 return m_size == 0;
103 }
104
105 bool full() const
106 {
107 return m_size == Capacity;
108 }
109
110 void push_back(const value_type& value)
111 {
112 if (not full())
113 {
114 m_data[m_size++] = value;
115 }
116 }
117
118 void push_back(value_type&& value)
119 {
120 if (not full())
121 {
122 m_data[m_size++] = std::move(value);
123 }
124 }
125
126 void pop_back()
127 {
128 if (m_size > 0)
129 {
130 --m_size;
131 }
132 }
133
135 {
136 if (n < m_size)
137 {
138 m_size = n;
139 }
140 else
141 {
142 std::fill(begin() + m_size, begin() + n, value_type{});
143 m_size = n;
144 }
145 }
146
147 void insert(iterator position, const value_type& val)
148 {
149 if (not full())
150 {
151 std::copy_backward(position, end(), end() + 1);
152 *position = val;
153 ++m_size;
154 }
155 }
156
157 void insert(iterator position, value_type&& val)
158 {
159 if (not full())
160 {
161 std::copy_backward(position, end(), end() + 1);
162 *position = std::move(val);
163 ++m_size;
164 }
165 }
166
167 void insert(iterator position, size_type n, const value_type& val)
168 {
169 if (m_size + n <= Capacity)
170 {
171 std::copy_backward(position, end(), end() + n);
172 std::fill(position, position + n, val);
173 m_size += n;
174 }
175 }
176
177 template <typename InputIterator, typename = typename std::enable_if<std::is_convertible<typename std::iterator_traits<InputIterator>::iterator_category, std::input_iterator_tag>::value>::type>
178 void insert(iterator position, InputIterator first, InputIterator last)
179 {
180 size_type n = std::distance(first, last);
181 if (m_size + n <= Capacity)
182 {
183 std::copy_backward(position, end(), end() + n);
184 std::copy(first, last, position);
185 m_size += n;
186 }
187 }
188
189 void erase(iterator position)
190 {
191 std::copy(position + 1, end(), position);
192 --m_size;
193 }
194
195 void erase(iterator first, iterator last)
196 {
197 std::copy(last, end(), first);
198 m_size -= std::distance(first, last);
199 }
200
201 void clear()
202 {
203 m_size = 0;
204 }
205
207 {
208 return *m_data;
209 }
210
212 {
213 return *m_data;
214 }
215
217 {
218 return m_data[m_size - 1];
219 }
220
222 {
223 return m_data[m_size - 1];
224 }
225
227 {
228 return m_data[index];
229 }
230
232 {
233 return m_data[index];
234 }
235
237 {
238 return m_data[index];
239 }
240
242 {
243 return m_data[index];
244 }
245
247 {
248 return m_data;
249 }
251 {
252 return m_data;
253 }
254
256 {
257 return m_data + m_size;
258 }
260 {
261 return m_data + m_size;
262 }
263
265 {
266 return m_data;
267 }
268
270 {
271 return m_data + m_size;
272 }
273
275 {
276 return reverse_iterator(m_data + m_size - 1);
277 }
279 {
280 return const_reverse_iterator(m_data + m_size - 1);
281 }
282
284 {
285 return reverse_iterator(m_data - 1);
286 }
288 {
289 return const_reverse_iterator(m_data - 1);
290 }
291
293 {
294 return const_reverse_iterator(m_data + m_size - 1);
295 }
296
298 {
299 return const_reverse_iterator(m_data - 1);
300 }
301
302#ifdef ARDUINO
303 void show() const
304 {
305 Serial.print("[");
306 for (size_type i = 0; i < m_size; ++i)
307 {
308 Serial.print(m_data[i]);
309 if (i < m_size - 1)
310 {
311 Serial.print(", ");
312 }
313 }
314 Serial.println("]");
315 }
316#endif
317 };
318} // namespace Udon
疑似可変長配列コンテナ
Definition StaticVector.hpp:22
bool full() const
Definition StaticVector.hpp:105
reference back()
Definition StaticVector.hpp:216
const_reverse_iterator rend() const
Definition StaticVector.hpp:287
const T * const_iterator
Definition StaticVector.hpp:31
StaticVector(StaticVector &&other)
Definition StaticVector.hpp:76
void insert(iterator position, value_type &&val)
Definition StaticVector.hpp:157
reference front()
Definition StaticVector.hpp:206
const T * const_pointer
Definition StaticVector.hpp:29
StaticVector(const StaticVector &other)
Definition StaticVector.hpp:70
const_iterator cend() const
Definition StaticVector.hpp:269
void erase(iterator position)
Definition StaticVector.hpp:189
reference operator[](size_type index)
Definition StaticVector.hpp:241
T value_type
Definition StaticVector.hpp:24
reverse_iterator rbegin()
Definition StaticVector.hpp:274
const_reference back() const
Definition StaticVector.hpp:221
iterator begin()
Definition StaticVector.hpp:246
void clear()
Definition StaticVector.hpp:201
const_reverse_iterator crbegin() const
Definition StaticVector.hpp:292
size_type capacity() const
Definition StaticVector.hpp:95
StaticVector()
Definition StaticVector.hpp:40
const_reference at(size_type index) const
Definition StaticVector.hpp:226
const_iterator begin() const
Definition StaticVector.hpp:250
const_iterator end() const
Definition StaticVector.hpp:259
void insert(iterator position, InputIterator first, InputIterator last)
Definition StaticVector.hpp:178
size_t size_type
Definition StaticVector.hpp:25
T * iterator
Definition StaticVector.hpp:30
size_type size() const
Definition StaticVector.hpp:90
const_reverse_iterator rbegin() const
Definition StaticVector.hpp:278
StaticVector(std::initializer_list< T > init)
Definition StaticVector.hpp:45
void push_back(const value_type &value)
Definition StaticVector.hpp:110
T * pointer
Definition StaticVector.hpp:28
const_reference operator[](size_type index) const
Definition StaticVector.hpp:236
const_reverse_iterator crend() const
Definition StaticVector.hpp:297
const_reference front() const
Definition StaticVector.hpp:211
reverse_iterator rend()
Definition StaticVector.hpp:283
StaticVector(size_type n, const value_type &value)
Definition StaticVector.hpp:57
StaticVector(InputIterator first, InputIterator last)
Definition StaticVector.hpp:64
void insert(iterator position, size_type n, const value_type &val)
Definition StaticVector.hpp:167
const T & const_reference
Definition StaticVector.hpp:27
iterator end()
Definition StaticVector.hpp:255
void pop_back()
Definition StaticVector.hpp:126
void resize(size_type n)
Definition StaticVector.hpp:134
std::reverse_iterator< iterator > reverse_iterator
Definition StaticVector.hpp:32
const_iterator cbegin() const
Definition StaticVector.hpp:264
bool empty() const
Definition StaticVector.hpp:100
void push_back(value_type &&value)
Definition StaticVector.hpp:118
void erase(iterator first, iterator last)
Definition StaticVector.hpp:195
StaticVector(size_type n)
Definition StaticVector.hpp:51
T & reference
Definition StaticVector.hpp:26
reference at(size_type index)
Definition StaticVector.hpp:231
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition StaticVector.hpp:33
void insert(iterator position, const value_type &val)
Definition StaticVector.hpp:147
Definition Bit.hpp:12
Definition Typedef.hpp:94