From eecf4b1e666211a13afa56f93477c55e8fd01621 Mon Sep 17 00:00:00 2001 From: HampusM Date: Thu, 2 Jun 2022 19:51:54 +0200 Subject: feat: implement game of life --- src/util/algorithm.hpp | 31 ++++++++++++++++++++ src/util/algorithm.tpp | 57 ++++++++++++++++++++++++++++++++++++ src/util/concepts.hpp | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 166 insertions(+) create mode 100644 src/util/algorithm.hpp create mode 100644 src/util/algorithm.tpp create mode 100644 src/util/concepts.hpp (limited to 'src/util') diff --git a/src/util/algorithm.hpp b/src/util/algorithm.hpp new file mode 100644 index 0000000..71a1724 --- /dev/null +++ b/src/util/algorithm.hpp @@ -0,0 +1,31 @@ +#pragma once + +#include "util/concepts.hpp" + +#include + +template +requires Container +constexpr auto container_find(const ContainerType &container, const Value &value) noexcept + -> typename ContainerType::const_iterator; + +template +requires Container +constexpr auto container_has(const ContainerType &container, const Value &value) noexcept + -> bool; + +template +requires Container && HasPushBack && + std::predicate +constexpr auto +container_filter(const ContainerType &container, Predicate predicate) noexcept + -> ContainerType; + +template +requires Container && + std::predicate +constexpr auto +container_filter(const ContainerType &container, Predicate predicate) noexcept + -> ContainerType; + +#include "algorithm.tpp" diff --git a/src/util/algorithm.tpp b/src/util/algorithm.tpp new file mode 100644 index 0000000..00269ed --- /dev/null +++ b/src/util/algorithm.tpp @@ -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/concepts.hpp b/src/util/concepts.hpp new file mode 100644 index 0000000..928ee39 --- /dev/null +++ b/src/util/concepts.hpp @@ -0,0 +1,78 @@ +#pragma once + +#include +#include + +/** + * Concept for the Container named requirement. + * + * https://en.cppreference.com/w/cpp/named_req/Container + */ +template +concept Container = requires(ContainerType container_a, const ContainerType container_b) +{ + typename ContainerType::value_type; + typename ContainerType::reference; + typename ContainerType::const_reference; + typename ContainerType::iterator; + typename ContainerType::const_iterator; + typename ContainerType::difference_type; + typename ContainerType::size_type; + + requires std::regular; + requires std::swappable; + requires std::destructible; + requires std:: + same_as; + requires std::same_as< + typename ContainerType::const_reference, + const typename ContainerType::value_type &>; + requires std::forward_iterator; + requires std::forward_iterator; + requires std::signed_integral; + requires std::same_as< + typename ContainerType::difference_type, + typename std::iterator_traits::difference_type>; + requires std::same_as< + typename ContainerType::difference_type, + typename std::iterator_traits< + typename ContainerType::const_iterator>::difference_type>; + requires std::convertible_to< + typename ContainerType::iterator, + typename ContainerType::const_iterator>; + + { + container_a.begin() + } -> std::same_as; + { + container_a.end() + } -> std::same_as; + { + container_b.begin() + } -> std::same_as; + { + container_b.end() + } -> std::same_as; + { + container_a.cbegin() + } -> std::same_as; + { + container_a.cend() + } -> std::same_as; + { + container_a.size() + } -> std::same_as; + { + container_a.max_size() + } -> std::same_as; + { + container_a.empty() + } -> std::same_as; +}; + +template +concept HasPushBack = + requires(ContainerType container, typename ContainerType::value_type value) +{ + container.push_back(value); +}; -- cgit v1.2.3-18-g5258