9#include "static_for.hpp"
19namespace templa::algorithms
38 template <
typename T,
typename... Formats, std::size_t N, std::size_t... Is>
39 std::tuple<Formats...>
as_tuple(std::array<T, N>
const &arr, std::index_sequence<Is...>)
41 return std::make_tuple(Formats{arr[Is]}...);
54 template <
typename T,
typename... Formats, std::size_t N>
55 std::tuple<Formats...>
as_tuple(std::array<T, N>
const &arr)
57 return as_tuple<Formats...>(arr, std::make_index_sequence<N>{});
77 template <
typename T1,
typename T2, std::size_t... I>
78 constexpr static auto zip(T1
const &t1, T2
const &t2, std::index_sequence<I...>)
80 return std::make_tuple((std::pair{std::get<I>(t1), std::get<I>(t2)})...);
101 template <
typename... Ts,
typename... Us>
102 requires(
sizeof...(Ts) ==
sizeof...(Us))
103 constexpr static auto zip(std::tuple<Ts...>
const &t1, std::tuple<Us...>
const &t2)
105 return zip(t1, t2, std::make_index_sequence<
sizeof...(Ts)>{});
110 template <
typename T, std::size_t N, std::size_t M, std::size_t... I, std::size_t... J>
111 constexpr auto concat_impl(std::array<T, N>
const &lhs, std::array<T, M>
const &rhs, std::index_sequence<I...>, std::index_sequence<J...>)
113 return std::array<T, N + M>{lhs[I]..., rhs[J]...};
116 template <
typename T, std::
size_t N>
117 consteval static bool exists_until(std::array<T, N>
const &arr,
const T &elem, std::size_t until)
119 for (std::size_t i = 0; i < until; i++)
127 template <
typename T, std::
size_t N>
128 consteval static std::size_t count_unique(std::array<T, N>
const &arr)
131 for (std::size_t i = 0; i < N; i++)
133 if (!internal::exists_until(arr, arr[i], i))
148 template <
auto... Es>
155 using typename identity_type::value_type;
159 *std::min_element(identity_type::identity_value.begin(), identity_type::identity_value.end());
177 using type =
typename decltype(e)::value_type;
180 constexpr static std::size_t
size = e.size();
188 []<std::size_t... I>(std::index_sequence<I...>)
consteval noexcept
191 }(std::make_index_sequence<size>{});
203 template <
auto... Es>
210 using typename identity_type::value_type;
214 *std::max_element(identity_type::identity_value.begin(), identity_type::identity_value.end(),
234 using type =
typename decltype(e)::value_type;
237 constexpr static std::size_t
size = e.size();
245 []<std::size_t... I>(std::index_sequence<I...>)
consteval noexcept
248 }(std::make_index_sequence<size>{});
259 template <
auto... Es>
277 constexpr std::size_t n_unique = internal::count_unique(identity_type::identity_value);
278 std::array<typename identity_type::value_type, n_unique> new_arr{};
280 constexpr auto lam = []<std::size_t... I>(
281 std::array<typename identity_type::value_type, n_unique> &n,
284 constexpr auto &old = identity_type::identity_value;
285 ((!internal::exists_until(old, old[I], I) ? (n[idx++] = old[I], void()) : void()), ...);
287 static_for<identity_type::size>(lam, new_arr, idx);
312 using old_array_type = std::array<typename forwarded_type::type, forwarded_type::size>;
328 constexpr auto lam = []<std::size_t... I>(
329 const old_array_type &o,
333 ((!internal::exists_until(old, old[I], I) ? (n[idx++] = old[I], void()) : void()), ...);
335 static_for<old.size()>(lam, old, new_arr, idx);
348 template <
auto... elems>
367 ((out[I] = in[in.size() - I - 1]), ...);
370 std::array<int, identity_type::size> ret{};
371 static_for<identity_type::size>(lam, identity_type::identity_value, ret);
401 constexpr decltype(a) ret{old[a.size() - I - 1]...};
403 }(std::make_index_sequence<a.size()>{});
421 template <
typename T, std::
size_t N, std::
size_t M>
422 constexpr static auto concat(
const std::array<T, N> &lhs,
const std::array<T, M> &rhs)
424 return internal::concat_impl(lhs, rhs, std::make_index_sequence<N>{}, std::make_index_sequence<M>{});
438 template <
const std::string_view &...Strs>
448 constexpr static auto arr = []()
consteval
450 constexpr std::size_t length = (Strs.size() + ... + 0);
451 std::array<char, length + 1> arr;
452 auto Joiner = [i = 0, &arr](
const std::string_view &s)
consteval mutable
468 constexpr static std::string_view
value = {arr.data(), arr.size() - 1};
476 template <std::string_view
const &...Strs>
477 constexpr static std::string_view join_v = join<Strs...>
::value;
489 template <
typename T,
typename O>
490 void flatten(
const std::vector<T> &in, std::vector<O> &out)
492 out.insert(out.end(), in.begin(), in.end());
505 template <
typename T,
typename O>
506 void flatten(
const std::vector<std::vector<T>> &in, std::vector<O> &out)
508 for (
const auto &e : in)
529 template <
typename Callable,
typename... Ts>
530 constexpr static void apply_to_tuple_cat(Callable &&c, Ts &&...tuples)
532 auto cat = std::tuple_cat<Ts...>(std::forward<Ts>(tuples)...);
533 std::apply(std::forward<Callable>(c), std::move(cat));
void flatten(const std::vector< T > &in, std::vector< O > &out)
Flattens a single-level std::vector<T> into another vector.
Definition algorithms.hpp:490
std::tuple< Formats... > as_tuple(std::array< T, N > const &arr, std::index_sequence< Is... >)
Converts a fixed-size array into a tuple using specified format types.
Definition algorithms.hpp:39
Concept to check if a type supports common comparison operators.
Definition concepts.hpp:57
Concept that models an STL-like container.
Definition concepts.hpp:245
Internal utilities and implementation details.
Compile-time string concatenation of multiple std::string_view references.
Definition algorithms.hpp:440
static constexpr std::string_view value
The resulting joined string as a std::string_view.
Definition algorithms.hpp:468
Computes the maximum value from a container-like object at compile time.
Definition algorithms.hpp:231
static constexpr std::size_t size
The number of elements in the container.
Definition algorithms.hpp:237
static constexpr auto value
The maximum value computed at compile time from the container.
Definition algorithms.hpp:244
typename decltype(e)::value_type type
The type of the elements in the container.
Definition algorithms.hpp:234
Computes the maximum value among a pack of compile-time constants.
Definition algorithms.hpp:205
typename templa::internal::uniform_element_identity< Es... > identity_type
The type that provides the uniform container of values.
Definition algorithms.hpp:207
static constexpr value_type value
The maximum value among the compile-time constants.
Definition algorithms.hpp:213
Computes the minimum value from a container-like object at compile time.
Definition algorithms.hpp:174
typename decltype(e)::value_type type
The type of the elements in the container.
Definition algorithms.hpp:177
static constexpr std::size_t size
The size of the container.
Definition algorithms.hpp:180
static constexpr auto value
The minimum value computed at compile time from the container.
Definition algorithms.hpp:187
Computes the minimum value among a pack of compile-time constants.
Definition algorithms.hpp:150
typename templa::internal::uniform_element_identity< Es... > identity_type
Alias for the identity type that represents the uniform compile-time values.
Definition algorithms.hpp:152
static constexpr value_type value
The minimum value among the compile-time constants.
Definition algorithms.hpp:158
Reverses the elements of a constexpr container at compile time.
Definition algorithms.hpp:388
static constexpr auto reverse_sequence
The reversed version of the input container.
Definition algorithms.hpp:399
Reverses a compile-time pack of constant values.
Definition algorithms.hpp:350
static constexpr auto reverse_sequence
The reversed array, computed at compile time.
Definition algorithms.hpp:363
typename templa::internal::uniform_element_identity< elems... > identity_type
The identity type wrapping the parameter pack.
Definition algorithms.hpp:352
typename identity_type::uniform_type array_type
The array type representing the uniform values.
Definition algorithms.hpp:355
Computes a compile-time unique sequence from a container expression.
Definition algorithms.hpp:306
static constexpr auto unique_sequence
The resulting array with unique elements from the input container.
Definition algorithms.hpp:324
Computes a compile-time unique sequence from a pack of values.
Definition algorithms.hpp:261
typename templa::internal::uniform_element_identity< Es... > identity_type
Type containing the uniform array of input values.
Definition algorithms.hpp:263
static constexpr auto unique_sequence
The resulting array with unique elements from the input pack.
Definition algorithms.hpp:275
typename identity_type::uniform_type old_array_type
The type of the original uniform array.
Definition algorithms.hpp:266
Extract elements from a container-like template parameter as a new container.
Definition pack.hpp:95
decltype(a)::value_type type
Value type of the container.
Definition pack.hpp:97
static constexpr auto value
Compile-time generated container with the elements of a. Uses index_sequence to access each element.
Definition pack.hpp:103
std::integral_constant< std::size_t, x > value
Integral constant wrapper for a size_t value.
Definition type_list.hpp:242