SyndicatQuebecoisdelaConstr.../SQCSim2021/array2d.h

62 lines
1.4 KiB
C
Raw Normal View History

2021-10-11 11:37:58 -04:00
#ifndef ARRAY2D_H__
#define ARRAY2D_H__
#include "define.h"
template <class T>
2021-10-19 14:29:06 -04:00
class Array2d {
public:
Array2d(int x, int y);
~Array2d();
Array2d(const Array2d& array);
2021-10-11 11:37:58 -04:00
2021-10-19 14:29:06 -04:00
void Set(int x, int y, T type);
T Get(int x, int y) const;
2021-12-03 11:49:59 -05:00
T Remove(int x, int y);
2021-10-11 11:37:58 -04:00
2021-10-19 14:29:06 -04:00
void Reset(T type);
2021-10-11 11:37:58 -04:00
2021-10-19 14:29:06 -04:00
private:
int m_x, m_y;
T* m_array;
2021-10-11 11:37:58 -04:00
2021-10-19 14:29:06 -04:00
int To1dIndex(int x, int y) const;
2021-10-11 11:37:58 -04:00
};
template <class T>
Array2d<T>::Array2d(int x, int y) : m_x(x), m_y(y) { m_array = new T[m_x * m_y]; }
2021-10-11 11:37:58 -04:00
template <class T>
Array2d<T>::~Array2d() { delete[] m_array; }
template <class T>
Array2d<T>::Array2d(const Array2d<T>& array) : m_x(array.m_x), m_y(array.m_y) {
m_array = new T[m_x * m_y];
2021-10-11 11:37:58 -04:00
for (int i = 0; i < m_x * m_y; ++i)
m_array[i] = array.m_array[i];
}
template <class T>
2021-10-19 14:29:06 -04:00
void Array2d<T>::Set(int x, int y, T type) { m_array[To1dIndex(x, y)] = type; }
2021-10-11 11:37:58 -04:00
template <class T>
2021-10-19 14:29:06 -04:00
T Array2d<T>::Get(int x, int y) const { return m_array[To1dIndex(x, y)]; }
2021-10-11 11:37:58 -04:00
2021-12-03 11:49:59 -05:00
template <class T>
T Array2d<T>::Remove(int x, int y) {
T thing = std::move(m_array[To1dIndex(x, y)]);
m_array[To1dIndex(x, y)] = nullptr;
return thing;
}
2021-10-11 11:37:58 -04:00
template <class T>
void Array2d<T>::Reset(T type) {
for (int i = 0; i < m_x * m_y; ++i)
m_array[i] = type;
}
template <class T>
2021-10-19 14:29:06 -04:00
int Array2d<T>::To1dIndex(int x, int y) const { return x + (y * m_x); }
2021-10-11 11:37:58 -04:00
#endif // ARRAY2D_H__