fixed_array.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /**************************************************************************/
  2. /* fixed_array.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. #ifndef FIXED_ARRAY_H
  31. #define FIXED_ARRAY_H
  32. #include "core/local_vector.h"
  33. // High performance fixed size array, single threaded.
  34. // Especially useful if you need to create an array on the stack, to
  35. // prevent dynamic allocations (especially in bottleneck code).
  36. template <class T, uint32_t CAPACITY = 8, bool force_trivial = false, uint32_t ALIGN = 1>
  37. class FixedArray {
  38. static_assert(ALIGN > 0, "ALIGN must be at least 1.");
  39. const static uint32_t UNIT_SIZE = ((sizeof(T) + ALIGN - 1) / ALIGN * ALIGN);
  40. const static bool CONSTRUCT = !std::is_trivially_constructible<T>::value && !force_trivial;
  41. const static bool DESTRUCT = !std::is_trivially_destructible<T>::value && !force_trivial;
  42. uint32_t _size = 0;
  43. uint8_t _data[CAPACITY * UNIT_SIZE];
  44. const T &get(uint32_t p_index) const {
  45. return *(const T *)&_data[p_index * UNIT_SIZE];
  46. }
  47. T &get(uint32_t p_index) {
  48. return *(T *)&_data[p_index * UNIT_SIZE];
  49. }
  50. public:
  51. uint32_t size() const { return _size; }
  52. bool is_empty() const { return !_size; }
  53. bool is_full() const { return _size == CAPACITY; }
  54. uint32_t capacity() const { return CAPACITY; }
  55. T *request(bool p_construct = true) {
  56. if (size() < CAPACITY) {
  57. T *ele = &get(_size++);
  58. if (CONSTRUCT && p_construct) {
  59. memnew_placement(ele, T);
  60. }
  61. return ele;
  62. }
  63. return nullptr;
  64. }
  65. void push_back(const T &p_val) {
  66. T *mem = request(false);
  67. ERR_FAIL_NULL(mem);
  68. *mem = p_val;
  69. }
  70. void clear() {
  71. resize(0);
  72. }
  73. void remove_unordered(uint32_t p_index) {
  74. ERR_FAIL_UNSIGNED_INDEX(p_index, _size);
  75. _size--;
  76. if (_size > p_index) {
  77. get(p_index) = get(_size);
  78. }
  79. if (DESTRUCT) {
  80. get(_size).~T();
  81. }
  82. }
  83. void resize(uint32_t p_size) {
  84. ERR_FAIL_COND(p_size > CAPACITY);
  85. if (DESTRUCT && (p_size < _size)) {
  86. for (uint32_t i = p_size; i < _size; i++) {
  87. get(i).~T();
  88. }
  89. }
  90. if (CONSTRUCT && (p_size > _size)) {
  91. for (uint32_t i = _size; i < p_size; i++) {
  92. memnew_placement(&get(i), T);
  93. }
  94. }
  95. _size = p_size;
  96. }
  97. const T &operator[](uint32_t p_index) const {
  98. DEV_ASSERT(p_index < size());
  99. return get(p_index);
  100. }
  101. T &operator[](uint32_t p_index) {
  102. DEV_ASSERT(p_index < size());
  103. return get(p_index);
  104. }
  105. operator Vector<T>() const {
  106. Vector<T> ret;
  107. if (size()) {
  108. ret.resize(size());
  109. T *dest = ret.ptrw();
  110. if (ALIGN <= 1) {
  111. memcpy(dest, _data, sizeof(T) * _size);
  112. } else {
  113. for (uint32_t n = 0; n < _size; n++) {
  114. dest[n] = get(n);
  115. }
  116. }
  117. }
  118. return ret;
  119. }
  120. };
  121. #endif // FIXED_ARRAY_H