Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Written by Chris Pikul <ChrisPikul510@gmail.com>
- */
- #pragma once
- #include <memory>
- #include <algorithm>
- /**
- Represents a 2 dimensional array of given type <T>
- Provides helper functions for getting and setting values.
- Example:
- [code]
- Array2D<int> array(100,50); //Create array with 100 wide, and 50 high
- array.setValue(3, 50, 40); //Set value at 50,40 to 3
- int val = array.getValue(50, 40); //Returns value at 50,40 being 3 in this ex.
- array[42] = 123; //Can also set individual indexes
- array(3, 50, 40); //Can set using short-hand functor
- val = array(50,40); //Use the shorter-hand functor to get a value as well
- array.setValueClamped(3, 120, 70); //Protective clamp function keeps in range
- array.setValueWrapped(3, 120, 70); //Protective wrap function wraps around into valid indice
- [/code]
- */
- template<class T> struct Array2D{
- /**
- Construct an array with the given square size.
- Both the X, and Y indices will be the same
- */
- Array2D(unsigned int initialSize) {
- this->sizeX = initialSize;
- this->sizeY = initialSize;
- this->data = new T[sizeX * sizeY];
- }
- /**
- Constructs the array with separate X, and Y sizes
- */
- Array2D(unsigned int initialSizeX, unsigned int initialSizeY) {
- this->sizeX = initialSizeX;
- this->sizeY = initialSizeY;
- this->data = new T[sizeX * sizeY];
- }
- /**
- Copies an existing Array2D
- */
- Array2D(Array2D<T>* copy) {
- this->sizeX = copy->sizeX;
- this->sizeY = copy->sizeY;
- this->data = new T[sizeX * sizeY];
- for (int y = 0; y < sizeY; y++) {
- for (int x = 0; x < sizeX; x++) {
- this->data[x*sizeX + y] = copy->data[x*sizeX + y];
- }
- }
- }
- ~Array2D() {
- if (data != NULL)
- delete[] data;
- }
- int getSize() {
- return sizeX;
- }
- int getSizeX() {
- return sizeX;
- }
- int getSizeY() {
- return sizeY;
- }
- inline int getIndex(int x, int y) {
- return x*sizeX + y;
- }
- inline int getIndexClamped(int x, int y) {
- return max(0, min(x, sizeX - 1)) * size + max(0, min(y, sizeY - 1));
- }
- inline int getIndexWrapped(int x, int y) {
- return (x < 0 ? sizeX + x : (x >= sizeX ? y % (sizeX - 1) : x)) * sizeX + (y<0 > sizeY + y : (y >= sizeX ? y % (sizeY - 1) : x));
- }
- /**
- Returns the value at the given index X, and Y.
- WARNING: Makes no protective measures for the validity of the indexes
- */
- T getValue(int x, int y) {
- return data[ getIndex(x,y) ];
- }
- /**
- Returns the value at the given index X, and Y.
- If the indices are not in range, it will clamp them to their respective borders
- */
- T getValueClamped(int x, int y) {
- return data[ getIndexClamped(x,y) ];
- }
- /**
- Returns the value at the given index X, and Y.
- If the indices are not in range, it will wrap the overflowing remainder around.
- This is intended for use in graphics/kernel arrays where a seamless action across
- the array is expected
- */
- T getValueWrapped(int x, int y) {
- return data[ getIndexWrapped(x,y) ];
- }
- /**
- Sets the value at the given indices X, and Y.
- WARNING: Makes no protections that the indices are in bounds!
- */
- void setValue(T value, int x, int y) {
- this->data[getIndex(x, y)] = value;
- }
- /**
- Sets the value at the given indices X, and Y.
- If the indices are not in range, it will clamp them to their respective borders
- */
- void setValueClamped(T value, int x, int y) {
- this->data[getIndexClamped(x, y)] = value;
- }
- /**
- Sets the value at the given indices X, and Y.
- If the indices are not in range, it will wrap the overflowing remainder around.
- This is intended for use in graphics/kernel arrays where a seamless action across
- the array is expected
- */
- void setValueWrapped(T value, int x, int y) {
- this->data[getIndexWrapped(x, y)] = value;
- }
- /**
- Copies an Array2D by assignment
- */
- T& operator=(const T& other) {
- if (this != &other) {
- if (other.sizeX != sizeX || other.sizeY != sizeY) {
- delete[] data;
- sizeX = other.sizeX;
- sizeY = other.sizeY;
- data = nullptr;
- data = new T[sizeX * sizeY];
- }
- std::copy(other.data, other.data + (other.sizeX * other.sizeY), data);
- }
- return *this;
- }
- /**
- Access an index directly as if Array2D itself was a 1D array.
- */
- T& operator[](std::size_t index) {
- return data[index];
- }
- /**
- Return a value from the data.
- Same as getValue(x,y)
- */
- T operator()(int x, int y) {
- return getValue(x, y);
- }
- /**
- Set a value
- Same as setValue(x,y)
- */
- void operator()(T value, int x, int y) {
- setValue(value, x, y);
- }
- private:
- T* data;
- unsigned int sizeX, sizeY;
- };
Add Comment
Please, Sign In to add comment