aboutsummaryrefslogtreecommitdiff
path: root/src/util/torus_array.h
blob: bef0c9f90610e7319f9290988acd99493e517de1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#ifndef LIFE_SRC_UTIL_TORUS_ARRAY_
#define LIFE_SRC_UTIL_TORUS_ARRAY_

#include <array>
#include <cstdint>

namespace life {
namespace util {

template<
	typename    TYPE,
	std::size_t WIDTH,
	std::size_t HEIGHT
>
class TorusArray {
	static std::size_t toMatrixColumn(const std::ptrdiff_t x) {
		if ( x >= 0 ) {
			return x % WIDTH;
		} else {
			return WIDTH - ( std::abs(x) % WIDTH );
		}
	}

	static std::size_t toMatrixRow(const std::ptrdiff_t y) {
		if ( y >= 0 ) {
			return y % HEIGHT;
		} else {
			return HEIGHT - ( std::abs(y) % HEIGHT );
		}
	}

	public:
		static const std::size_t width  = WIDTH;
		static const std::size_t height = HEIGHT;

		TYPE get(const std::ptrdiff_t x, const std::ptrdiff_t y) const {
			return this->matrix_[toMatrixRow(y)][toMatrixColumn(x)];
		}

		void set(const std::ptrdiff_t x, const std::ptrdiff_t y, TYPE&& value) {
			this->matrix_[toMatrixRow(y)][toMatrixColumn(x)] = value;
		}
	
	private:
		std::array<std::array<TYPE, WIDTH>, HEIGHT> matrix_;

};

}
}

#endif  // LIFE_SRC_UTIL_TORUS_ARRAY_