aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorswarmer <swarmer@yandex-team.com>2024-08-08 00:09:18 +0300
committerswarmer <swarmer@yandex-team.com>2024-08-08 00:34:39 +0300
commit38cff6773bff060c5772a03ad45f5d4b6ef8afe1 (patch)
tree5950c3458746a202499b9dfa471149a5c0b1d5bf
parent464addc69515ab37bf1f3ecb46d7b802c259f4eb (diff)
downloadydb-38cff6773bff060c5772a03ad45f5d4b6ef8afe1.tar.gz
util: simpler implementation of the `EqualToOneOf` and `CountOf` algorithms using the fold expression
62d55507536a408dd9c37a5497efaa0a1333cc6c
-rw-r--r--util/generic/algorithm.h22
1 files changed, 6 insertions, 16 deletions
diff --git a/util/generic/algorithm.h b/util/generic/algorithm.h
index bf32a19643..8286275e79 100644
--- a/util/generic/algorithm.h
+++ b/util/generic/algorithm.h
@@ -208,24 +208,14 @@ constexpr size_t FindIndexIf(C&& c, P p) {
}
//EqualToOneOf(x, "apple", "orange") means (x == "apple" || x == "orange")
-template <typename T>
-constexpr bool EqualToOneOf(const T&) {
- return false;
+template <typename T, typename... Other>
+constexpr bool EqualToOneOf(const T& x, const Other&... values) {
+ return (... || (x == values));
}
-template <typename T, typename U, typename... Other>
-constexpr bool EqualToOneOf(const T& x, const U& y, const Other&... other) {
- return x == y || EqualToOneOf(x, other...);
-}
-
-template <typename T>
-constexpr size_t CountOf(const T&) {
- return 0;
-}
-
-template <typename T, typename U, typename... Other>
-constexpr size_t CountOf(const T& x, const U& y, const Other&... other) {
- return static_cast<size_t>(x == y) + CountOf(x, other...);
+template <typename T, typename... Other>
+constexpr size_t CountOf(const T& x, const Other&... values) {
+ return (0 + ... + static_cast<size_t>(x == values));
}
template <class I>