summaryrefslogtreecommitdiff
path: root/src/grid.hpp
blob: 4708811dfaac6e419b87db45b50df21f5503fd25 (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#pragma once

#include <boost/multi_array.hpp>

/**
 * 2D grid of T's.
 */
template <typename T>
struct grid {

private:
	boost::multi_array<T, 2> impl;

public:
	/**
	 * Get reference to tile.
	 */
	T &operator ()(std::size_t x, std::size_t y)
	{
		return impl[y][x];
	}

	/**
	 * Get const reference to tile.
	 */
	T const &operator ()(std::size_t x, std::size_t y) const
	{
		return impl[y][x];
	}

	/**
	 * Resize grid. There is no guarantee whether
	 * existing elements will be preserved or not.
	 */
	void resize(std::size_t width, std::size_t height)
	{
		impl.resize(boost::extents[height][width]);
	}

	/**
	 * Get current width.
	 */
	std::size_t width() const
	{
		return impl.shape()[1];
	}

	/**
	 * Get current height.
	 */
	std::size_t height() const
	{
		return impl.shape()[0];
	}

	/**
	 * Set width. Same caveats apply as for resize().
	 */
	void width(std::size_t width)
	{
		resize(width, height());
	}

	/**
	 * Set height. Same caveats apply as for resize().
	 */
	void height(std::size_t height)
	{
		resize(width(), height);
	}

};