Browse Source

Made a header for the shared collection functions to make the code easier to understand.

David Piuva 3 years ago
parent
commit
00d0ab2677

+ 1 - 1
Source/DFPSR/api/fileAPI.h

@@ -34,7 +34,7 @@ TODO:
 #ifndef DFPSR_API_FILE
 #ifndef DFPSR_API_FILE
 #define DFPSR_API_FILE
 #define DFPSR_API_FILE
 
 
-#include "../api/stringAPI.h"
+#include "stringAPI.h"
 #include "bufferAPI.h"
 #include "bufferAPI.h"
 #if defined(WIN32) || defined(_WIN32)
 #if defined(WIN32) || defined(_WIN32)
 	#define USE_MICROSOFT_WINDOWS
 	#define USE_MICROSOFT_WINDOWS

+ 1 - 1
Source/DFPSR/api/stringAPI.h

@@ -29,7 +29,7 @@
 #include <iostream>
 #include <iostream>
 #include <sstream>
 #include <sstream>
 #include <functional>
 #include <functional>
-#include "../api/bufferAPI.h"
+#include "bufferAPI.h"
 #include "../collection/List.h"
 #include "../collection/List.h"
 
 
 // Define DFPSR_INTERNAL_ACCESS before any include to get internal access to exposed types
 // Define DFPSR_INTERNAL_ACCESS before any include to get internal access to exposed types

+ 4 - 8
Source/DFPSR/collection/Array.h

@@ -25,14 +25,10 @@
 #ifndef DFPSR_COLLECTION_ARRAY
 #ifndef DFPSR_COLLECTION_ARRAY
 #define DFPSR_COLLECTION_ARRAY
 #define DFPSR_COLLECTION_ARRAY
 
 
-#include <stdint.h>
+#include "collections.h"
 
 
 namespace dsr {
 namespace dsr {
 
 
-// Inlined Boundchecks.h
-void nonZeroLengthCheck(int64_t length, const char* property);
-void baseZeroBoundCheck(int64_t index, int64_t length, const char* property);
-
 // The simplest possible automatically deallocating array with bound checks.
 // The simplest possible automatically deallocating array with bound checks.
 //   Indices use signed indices, which can be used directly from high-level algorithms.
 //   Indices use signed indices, which can be used directly from high-level algorithms.
 // Because std::vector is a list of members, not a fixed size array of values.
 // Because std::vector is a list of members, not a fixed size array of values.
@@ -48,7 +44,7 @@ public:
 	// Constructor
 	// Constructor
 	Array(const int32_t newLength, const T& defaultValue)
 	Array(const int32_t newLength, const T& defaultValue)
 	  : elementCount(newLength) {
 	  : elementCount(newLength) {
-  		nonZeroLengthCheck(newLength, "New array length");
+  		impl_nonZeroLengthCheck(newLength, "New array length");
 		this->elements = new T[newLength];
 		this->elements = new T[newLength];
 		for (int32_t index = 0; index < newLength; index++) {
 		for (int32_t index = 0; index < newLength; index++) {
 			this->elements[index] = defaultValue;
 			this->elements[index] = defaultValue;
@@ -61,11 +57,11 @@ public:
 	~Array() { delete[] this->elements; }
 	~Array() { delete[] this->elements; }
 	// Element access
 	// Element access
 	T& operator[] (const int32_t index) {
 	T& operator[] (const int32_t index) {
-		baseZeroBoundCheck(index, this->length(), "Array index");
+		impl_baseZeroBoundCheck(index, this->length(), "Array index");
 		return this->elements[index];
 		return this->elements[index];
 	}
 	}
 	const T& operator[] (const int32_t index) const {
 	const T& operator[] (const int32_t index) const {
-		baseZeroBoundCheck(index, this->length(), "Array index");
+		impl_baseZeroBoundCheck(index, this->length(), "Array index");
 		return this->elements[index];
 		return this->elements[index];
 	}
 	}
 	int32_t length() const {
 	int32_t length() const {

+ 3 - 7
Source/DFPSR/collection/Field.h

@@ -25,15 +25,11 @@
 #ifndef DFPSR_COLLECTION_FIELD
 #ifndef DFPSR_COLLECTION_FIELD
 #define DFPSR_COLLECTION_FIELD
 #define DFPSR_COLLECTION_FIELD
 
 
-#include <stdint.h>
+#include "collections.h"
 #include "../math/IVector.h"
 #include "../math/IVector.h"
 
 
 namespace dsr {
 namespace dsr {
 
 
-// Inlined Boundchecks.h
-void nonZeroLengthCheck(int64_t length, const char* property);
-void baseZeroBoundCheck(int64_t index, int64_t length, const char* property);
-
 // A 2D version of Array with built-in support for accessing elements out of bound.
 // A 2D version of Array with built-in support for accessing elements out of bound.
 template <typename T>
 template <typename T>
 class Field {
 class Field {
@@ -44,8 +40,8 @@ public:
 	// Constructor
 	// Constructor
 	Field(const int32_t width, const int32_t height, const T& defaultValue)
 	Field(const int32_t width, const int32_t height, const T& defaultValue)
 	  : elementWidth(width), elementHeight(height) {
 	  : elementWidth(width), elementHeight(height) {
-		nonZeroLengthCheck(width, "New array width");
-  		nonZeroLengthCheck(height, "New array height");
+		impl_nonZeroLengthCheck(width, "New array width");
+  		impl_nonZeroLengthCheck(height, "New array height");
 		int32_t size = width * height;
 		int32_t size = width * height;
 		this->elements = new T[size];
 		this->elements = new T[size];
 		for (int32_t index = 0; index < size; index++) {
 		for (int32_t index = 0; index < size; index++) {

+ 9 - 12
Source/DFPSR/collection/List.h

@@ -25,16 +25,13 @@
 #ifndef DFPSR_COLLECTION_LIST
 #ifndef DFPSR_COLLECTION_LIST
 #define DFPSR_COLLECTION_LIST
 #define DFPSR_COLLECTION_LIST
 
 
+#include "collections.h"
 #include <stdint.h>
 #include <stdint.h>
 #include <vector>
 #include <vector>
 #include <algorithm>
 #include <algorithm>
 
 
 namespace dsr {
 namespace dsr {
 
 
-// Inlined Boundchecks.h
-void nonZeroLengthCheck(int64_t length, const char* property);
-void baseZeroBoundCheck(int64_t index, int64_t length, const char* property);
-
 // TODO: Remove the std::vector dependency by reimplementing the basic features.
 // TODO: Remove the std::vector dependency by reimplementing the basic features.
 
 
 // An array list for constant time random access to elements in a LIFO stack.
 // An array list for constant time random access to elements in a LIFO stack.
@@ -59,32 +56,32 @@ public:
 	}
 	}
 	// Post-condition: Returns the element at index from the range 0..length-1
 	// Post-condition: Returns the element at index from the range 0..length-1
 	T& operator[] (int64_t index) {
 	T& operator[] (int64_t index) {
-		baseZeroBoundCheck(index, this->length(), "List index");
+		impl_baseZeroBoundCheck(index, this->length(), "List index");
 		return this->backend[index];
 		return this->backend[index];
 	}
 	}
 	// Post-condition: Returns the write-protected element at index from the range 0..length-1
 	// Post-condition: Returns the write-protected element at index from the range 0..length-1
 	const T& operator[] (int64_t index) const {
 	const T& operator[] (int64_t index) const {
-		baseZeroBoundCheck(index, this->length(), "List index");
+		impl_baseZeroBoundCheck(index, this->length(), "List index");
 		return this->backend[index];
 		return this->backend[index];
 	}
 	}
 	// Post-condition: Returns a reference to the first element
 	// Post-condition: Returns a reference to the first element
 	T& first() {
 	T& first() {
-		nonZeroLengthCheck(this->length(), "Length");
+		impl_nonZeroLengthCheck(this->length(), "Length");
 		return this->backend[0];
 		return this->backend[0];
 	}
 	}
 	// Post-condition: Returns a reference to the first element from a write protected array list
 	// Post-condition: Returns a reference to the first element from a write protected array list
 	const T& first() const {
 	const T& first() const {
-		nonZeroLengthCheck(this->length(), "Length");
+		impl_nonZeroLengthCheck(this->length(), "Length");
 		return this->backend[0];
 		return this->backend[0];
 	}
 	}
 	// Post-condition: Returns a reference to the last element
 	// Post-condition: Returns a reference to the last element
 	T& last() {
 	T& last() {
-		nonZeroLengthCheck(this->length(), "Length");
+		impl_nonZeroLengthCheck(this->length(), "Length");
 		return this->backend[this->length() - 1];
 		return this->backend[this->length() - 1];
 	}
 	}
 	// Post-condition: Returns a reference to the last element from a write protected array list
 	// Post-condition: Returns a reference to the last element from a write protected array list
 	const T& last() const {
 	const T& last() const {
-		nonZeroLengthCheck(this->length(), "Length");
+		impl_nonZeroLengthCheck(this->length(), "Length");
 		return this->backend[this->length() - 1];
 		return this->backend[this->length() - 1];
 	}
 	}
 	// Side-effect: Removes all elements by setting the count to zero
 	// Side-effect: Removes all elements by setting the count to zero
@@ -99,8 +96,8 @@ public:
 	// Side-effect: Swap the order of two elements
 	// Side-effect: Swap the order of two elements
 	//   Useful for moving and sorting elements
 	//   Useful for moving and sorting elements
 	void swap(int64_t indexA, int64_t indexB) {
 	void swap(int64_t indexA, int64_t indexB) {
-		baseZeroBoundCheck(indexA, this->length(), "Swap index A");
-		baseZeroBoundCheck(indexB, this->length(), "Swap index B");
+		impl_baseZeroBoundCheck(indexA, this->length(), "Swap index A");
+		impl_baseZeroBoundCheck(indexB, this->length(), "Swap index B");
 		std::swap(this->backend[indexA], this->backend[indexB]);
 		std::swap(this->backend[indexA], this->backend[indexB]);
 	}
 	}
 	// Side-effect: Adds a new element at the end
 	// Side-effect: Adds a new element at the end

+ 3 - 3
Source/DFPSR/collection/BoundChecks.cpp → Source/DFPSR/collection/collections.cpp

@@ -1,7 +1,7 @@
 
 
 // zlib open source license
 // zlib open source license
 //
 //
-// Copyright (c) 2019 David Forsgren Piuva
+// Copyright (c) 2019 to 2022 David Forsgren Piuva
 // 
 // 
 // This software is provided 'as-is', without any express or implied
 // This software is provided 'as-is', without any express or implied
 // warranty. In no event will the authors be held liable for any damages
 // warranty. In no event will the authors be held liable for any damages
@@ -26,13 +26,13 @@
 
 
 namespace dsr {
 namespace dsr {
 
 
-void nonZeroLengthCheck(int64_t length, const char* property) {
+void impl_nonZeroLengthCheck(int64_t length, const char* property) {
 	if (length <= 0) {
 	if (length <= 0) {
 		throwError(property, " may not be zero!\n");
 		throwError(property, " may not be zero!\n");
 	}
 	}
 }
 }
 
 
-void baseZeroBoundCheck(int64_t index, int64_t length, const char* property) {
+void impl_baseZeroBoundCheck(int64_t index, int64_t length, const char* property) {
 	if (index < 0 || index >= length) {
 	if (index < 0 || index >= length) {
 		throwError(property, " ", index, " is out of bound 0..", (length - 1), "!\n");
 		throwError(property, " ", index, " is out of bound 0..", (length - 1), "!\n");
 	}
 	}

+ 40 - 0
Source/DFPSR/collection/collections.h

@@ -0,0 +1,40 @@
+
+// zlib open source license
+//
+// Copyright (c) 2019 to 2022 David Forsgren Piuva
+// 
+// This software is provided 'as-is', without any express or implied
+// warranty. In no event will the authors be held liable for any damages
+// arising from the use of this software.
+// 
+// Permission is granted to anyone to use this software for any purpose,
+// including commercial applications, and to alter it and redistribute it
+// freely, subject to the following restrictions:
+// 
+//    1. The origin of this software must not be misrepresented; you must not
+//    claim that you wrote the original software. If you use this software
+//    in a product, an acknowledgment in the product documentation would be
+//    appreciated but is not required.
+// 
+//    2. Altered source versions must be plainly marked as such, and must not be
+//    misrepresented as being the original software.
+// 
+//    3. This notice may not be removed or altered from any source
+//    distribution.
+
+// Because most collections have little need for their own implementation files, all the bound checks are placed in a common implementation file.
+
+#ifndef DFPSR_COLLECTIONS
+#define DFPSR_COLLECTIONS
+
+#include <stdint.h>
+
+namespace dsr {
+
+// Helper functions for collections.
+void impl_nonZeroLengthCheck(int64_t length, const char* property);
+void impl_baseZeroBoundCheck(int64_t index, int64_t length, const char* property);
+
+}
+
+#endif

+ 0 - 1
Source/DFPSR/collection/includeCollection.h

@@ -4,4 +4,3 @@
 #include "Array.h"
 #include "Array.h"
 #include "Field.h"
 #include "Field.h"
 #include "List.h"
 #include "List.h"
-