Field.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. 
  2. // zlib open source license
  3. //
  4. // Copyright (c) 2018 to 2019 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 <stdint.h>
  27. #include "../math/IVector.h"
  28. namespace dsr {
  29. // Inlined Boundchecks.h
  30. void nonZeroLengthCheck(int64_t length, const char* property);
  31. void baseZeroBoundCheck(int64_t index, int64_t length, const char* property);
  32. // A 2D version of Array with built-in support for accessing elements out of bound.
  33. template <typename T>
  34. class Field {
  35. private:
  36. const int32_t elementWidth, elementHeight;
  37. T *elements = nullptr;
  38. public:
  39. // Constructor
  40. Field(const int32_t width, const int32_t height, const T& defaultValue)
  41. : elementWidth(width), elementHeight(height) {
  42. nonZeroLengthCheck(width, "New array width");
  43. nonZeroLengthCheck(height, "New array height");
  44. int32_t size = width * height;
  45. this->elements = new T[size];
  46. for (int32_t index = 0; index < size; index++) {
  47. this->elements[index] = defaultValue;
  48. }
  49. }
  50. private:
  51. // Direct memory access
  52. // Precondition: this->inside(location.x, location.y)
  53. T& writeAccess(const IVector2D& location) {
  54. return this->elements[location.x + location.y * this->elementWidth];
  55. }
  56. const T& readAccess(const IVector2D& location) const {
  57. return this->elements[location.x + location.y * this->elementWidth];
  58. }
  59. public:
  60. // No implicit copies, only pass by reference
  61. Field(const Field&) = delete;
  62. Field& operator=(const Field&) = delete;
  63. // Destructor
  64. ~Field() { delete[] this->elements; }
  65. // Bound check
  66. bool inside(const IVector2D& location) const {
  67. return location.x >= 0 && location.x < this->elementWidth && location.y >= 0 && location.y < this->elementHeight;
  68. }
  69. // Read access
  70. T read_border(const IVector2D& location, const T& outside) const {
  71. if (this->inside(location)) {
  72. return this->readAccess(location);
  73. } else {
  74. return outside;
  75. }
  76. }
  77. T read_clamp(IVector2D location) const {
  78. if (location.x < 0) location.x = 0;
  79. if (location.x >= this->elementWidth) location.x = this->elementWidth - 1;
  80. if (location.y < 0) location.y = 0;
  81. if (location.y >= this->elementHeight) location.y = this->elementHeight - 1;
  82. return this->readAccess(location);
  83. }
  84. // Write access
  85. void write_ignore(const IVector2D& location, const T& value) {
  86. if (this->inside(location)) {
  87. this->writeAccess(location) = value;
  88. }
  89. }
  90. int32_t width() const {
  91. return this->elementWidth;
  92. }
  93. int32_t height() const {
  94. return this->elementHeight;
  95. }
  96. };
  97. }
  98. #endif