Browse Source

Allowing Array and Field to be cloned implicitly.

David Piuva 2 years ago
parent
commit
42f865ea1f

+ 5 - 5
Source/DFPSR/api/algorithmAPI.h

@@ -59,7 +59,7 @@ bool operator==(const Field<T>& a, const Field<T>& b) {
 	 || a.height() != b.height()) return false;
 	for (int64_t y = 0; y < a.height(); y++) {
 		for (int64_t x = 0; x < a.width(); x++) {
-			if (!(a.unsafe_readAccess(IVector2D(x, y)) == b.unsafe_readAccess(IVector2D(x, y)))) return false;
+			if (!(a.unsafe_readAccess(x, y) == b.unsafe_readAccess(x, y))) return false;
 		}
 	}
 	return true;
@@ -112,7 +112,7 @@ String& string_toStreamIndented(String& target, const Field<T>& collection, cons
 	int64_t maxX = collection.width() - 1;
 	int64_t maxY = collection.height() - 1;
 	for (int64_t y = 0; y <= maxY; y++) {
-		string_append(target, indentation, U"\t{");
+		string_append(target, indentation, U"\t{\n");
 		for (int64_t x = 0; x <= maxX; x++) {
 			string_toStreamIndented(target, collection.unsafe_readAccess(IVector2D(x, y)), indentation + "\t\t");
 			if (x < maxX) {
@@ -120,11 +120,11 @@ String& string_toStreamIndented(String& target, const Field<T>& collection, cons
 			}
 			string_append(target, U"\n");
 		}
+		string_append(target, indentation, U"\t}");
 		if (y < maxY) {
-			// Comma separate rows on a separate line, to avoid having too much indentation.
-			string_append(target, U"\n", indentation, U"\t,");
+			string_append(target, U",");
 		}
-		string_append(target, U"\n", indentation, U"\t{\n");
+		string_append(target, U"\n");
 	}
 	string_append(target, indentation, U"}");
 	return target;

+ 34 - 9
Source/DFPSR/collection/Array.h

@@ -30,29 +30,55 @@
 namespace dsr {
 
 // A fixed size collection of elements initialized to the same default value.
-// TODO: Should implicit cloning be allowed just for consistency with List?
-//       Having one set of rules for lists and another for Arrays is inconsistent.
+//   Unlike Buffer, Array is a value type, so be careful not to pass it by value unless you intend to clone its content.
 template <typename T>
 class Array {
 private:
-	const int64_t elementCount;
+	int64_t elementCount = 0;
 	T *elements = nullptr;
 public:
 	// Constructor
 	Array(const int64_t newLength, const T& defaultValue)
 	  : elementCount(newLength) {
   		impl_nonZeroLengthCheck(newLength, "New array length");
-  		// TODO: Try to initialize once, so that elements don't need to have the assignment operator defined.
 		this->elements = new T[newLength];
 		for (int64_t index = 0; index < newLength; index++) {
 			this->elements[index] = defaultValue;
 		}
 	}
-	// No implicit copies, only pass by reference
-	Array(const Array&) = delete;
-	Array& operator=(const Array&) = delete;
+	// Clonable by default!
+	//   Be very careful not to accidentally pass an Array by value instead of reference,
+	//   otherwise your side-effects might write to a temporary copy
+	//   or time is wasted to clone an Array every time you look something up.
+	Array(const Array<T>& source) {
+		// Allocate to the same size as source.
+		this->elements = new T[source.elementCount];
+		this->elementCount = source.elementCount;
+		// Copy elements from source.
+		for (int64_t e = 0; e < this->elementCount; 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 array, memory can be reused when the size is the same.
+	Array& operator=(const Array<T>& source) {
+		// Reallocate to the same size as source if needed.
+		if (this->elementCount != source.elementCount) {
+			if (this->elements) delete[] this->elements;
+			this->elements = new T[source.elementCount];
+		}
+		this->elementCount = source.elementCount;
+		// Copy elements from source.
+		for (int64_t e = 0; e < this->elementCount; 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
-	~Array() { delete[] this->elements; }
+	~Array() { if (this->elements) delete[] this->elements; }
 	// Element access
 	T& operator[] (const int64_t index) {
 		impl_baseZeroBoundCheck(index, this->length(), "Array index");
@@ -70,4 +96,3 @@ public:
 }
 
 #endif
-

+ 95 - 36
Source/DFPSR/collection/Field.h

@@ -27,19 +27,20 @@
 
 #include "collections.h"
 #include "../math/IVector.h"
+#include "../math/LVector.h"
+#include "../math/UVector.h"
 
 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
 //     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>
 class Field {
 private:
-	const int64_t elementWidth, elementHeight;
+	int64_t elementWidth = 0;
+	int64_t elementHeight = 0;
 	T *elements = nullptr;
 public:
 	// Constructor
@@ -53,45 +54,80 @@ public:
 			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.
-	// 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
-	~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 {
 			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 {
@@ -100,9 +136,32 @@ public:
 	int64_t height() const {
 		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
-

+ 4 - 2
Source/DFPSR/collection/List.h

@@ -44,6 +44,7 @@ namespace dsr {
 //       so that you get memory bound and alignment checks for SIMD vectors.
 //     * Unsigned indices will either force dangerous casting from signed, or prevent
 //       the ability to loop backwards without crashing when the x < 0u criteria cannot be met.
+//   Unlike Buffer, List is a value type, so be careful not to pass it by value unless you intend to clone its content.
 template <typename T>
 class List {
 private:
@@ -53,7 +54,9 @@ public:
 	// Constructor
 	List() {}
 	// Clonable by default!
-	//   Pass by reference if you don't want to lose your changes and waste time duplicating memory.
+	//   Be very careful not to accidentally pass a List by value instead of reference,
+	//   otherwise your side-effects might write to a temporary copy
+	//   or time is wasted to clone a List every time you look something up.
 	List(const List& source) : backend(std::vector<T>(source.backend.begin(), source.backend.end())) {}
 	// Construct using one argument per element.
 	template<typename... ELEMENTS>
@@ -170,4 +173,3 @@ public:
 }
 
 #endif
-

+ 91 - 0
Source/test/tests/ArrayTest.cpp

@@ -0,0 +1,91 @@
+
+#include "../testTools.h"
+
+START_TEST(Array)
+	{
+		Array<int> a = Array<int>(4, 123);
+		a[1] = 85;
+		a[3] = -100;
+		ASSERT_EQUAL(a.length(), 4);
+		ASSERT_EQUAL(a[0], 123);
+		ASSERT_EQUAL(a[1], 85);
+		ASSERT_EQUAL(a[2], 123);
+		ASSERT_EQUAL(a[3], -100);
+		ASSERT_EQUAL(string_combine(a),
+		  U"{\n"
+		  U"	123,\n"
+		  U"	85,\n"
+		  U"	123,\n"
+		  U"	-100\n"
+		  U"}"
+		);
+		// An initial assignment uses the copy constructor, because there is no pre-existing data in b.
+		Array<int> b = a;
+		b[0] = 200;
+		b[2] = 100000;
+		// The b array has changed...
+		ASSERT_EQUAL(string_combine(b),
+		  U"{\n"
+		  U"	200,\n"
+		  U"	85,\n"
+		  U"	100000,\n"
+		  U"	-100\n"
+		  U"}"
+		);
+		// ...but a remains the same, because the data was cloned when assigning.
+		ASSERT_EQUAL(string_combine(a),
+		  U"{\n"
+		  U"	123,\n"
+		  U"	85,\n"
+		  U"	123,\n"
+		  U"	-100\n"
+		  U"}"
+		);
+		// They are not equal
+		ASSERT_NOT_EQUAL(a, b);
+		// Assigning from copy construction is optimized into an assignment operation, because b already exists.
+		a = Array<int>(b);
+		// Now they are equal
+		ASSERT_EQUAL(a, b);
+		// Create another length
+		Array<int> c = Array<int>(7, 75);
+		ASSERT_EQUAL(string_combine(c),
+		  U"{\n"
+		  U"	75,\n"
+		  U"	75,\n"
+		  U"	75,\n"
+		  U"	75,\n"
+		  U"	75,\n"
+		  U"	75,\n"
+		  U"	75\n"
+		  U"}"
+		);
+		// Assign larger array
+		a = c;
+		ASSERT_EQUAL(string_combine(a),
+		  U"{\n"
+		  U"	75,\n"
+		  U"	75,\n"
+		  U"	75,\n"
+		  U"	75,\n"
+		  U"	75,\n"
+		  U"	75,\n"
+		  U"	75\n"
+		  U"}"
+		);
+		ASSERT_EQUAL(a, c);
+		ASSERT_NOT_EQUAL(a, b);
+		// Assign smaller array
+		c = b;
+		ASSERT_EQUAL(string_combine(c),
+		  U"{\n"
+		  U"	200,\n"
+		  U"	85,\n"
+		  U"	100000,\n"
+		  U"	-100\n"
+		  U"}"
+		);
+		ASSERT_EQUAL(c, b);
+		ASSERT_NOT_EQUAL(a, c);
+	}
+END_TEST

+ 98 - 0
Source/test/tests/FieldTest.cpp

@@ -0,0 +1,98 @@
+
+#include "../testTools.h"
+
+START_TEST(Field)
+	{
+		// Allocate 3 x 2 integers, initialized to 123 in each element.
+		Field<int> a = Field<int>(3, 2, 123);
+		ASSERT_EQUAL(a.width(), 3);
+		ASSERT_EQUAL(a.height(), 2);
+		ASSERT_EQUAL(string_combine(a),
+		  U"{\n"
+		  U"	{\n"
+		  U"		123,\n"
+		  U"		123,\n"
+		  U"		123\n"
+		  U"	},\n"
+		  U"	{\n"
+		  U"		123,\n"
+		  U"		123,\n"
+		  U"		123\n"
+		  U"	}\n"
+		  U"}"
+		);
+		// Skip writing outside.
+		a.write_ignore(-1, 0, 55555);
+		a.write_ignore(3, 1, 88);		
+		a.write_ignore(0, -1, 999);
+		a.write_ignore(2, 2, 12345);
+		// Write inside.
+		a.write_ignore(0, 0, 11);
+		a.write_ignore(1, 0, 21);
+		a.write_ignore(0, 1, 12);
+		// Copy to b.
+		Field<int> b = a;
+		// Write inside.
+		a.write_ignore(2, 0, 31);
+		a.write_ignore(1, 1, 22);
+		a.write_ignore(2, 1, 32);
+		ASSERT_EQUAL(string_combine(a),
+		  U"{\n"
+		  U"	{\n"
+		  U"		11,\n"
+		  U"		21,\n"
+		  U"		31\n"
+		  U"	},\n"
+		  U"	{\n"
+		  U"		12,\n"
+		  U"		22,\n"
+		  U"		32\n"
+		  U"	}\n"
+		  U"}"
+		);
+		ASSERT_EQUAL(string_combine(b),
+		  U"{\n"
+		  U"	{\n"
+		  U"		11,\n"
+		  U"		21,\n"
+		  U"		123\n"
+		  U"	},\n"
+		  U"	{\n"
+		  U"		12,\n"
+		  U"		123,\n"
+		  U"		123\n"
+		  U"	}\n"
+		  U"}"
+		);
+		// Read with border.
+		ASSERT_EQUAL(a.read_border(-2, -2,  8),  8); // Outside
+		ASSERT_EQUAL(a.read_border(-1, -2, -1), -1); // Outside
+		ASSERT_EQUAL(a.read_border(-1, -1, -1), -1); // Outside
+		ASSERT_EQUAL(a.read_border( 0, -1, -1), -1); // Outside
+		ASSERT_EQUAL(a.read_border( 0,  0, -2), 11); // Inside
+		ASSERT_EQUAL(a.read_border( 1,  0, -1), 21); // Inside
+		ASSERT_EQUAL(a.read_border( 1,  1, 55), 22); // Inside
+		ASSERT_EQUAL(a.read_border( 2,  1, -1), 32); // Inside
+		ASSERT_EQUAL(a.read_border( 2,  2, 12), 12); // Outside
+		ASSERT_EQUAL(a.read_border( 3,  2, -1), -1); // Outside
+		ASSERT_EQUAL(a.read_border( 3,  3, 13), 13); // Outside
+		ASSERT_EQUAL(a.read_border( 4,  3, -1), -1); // Outside
+		// Read with clamping.
+		ASSERT_EQUAL(a.read_clamp(-2, -2), 11); // Outside
+		ASSERT_EQUAL(a.read_clamp(-1, -2), 11); // Outside
+		ASSERT_EQUAL(a.read_clamp(-1, -1), 11); // Outside
+		ASSERT_EQUAL(a.read_clamp( 0, -1), 11); // Outside
+		ASSERT_EQUAL(a.read_clamp( 0,  0), 11); // Outside
+		ASSERT_EQUAL(a.read_clamp( 1,  0), 21); // Inside
+		ASSERT_EQUAL(a.read_clamp( 1,  1), 22); // Inside
+		ASSERT_EQUAL(a.read_clamp( 2,  1), 32); // Inside
+		ASSERT_EQUAL(a.read_clamp( 2,  2), 32); // Outside
+		ASSERT_EQUAL(a.read_clamp(-1,  2), 12); // Outside
+		ASSERT_EQUAL(a.read_clamp( 3,  3), 32); // Outside
+		ASSERT_EQUAL(a.read_clamp( 4, -1), 31); // Outside
+		// Assign b to a and check that they went from not equal to equal.
+		ASSERT_NOT_EQUAL(a, b);
+		a = b;
+		ASSERT_EQUAL(a, b);
+	}
+END_TEST