From 7578eb6f79afbb421298088ee53da620eb04037f Mon Sep 17 00:00:00 2001 From: HampusM Date: Thu, 2 Jun 2022 20:50:28 +0200 Subject: refactor: rename .tpp files to end with _impl.hpp --- src/game/cell_helper.hpp | 2 +- src/game/cell_helper.tpp | 106 ------------------------------------------ src/game/cell_helper_impl.hpp | 106 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 107 insertions(+), 107 deletions(-) delete mode 100644 src/game/cell_helper.tpp create mode 100644 src/game/cell_helper_impl.hpp (limited to 'src/game') diff --git a/src/game/cell_helper.hpp b/src/game/cell_helper.hpp index cf41c6f..f76497a 100644 --- a/src/game/cell_helper.hpp +++ b/src/game/cell_helper.hpp @@ -31,4 +31,4 @@ private: -> std::list; }; -#include "cell_helper.tpp" +#include "cell_helper_impl.hpp" diff --git a/src/game/cell_helper.tpp b/src/game/cell_helper.tpp deleted file mode 100644 index ebb35aa..0000000 --- a/src/game/cell_helper.tpp +++ /dev/null @@ -1,106 +0,0 @@ -#pragma once - -#include "cell_helper.hpp" - -#include "util/algorithm.hpp" - -template -constexpr auto has_matrix_value( - const IMatrix &matrix, - const MatrixElement &value) noexcept -{ - return [&matrix, &value](const Vector2 &pos) - { - return matrix.get(pos) == value; - }; -} - -template -CellHelper::CellHelper(const IMatrix &matrix) noexcept - : _matrix(matrix) -{ -} - -template -auto CellHelper::is_cell_dying(const Vector2 &cell_pos) const noexcept - -> bool -{ - const auto neighbour_cell_positions = - container_filter(find_neighbours(cell_pos), has_matrix_value(_matrix, 'x')); - - const auto neighbour_cell_cnt = neighbour_cell_positions.size(); - - return neighbour_cell_cnt < 2 || neighbour_cell_cnt >= 4; -} - -template -auto CellHelper::get_birth_cell_positions( - const std::list &cell_positions) const noexcept -> std::list -{ - auto all_empty_neighbour_positions = std::list(); - - for (const auto &cell_pos : cell_positions) - { - const std::list empty_neighbour_positions = - container_filter(find_neighbours(cell_pos), has_matrix_value(_matrix, ' ')); - - all_empty_neighbour_positions.insert( - all_empty_neighbour_positions.end(), - empty_neighbour_positions.begin(), - empty_neighbour_positions.end()); - } - - // Remove duplicates - all_empty_neighbour_positions.sort(); - all_empty_neighbour_positions.unique(); - - auto birth_cell_positions = container_filter( - all_empty_neighbour_positions, - [this](const Vector2 &cell_pos) - { - const auto neighbour_cell_positions = container_filter( - find_neighbours(cell_pos), - has_matrix_value(_matrix, 'x')); - - return neighbour_cell_positions.size() == 3; - }); - - return birth_cell_positions; -} - -template -auto CellHelper::find_neighbours(const Vector2 &cell_pos) const noexcept - -> std::list -{ - std::list cell_positions = {}; - - const auto matrix_size = - Bounds({.width = _matrix.get_column_cnt(), .height = _matrix.get_row_cnt()}); - - const auto neighbours = _get_position_neighbours(cell_pos); - - for (const auto &neighbour_pos : neighbours) - { - if (matrix_size.validate_coords(neighbour_pos) == CoordsValidation::VALID) - { - cell_positions.push_back(neighbour_pos); - } - } - - return cell_positions; -} - -template -auto CellHelper::_get_position_neighbours( - const Vector2 &position) noexcept -> std::list -{ - return { - position + Vector2::up(), - position + Vector2::down(), - position + Vector2::left(), - position + Vector2::right(), - position + Vector2::up() + Vector2::left(), - position + Vector2::up() + Vector2::right(), - position + Vector2::down() + Vector2::left(), - position + Vector2::down() + Vector2::right()}; -} diff --git a/src/game/cell_helper_impl.hpp b/src/game/cell_helper_impl.hpp new file mode 100644 index 0000000..ebb35aa --- /dev/null +++ b/src/game/cell_helper_impl.hpp @@ -0,0 +1,106 @@ +#pragma once + +#include "cell_helper.hpp" + +#include "util/algorithm.hpp" + +template +constexpr auto has_matrix_value( + const IMatrix &matrix, + const MatrixElement &value) noexcept +{ + return [&matrix, &value](const Vector2 &pos) + { + return matrix.get(pos) == value; + }; +} + +template +CellHelper::CellHelper(const IMatrix &matrix) noexcept + : _matrix(matrix) +{ +} + +template +auto CellHelper::is_cell_dying(const Vector2 &cell_pos) const noexcept + -> bool +{ + const auto neighbour_cell_positions = + container_filter(find_neighbours(cell_pos), has_matrix_value(_matrix, 'x')); + + const auto neighbour_cell_cnt = neighbour_cell_positions.size(); + + return neighbour_cell_cnt < 2 || neighbour_cell_cnt >= 4; +} + +template +auto CellHelper::get_birth_cell_positions( + const std::list &cell_positions) const noexcept -> std::list +{ + auto all_empty_neighbour_positions = std::list(); + + for (const auto &cell_pos : cell_positions) + { + const std::list empty_neighbour_positions = + container_filter(find_neighbours(cell_pos), has_matrix_value(_matrix, ' ')); + + all_empty_neighbour_positions.insert( + all_empty_neighbour_positions.end(), + empty_neighbour_positions.begin(), + empty_neighbour_positions.end()); + } + + // Remove duplicates + all_empty_neighbour_positions.sort(); + all_empty_neighbour_positions.unique(); + + auto birth_cell_positions = container_filter( + all_empty_neighbour_positions, + [this](const Vector2 &cell_pos) + { + const auto neighbour_cell_positions = container_filter( + find_neighbours(cell_pos), + has_matrix_value(_matrix, 'x')); + + return neighbour_cell_positions.size() == 3; + }); + + return birth_cell_positions; +} + +template +auto CellHelper::find_neighbours(const Vector2 &cell_pos) const noexcept + -> std::list +{ + std::list cell_positions = {}; + + const auto matrix_size = + Bounds({.width = _matrix.get_column_cnt(), .height = _matrix.get_row_cnt()}); + + const auto neighbours = _get_position_neighbours(cell_pos); + + for (const auto &neighbour_pos : neighbours) + { + if (matrix_size.validate_coords(neighbour_pos) == CoordsValidation::VALID) + { + cell_positions.push_back(neighbour_pos); + } + } + + return cell_positions; +} + +template +auto CellHelper::_get_position_neighbours( + const Vector2 &position) noexcept -> std::list +{ + return { + position + Vector2::up(), + position + Vector2::down(), + position + Vector2::left(), + position + Vector2::right(), + position + Vector2::up() + Vector2::left(), + position + Vector2::up() + Vector2::right(), + position + Vector2::down() + Vector2::left(), + position + Vector2::down() + Vector2::right()}; +} -- cgit v1.2.3-18-g5258