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/engine/graphics/matrix.hpp | 2 +- src/engine/graphics/matrix.tpp | 156 ------------------------------------ src/engine/graphics/matrix_impl.hpp | 156 ++++++++++++++++++++++++++++++++++++ src/engine/matrix_iterator.hpp | 2 +- src/engine/matrix_iterator.tpp | 115 -------------------------- src/engine/matrix_iterator_impl.hpp | 115 ++++++++++++++++++++++++++ src/game/cell_helper.hpp | 2 +- src/game/cell_helper.tpp | 106 ------------------------ src/game/cell_helper_impl.hpp | 106 ++++++++++++++++++++++++ src/util/algorithm.hpp | 2 +- src/util/algorithm.tpp | 57 ------------- src/util/algorithm_impl.hpp | 57 +++++++++++++ src/util/function.hpp | 2 +- src/util/function.tpp | 76 ------------------ src/util/function_impl.hpp | 76 ++++++++++++++++++ src/util/hash.hpp | 2 +- src/util/hash.tpp | 22 ----- src/util/hash_impl.hpp | 22 +++++ 18 files changed, 538 insertions(+), 538 deletions(-) delete mode 100644 src/engine/graphics/matrix.tpp create mode 100644 src/engine/graphics/matrix_impl.hpp delete mode 100644 src/engine/matrix_iterator.tpp create mode 100644 src/engine/matrix_iterator_impl.hpp delete mode 100644 src/game/cell_helper.tpp create mode 100644 src/game/cell_helper_impl.hpp delete mode 100644 src/util/algorithm.tpp create mode 100644 src/util/algorithm_impl.hpp delete mode 100644 src/util/function.tpp create mode 100644 src/util/function_impl.hpp delete mode 100644 src/util/hash.tpp create mode 100644 src/util/hash_impl.hpp (limited to 'src') diff --git a/src/engine/graphics/matrix.hpp b/src/engine/graphics/matrix.hpp index 5a7d893..fb08b29 100644 --- a/src/engine/graphics/matrix.hpp +++ b/src/engine/graphics/matrix.hpp @@ -51,4 +51,4 @@ private: void _copy_matrix_from(const Matrix &source) noexcept; }; -#include "matrix.tpp" +#include "matrix_impl.hpp" diff --git a/src/engine/graphics/matrix.tpp b/src/engine/graphics/matrix.tpp deleted file mode 100644 index 527c61f..0000000 --- a/src/engine/graphics/matrix.tpp +++ /dev/null @@ -1,156 +0,0 @@ -#pragma once - -#include "matrix.hpp" - -template -Matrix::Matrix(const Bounds &bounds) noexcept - : _matrix(new Element *[bounds.get_height()]), - _row_cnt(bounds.get_height()), - _column_cnt(bounds.get_width()) -{ - for (gsl::owner row = _matrix; row != _matrix + _row_cnt; row++) - { - *row = static_cast>(new Element[bounds.get_width()]); - } -}; - -template -Matrix::Matrix(const Matrix &matrix) noexcept - : _matrix(new Element *[matrix._row_cnt]), - _row_cnt(matrix._row_cnt), - _column_cnt(matrix._column_cnt) -{ - _copy_matrix_from(matrix); -} - -template -Matrix::Matrix(Matrix &&matrix) noexcept - : _matrix(matrix._matrix), _row_cnt(matrix._row_cnt), _column_cnt(matrix._column_cnt) -{ - matrix._matrix = nullptr; -} - -template -Matrix::~Matrix() noexcept -{ - _delete_matrix(); -} - -template -void Matrix::fill(Element element) noexcept -{ - for (auto row : *this) - { - for (auto &col : row) - { - col = element; - } - } -} - -template -auto Matrix::get(const Vector2 &pos) const noexcept -> Element -{ - - auto x = static_cast(pos.get_x()); - auto y = static_cast(pos.get_y()); - - // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic) - return _matrix[y][x]; -} - -template -void Matrix::set(const Vector2 &pos, Element element) noexcept -{ - auto x = static_cast(pos.get_x()); - auto y = static_cast(pos.get_y()); - - // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic) - _matrix[y][x] = element; -} - -template -auto Matrix::get_row_cnt() const noexcept -> uint32_t -{ - return _row_cnt; -} - -template -auto Matrix::get_column_cnt() const noexcept -> uint32_t -{ - return _column_cnt; -} - -template -auto Matrix::begin() const noexcept -> MatrixIterator -{ - return MatrixIterator(_matrix, _column_cnt); -} - -template -auto Matrix::end() const noexcept -> MatrixIterator -{ - return MatrixIterator(_matrix + _row_cnt, _column_cnt); -} - -template -auto Matrix::operator=(const Matrix &rhs) noexcept -> Matrix & -{ - if (&rhs != this) - { - _delete_matrix(); - - // NOLINTNEXTLINE(cppcoreguidelines-owning-memory) - _matrix = nullptr; - - _matrix = new Element *[rhs._row_cnt]; - _copy_matrix_from(rhs); - } - - return *this; -} - -template -auto Matrix::operator=(Matrix &&rhs) noexcept -> Matrix & -{ - if (&rhs != this) - { - _delete_matrix(); - - // NOLINTNEXTLINE(cppcoreguidelines-owning-memory) - _matrix = rhs._matrix; - - rhs._matrix = nullptr; - } - - return *this; -} - -template -void Matrix::_delete_matrix() noexcept -{ - for (gsl::owner row = _matrix; row != _matrix + _row_cnt; row++) - { - delete[](*row); - } - - delete[] _matrix; -} - -template -void Matrix::_copy_matrix_from(const Matrix &source) noexcept -{ - // NOLINTNEXTLINE(cppcoreguidelines-owning-memory) - gsl::owner source_row = source._matrix; - - for (gsl::owner row = _matrix; row != _matrix + _row_cnt; - row++, source_row++) - { - *row = static_cast>(new Element[_column_cnt]); - - // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic) - auto *end = *source_row + _column_cnt; - - std::copy(*source_row, end, *row); - } -} diff --git a/src/engine/graphics/matrix_impl.hpp b/src/engine/graphics/matrix_impl.hpp new file mode 100644 index 0000000..527c61f --- /dev/null +++ b/src/engine/graphics/matrix_impl.hpp @@ -0,0 +1,156 @@ +#pragma once + +#include "matrix.hpp" + +template +Matrix::Matrix(const Bounds &bounds) noexcept + : _matrix(new Element *[bounds.get_height()]), + _row_cnt(bounds.get_height()), + _column_cnt(bounds.get_width()) +{ + for (gsl::owner row = _matrix; row != _matrix + _row_cnt; row++) + { + *row = static_cast>(new Element[bounds.get_width()]); + } +}; + +template +Matrix::Matrix(const Matrix &matrix) noexcept + : _matrix(new Element *[matrix._row_cnt]), + _row_cnt(matrix._row_cnt), + _column_cnt(matrix._column_cnt) +{ + _copy_matrix_from(matrix); +} + +template +Matrix::Matrix(Matrix &&matrix) noexcept + : _matrix(matrix._matrix), _row_cnt(matrix._row_cnt), _column_cnt(matrix._column_cnt) +{ + matrix._matrix = nullptr; +} + +template +Matrix::~Matrix() noexcept +{ + _delete_matrix(); +} + +template +void Matrix::fill(Element element) noexcept +{ + for (auto row : *this) + { + for (auto &col : row) + { + col = element; + } + } +} + +template +auto Matrix::get(const Vector2 &pos) const noexcept -> Element +{ + + auto x = static_cast(pos.get_x()); + auto y = static_cast(pos.get_y()); + + // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic) + return _matrix[y][x]; +} + +template +void Matrix::set(const Vector2 &pos, Element element) noexcept +{ + auto x = static_cast(pos.get_x()); + auto y = static_cast(pos.get_y()); + + // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic) + _matrix[y][x] = element; +} + +template +auto Matrix::get_row_cnt() const noexcept -> uint32_t +{ + return _row_cnt; +} + +template +auto Matrix::get_column_cnt() const noexcept -> uint32_t +{ + return _column_cnt; +} + +template +auto Matrix::begin() const noexcept -> MatrixIterator +{ + return MatrixIterator(_matrix, _column_cnt); +} + +template +auto Matrix::end() const noexcept -> MatrixIterator +{ + return MatrixIterator(_matrix + _row_cnt, _column_cnt); +} + +template +auto Matrix::operator=(const Matrix &rhs) noexcept -> Matrix & +{ + if (&rhs != this) + { + _delete_matrix(); + + // NOLINTNEXTLINE(cppcoreguidelines-owning-memory) + _matrix = nullptr; + + _matrix = new Element *[rhs._row_cnt]; + _copy_matrix_from(rhs); + } + + return *this; +} + +template +auto Matrix::operator=(Matrix &&rhs) noexcept -> Matrix & +{ + if (&rhs != this) + { + _delete_matrix(); + + // NOLINTNEXTLINE(cppcoreguidelines-owning-memory) + _matrix = rhs._matrix; + + rhs._matrix = nullptr; + } + + return *this; +} + +template +void Matrix::_delete_matrix() noexcept +{ + for (gsl::owner row = _matrix; row != _matrix + _row_cnt; row++) + { + delete[](*row); + } + + delete[] _matrix; +} + +template +void Matrix::_copy_matrix_from(const Matrix &source) noexcept +{ + // NOLINTNEXTLINE(cppcoreguidelines-owning-memory) + gsl::owner source_row = source._matrix; + + for (gsl::owner row = _matrix; row != _matrix + _row_cnt; + row++, source_row++) + { + *row = static_cast>(new Element[_column_cnt]); + + // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic) + auto *end = *source_row + _column_cnt; + + std::copy(*source_row, end, *row); + } +} diff --git a/src/engine/matrix_iterator.hpp b/src/engine/matrix_iterator.hpp index 674216f..70a59bf 100644 --- a/src/engine/matrix_iterator.hpp +++ b/src/engine/matrix_iterator.hpp @@ -62,4 +62,4 @@ private: const uint32_t &_column_cnt; }; -#include "matrix_iterator.tpp" +#include "matrix_iterator_impl.hpp" diff --git a/src/engine/matrix_iterator.tpp b/src/engine/matrix_iterator.tpp deleted file mode 100644 index 4b2c785..0000000 --- a/src/engine/matrix_iterator.tpp +++ /dev/null @@ -1,115 +0,0 @@ -#pragma once - -#include "matrix_iterator.hpp" - -// Matrix row iterator - -template -MatrixRowIterator::MatrixRowIterator(ColumnPtr column_ptr) noexcept - : _column_ptr(column_ptr) -{ -} - -template -auto MatrixRowIterator::operator++() noexcept -> MatrixRowIterator & -{ - ++_column_ptr; - - return *this; -} - -template -auto MatrixRowIterator::operator++(int) noexcept -> MatrixRowIterator -{ - auto copy = *this; - - ++(*this); - - return copy; -} - -template -auto MatrixRowIterator::operator*() const noexcept -> Element & -{ - return *_column_ptr; -} - -template -auto MatrixRowIterator::operator==(const MatrixRowIterator &rhs) const noexcept - -> bool -{ - return _column_ptr == rhs._column_ptr; -} - -template -auto MatrixRowIterator::operator!=(const MatrixRowIterator &rhs) const noexcept - -> bool -{ - return _column_ptr != rhs._column_ptr; -} - -// Matrix row - -template -MatrixRow::MatrixRow(RowPtr row_ptr, const uint32_t &column_cnt) noexcept - : _row_ptr(row_ptr), _column_cnt(column_cnt) -{ -} - -template -auto MatrixRow::begin() const noexcept -> MatrixRowIterator -{ - return MatrixRowIterator(_row_ptr); -} - -template -auto MatrixRow::end() const noexcept -> MatrixRowIterator -{ - return MatrixRowIterator(_row_ptr + _column_cnt); -} - -// Matrix iterator - -template -MatrixIterator::MatrixIterator( - RowPtr row_ptr, - const uint32_t &column_cnt) noexcept - : _row_ptr(row_ptr), _column_cnt(column_cnt) -{ -} - -template -auto MatrixIterator::operator++() noexcept -> MatrixIterator & -{ - ++_row_ptr; - - return *this; -} - -template -auto MatrixIterator::operator++(int) noexcept -> MatrixIterator -{ - auto copy = *this; - - ++(*this); - - return copy; -} - -template -auto MatrixIterator::operator*() const noexcept -> MatrixRow -{ - return MatrixRow(*_row_ptr, _column_cnt); -} - -template -auto MatrixIterator::operator==(const MatrixIterator &rhs) const noexcept -> bool -{ - return _row_ptr == rhs._row_ptr; -} - -template -auto MatrixIterator::operator!=(const MatrixIterator &rhs) const noexcept -> bool -{ - return _row_ptr != rhs._row_ptr; -} diff --git a/src/engine/matrix_iterator_impl.hpp b/src/engine/matrix_iterator_impl.hpp new file mode 100644 index 0000000..4b2c785 --- /dev/null +++ b/src/engine/matrix_iterator_impl.hpp @@ -0,0 +1,115 @@ +#pragma once + +#include "matrix_iterator.hpp" + +// Matrix row iterator + +template +MatrixRowIterator::MatrixRowIterator(ColumnPtr column_ptr) noexcept + : _column_ptr(column_ptr) +{ +} + +template +auto MatrixRowIterator::operator++() noexcept -> MatrixRowIterator & +{ + ++_column_ptr; + + return *this; +} + +template +auto MatrixRowIterator::operator++(int) noexcept -> MatrixRowIterator +{ + auto copy = *this; + + ++(*this); + + return copy; +} + +template +auto MatrixRowIterator::operator*() const noexcept -> Element & +{ + return *_column_ptr; +} + +template +auto MatrixRowIterator::operator==(const MatrixRowIterator &rhs) const noexcept + -> bool +{ + return _column_ptr == rhs._column_ptr; +} + +template +auto MatrixRowIterator::operator!=(const MatrixRowIterator &rhs) const noexcept + -> bool +{ + return _column_ptr != rhs._column_ptr; +} + +// Matrix row + +template +MatrixRow::MatrixRow(RowPtr row_ptr, const uint32_t &column_cnt) noexcept + : _row_ptr(row_ptr), _column_cnt(column_cnt) +{ +} + +template +auto MatrixRow::begin() const noexcept -> MatrixRowIterator +{ + return MatrixRowIterator(_row_ptr); +} + +template +auto MatrixRow::end() const noexcept -> MatrixRowIterator +{ + return MatrixRowIterator(_row_ptr + _column_cnt); +} + +// Matrix iterator + +template +MatrixIterator::MatrixIterator( + RowPtr row_ptr, + const uint32_t &column_cnt) noexcept + : _row_ptr(row_ptr), _column_cnt(column_cnt) +{ +} + +template +auto MatrixIterator::operator++() noexcept -> MatrixIterator & +{ + ++_row_ptr; + + return *this; +} + +template +auto MatrixIterator::operator++(int) noexcept -> MatrixIterator +{ + auto copy = *this; + + ++(*this); + + return copy; +} + +template +auto MatrixIterator::operator*() const noexcept -> MatrixRow +{ + return MatrixRow(*_row_ptr, _column_cnt); +} + +template +auto MatrixIterator::operator==(const MatrixIterator &rhs) const noexcept -> bool +{ + return _row_ptr == rhs._row_ptr; +} + +template +auto MatrixIterator::operator!=(const MatrixIterator &rhs) const noexcept -> bool +{ + return _row_ptr != rhs._row_ptr; +} 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()}; +} diff --git a/src/util/algorithm.hpp b/src/util/algorithm.hpp index 71a1724..e3d8b6f 100644 --- a/src/util/algorithm.hpp +++ b/src/util/algorithm.hpp @@ -28,4 +28,4 @@ constexpr auto container_filter(const ContainerType &container, Predicate predicate) noexcept -> ContainerType; -#include "algorithm.tpp" +#include "algorithm_impl.hpp" diff --git a/src/util/algorithm.tpp b/src/util/algorithm.tpp deleted file mode 100644 index 00269ed..0000000 --- a/src/util/algorithm.tpp +++ /dev/null @@ -1,57 +0,0 @@ -#pragma once - -#include "algorithm.hpp" - -#include - -template -requires Container -constexpr auto container_find(const ContainerType &container, const Value &value) noexcept - -> typename ContainerType::const_iterator -{ - return std::find(container.begin(), container.end(), value); -} - -template -requires Container -constexpr auto container_has(const ContainerType &container, const Value &value) noexcept - -> bool -{ - return container_find(container, value) != container.end(); -} - -template -requires Container && HasPushBack && - std::predicate -constexpr auto -container_filter(const ContainerType &container, Predicate predicate) noexcept - -> ContainerType -{ - ContainerType filtered_container; - - std::copy_if( - std::begin(container), - std::end(container), - std::back_inserter(filtered_container), - predicate); - - return filtered_container; -} - -template -requires Container && - std::predicate -constexpr auto -container_filter(const ContainerType &container, Predicate predicate) noexcept - -> ContainerType -{ - ContainerType filtered_container; - - std::copy_if( - std::begin(container), - std::end(container), - std::inserter(filtered_container, filtered_container.begin()), - predicate); - - return filtered_container; -} diff --git a/src/util/algorithm_impl.hpp b/src/util/algorithm_impl.hpp new file mode 100644 index 0000000..00269ed --- /dev/null +++ b/src/util/algorithm_impl.hpp @@ -0,0 +1,57 @@ +#pragma once + +#include "algorithm.hpp" + +#include + +template +requires Container +constexpr auto container_find(const ContainerType &container, const Value &value) noexcept + -> typename ContainerType::const_iterator +{ + return std::find(container.begin(), container.end(), value); +} + +template +requires Container +constexpr auto container_has(const ContainerType &container, const Value &value) noexcept + -> bool +{ + return container_find(container, value) != container.end(); +} + +template +requires Container && HasPushBack && + std::predicate +constexpr auto +container_filter(const ContainerType &container, Predicate predicate) noexcept + -> ContainerType +{ + ContainerType filtered_container; + + std::copy_if( + std::begin(container), + std::end(container), + std::back_inserter(filtered_container), + predicate); + + return filtered_container; +} + +template +requires Container && + std::predicate +constexpr auto +container_filter(const ContainerType &container, Predicate predicate) noexcept + -> ContainerType +{ + ContainerType filtered_container; + + std::copy_if( + std::begin(container), + std::end(container), + std::inserter(filtered_container, filtered_container.begin()), + predicate); + + return filtered_container; +} diff --git a/src/util/function.hpp b/src/util/function.hpp index 13f5356..329ad40 100644 --- a/src/util/function.hpp +++ b/src/util/function.hpp @@ -8,4 +8,4 @@ struct Signature template constexpr auto normalize_lambda(Function &&func) noexcept; -#include "function.tpp" +#include "function_impl.hpp" diff --git a/src/util/function.tpp b/src/util/function.tpp deleted file mode 100644 index 9d3d38c..0000000 --- a/src/util/function.tpp +++ /dev/null @@ -1,76 +0,0 @@ -#pragma once - -#include "function.hpp" - -#include - -template -struct remove_cv_seq; - -template -struct remove_cv_seq -{ - using type = Return(Args...); -}; - -template -struct remove_cv_seq -{ - using type = Return(Args...); -}; - -template -struct remove_cv_seq -{ - using type = Return(Args...); -}; - -template -constexpr inline auto extract_signature(Function * /*unused*/) noexcept -{ - return Signature::type>(); -} - -template -constexpr inline auto extract_signature(Function Class::* /*unused*/) noexcept -{ - return Signature::type>(); -} - -template -constexpr inline auto extract_signature(Function const & /*unused*/) noexcept - -> decltype(&Function::operator(), extract_signature(&Function::operator())) -{ - return extract_signature(&Function::operator()); -} - -template -inline auto -get_normalized_lambda(Function &&func, Signature /*unused*/) noexcept -{ - // Static copy of func. This will make it accessible for the lambda without using a - // lamda capture - static auto static_func = Function(std::forward(func)); - - return +[](Args... args) noexcept( - noexcept(std::declval()(std::forward(args)...))) -> Return - { - return static_func(std::forward(args)...); - }; -} - -/** - * Normalizes the type signature of a lambda function. - * - * This will make a lambda with captures passable to other functions. - * - * @param func A lambda function - * @returns The lambda function normalized. - */ -template -constexpr auto normalize_lambda(Function &&func) noexcept -{ - return get_normalized_lambda( - std::forward(func), - extract_signature(std::forward(func))); -} diff --git a/src/util/function_impl.hpp b/src/util/function_impl.hpp new file mode 100644 index 0000000..9d3d38c --- /dev/null +++ b/src/util/function_impl.hpp @@ -0,0 +1,76 @@ +#pragma once + +#include "function.hpp" + +#include + +template +struct remove_cv_seq; + +template +struct remove_cv_seq +{ + using type = Return(Args...); +}; + +template +struct remove_cv_seq +{ + using type = Return(Args...); +}; + +template +struct remove_cv_seq +{ + using type = Return(Args...); +}; + +template +constexpr inline auto extract_signature(Function * /*unused*/) noexcept +{ + return Signature::type>(); +} + +template +constexpr inline auto extract_signature(Function Class::* /*unused*/) noexcept +{ + return Signature::type>(); +} + +template +constexpr inline auto extract_signature(Function const & /*unused*/) noexcept + -> decltype(&Function::operator(), extract_signature(&Function::operator())) +{ + return extract_signature(&Function::operator()); +} + +template +inline auto +get_normalized_lambda(Function &&func, Signature /*unused*/) noexcept +{ + // Static copy of func. This will make it accessible for the lambda without using a + // lamda capture + static auto static_func = Function(std::forward(func)); + + return +[](Args... args) noexcept( + noexcept(std::declval()(std::forward(args)...))) -> Return + { + return static_func(std::forward(args)...); + }; +} + +/** + * Normalizes the type signature of a lambda function. + * + * This will make a lambda with captures passable to other functions. + * + * @param func A lambda function + * @returns The lambda function normalized. + */ +template +constexpr auto normalize_lambda(Function &&func) noexcept +{ + return get_normalized_lambda( + std::forward(func), + extract_signature(std::forward(func))); +} diff --git a/src/util/hash.hpp b/src/util/hash.hpp index 72e9b82..718fa83 100644 --- a/src/util/hash.hpp +++ b/src/util/hash.hpp @@ -7,4 +7,4 @@ constexpr auto GOLDEN_RATIO = 0x9e3779b9; template void hash_combine(std::size_t &seed, const Value &value) noexcept; -#include "hash.tpp" +#include "hash_impl.hpp" diff --git a/src/util/hash.tpp b/src/util/hash.tpp deleted file mode 100644 index 146cfa0..0000000 --- a/src/util/hash.tpp +++ /dev/null @@ -1,22 +0,0 @@ -#pragma once - -#include "hash.hpp" - -#include -#include - -/** - * Combines the hash 'seed' with the hash of 'value'. - * - * @param seed A hash that will be mutated - * @param value A hashable value - */ -template -void hash_combine(std::size_t &seed, const Value &value) noexcept -{ - constexpr uint32_t shift_left = 6; - constexpr uint32_t shift_right = 2; - - seed ^= std::hash()(value) + GOLDEN_RATIO + (seed << shift_left) + - (seed >> shift_right); -} diff --git a/src/util/hash_impl.hpp b/src/util/hash_impl.hpp new file mode 100644 index 0000000..146cfa0 --- /dev/null +++ b/src/util/hash_impl.hpp @@ -0,0 +1,22 @@ +#pragma once + +#include "hash.hpp" + +#include +#include + +/** + * Combines the hash 'seed' with the hash of 'value'. + * + * @param seed A hash that will be mutated + * @param value A hashable value + */ +template +void hash_combine(std::size_t &seed, const Value &value) noexcept +{ + constexpr uint32_t shift_left = 6; + constexpr uint32_t shift_right = 2; + + seed ^= std::hash()(value) + GOLDEN_RATIO + (seed << shift_left) + + (seed >> shift_right); +} -- cgit v1.2.3-18-g5258