diff options
author | Adrian Kummerlaender | 2017-03-24 22:47:19 +0100 |
---|---|---|
committer | Adrian Kummerlaender | 2017-03-24 23:12:22 +0100 |
commit | c09a4f39f742a94b150cad6a3fb0bbbfbb99e495 (patch) | |
tree | f1ecea6ba17f75af4c71b8a30beeafac09454673 /src/util | |
parent | 23a3e7a41a05ce3e4c8dcc2bc75ce366d8fd8fbb (diff) | |
download | termlife-c09a4f39f742a94b150cad6a3fb0bbbfbb99e495.tar termlife-c09a4f39f742a94b150cad6a3fb0bbbfbb99e495.tar.gz termlife-c09a4f39f742a94b150cad6a3fb0bbbfbb99e495.tar.bz2 termlife-c09a4f39f742a94b150cad6a3fb0bbbfbb99e495.tar.lz termlife-c09a4f39f742a94b150cad6a3fb0bbbfbb99e495.tar.xz termlife-c09a4f39f742a94b150cad6a3fb0bbbfbb99e495.tar.zst termlife-c09a4f39f742a94b150cad6a3fb0bbbfbb99e495.zip |
Switch to toroidial world matrix
i.e. glue together top and bottom respectively right hand side and left hand side borders.
This makes for nicer life behaviour in constrained worlds.
Diffstat (limited to 'src/util')
-rw-r--r-- | src/util/torus_array.h | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/src/util/torus_array.h b/src/util/torus_array.h new file mode 100644 index 0000000..bef0c9f --- /dev/null +++ b/src/util/torus_array.h @@ -0,0 +1,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_ |