C++ Essence Library 0.1.0
A Utility Library for Modern C++ Programming
Loading...
Searching...
No Matches
tuple.hpp
1/*
2 * Copyright (c) 2024 The RefValue Project
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23#pragma once
24
25#include <concepts>
26#include <cstddef>
27#include <limits>
28#include <tuple>
29#include <type_traits>
30#include <utility>
31
32namespace essence {
38 template <auto GetTuple, auto Predicate>
39 struct filter_tuple {
40 static constexpr auto tuple = GetTuple();
41
42 template <std::size_t I>
43 static consteval auto do_single() noexcept {
44 constexpr auto value = std::get<I>(tuple);
45
46 if constexpr (Predicate(value)) {
47 return std::tuple{value};
48 } else {
49 return std::tuple<>{};
50 }
51 }
52
53 static consteval auto do_filter() noexcept {
54 return []<std::size_t... Is>(std::index_sequence<Is...>) { return std::tuple_cat(do_single<Is>()...); }(
55 std::make_index_sequence<std::tuple_size_v<decltype(tuple)>>{});
56 }
57
58 static constexpr auto value = do_filter();
59 };
60
66 template <auto GetTuple, auto Predicate>
67 inline constexpr auto filter_tuple_v = filter_tuple<GetTuple, Predicate>::value;
68
77 template <typename... Ts, std::predicate<Ts...> Callable>
78 constexpr std::size_t get_index_if(const std::tuple<Ts...>& tuple, Callable&& predicate) noexcept(
79 (noexcept(std::declval<Callable>()(std::declval<Ts>())) && ...)) {
80 return [&]<std::size_t... Is>(std::index_sequence<Is...>) {
81 auto index = std::numeric_limits<std::size_t>::max();
82
83 auto handler = [&]<std::size_t I>(std::integral_constant<std::size_t, I>) {
84 if (index == std::numeric_limits<std::size_t>::max()
85 && std::forward<Callable>(predicate)(std::get<I>(tuple))) {
86 index = I;
87 }
88 };
89
90 (handler(std::integral_constant<std::size_t, Is>{}), ...);
91
92 return index;
93 }(std::make_index_sequence<sizeof...(Ts)>{});
94 }
95} // namespace essence
Filters a tuple given a predicate.
Definition tuple.hpp:39