Ocean
Loading...
Searching...
No Matches
StaticVector.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8#ifndef META_OCEAN_BASE_STATIC_VECTOR_H
9#define META_OCEAN_BASE_STATIC_VECTOR_H
10
11#include "ocean/base/Base.h"
13
14namespace Ocean
15{
16
17/**
18 * This class implements a static vector that has a fixed capacity.
19 * @tparam T Data type of the elements that will be stored
20 * @tparam tCapacity Number of elements that can be stored, with range [1, infinity)
21 * @ingroup base
22 */
23template <typename T, size_t tCapacity>
24class StaticVector : public StaticBuffer<T, tCapacity>
25{
26 public:
27
28 /**
29 * Creates a new vector object.
30 */
31 StaticVector() = default;
32
33 /**
34 * Creates a new vector object.
35 * @param value The value that will be set as first element
36 */
37 explicit inline StaticVector(const T& value);
38
39 /**
40 * Creates a new vector object.
41 * @param value The value that will be set as first element
42 */
43 explicit inline StaticVector(T&& value);
44
45 /**
46 * Creates a new vector object.
47 * @param number The number of elements to be created
48 * @param value The value that will be created in the first 'number' elements of this vector
49 */
50 inline StaticVector(const size_t number, const T& value);
51
52 /**
53 * Creates a new vector object.
54 * @param values The values to be copied into this vector object, can be nullptr if 'size == 0'
55 * @param size The number of values to copy, with range [0, tCapacity]
56 */
57 inline StaticVector(const T* values, const size_t size);
58
59 /**
60 * Creates a new vector object.
61 * This constructor converts a stl vector object to a static vector object.<br>
62 * Only the first tCapacity elements of the given vector are copied.
63 * @param values The values that will be used as first elements
64 */
65 explicit inline StaticVector(const std::vector<T>& values);
66
67 /**
68 * Creates a new vector object.
69 * This constructor converts a stl vector object to a static vector object.<br>
70 * Only the first tCapacity elements of the given vector are copied.
71 * @param values The values that will be used as first elements
72 */
73 explicit inline StaticVector(std::vector<T>&& values);
74
75 /**
76 * Returns the size of this vector.
77 * @return Vector size
78 */
79 inline size_t size() const;
80
81 /**
82 * Returns whether no free space is left.
83 * @return True, if capacity() - size() == 0
84 */
85 inline bool occupied() const;
86
87 /**
88 * Adds a new element to this vector.
89 * Beware: No range check is applied.
90 * @param value The value to be added
91 * @see securePushBack().
92 */
93 inline void pushBack(const T& value);
94
95 /**
96 * Adds a new element to this vector.
97 * Beware: No range check is applied.
98 * @param value The value to be added
99 * @see securePushBack().
100 */
101 inline void pushBack(T&& value);
102
103 /**
104 * Adds a new element to this vector if this vector has free elements left, otherwise nothing happens
105 * @param value The value to be added
106 * @return True, if succeeded
107 * @see pushBack().
108 */
109 inline bool securePushBack(const T& value);
110
111 /**
112 * Adds a new element to this vector if this vector has free elements left, otherwise nothing happens
113 * @param value The value to be added
114 * @return True, if succeeded
115 * @see pushBack().
116 */
117 inline bool securePushBack(T&& value);
118
119 /**
120 * Constructs a new element in-place at the end of this vector.
121 * Beware: No range check is applied.
122 * @param args The arguments to forward to the constructor of the element
123 * @tparam TArgs The types of the arguments
124 * @see secureEmplaceBack().
125 */
126 template <typename... TArgs>
127 inline void emplaceBack(TArgs&&... args);
128
129 /**
130 * Constructs a new element in-place at the end of this vector if this vector has free elements left.
131 * @param args The arguments to forward to the constructor of the element
132 * @tparam TArgs The types of the arguments
133 * @return True, if succeeded
134 * @see emplaceBack().
135 */
136 template <typename... TArgs>
137 inline bool secureEmplaceBack(TArgs&&... args);
138
139 /**
140 * Adds a new elements to this vector.
141 * This function avoids a memory overflow.<br>
142 * @param value Values to be added
143 * @tparam tCapacity2 Capacity of the second vector
144 */
145 template <size_t tCapacity2>
146 inline void pushBack(const StaticVector<T, tCapacity2>& value);
147
148 /**
149 * Adds a new elements to this vector.
150 * This function avoids a memory overflow.<br>
151 * @param value Values to be added
152 */
153 inline void pushBack(const std::vector<T>& value);
154
155 /**
156 * Removes the last element from this vector.
157 * Beware: No range check is applied.
158 * Thus: Check that this vector holds at least one element!<br>
159 * @see weakPopBack(), pushBack().
160 */
161 inline void popBack();
162
163 /**
164 * Removes the last element from this vector.
165 * If this vector holds no element, nothing is happen.<br>
166 * @see weakPopBack(), pushBack().
167 */
168 inline void securePopBack();
169
170 /**
171 * Removes the last element from this vector.
172 * This function simply decreases the element counter, the last element is untouched.<br>
173 * Beware: No range check is applied.
174 * Thus: Check that this vector holds at least one element!<br>
175 * @see popBack().
176 */
177 inline void weakPopBack();
178
179 /**
180 * Removes the last element from this vector.
181 * This function simply decreases the element counter, the last element is untouched.<br>
182 * If this vector holds no element, nothing is happen.<br>
183 * @see popBack().
184 */
185 inline void secureWeakPopBack();
186
187 /**
188 * Erases one element of this vector.
189 * @param index The index of the element that will be removed, with range [0, size())
190 * @see unstableErase().
191 */
192 inline void erase(const size_t index);
193
194 /**
195 * Erases one element from this vector.
196 * The free element is replace by the last element in the vector, thus the previous order of the elements inside this vector is lost.<br>
197 * This erase function is faster than the standard erase function.
198 * @param index The index of the element that will be removed, with range [0, size())
199 * @see erase().
200 */
201 inline void unstableErase(const size_t index);
202
203 /**
204 * Resizes this vector.
205 * @param size The size to be applied, with range [0, tCapacity]
206 * @see weakResize().
207 */
208 inline void resize(const size_t size);
209
210 /**
211 * Resizes this vector.
212 * This function simply sets the element counter.<br>
213 * @param size The size to be applied, with range [0, tCapacity]
214 * @see resize().
215 */
216 inline void weakResize(const size_t size);
217
218 /**
219 * Clears all elements of this vector.
220 * @see weakClear().
221 */
222 inline void clear();
223
224 /**
225 * Clears all elements of this vector by setting the internal index to zero (all stored elements are untouched).
226 * @see clear().
227 */
228 inline void weakClear();
229
230 /**
231 * Returns the first elements of this vector.
232 * @return First element
233 */
234 inline const T& front() const;
235
236 /**
237 * Returns the first elements of this vector.
238 * @return First element
239 */
240 inline T& front();
241
242 /**
243 * Returns the last elements of this vector.
244 * @return Last element
245 */
246 inline const T& back() const;
247
248 /**
249 * Returns the last elements of this vector.
250 * @return Last element
251 */
252 inline T& back();
253
254 /**
255 * Returns an iterator to the beginning of the vector.
256 * @return Iterator to the first element
257 */
258 inline T* begin();
259
260 /**
261 * Returns a const iterator to the beginning of the vector.
262 * @return Const iterator to the first element
263 */
264 inline const T* begin() const;
265
266 /**
267 * Returns an iterator to the end of the vector.
268 * @return Iterator to the element following the last element
269 */
270 inline T* end();
271
272 /**
273 * Returns a const iterator to the end of the vector.
274 * @return Const iterator to the element following the last element
275 */
276 inline const T* end() const;
277
278 /**
279 * Returns whether this vector hold no element.
280 * @return True, if so
281 */
282 inline bool empty() const;
283
284 /**
285 * Returns one element of this vector.
286 * Beware: No range check is done.
287 * @param index The index of the element that will be returned, with range [0, size())
288 * @return Vector element
289 */
290 inline const T& operator[](const size_t index) const;
291
292 /**
293 * Returns one element of this vector.
294 * Beware: No range check is done.
295 * @param index The index of the element that will be returned, with range [0, size())
296 * @return Vector element
297 */
298 inline T& operator[](const size_t index);
299
300 /**
301 * Returns whether two vectors are identical.
302 * @param second The second vector object
303 * @return True, if so
304 */
305 inline bool operator==(const StaticVector<T, tCapacity>& second) const;
306
307 /**
308 * Returns whether two vectors are not identical.
309 * @param second The second vector object
310 * @return True, if so
311 */
312 inline bool operator!=(const StaticVector<T, tCapacity>& second) const;
313
314 /**
315 * Returns whether this vector holds at least one element.
316 * @return True, if so
317 */
318 explicit inline operator bool() const;
319
320 protected:
321
322 /// The current number of stored elements, with range [0, tCapacity]
323 size_t size_ = 0;
324};
325
326template <typename T, size_t tCapacity>
328 StaticBuffer<T, tCapacity>(value),
329 size_(1)
330{
331 static_assert(tCapacity > 0, "Invalid vector capacity!");
332}
333
334template <typename T, size_t tCapacity>
336 StaticBuffer<T, tCapacity>(std::move(value)),
337 size_(1)
338{
339 static_assert(tCapacity > 0, "Invalid vector capacity!");
340}
341
342template <typename T, size_t tCapacity>
343inline StaticVector<T, tCapacity>::StaticVector(const size_t number, const T& value) :
344 StaticBuffer<T, tCapacity>(std::min(number, tCapacity), value),
345 size_(std::min(number, tCapacity))
346{
347 static_assert(tCapacity > 0, "Invalid vector capacity!");
348
349 ocean_assert(number <= tCapacity);
350}
351
352template <typename T, size_t tCapacity>
353inline StaticVector<T, tCapacity>::StaticVector(const T* values, const size_t size)
354{
355 static_assert(tCapacity > 0, "Invalid vector capacity!");
356
357 ocean_assert(size <= tCapacity);
358
359 const size_t actualSize = std::min(size, tCapacity);
360
361 for (size_t n = 0; n < actualSize; ++n)
362 {
363 this->elements_[n] = values[n];
364 }
365
366 size_ = actualSize;
367}
368
369template <typename T, size_t tCapacity>
370inline StaticVector<T, tCapacity>::StaticVector(const std::vector<T>& values) :
371 StaticBuffer<T, tCapacity>(values),
372 size_(std::min(tCapacity, values.size()))
373{
374 static_assert(tCapacity > 0, "Invalid vector capacity!");
375}
376
377template <typename T, size_t tCapacity>
378inline StaticVector<T, tCapacity>::StaticVector(std::vector<T>&& values) :
379 size_(std::min(tCapacity, values.size()))
380{
381 static_assert(tCapacity > 0, "Invalid vector capacity!");
382
383 for (size_t n = 0; n < size_; ++n)
384 {
385 this->elements_[n] = std::move(values[n]);
386 }
387}
388
389template <typename T, size_t tCapacity>
391{
392 return size_;
393}
394
395template <typename T, size_t tCapacity>
397{
398 return size_ == tCapacity;
399}
400
401template <typename T, size_t tCapacity>
402inline void StaticVector<T, tCapacity>::pushBack(const T& value)
403{
404 ocean_assert(size_ < tCapacity);
405
406 this->elements_[size_++] = value;
407}
408
409template <typename T, size_t tCapacity>
411{
412 ocean_assert(size_ < tCapacity);
413
414 this->elements_[size_++] = std::move(value);
415}
416
417template <typename T, size_t tCapacity>
419{
420 if (size_ >= tCapacity)
421 {
422 return false;
423 }
424
425 this->elements_[size_++] = value;
426
427 return true;
428}
429
430template <typename T, size_t tCapacity>
432{
433 if (size_ >= tCapacity)
434 {
435 return false;
436 }
437
438 this->elements_[size_++] = std::move(value);
439
440 return true;
441}
442
443template <typename T, size_t tCapacity>
444template <typename... TArgs>
445inline void StaticVector<T, tCapacity>::emplaceBack(TArgs&&... args)
446{
447 ocean_assert(size_ < tCapacity);
448
449 this->elements_[size_++] = T(std::forward<TArgs>(args)...);
450}
451
452template <typename T, size_t tCapacity>
453template <typename... TArgs>
455{
456 if (size_ >= tCapacity)
457 {
458 return false;
459 }
460
461 this->elements_[size_++] = T(std::forward<TArgs>(args)...);
462
463 return true;
464}
465
466template <typename T, size_t tCapacity>
467template <size_t tCapacity2>
469{
470 size_t elements = std::min(value.size(), tCapacity - size_);
471
472 for (size_t n = 0; n < elements; ++n)
473 {
474 this->elements_[n + size_] = value[n];
475 }
476
477 size_ += elements;
478}
479
480template <typename T, size_t tCapacity>
481inline void StaticVector<T, tCapacity>::pushBack(const std::vector<T>& value)
482{
483 size_t elements = std::min(value.size(), tCapacity - size_);
484
485 for (size_t n = 0; n < elements; ++n)
486 {
487 this->elements_[n + size_] = value[n];
488 }
489
490 size_ += elements;
491}
492
493template <typename T, size_t tCapacity>
495{
496 ocean_assert(size_ > 0);
497
498 this->elements_[--size_] = T();
499}
500
501template <typename T, size_t tCapacity>
503{
504 if (size_ > 0)
505 {
506 this->elements_[--size_] = T();
507 }
508}
509
510template <typename T, size_t tCapacity>
512{
513 ocean_assert(size_ > 0);
514
515 --size_;
516}
517
518template <typename T, size_t tCapacity>
520{
521 if (size_ > 0)
522 {
523 --size_;
524 }
525}
526
527template <typename T, size_t tCapacity>
529{
530 ocean_assert(!empty());
531
532 return this->elements_[0];
533}
534
535template <typename T, size_t tCapacity>
537{
538 ocean_assert(!empty());
539
540 return this->elements_[0];
541}
542
543template <typename T, size_t tCapacity>
544inline const T& StaticVector<T, tCapacity>::back() const
545{
546 ocean_assert(!empty());
547
548 return this->elements_[size_ - 1];
549}
550
551template <typename T, size_t tCapacity>
553{
554 ocean_assert(!empty());
555
556 return this->elements_[size_ - 1];
557}
558
559template <typename T, size_t tCapacity>
561{
562 return this->elements_;
563}
564
565template <typename T, size_t tCapacity>
567{
568 return this->elements_;
569}
570
571template <typename T, size_t tCapacity>
573{
574 return this->elements_ + size_;
575}
576
577template <typename T, size_t tCapacity>
578inline const T* StaticVector<T, tCapacity>::end() const
579{
580 return this->elements_ + size_;
581}
582
583template <typename T, size_t tCapacity>
585{
586 return size_ == 0;
587}
588
589template <typename T, size_t tCapacity>
590inline void StaticVector<T, tCapacity>::erase(const size_t index)
591{
592 ocean_assert(index < size_);
593
594 for (size_t n = index + 1; n < size_; ++n)
595 {
596 this->elements_[n - 1] = std::move(this->elements_[n]);
597 }
598
599 this->elements_[--size_] = T();
600}
601
602template <typename T, size_t tCapacity>
603inline void StaticVector<T, tCapacity>::unstableErase(const size_t index)
604{
605 ocean_assert(index < size_);
606
607 --size_;
608
609 if (index < size_)
610 {
611 this->elements_[index] = std::move(this->elements_[size_]);
612 }
613
614 this->elements_[size_] = T();
615}
616
617template <typename T, size_t tCapacity>
618inline void StaticVector<T, tCapacity>::resize(const size_t size)
619{
620 ocean_assert(size <= tCapacity);
621
622 if (size < size_)
623 {
624 // we need to overwrite existing elements
625
626 for (size_t n = size; n < size_; ++n)
627 {
628 this->elements_[n] = T();
629 }
630 }
631 else
632 {
633 // we need to initialize new elements
634
635 for (size_t n = size_; n < size; ++n)
636 {
637 this->elements_[n] = T();
638 }
639 }
640
641 size_ = size;
642}
643
644template <typename T, size_t tCapacity>
645inline void StaticVector<T, tCapacity>::weakResize(const size_t size)
646{
647 ocean_assert(size <= tCapacity);
648
649 size_ = size;
650}
651
652template <typename T, size_t tCapacity>
654{
655 for (size_t n = 0; n < size_; ++n)
656 {
657 this->elements_[n] = T();
658 }
659
660 size_ = 0;
661}
662
663template <typename T, size_t tCapacity>
665{
666 size_ = 0;
667}
668
669template <typename T, size_t tCapacity>
670inline const T& StaticVector<T, tCapacity>::operator[](const size_t index) const
671{
672 ocean_assert(index < size_);
673
674 return this->elements_[index];
675}
676
677template <typename T, size_t tCapacity>
678inline T& StaticVector<T, tCapacity>::operator[](const size_t index)
679{
680 ocean_assert(index < size_);
681
682 return this->elements_[index];
683}
684
685template <typename T, size_t tCapacity>
687{
688 if (size_ != second.size_)
689 {
690 return false;
691 }
692
693 for (size_t n = 0; n < size_; ++n)
694 {
695 if (this->elements_[n] != second.elements_[n])
696 {
697 return false;
698 }
699 }
700
701 return true;
702}
703
704template <typename T, size_t tCapacity>
706{
707 return !(*this == second);
708}
709
710template <typename T, size_t tCapacity>
712{
713 return size_ > 0;
714}
715
716}
717
718#endif // META_OCEAN_BASE_STATIC_VECTOR_H
This class implements a static buffer that has a fixed capacity.
Definition StaticBuffer.h:24
T elements_[tCapacity > size_t(0) ? tCapacity :size_t(1)]
Elements of this buffer (with at least one entry).
Definition StaticBuffer.h:160
This class implements a static vector that has a fixed capacity.
Definition StaticVector.h:25
size_t size_
The current number of stored elements, with range [0, tCapacity].
Definition StaticVector.h:323
void emplaceBack(TArgs &&... args)
Constructs a new element in-place at the end of this vector.
Definition StaticVector.h:445
bool securePushBack(T &&value)
Adds a new element to this vector if this vector has free elements left, otherwise nothing happens.
Definition StaticVector.h:431
StaticVector(const T *values, const size_t size)
Creates a new vector object.
Definition StaticVector.h:353
StaticVector(const size_t number, const T &value)
Creates a new vector object.
Definition StaticVector.h:343
const T & front() const
Returns the first elements of this vector.
Definition StaticVector.h:528
void pushBack(const std::vector< T > &value)
Adds a new elements to this vector.
Definition StaticVector.h:481
void secureWeakPopBack()
Removes the last element from this vector.
Definition StaticVector.h:519
size_t size() const
Returns the size of this vector.
Definition StaticVector.h:390
bool occupied() const
Returns whether no free space is left.
Definition StaticVector.h:396
T & back()
Returns the last elements of this vector.
Definition StaticVector.h:552
void popBack()
Removes the last element from this vector.
Definition StaticVector.h:494
bool secureEmplaceBack(TArgs &&... args)
Constructs a new element in-place at the end of this vector if this vector has free elements left.
Definition StaticVector.h:454
void erase(const size_t index)
Erases one element of this vector.
Definition StaticVector.h:590
T & operator[](const size_t index)
Returns one element of this vector.
Definition StaticVector.h:678
void clear()
Clears all elements of this vector.
Definition StaticVector.h:653
T * end()
Returns an iterator to the end of the vector.
Definition StaticVector.h:572
void pushBack(T &&value)
Adds a new element to this vector.
Definition StaticVector.h:410
void securePopBack()
Removes the last element from this vector.
Definition StaticVector.h:502
const T * begin() const
Returns a const iterator to the beginning of the vector.
Definition StaticVector.h:566
StaticVector()=default
Creates a new vector object.
StaticVector(std::vector< T > &&values)
Creates a new vector object.
Definition StaticVector.h:378
void weakClear()
Clears all elements of this vector by setting the internal index to zero (all stored elements are unt...
Definition StaticVector.h:664
StaticVector(const T &value)
Creates a new vector object.
Definition StaticVector.h:327
void resize(const size_t size)
Resizes this vector.
Definition StaticVector.h:618
void unstableErase(const size_t index)
Erases one element from this vector.
Definition StaticVector.h:603
StaticVector(T &&value)
Creates a new vector object.
Definition StaticVector.h:335
void pushBack(const StaticVector< T, tCapacity2 > &value)
Adds a new elements to this vector.
Definition StaticVector.h:468
bool operator!=(const StaticVector< T, tCapacity > &second) const
Returns whether two vectors are not identical.
Definition StaticVector.h:705
StaticVector(const std::vector< T > &values)
Creates a new vector object.
Definition StaticVector.h:370
void weakResize(const size_t size)
Resizes this vector.
Definition StaticVector.h:645
void weakPopBack()
Removes the last element from this vector.
Definition StaticVector.h:511
T & front()
Returns the first elements of this vector.
Definition StaticVector.h:536
bool empty() const
Returns whether this vector hold no element.
Definition StaticVector.h:584
void pushBack(const T &value)
Adds a new element to this vector.
Definition StaticVector.h:402
bool securePushBack(const T &value)
Adds a new element to this vector if this vector has free elements left, otherwise nothing happens.
Definition StaticVector.h:418
const T * end() const
Returns a const iterator to the end of the vector.
Definition StaticVector.h:578
const T & operator[](const size_t index) const
Returns one element of this vector.
Definition StaticVector.h:670
const T & back() const
Returns the last elements of this vector.
Definition StaticVector.h:544
T * begin()
Returns an iterator to the beginning of the vector.
Definition StaticVector.h:560
bool operator==(const StaticVector< T, tCapacity > &second) const
Returns whether two vectors are identical.
Definition StaticVector.h:686
The namespace covering the entire Ocean framework.
Definition Accessor.h:15