fixed_vector.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /**************************************************************************/
  2. /* fixed_vector.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #pragma once
  31. #include "core/templates/span.h"
  32. /**
  33. * A high performance Vector of fixed capacity.
  34. * Especially useful if you need to create an array on the stack, to
  35. * prevent dynamic allocations (especially in bottleneck code).
  36. *
  37. * Choose CAPACITY such that it is enough for all elements that could be added through all branches.
  38. *
  39. */
  40. template <class T, uint32_t CAPACITY>
  41. class FixedVector {
  42. // This declaration allows us to access other FixedVector's private members.
  43. template <class T_, uint32_t CAPACITY_>
  44. friend class FixedVector;
  45. uint32_t _size = 0;
  46. alignas(T) uint8_t _data[CAPACITY * sizeof(T)];
  47. constexpr static uint32_t DATA_PADDING = MAX(alignof(T), alignof(uint32_t)) - alignof(uint32_t);
  48. public:
  49. _FORCE_INLINE_ constexpr FixedVector() = default;
  50. constexpr FixedVector(std::initializer_list<T> p_init) {
  51. ERR_FAIL_COND(p_init.size() > CAPACITY);
  52. for (const T &element : p_init) {
  53. memnew_placement(ptr() + _size++, T(element));
  54. }
  55. }
  56. template <uint32_t p_capacity>
  57. constexpr FixedVector(const FixedVector<T, p_capacity> &p_from) {
  58. ERR_FAIL_COND(p_from.size() > CAPACITY);
  59. if constexpr (std::is_trivially_copyable_v<T>) {
  60. // Copy size and all provided elements at once.
  61. memcpy((void *)&_size, (void *)&p_from._size, sizeof(_size) + DATA_PADDING + p_from.size() * sizeof(T));
  62. } else {
  63. for (const T &element : p_from) {
  64. memnew_placement(ptr() + _size++, T(element));
  65. }
  66. }
  67. }
  68. template <uint32_t p_capacity>
  69. constexpr FixedVector(FixedVector<T, p_capacity> &&p_from) {
  70. ERR_FAIL_COND(p_from.size() > CAPACITY);
  71. // Copy size and all provided elements at once.
  72. // Note: Assumes trivial relocatability.
  73. memcpy((void *)&_size, (void *)&p_from._size, sizeof(_size) + DATA_PADDING + p_from.size() * sizeof(T));
  74. p_from._size = 0;
  75. }
  76. ~FixedVector() {
  77. if constexpr (!std::is_trivially_destructible_v<T>) {
  78. for (uint32_t i = 0; i < _size; i++) {
  79. ptr()[i].~T();
  80. }
  81. }
  82. }
  83. _FORCE_INLINE_ constexpr T *ptr() { return (T *)(_data); }
  84. _FORCE_INLINE_ constexpr const T *ptr() const { return (const T *)(_data); }
  85. _FORCE_INLINE_ constexpr operator Span<T>() const { return Span<T>(ptr(), size()); }
  86. _FORCE_INLINE_ constexpr Span<T> span() const { return operator Span<T>(); }
  87. _FORCE_INLINE_ constexpr uint32_t size() const { return _size; }
  88. _FORCE_INLINE_ constexpr bool is_empty() const { return !_size; }
  89. _FORCE_INLINE_ constexpr bool is_full() const { return _size == CAPACITY; }
  90. _FORCE_INLINE_ constexpr uint32_t capacity() const { return CAPACITY; }
  91. _FORCE_INLINE_ constexpr void clear() { resize_initialized(0); }
  92. /// Changes the size of the vector.
  93. /// If p_size > size(), constructs new elements.
  94. /// If p_size < size(), destructs new elements.
  95. constexpr Error resize_initialized(uint32_t p_size) {
  96. if (p_size > _size) {
  97. ERR_FAIL_COND_V(p_size > CAPACITY, ERR_OUT_OF_MEMORY);
  98. memnew_arr_placement(ptr() + _size, p_size - _size);
  99. } else if (p_size < _size) {
  100. if constexpr (!std::is_trivially_destructible_v<T>) {
  101. for (uint32_t i = p_size; i < _size; i++) {
  102. ptr()[i].~T();
  103. }
  104. }
  105. }
  106. _size = p_size;
  107. return OK;
  108. }
  109. /// Changes the size of the vector.
  110. /// The initializer of new elements is skipped, making this function faster than resize_initialized.
  111. /// The caller is required to initialize the new values.
  112. constexpr Error resize_uninitialized(uint32_t p_size) {
  113. static_assert(std::is_trivially_destructible_v<T>, "resize_uninitialized is unsafe to call if T is not trivially destructible.");
  114. ERR_FAIL_COND_V(p_size > CAPACITY, ERR_OUT_OF_MEMORY);
  115. _size = p_size;
  116. return OK;
  117. }
  118. constexpr void push_back(const T &p_val) {
  119. ERR_FAIL_COND(_size >= CAPACITY);
  120. memnew_placement(ptr() + _size, T(p_val));
  121. _size++;
  122. }
  123. constexpr void pop_back() {
  124. ERR_FAIL_COND(_size == 0);
  125. _size--;
  126. ptr()[_size].~T();
  127. }
  128. // NOTE: Subscripts sanity check the bounds to avoid undefined behavior.
  129. // This is slower than direct buffer access and can prevent autovectorization.
  130. // If the bounds are known, use ptr() subscript instead.
  131. constexpr const T &operator[](uint32_t p_index) const {
  132. CRASH_COND(p_index >= _size);
  133. return ptr()[p_index];
  134. }
  135. constexpr T &operator[](uint32_t p_index) {
  136. CRASH_COND(p_index >= _size);
  137. return ptr()[p_index];
  138. }
  139. _FORCE_INLINE_ constexpr T *begin() { return ptr(); }
  140. _FORCE_INLINE_ constexpr T *end() { return ptr() + _size; }
  141. _FORCE_INLINE_ constexpr const T *begin() const { return ptr(); }
  142. _FORCE_INLINE_ constexpr const T *end() const { return ptr() + _size; }
  143. };