Field.h 3.3 KB

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