Field.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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_FIELD
  25. #define DFPSR_COLLECTION_FIELD
  26. #include "collections.h"
  27. #include "../math/IVector.h"
  28. #include "../math/LVector.h"
  29. #include "../math/UVector.h"
  30. namespace dsr {
  31. // A 2D version of Array with methods for padding reads and ignoring writes that are out-of-bound.
  32. // If you need more speed, pack elements into a Buffer and iterate
  33. // over them using SafePointer with SIMD aligned stride between rows.
  34. // Unlike Buffer, Field is a value type, so be careful not to pass it by value unless you intend to clone its content.
  35. template <typename T>
  36. class Field {
  37. private:
  38. Array<T> impl_elements;
  39. intptr_t impl_elementWidth = 0;
  40. intptr_t impl_elementHeight = 0;
  41. public:
  42. // Constructors
  43. Field()
  44. : impl_elements(), impl_elementWidth(0), impl_elementHeight(0) {
  45. }
  46. Field(const intptr_t width, const intptr_t height, const T& defaultValue) {
  47. if (width > 0 && height > 0) {
  48. this->impl_elements = Array<T>(width * height, defaultValue);
  49. this->impl_elementWidth = width;
  50. this->impl_elementHeight = height;
  51. }
  52. }
  53. // Bound check
  54. inline bool inside(intptr_t x, intptr_t y) const {
  55. return x >= 0 && x < this->impl_elementWidth && y >= 0 && y < this->impl_elementHeight;
  56. }
  57. // Direct memory access where bound checks are only applied in debug mode, so access out of bound will crash.
  58. // Precondition: this->inside(x, y)
  59. inline T& unsafe_writeAccess(intptr_t x, intptr_t y) {
  60. assert(this->inside(x, y));
  61. return this->impl_elements.unsafe_writeAccess(x + y * this->impl_elementWidth);
  62. }
  63. // Precondition: this->inside(x, y)
  64. inline const T& unsafe_readAccess(intptr_t x, intptr_t y) const {
  65. assert(this->inside(x, y));
  66. return this->impl_elements.unsafe_readAccess(x + y * this->impl_elementWidth);
  67. }
  68. /*
  69. // Clonable by default!
  70. // Be very careful not to accidentally pass a Field by value instead of reference,
  71. // otherwise your side-effects might write to a temporary copy
  72. // or time is wasted to clone an Field every time you look something up.
  73. Field(const Field<T>& source) {
  74. // Allocate to the same size as source.
  75. intptr_t newSize = source.impl_elementWidth * source.impl_elementHeight;
  76. this->impl_elements = new T[newSize];
  77. this->impl_elementWidth = source.impl_elementWidth;
  78. this->impl_elementHeight = source.impl_elementHeight;
  79. // Copy elements from source.
  80. for (intptr_t e = 0; e < newSize; e++) {
  81. // Assign one element at a time, so that objects can be copy constructed.
  82. // If the element type T is trivial and does not require calling constructors, using safeMemoryCopy with SafePointer will be much faster than using Array<T>.
  83. this->impl_elements[e] = source.impl_elements[e];
  84. }
  85. };
  86. // When assigning to the field, memory can be reused when the number of elements is the same.
  87. Field& operator=(const Field<T>& source) {
  88. intptr_t oldSize = this->impl_elementWidth * this->impl_elementHeight;
  89. intptr_t newSize = source.impl_elementWidth * source.impl_elementHeight;
  90. // Reallocate to the same size as source if needed.
  91. if (oldSize != newSize) {
  92. if (this->impl_elements) delete[] this->impl_elements;
  93. this->impl_elements = new T[newSize];
  94. }
  95. // Update dimensions, even if the combined allocation size is the same.
  96. this->impl_elementWidth = source.impl_elementWidth;
  97. this->impl_elementHeight = source.impl_elementHeight;
  98. // Copy elements from source.
  99. for (intptr_t e = 0; e < newSize; e++) {
  100. // Assign one element at a time, so that objects can be copy constructed.
  101. // If the element type T is trivial and does not require calling constructors, using safeMemoryCopy with SafePointer will be much faster than using Array<T>.
  102. this->impl_elements[e] = source.impl_elements[e];
  103. }
  104. return *this;
  105. };
  106. */
  107. // Destructor
  108. //~Field() { if (this->impl_elements) delete[] this->impl_elements; }
  109. // Get the element at (x, y) or the outside value when (x, y) is out-of-bound.
  110. T read_border(intptr_t x, intptr_t y, const T& outside) const {
  111. if (this->inside(x, y)) {
  112. return this->unsafe_readAccess(x, y);
  113. } else {
  114. return outside;
  115. }
  116. }
  117. // Get the element closest to (x, y), by clamping the coordinate to valid bounds.
  118. T read_clamp(intptr_t x, intptr_t y) const {
  119. if (x < 0) x = 0;
  120. if (x >= this->impl_elementWidth) x = this->impl_elementWidth - 1;
  121. if (y < 0) y = 0;
  122. if (y >= this->impl_elementHeight) y = this->impl_elementHeight - 1;
  123. return this->unsafe_readAccess(x, y);
  124. }
  125. // Write value to the element at (x, y) when inside of the bounds, ignoring the operation silently when outside.
  126. void write_ignore(intptr_t x, intptr_t y, const T& value) {
  127. if (this->inside(x, y)) {
  128. this->unsafe_writeAccess(x, y) = value;
  129. }
  130. }
  131. inline intptr_t width() const {
  132. return this->impl_elementWidth;
  133. }
  134. inline intptr_t height() const {
  135. return this->impl_elementHeight;
  136. }
  137. // Wrappers for access using UVector instead of separate (x, y) coordinates.
  138. bool inside(const UVector2D& location) const { return this->inside(location.x, location.y); }
  139. T& unsafe_writeAccess(const UVector2D &location) { return this->unsafe_writeAccess(location.x, location.y); }
  140. const T& unsafe_readAccess(const UVector2D &location) const { return this->unsafe_readAccess(location.x, location.y); }
  141. T read_border(const UVector2D& location, const T& outside) const { return this->read_border(location.x, location.y, outside); }
  142. T read_clamp(UVector2D location) const { return this->read_clamp(location.x, location.y); }
  143. void write_ignore(const UVector2D& location, const T& value) { this->write_ignore(location.x, location.y); }
  144. // Wrappers for access using IVector instead of separate (x, y) coordinates.
  145. bool inside(const IVector2D& location) const { return this->inside(location.x, location.y); }
  146. T& unsafe_writeAccess(const IVector2D &location) { return this->unsafe_writeAccess(location.x, location.y); }
  147. const T& unsafe_readAccess(const IVector2D &location) const { return this->unsafe_readAccess(location.x, location.y); }
  148. T read_border(const IVector2D& location, const T& outside) const { return this->read_border(location.x, location.y, outside); }
  149. T read_clamp(IVector2D location) const { return this->read_clamp(location.x, location.y); }
  150. void write_ignore(const IVector2D& location, const T& value) { this->write_ignore(location.x, location.y); }
  151. // Wrappers for access using LVector instead of separate (x, y) coordinates.
  152. bool inside(const LVector2D& location) const { return this->inside(location.x, location.y); }
  153. T& unsafe_writeAccess(const LVector2D &location) { return this->unsafe_writeAccess(location.x, location.y); }
  154. const T& unsafe_readAccess(const LVector2D &location) const { return this->unsafe_readAccess(location.x, location.y); }
  155. T read_border(const LVector2D& location, const T& outside) const { return this->read_border(location.x, location.y, outside); }
  156. T read_clamp(LVector2D location) const { return this->read_clamp(location.x, location.y); }
  157. void write_ignore(const LVector2D& location, const T& value) { this->write_ignore(location.x, location.y); }
  158. };
  159. }
  160. #endif