From 7e3ea6b0080b2c9127964a096a27369d8cc59c67 Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Sun, 26 Mar 2017 15:38:58 +0200 Subject: Rename `TorusArray` to `TorusMatrix` to better fit the represented structure --- life.cc | 4 ++-- src/util/torus_array.h | 52 ------------------------------------------------- src/util/torus_matrix.h | 52 +++++++++++++++++++++++++++++++++++++++++++++++++ src/world.h | 4 ++-- 4 files changed, 56 insertions(+), 56 deletions(-) delete mode 100644 src/util/torus_array.h create mode 100644 src/util/torus_matrix.h diff --git a/life.cc b/life.cc index a60667e..557ece4 100644 --- a/life.cc +++ b/life.cc @@ -30,10 +30,10 @@ void draw( } }); - util::print_tb(1, 1, TB_WHITE, TB_DEFAULT, "Age:"); + util::print_tb(1, 1, TB_WHITE, TB_DEFAULT, "Age:"); util::print_tb(13, 1, TB_WHITE, TB_DEFAULT, std::to_string(world.getAge())); - util::print_tb(1, 2, TB_WHITE, TB_DEFAULT, "Population:"); + util::print_tb(1, 2, TB_WHITE, TB_DEFAULT, "Population:"); util::print_tb(13, 2, TB_WHITE, TB_DEFAULT, std::to_string(world.getPopulation())); tb_present(); diff --git a/src/util/torus_array.h b/src/util/torus_array.h deleted file mode 100644 index bef0c9f..0000000 --- a/src/util/torus_array.h +++ /dev/null @@ -1,52 +0,0 @@ -#ifndef LIFE_SRC_UTIL_TORUS_ARRAY_ -#define LIFE_SRC_UTIL_TORUS_ARRAY_ - -#include -#include - -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, HEIGHT> matrix_; - -}; - -} -} - -#endif // LIFE_SRC_UTIL_TORUS_ARRAY_ diff --git a/src/util/torus_matrix.h b/src/util/torus_matrix.h new file mode 100644 index 0000000..d09b99e --- /dev/null +++ b/src/util/torus_matrix.h @@ -0,0 +1,52 @@ +#ifndef LIFE_SRC_UTIL_TORUS_MATRIX_ +#define LIFE_SRC_UTIL_TORUS_MATRIX_ + +#include +#include + +namespace life { +namespace util { + +template< + typename TYPE, + std::size_t WIDTH, + std::size_t HEIGHT +> +class TorusMatrix { + 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, HEIGHT> matrix_; + +}; + +} +} + +#endif // LIFE_SRC_UTIL_TORUS_MATRIX_ diff --git a/src/world.h b/src/world.h index a09eb5a..a7a68a0 100644 --- a/src/world.h +++ b/src/world.h @@ -6,7 +6,7 @@ #include #include "util/box_traverser.h" -#include "util/torus_array.h" +#include "util/torus_matrix.h" namespace life { @@ -117,9 +117,9 @@ class World { private: const util::BoxTraverser area_; + util::TorusMatrix matrix_; std::size_t age_{}; std::size_t population_{}; - util::TorusArray matrix_; }; -- cgit v1.2.3