tuples.hh 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. // -*- mode: c++; coding: utf-8 -*-
  2. // ra-ra - Tuple library.
  3. // (c) Daniel Llorens - 2005-2023
  4. // This library is free software; you can redistribute it and/or modify it under
  5. // the terms of the GNU Lesser General Public License as published by the Free
  6. // Software Foundation; either version 3 of the License, or (at your option) any
  7. // later version.
  8. #pragma once
  9. #include <tuple>
  10. #include <limits>
  11. #include <algorithm>
  12. #include "macros.hh"
  13. namespace ra {
  14. template <class T> constexpr bool is_constant = false;
  15. template <class T, T N> constexpr bool is_constant<std::integral_constant<T, N>> = true;
  16. template <int V> using int_c = std::integral_constant<int, V>;
  17. template <bool V> using bool_c = std::integral_constant<bool, V>;
  18. template <auto V> using ic_t = std::integral_constant<decltype(V), V>;
  19. template <auto V> constexpr std::integral_constant<decltype(V), V> ic {};
  20. template <class ... T> constexpr bool always_false = false; // p2593r0
  21. } // namespace ra
  22. namespace ra::mp {
  23. // xxx<...> is user facing and xxx_<...>::type (if needed) is implementation.
  24. using std::tuple;
  25. using nil = tuple<>;
  26. template <class T> constexpr bool nilp = std::is_same_v<nil, T>;
  27. template <class A> constexpr int len = std::tuple_size_v<A>;
  28. template <int ... I> using int_list = tuple<int_c<I> ...>;
  29. template <class T> constexpr bool is_tuple = false;
  30. template <class ... A> constexpr bool is_tuple<tuple<A ...>> = true;
  31. template <class A, class B> struct cons_ { static_assert(is_tuple<B>); };
  32. template <class A0, class ... A> struct cons_<A0, tuple<A ...>> { using type = tuple<A0, A ...>; };
  33. template <class A, class B> using cons = typename cons_<A, B>::type;
  34. template <class A, class B> struct append_ { static_assert(is_tuple<A> && is_tuple<B>); };
  35. template <class ... A, class ... B> struct append_<tuple<A ...>, tuple<B ...>> { using type = tuple<A ..., B ...>; };
  36. template <class A, class B> using append = typename append_<A, B>::type;
  37. template <class A, class B> struct zip_ { static_assert(is_tuple<A> && is_tuple<B>); };
  38. template <class ... A, class ... B> struct zip_<tuple<A ...>, tuple<B ...>> { using type = tuple<tuple<A, B> ...>; };
  39. template <class A, class B> using zip = typename zip_<A, B>::type;
  40. template <int n, int o=0, int s=1> struct iota_ { static_assert(n>0); using type = cons<int_c<o>, typename iota_<n-1, o+s, s>::type>; };
  41. template <int o, int s> struct iota_<0, o, s> { using type = nil; };
  42. template <int n, int o=0, int s=1> using iota = typename iota_<n, o, s>::type;
  43. template <int n, class T> struct makelist_ { static_assert(n>0); using type = cons<T, typename makelist_<n-1, T>::type>; };
  44. template <class T> struct makelist_<0, T> { using type = nil; };
  45. template <int n, class T> using makelist = typename makelist_<n, T>::type;
  46. // A is a nested list, I the indices at each level.
  47. template <class A, int ... I> struct ref_ { using type = A; };
  48. template <class A, int ... I> using ref = typename ref_<A, I ...>::type;
  49. template <class A, int I0, int ... I> struct ref_<A, I0, I ...> { using type = ref<std::tuple_element_t<I0, A>, I ...>; };
  50. template <class A> using first = ref<A, 0>;
  51. template <class A> using last = ref<A, (len<A> - 1)>;
  52. template <bool a> using when = bool_c<a>;
  53. template <bool a> using unless = bool_c<(!a)>;
  54. // Return the index of a type in a type list, or -1 if not found.
  55. template <class A, class T, int i=0> struct index_ { using type = int_c<-1>; };
  56. template <class A, class T, int i=0> using index = typename index_<A, T, i>::type;
  57. template <class ... A, class T, int i> struct index_<tuple<T, A ...>, T, i> { using type = int_c<i>; };
  58. template <class A0, class ... A, class T, int i> struct index_<tuple<A0, A ...>, T, i> { using type = index<tuple<A ...>, T, i+1>; };
  59. // Index (& type) of the 1st item for which Pred<> is true, or -1 (& nil).
  60. template <class A, template <class> class Pred, int i=0>
  61. struct IndexIf
  62. {
  63. constexpr static int value = -1;
  64. using type = nil;
  65. };
  66. template <class A0, class ... A, template <class> class Pred, int i>
  67. requires (Pred<A0>::value)
  68. struct IndexIf<tuple<A0, A ...>, Pred, i>
  69. {
  70. using type = A0;
  71. constexpr static int value = i;
  72. };
  73. template <class A0, class ... A, template <class> class Pred, int i>
  74. requires (!(Pred<A0>::value))
  75. struct IndexIf<tuple<A0, A ...>, Pred, i>
  76. {
  77. using next = IndexIf<tuple<A ...>, Pred, i+1>;
  78. using type = typename next::type;
  79. constexpr static int value = next::value;
  80. };
  81. // Index (& type) of pairwise winner. A variant of fold.
  82. template <template <class A, class B> class pick_i, class T, int k=1, int sel=0> struct indexof_;
  83. template <template <class A, class B> class pick_i, class T0, int k, int sel>
  84. struct indexof_<pick_i, tuple<T0>, k, sel>
  85. {
  86. constexpr static int value = sel;
  87. using type = T0;
  88. };
  89. template <template <class A, class B> class pick_i, class T0, class T1, class ... Ti, int k, int sel>
  90. struct indexof_<pick_i, tuple<T0, T1, Ti ...>, k, sel>
  91. {
  92. constexpr static int i = pick_i<std::decay_t<T0>, std::decay_t<T1>>::value;
  93. using next = indexof_<pick_i, tuple<std::conditional_t<i==0, T0, T1>, Ti ...>, k+1, i==0 ? sel : k>;
  94. using type = typename next::type;
  95. constexpr static int value = next::value;
  96. };
  97. template <template <class A, class B> class pick_i, class T>
  98. constexpr int indexof = indexof_<pick_i, T>::value;
  99. // Return the first tail of A headed by Val, like find-tail.
  100. template <class A, class Val> struct findtail_;
  101. template <class A, class Val> using findtail = typename findtail_<A, Val>::type;
  102. template <class Val> struct findtail_<nil, Val> { using type = nil; };
  103. template <class ... A, class Val> struct findtail_<tuple<Val, A ...>, Val> { using type = tuple<Val, A ...>; };
  104. template <class A0, class ... A, class Val> struct findtail_<tuple<A0, A ...>, Val> { using type = findtail<tuple<A ...>, Val>; };
  105. // Reverse list. See TSPL^3, p. 137.
  106. template <class A, class B=nil> struct reverse_ { using type = B; };
  107. template <class A, class B=nil> using reverse = typename reverse_<A, B>::type;
  108. template <class A0, class ... A, class B> struct reverse_<tuple<A0, A ...>, B> { using type = reverse<tuple<A ...>, cons<A0, B>>; };
  109. // drop1 is needed to avoid ambiguity in the declarations of drop, take.
  110. template <class A> struct drop1_;
  111. template <class A0, class ... A> struct drop1_<tuple<A0, A ...>> { using type = tuple<A ...>; };
  112. template <class A> using drop1 = typename drop1_<A>::type;
  113. template <class A, int n> struct drop_ { static_assert(n>0); using type = typename drop_<drop1<A>, n-1>::type; };
  114. template <class A> struct drop_<A, 0> { using type = A; };
  115. template <class A, int n> using drop = typename drop_<A, n>::type;
  116. template <class A, int n> struct take_ { static_assert(n>0); using type = cons<first<A>, typename take_<drop1<A>, n-1>::type>; };
  117. template <class A> struct take_<A, 0> { using type = nil; };
  118. template <class A, int n> using take = typename take_<A, n>::type;
  119. template <template <class ... A> class F, class L> struct apply_;
  120. template <template <class ... A> class F, class ... L> struct apply_<F, tuple<L ...>> { using type = F<L ...>; };
  121. template <template <class ... A> class F, class L> using apply = typename apply_<F, L>::type;
  122. // As map.
  123. template <template <class ... A> class F, class ... L>
  124. struct map_ { using type = cons<F<first<L> ...>, typename map_<F, drop1<L> ...>::type>; };
  125. template <template <class ... A> class F, class ... L>
  126. struct map_<F, nil, L ...> { using type = nil; };
  127. template <template <class ... A> class F>
  128. struct map_<F> { using type = nil; };
  129. template <template <class ... A> class F, class ... L> using map = typename map_<F, L ...>::type;
  130. template <class A, class B> struct Filter
  131. {
  132. using type = mp::append<std::conditional_t<mp::first<A>::value, mp::take<B, 1>, mp::nil>,
  133. typename Filter<mp::drop1<A>, mp::drop1<B>>::type>;
  134. };
  135. template <class B> struct Filter<mp::nil, B> { using type = B; };
  136. template <class A, class B> using Filter_ = typename Filter<A, B>::type;
  137. // As SRFI-1 fold (= fold-left).
  138. template <template <class ... A> class F, class Def, class ... L>
  139. struct fold_
  140. {
  141. using def = std::conditional_t<std::is_same_v<void, Def>, F<>, Def>;
  142. using type = typename fold_<F, F<def, first<L> ...>, drop1<L> ...>::type;
  143. };
  144. template <template <class ... A> class F, class Def, class ... L>
  145. struct fold_<F, Def, nil, L ...>
  146. {
  147. using type = std::conditional_t<std::is_same_v<void, Def>, F<>, Def>;
  148. };
  149. template <template <class ... A> class F, class Def>
  150. struct fold_<F, Def>
  151. {
  152. using type = std::conditional_t<std::is_same_v<void, Def>, F<>, Def>;
  153. };
  154. template <template <class ... A> class F, class Def, class ... L>
  155. using fold = typename fold_<F, Def, L ...>::type;
  156. template <class ... A> struct max_ { using type = int_c<std::numeric_limits<int>::min()>; };
  157. template <class ... A> using max = typename max_<A ...>::type;
  158. template <class A0, class ... A> struct max_<A0, A ...> { using type = int_c<std::max(A0::value, max<A ...>::value)>; };
  159. template <class ... A> struct min_ { using type = int_c<std::numeric_limits<int>::max()>; };
  160. template <class ... A> using min = typename min_<A ...>::type;
  161. template <class A0, class ... A> struct min_<A0, A ...> { using type = int_c<std::min(A0::value, min<A ...>::value)>; };
  162. // Operations on int_c arguments.
  163. template <class ... A> using sum = int_c<(A::value + ... + 0)>;
  164. template <class ... A> using prod = int_c<(A::value * ... * 1)>;
  165. template <class ... A> using andb = bool_c<(A::value && ...)>;
  166. template <class ... A> using orb = bool_c<(A::value || ...)>;
  167. // Remove from the second list the elements of the first list. None may have repeated elements, but they may be unsorted.
  168. template <class S, class T, class SS=S> struct complement_list_;
  169. template <class S, class T, class SS=S> using complement_list = typename complement_list_<S, T, SS>::type;
  170. // end of T.
  171. template <class S, class SS>
  172. struct complement_list_<S, nil, SS>
  173. {
  174. using type = nil;
  175. };
  176. // end search on S, did not find.
  177. template <class T0, class ... T, class SS>
  178. struct complement_list_<nil, tuple<T0, T ...>, SS>
  179. {
  180. using type = cons<T0, complement_list<SS, tuple<T ...>>>;
  181. };
  182. // end search on S, found.
  183. template <class F, class ... S, class ... T, class SS>
  184. struct complement_list_<tuple<F, S ...>, tuple<F, T ...>, SS>
  185. {
  186. using type = complement_list<SS, tuple<T ...>>;
  187. };
  188. // keep searching on S.
  189. template <class S0, class ... S, class T0, class ... T, class SS>
  190. struct complement_list_<tuple<S0, S ...>, tuple<T0, T ...>, SS>
  191. {
  192. using type = complement_list<tuple<S ...>, tuple<T0, T ...>, SS>;
  193. };
  194. // Like complement_list, but assume that both lists are sorted.
  195. template <class S, class T> struct complement_sorted_list_ { using type = nil; };
  196. template <class S, class T> using complement_sorted_list = typename complement_sorted_list_<S, T>::type;
  197. template <class T>
  198. struct complement_sorted_list_<nil, T>
  199. {
  200. using type = T;
  201. };
  202. template <class F, class ... S, class ... T>
  203. struct complement_sorted_list_<tuple<F, S ...>, tuple<F, T ...>>
  204. {
  205. using type = complement_sorted_list<tuple<S ...>, tuple<T ...>>;
  206. };
  207. template <class S0, class ... S, class T0, class ... T>
  208. struct complement_sorted_list_<tuple<S0, S ...>, tuple<T0, T ...>>
  209. {
  210. static_assert(T0::value<=S0::value, "bad lists for complement_sorted_list<>");
  211. using type = cons<T0, complement_sorted_list<tuple<S0, S ...>, tuple<T ...>>>;
  212. };
  213. // Variant of complement_list where the second argument is [0 .. end-1].
  214. template <class S, int end> using complement = complement_sorted_list<S, iota<end>>;
  215. // Prepend an element to each of a list of lists.
  216. template <class c, class A> struct MapCons;
  217. template <class c, class A> using MapCons_ = typename MapCons<c, A>::type;
  218. template <class c, class ... A> struct MapCons<c, tuple<A ...>> { using type = tuple<cons<c, A> ...>; };
  219. // Prepend a list to each list in a list of lists.
  220. template <class c, class A> struct MapPrepend;
  221. template <class c, class A> using MapPrepend_ = typename MapPrepend<c, A>::type;
  222. template <class c, class ... A> struct MapPrepend<c, tuple<A ...>> { using type = tuple<append<c, A> ...>; };
  223. // Form all possible lists by prepending an element of A to an element of B.
  224. template <class A, class B> struct ProductAppend { using type = nil; };
  225. template <class A, class B> using ProductAppend_ = typename ProductAppend<A, B>::type;
  226. template <class A0, class ... A, class B> struct ProductAppend<tuple<A0, A ...>, B> { using type = append<MapPrepend_<A0, B>, ProductAppend_<tuple<A ...>, B>>; };
  227. // Compute the K-combinations of the N elements of list A.
  228. template <class A, int K, int N=len<A>> struct combinations_;
  229. template <class A, int k, int N=len<A>> using combinations = typename combinations_<A, k, N>::type;
  230. // In this case, return a list with one element: the empty list.
  231. template <class A, int N> struct combinations_<A, 0, N> { using type = tuple<nil>; };
  232. // In this case, return a list with one element: the whole list.
  233. template <class A, int N> struct combinations_<A, N, N> { using type = tuple<A>; };
  234. // Special case for 0 over 0, to resolve ambiguity of 0/N and N/N when N=0.
  235. template <> struct combinations_<nil, 0> { using type = tuple<nil>; };
  236. template <class A, int K, int N>
  237. struct combinations_
  238. {
  239. static_assert(is_tuple<A>);
  240. static_assert(N>=0 && K>=0);
  241. static_assert(K<=N);
  242. using Rest = drop1<A>;
  243. using type = append<MapCons_<first<A>, combinations<Rest, K-1, N-1>>, combinations<Rest, K, N-1>>;
  244. };
  245. // Sign of permutations.
  246. template <class C, class R> struct PermutationSign;
  247. template <int w, class C, class R>
  248. constexpr int PermutationSignIfFound = PermutationSign<append<take<C, w>, drop<C, w+1>>, drop1<R>>::value
  249. * ((w & 1) ? -1 : +1);
  250. template <class C, class R>
  251. constexpr int PermutationSignIfFound<-1, C, R> = 0;
  252. template <> struct PermutationSign<nil, nil> { constexpr static int value = 1; };
  253. template <class C> struct PermutationSign<C, nil> { constexpr static int value = 0; };
  254. template <class R> struct PermutationSign<nil, R> { constexpr static int value = 0; };
  255. template <class C, class Org>
  256. struct PermutationSign
  257. {
  258. constexpr static int value = PermutationSignIfFound<index<C, first<Org>>::value, C, Org>;
  259. };
  260. // increment the w-th element of an int_list
  261. template <class L, int w> using inc = append<take<L, w>, cons<int_c<ref<L, w>::value+1>, drop<L, w+1>>>;
  262. template <class A> struct InvertIndex_;
  263. template <class ... A> struct InvertIndex_<tuple<A ...>>
  264. {
  265. using AT = tuple<A ...>;
  266. template <class T> using IndexA = int_c<index<AT, T>::value>;
  267. constexpr static int N = apply<max, AT>::value;
  268. using type = map<IndexA, iota<(N>=0 ? N+1 : 0)>>;
  269. };
  270. template <class A> using InvertIndex = typename InvertIndex_<A>::type;
  271. // Used in tests.
  272. template <class A, int ... I> struct check_idx { constexpr static bool value = false; };
  273. template <> struct check_idx<nil> { constexpr static bool value = true; };
  274. template <class A0, int I0, class ... A, int ... I>
  275. struct check_idx<tuple<A0, A ...>, I0, I ...>
  276. {
  277. constexpr static bool value = (A0::value==I0) && check_idx<tuple<A ...>, I ...>::value;
  278. };
  279. // -------------------------
  280. // Tuples in dynamic context
  281. // -------------------------
  282. // like std::make_trom_tuple, but use brace constructor (e.g. for std::array).
  283. template <class C, class T>
  284. constexpr C
  285. from_tuple(T && t)
  286. {
  287. return std::apply([](auto && ... x) { return C { RA_FWD(x) ... }; }, t);
  288. }
  289. template <class C, class T>
  290. consteval auto
  291. tuple_values()
  292. {
  293. return std::apply([](auto ... t) { return std::array<C, len<T>> { C(t) ... }; }, T {});
  294. }
  295. template <class C, class T, class I>
  296. constexpr C
  297. map_indices(I const & i)
  298. {
  299. return std::apply([&i](auto ... t) { return std::array<C, len<T>> { i[t] ... }; }, T {});
  300. };
  301. template <class T, int k=0>
  302. constexpr int
  303. int_list_index(int i)
  304. {
  305. if constexpr (k>=mp::len<T>) {
  306. return -1;
  307. } else {
  308. return (i==mp::ref<T, k>::value) ? k : int_list_index<T, k+1>(i);
  309. }
  310. }
  311. template <class K, class T, class F, class I = int_c<0>>
  312. constexpr auto
  313. fold_tuple(K && k, T && t, F && f, I && i = int_c<0> {})
  314. {
  315. if constexpr (I::value==len<std::decay_t<T>>) {
  316. return k;
  317. } else {
  318. return fold_tuple(f(k, std::get<I::value>(t)), t, f, int_c<I::value+1> {});
  319. }
  320. }
  321. } // namespace ra::mp