From eea082ffd1a3ecf73b7c464cc28da5ef74d3a30f Mon Sep 17 00:00:00 2001 From: Bardur Arantsson Date: Sat, 17 Sep 2016 09:58:15 +0200 Subject: Add grid<> template class for representing 2D grids --- src/CMakeLists.txt | 1 + src/grid.hpp | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ tests/grid.cc | 52 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 125 insertions(+) create mode 100644 src/grid.hpp create mode 100644 tests/grid.cc diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 7d64a66f..bf970c59 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -122,6 +122,7 @@ SET(SRCS_TESTS ../tests/harness.cc ../tests/lua_get_level.cc ../tests/flag_set.cc + ../tests/grid.cc ) ADD_LIBRARY(game diff --git a/src/grid.hpp b/src/grid.hpp new file mode 100644 index 00000000..4708811d --- /dev/null +++ b/src/grid.hpp @@ -0,0 +1,72 @@ +#pragma once + +#include + +/** + * 2D grid of T's. + */ +template +struct grid { + +private: + boost::multi_array 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); + } + +}; diff --git a/tests/grid.cc b/tests/grid.cc new file mode 100644 index 00000000..ff618088 --- /dev/null +++ b/tests/grid.cc @@ -0,0 +1,52 @@ +#include "grid.hpp" +#include +using namespace bandit; + +go_bandit([]() { + describe("grid<>", []() { + + auto w0 = size_t { 123 }; + auto h0 = size_t { 42 }; + + it("width(...) properly sets returned width", [&](){ + // Setup + grid g; + // Exercise + g.width(w0); + // Verify + AssertThat(g.width(), Equals(w0)); + }); + + it("height(...) properly sets returned height", [&](){ + // Setup + grid g; + // Exercise + g.height(h0); + // Verify + AssertThat(g.height(), Equals(h0)); + }); + + it("resize(w, h) properly sets returned width and height", [&](){ + // Setup + grid g; + // Exercise + g.resize(w0, h0); + // Verify + AssertThat(g.width(), Equals(w0)); + AssertThat(g.height(), Equals(h0)); + }); + + it("operator () can access ((w-1), (h-1)) element", [&](){ + // Class with 'magic' default value + struct X { int magic = 1001; }; + // Setup + grid g; + g.resize(w0 + 1, h0 + 1); + // Exercise + auto x = g(w0, h0); + // Verify + AssertThat(x.magic, Equals(1001)); + }); + + }); +}); -- cgit v1.2.3