diff options
| author | mitaclaw <140017135+mitaclaw@users.noreply.github.com> | 2024-09-29 14:29:06 -0700 |
|---|---|---|
| committer | mitaclaw <140017135+mitaclaw@users.noreply.github.com> | 2025-01-01 09:52:01 -0800 |
| commit | 6f10acea3fee4d08c4e52e66f862da45ad06d1ed (patch) | |
| tree | a11448877c15b08770909dd7ad796a0b99784eca /Source | |
| parent | b8921b133826a19603997737719aa8d7f4b3e7d3 (diff) | |
Common: Create "Contains.h" Algorithm Header
The new `Common::Contains` and `Common::ContainsSubrange` function objects mirror C++23's `std::ranges::contains` and `std::ranges::contains_subrange`, respectively.
Diffstat (limited to 'Source')
| -rw-r--r-- | Source/Core/Common/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | Source/Core/Common/Contains.h | 61 | ||||
| -rw-r--r-- | Source/Core/DolphinLib.props | 1 |
3 files changed, 63 insertions, 0 deletions
diff --git a/Source/Core/Common/CMakeLists.txt b/Source/Core/Common/CMakeLists.txt index 81431e564c..d7262eaa4e 100644 --- a/Source/Core/Common/CMakeLists.txt +++ b/Source/Core/Common/CMakeLists.txt @@ -34,6 +34,7 @@ add_library(common Config/Enums.h Config/Layer.cpp Config/Layer.h + Contains.h CPUDetect.h Crypto/AES.cpp Crypto/AES.h diff --git a/Source/Core/Common/Contains.h b/Source/Core/Common/Contains.h new file mode 100644 index 0000000000..f707cb0f82 --- /dev/null +++ b/Source/Core/Common/Contains.h @@ -0,0 +1,61 @@ +// Copyright 2025 Dolphin Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +#include <algorithm> +#include <iterator> + +namespace Common +{ +struct ContainsFn +{ + template <std::input_iterator I, std::sentinel_for<I> S, class T, class Proj = std::identity> + requires std::indirect_binary_predicate < std::ranges::equal_to, std::projected<I, Proj>, + const T* > constexpr bool operator()(I first, S last, const T& value, Proj proj = {}) const + { + return std::ranges::find(std::move(first), last, value, std::move(proj)) != last; + } + + template <std::ranges::input_range R, class T, class Proj = std::identity> + requires std::indirect_binary_predicate < std::ranges::equal_to, + std::projected<std::ranges::iterator_t<R>, Proj>, + const T* > constexpr bool operator()(R&& r, const T& value, Proj proj = {}) const + { + return (*this)(std::ranges::begin(r), std::ranges::end(r), value, std::move(proj)); + } +}; + +struct ContainsSubrangeFn +{ + template <std::forward_iterator I1, std::sentinel_for<I1> S1, std::forward_iterator I2, + std::sentinel_for<I2> S2, class Pred = std::ranges::equal_to, + class Proj1 = std::identity, class Proj2 = std::identity> + requires std::indirectly_comparable<I1, I2, Pred, Proj1, Proj2> + constexpr bool operator()(I1 first1, S1 last1, I2 first2, S2 last2, Pred pred = {}, + Proj1 proj1 = {}, Proj2 proj2 = {}) const + { + return !std::ranges::search(std::move(first1), std::move(last1), std::move(first2), + std::move(last2), std::move(pred), std::move(proj1), + std::move(proj2)) + .empty(); + } + + template <std::ranges::forward_range R1, std::ranges::forward_range R2, + class Pred = std::ranges::equal_to, class Proj1 = std::identity, + class Proj2 = std::identity> + requires std::indirectly_comparable<std::ranges::iterator_t<R1>, std::ranges::iterator_t<R2>, + Pred, Proj1, Proj2> + constexpr bool operator()(R1&& r1, R2&& r2, Pred pred = {}, Proj1 proj1 = {}, + Proj2 proj2 = {}) const + { + return (*this)(std::ranges::begin(r1), std::ranges::end(r1), std::ranges::begin(r2), + std::ranges::end(r2), std::move(pred), std::move(proj1), std::move(proj2)); + } +}; + +// TODO C++23: Replace with std::ranges::contains. +inline constexpr ContainsFn Contains{}; +// TODO C++23: Replace with std::ranges::contains_subrange. +inline constexpr ContainsSubrangeFn ContainsSubrange{}; +} // namespace Common diff --git a/Source/Core/DolphinLib.props b/Source/Core/DolphinLib.props index a61f0d822a..e41e9fc26a 100644 --- a/Source/Core/DolphinLib.props +++ b/Source/Core/DolphinLib.props @@ -38,6 +38,7 @@ <ClInclude Include="Common\Config\ConfigInfo.h" /> <ClInclude Include="Common\Config\Enums.h" /> <ClInclude Include="Common\Config\Layer.h" /> + <ClInclude Include="Common\Contains.h" /> <ClInclude Include="Common\CPUDetect.h" /> <ClInclude Include="Common\Crypto\AES.h" /> <ClInclude Include="Common\Crypto\bn.h" /> |
