Array.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. 
  2. // zlib open source license
  3. //
  4. // Copyright (c) 2018 to 2025 David Forsgren Piuva
  5. //
  6. // This software is provided 'as-is', without any express or implied
  7. // warranty. In no event will the authors be held liable for any damages
  8. // arising from the use of this software.
  9. //
  10. // Permission is granted to anyone to use this software for any purpose,
  11. // including commercial applications, and to alter it and redistribute it
  12. // freely, subject to the following restrictions:
  13. //
  14. // 1. The origin of this software must not be misrepresented; you must not
  15. // claim that you wrote the original software. If you use this software
  16. // in a product, an acknowledgment in the product documentation would be
  17. // appreciated but is not required.
  18. //
  19. // 2. Altered source versions must be plainly marked as such, and must not be
  20. // misrepresented as being the original software.
  21. //
  22. // 3. This notice may not be removed or altered from any source
  23. // distribution.
  24. #ifndef DFPSR_COLLECTION_ARRAY
  25. #define DFPSR_COLLECTION_ARRAY
  26. #include "collections.h"
  27. namespace dsr {
  28. // A fixed size collection of impl_elements initialized to the same default value.
  29. // Unlike Buffer, Array is a value type, so be careful not to pass it by value unless you intend to clone its content.
  30. template <typename T>
  31. class Array {
  32. private:
  33. intptr_t impl_elementCount = 0;
  34. T *impl_elements = nullptr;
  35. void impl_free() {
  36. if (this->impl_elements != nullptr) {
  37. heap_decreaseUseCount(this->impl_elements);
  38. this->impl_elements = nullptr;
  39. }
  40. }
  41. // Pre-condition: this->impl_elements == nullptr
  42. void impl_allocate(intptr_t elementCount) {
  43. this->impl_elementCount = elementCount;
  44. UnsafeAllocation newAllocation = heap_allocate(elementCount * sizeof(T), false);
  45. #ifdef SAFE_POINTER_CHECKS
  46. heap_setAllocationName(newAllocation.data, "Array allocation");
  47. #endif
  48. this->impl_elements = (T*)(newAllocation.data);
  49. heap_increaseUseCount(newAllocation.header);
  50. }
  51. void impl_reallocate(intptr_t elementCount) {
  52. // Check how much space is available in the target.
  53. uintptr_t allocationSize = heap_getAllocationSize(this->impl_elements);
  54. uintptr_t neededSize = elementCount * sizeof(T);
  55. if (neededSize > allocationSize) {
  56. // Need to replace the old allocation.
  57. this->impl_free();
  58. this->impl_allocate(elementCount);
  59. } else {
  60. // Resize the allocation within the available space.
  61. heap_setUsedSize(this->impl_elements, neededSize);
  62. this->impl_elementCount = elementCount;
  63. }
  64. }
  65. void impl_destroy() {
  66. for (intptr_t index = 0; index < this->impl_elementCount; index++) {
  67. this->impl_elements[index].~T();
  68. }
  69. }
  70. public:
  71. // Constructors.
  72. Array() : impl_elementCount(0), impl_elements(nullptr) {}
  73. Array(const intptr_t newLength, const T& defaultValue) {
  74. if (newLength > 0) {
  75. this->impl_allocate(newLength);
  76. for (intptr_t index = 0; index < newLength; index++) {
  77. new (this->impl_elements + index) T(defaultValue);
  78. }
  79. } else {
  80. this->impl_elementCount = 0;
  81. }
  82. }
  83. // Copy constructor.
  84. Array(const Array<T>& source) {
  85. this->impl_allocate(source.impl_elementCount);
  86. for (intptr_t e = 0; e < this->impl_elementCount; e++) {
  87. new (this->impl_elements + e) T(source.impl_elements[e]);
  88. }
  89. };
  90. // Move constructor.
  91. Array(Array<T> &&source) noexcept
  92. : impl_elementCount(source.impl_elementCount), impl_elements(source.impl_elements) {
  93. source.impl_elementCount = 0;
  94. source.impl_elements = nullptr;
  95. }
  96. // Copy assignment.
  97. Array<T>& operator = (const Array<T>& source) {
  98. if (this != &source) {
  99. this->impl_destroy();
  100. this->impl_reallocate(source.impl_elementCount);
  101. // Copy impl_elements from source.
  102. for (intptr_t e = 0; e < this->impl_elementCount; e++) {
  103. new (this->impl_elements + e) T(source.impl_elements[e]);
  104. }
  105. }
  106. return *this;
  107. };
  108. // Move assignment.
  109. Array<T>& operator = (Array<T> &&source) {
  110. if (this != &source) {
  111. this->impl_destroy();
  112. this->impl_free();
  113. this->impl_elementCount = source.impl_elementCount;
  114. this->impl_elements = source.impl_elements;
  115. source.impl_elementCount = 0;
  116. source.impl_elements = nullptr;
  117. }
  118. return *this;
  119. }
  120. // Destructor
  121. ~Array() {
  122. this->impl_destroy();
  123. this->impl_free();
  124. }
  125. // Bound check
  126. inline bool inside(intptr_t index) const {
  127. return 0 <= index && index < this->impl_elementCount;
  128. }
  129. inline T& unsafe_writeAccess(intptr_t index) {
  130. assert(this->inside(index));
  131. return this->impl_elements[index];
  132. }
  133. inline const T& unsafe_readAccess(intptr_t index) const {
  134. assert(this->inside(index));
  135. return this->impl_elements[index];
  136. }
  137. // Element access
  138. T& operator[] (const intptr_t index) {
  139. impl_baseZeroBoundCheck(index, this->length(), "Array index");
  140. return this->impl_elements[index];
  141. }
  142. const T& operator[] (const intptr_t index) const {
  143. impl_baseZeroBoundCheck(index, this->length(), "Array index");
  144. return this->impl_elements[index];
  145. }
  146. inline intptr_t length() const {
  147. return this->impl_elementCount;
  148. }
  149. };
  150. }
  151. #endif