|
@@ -27,19 +27,20 @@
|
|
|
|
|
|
|
|
#include "collections.h"
|
|
#include "collections.h"
|
|
|
#include "../math/IVector.h"
|
|
#include "../math/IVector.h"
|
|
|
|
|
+#include "../math/LVector.h"
|
|
|
|
|
+#include "../math/UVector.h"
|
|
|
|
|
|
|
|
namespace dsr {
|
|
namespace dsr {
|
|
|
|
|
|
|
|
-// TODO: Should this be cloned automatically for consistency with List?
|
|
|
|
|
-// TODO: Implement generic operations for Field.
|
|
|
|
|
-
|
|
|
|
|
-// A 2D version of Array with built-in support for accessing elements out of bound.
|
|
|
|
|
|
|
+// A 2D version of Array with methods for padding reads and ignoring writes that are out-of-bound.
|
|
|
// If you need more speed, pack elements into a Buffer and iterate
|
|
// If you need more speed, pack elements into a Buffer and iterate
|
|
|
// over them using SafePointer with SIMD aligned stride between rows.
|
|
// over them using SafePointer with SIMD aligned stride between rows.
|
|
|
|
|
+// Unlike Buffer, Field is a value type, so be careful not to pass it by value unless you intend to clone its content.
|
|
|
template <typename T>
|
|
template <typename T>
|
|
|
class Field {
|
|
class Field {
|
|
|
private:
|
|
private:
|
|
|
- const int64_t elementWidth, elementHeight;
|
|
|
|
|
|
|
+ int64_t elementWidth = 0;
|
|
|
|
|
+ int64_t elementHeight = 0;
|
|
|
T *elements = nullptr;
|
|
T *elements = nullptr;
|
|
|
public:
|
|
public:
|
|
|
// Constructor
|
|
// Constructor
|
|
@@ -53,45 +54,80 @@ public:
|
|
|
this->elements[index] = defaultValue;
|
|
this->elements[index] = defaultValue;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+ // Bound check
|
|
|
|
|
+ bool inside(int64_t x, int64_t y) const {
|
|
|
|
|
+ return x >= 0 && x < this->elementWidth && y >= 0 && y < this->elementHeight;
|
|
|
|
|
+ }
|
|
|
// Direct memory access where bound checks are only applied in debug mode, so access out of bound will crash.
|
|
// Direct memory access where bound checks are only applied in debug mode, so access out of bound will crash.
|
|
|
- // Precondition: this->inside(location.x, location.y)
|
|
|
|
|
- T& unsafe_writeAccess(const IVector2D& location) {
|
|
|
|
|
- assert(this->inside(location));
|
|
|
|
|
- return this->elements[location.x + location.y * this->elementWidth];
|
|
|
|
|
|
|
+ // Precondition: this->inside(x, y)
|
|
|
|
|
+ T& unsafe_writeAccess(int64_t x, int64_t y) {
|
|
|
|
|
+ assert(this->inside(x, y));
|
|
|
|
|
+ return this->elements[x + y * this->elementWidth];
|
|
|
}
|
|
}
|
|
|
- // Precondition: this->inside(location.x, location.y)
|
|
|
|
|
- const T& unsafe_readAccess(const IVector2D& location) const {
|
|
|
|
|
- assert(this->inside(location));
|
|
|
|
|
- return this->elements[location.x + location.y * this->elementWidth];
|
|
|
|
|
|
|
+ // Precondition: this->inside(x, y)
|
|
|
|
|
+ const T& unsafe_readAccess(int64_t x, int64_t y) const {
|
|
|
|
|
+ assert(this->inside(x, y));
|
|
|
|
|
+ return this->elements[x + y * this->elementWidth];
|
|
|
}
|
|
}
|
|
|
- // No implicit copies, only pass by reference
|
|
|
|
|
- Field(const Field&) = delete;
|
|
|
|
|
- Field& operator=(const Field&) = delete;
|
|
|
|
|
|
|
+ // Clonable by default!
|
|
|
|
|
+ // Be very careful not to accidentally pass a Field by value instead of reference,
|
|
|
|
|
+ // otherwise your side-effects might write to a temporary copy
|
|
|
|
|
+ // or time is wasted to clone an Field every time you look something up.
|
|
|
|
|
+ Field(const Field<T>& source) {
|
|
|
|
|
+ // Allocate to the same size as source.
|
|
|
|
|
+ int64_t newSize = source.elementWidth * source.elementHeight;
|
|
|
|
|
+ this->elements = new T[newSize];
|
|
|
|
|
+ this->elementWidth = source.elementWidth;
|
|
|
|
|
+ this->elementHeight = source.elementHeight;
|
|
|
|
|
+ // Copy elements from source.
|
|
|
|
|
+ for (int64_t e = 0; e < newSize; e++) {
|
|
|
|
|
+ // Assign one element at a time, so that objects can be copy constructed.
|
|
|
|
|
+ // 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>.
|
|
|
|
|
+ this->elements[e] = source.elements[e];
|
|
|
|
|
+ }
|
|
|
|
|
+ };
|
|
|
|
|
+ // When assigning to the field, memory can be reused when the number of elements is the same.
|
|
|
|
|
+ Field& operator=(const Field<T>& source) {
|
|
|
|
|
+ int64_t oldSize = this->elementWidth * this->elementHeight;
|
|
|
|
|
+ int64_t newSize = source.elementWidth * source.elementHeight;
|
|
|
|
|
+ // Reallocate to the same size as source if needed.
|
|
|
|
|
+ if (oldSize != newSize) {
|
|
|
|
|
+ if (this->elements) delete[] this->elements;
|
|
|
|
|
+ this->elements = new T[newSize];
|
|
|
|
|
+ }
|
|
|
|
|
+ // Update dimensions, even if the combined allocation size is the same.
|
|
|
|
|
+ this->elementWidth = source.elementWidth;
|
|
|
|
|
+ this->elementHeight = source.elementHeight;
|
|
|
|
|
+ // Copy elements from source.
|
|
|
|
|
+ for (int64_t e = 0; e < newSize; e++) {
|
|
|
|
|
+ // Assign one element at a time, so that objects can be copy constructed.
|
|
|
|
|
+ // 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>.
|
|
|
|
|
+ this->elements[e] = source.elements[e];
|
|
|
|
|
+ }
|
|
|
|
|
+ return *this;
|
|
|
|
|
+ };
|
|
|
// Destructor
|
|
// Destructor
|
|
|
- ~Field() { delete[] this->elements; }
|
|
|
|
|
- // Bound check
|
|
|
|
|
- bool inside(const IVector2D& location) const {
|
|
|
|
|
- return location.x >= 0 && location.x < this->elementWidth && location.y >= 0 && location.y < this->elementHeight;
|
|
|
|
|
- }
|
|
|
|
|
- // Read access
|
|
|
|
|
- T read_border(const IVector2D& location, const T& outside) const {
|
|
|
|
|
- if (this->inside(location)) {
|
|
|
|
|
- return this->unsafe_readAccess(location);
|
|
|
|
|
|
|
+ ~Field() { if (this->elements) delete[] this->elements; }
|
|
|
|
|
+ // Get the element at (x, y) or the outside value when (x, y) is out-of-bound.
|
|
|
|
|
+ T read_border(int64_t x, int64_t y, const T& outside) const {
|
|
|
|
|
+ if (this->inside(x, y)) {
|
|
|
|
|
+ return this->unsafe_readAccess(x, y);
|
|
|
} else {
|
|
} else {
|
|
|
return outside;
|
|
return outside;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
- T read_clamp(IVector2D location) const {
|
|
|
|
|
- if (location.x < 0) location.x = 0;
|
|
|
|
|
- if (location.x >= this->elementWidth) location.x = this->elementWidth - 1;
|
|
|
|
|
- if (location.y < 0) location.y = 0;
|
|
|
|
|
- if (location.y >= this->elementHeight) location.y = this->elementHeight - 1;
|
|
|
|
|
- return this->unsafe_readAccess(location);
|
|
|
|
|
|
|
+ // Get the element closest to (x, y), by clamping the coordinate to valid bounds.
|
|
|
|
|
+ T read_clamp(int64_t x, int64_t y) const {
|
|
|
|
|
+ if (x < 0) x = 0;
|
|
|
|
|
+ if (x >= this->elementWidth) x = this->elementWidth - 1;
|
|
|
|
|
+ if (y < 0) y = 0;
|
|
|
|
|
+ if (y >= this->elementHeight) y = this->elementHeight - 1;
|
|
|
|
|
+ return this->unsafe_readAccess(x, y);
|
|
|
}
|
|
}
|
|
|
- // Write access
|
|
|
|
|
- void write_ignore(const IVector2D& location, const T& value) {
|
|
|
|
|
- if (this->inside(location)) {
|
|
|
|
|
- this->unsafe_writeAccess(location) = value;
|
|
|
|
|
|
|
+ // Write value to the element at (x, y) when inside of the bounds, ignoring the operation silently when outside.
|
|
|
|
|
+ void write_ignore(int64_t x, int64_t y, const T& value) {
|
|
|
|
|
+ if (this->inside(x, y)) {
|
|
|
|
|
+ this->unsafe_writeAccess(x, y) = value;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
int64_t width() const {
|
|
int64_t width() const {
|
|
@@ -100,9 +136,32 @@ public:
|
|
|
int64_t height() const {
|
|
int64_t height() const {
|
|
|
return this->elementHeight;
|
|
return this->elementHeight;
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ // Wrappers for access using UVector instead of separate (x, y) coordinates.
|
|
|
|
|
+ bool inside(const UVector2D& location) const { return this->inside(location.x, location.y); }
|
|
|
|
|
+ T& unsafe_writeAccess(const UVector2D &location) { return this->unsafe_writeAccess(location.x, location.y); }
|
|
|
|
|
+ const T& unsafe_readAccess(const UVector2D &location) const { return this->unsafe_readAccess(location.x, location.y); }
|
|
|
|
|
+ T read_border(const UVector2D& location, const T& outside) const { return this->read_border(location.x, location.y, outside); }
|
|
|
|
|
+ T read_clamp(UVector2D location) const { return this->read_clamp(location.x, location.y); }
|
|
|
|
|
+ void write_ignore(const UVector2D& location, const T& value) { this->write_ignore(location.x, location.y); }
|
|
|
|
|
+
|
|
|
|
|
+ // Wrappers for access using IVector instead of separate (x, y) coordinates.
|
|
|
|
|
+ bool inside(const IVector2D& location) const { return this->inside(location.x, location.y); }
|
|
|
|
|
+ T& unsafe_writeAccess(const IVector2D &location) { return this->unsafe_writeAccess(location.x, location.y); }
|
|
|
|
|
+ const T& unsafe_readAccess(const IVector2D &location) const { return this->unsafe_readAccess(location.x, location.y); }
|
|
|
|
|
+ T read_border(const IVector2D& location, const T& outside) const { return this->read_border(location.x, location.y, outside); }
|
|
|
|
|
+ T read_clamp(IVector2D location) const { return this->read_clamp(location.x, location.y); }
|
|
|
|
|
+ void write_ignore(const IVector2D& location, const T& value) { this->write_ignore(location.x, location.y); }
|
|
|
|
|
+
|
|
|
|
|
+ // Wrappers for access using LVector instead of separate (x, y) coordinates.
|
|
|
|
|
+ bool inside(const LVector2D& location) const { return this->inside(location.x, location.y); }
|
|
|
|
|
+ T& unsafe_writeAccess(const LVector2D &location) { return this->unsafe_writeAccess(location.x, location.y); }
|
|
|
|
|
+ const T& unsafe_readAccess(const LVector2D &location) const { return this->unsafe_readAccess(location.x, location.y); }
|
|
|
|
|
+ T read_border(const LVector2D& location, const T& outside) const { return this->read_border(location.x, location.y, outside); }
|
|
|
|
|
+ T read_clamp(LVector2D location) const { return this->read_clamp(location.x, location.y); }
|
|
|
|
|
+ void write_ignore(const LVector2D& location, const T& value) { this->write_ignore(location.x, location.y); }
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
#endif
|
|
|
-
|
|
|