//// Copyright 2017 Peter Dimov Distributed under the Boost Software License, Version 1.0. See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt //// [#algorithm] # Algorithms, :toc: :toc-title: :idprefix: ## mp_assign template using mp_assign = /*...*/; `mp_assign, L2>` is an alias for `L1`. That is, it replaces the elements of `L1` with those of `L2`. .Using mp_assign with mp_list and std::tuple ``` using L1 = std::tuple; using L2 = mp_list; using R1 = mp_assign; // std::tuple ``` .Using mp_assign with mp_list and std::pair ``` using L1 = std::pair; using L2 = mp_list; using R1 = mp_assign; // std::pair ``` ## mp_clear template using mp_clear = mp_assign>; `mp_clear>` is an alias for `L<>`, that is, it removes the elements of `L`. .Using mp_clear with std::tuple ``` using L1 = std::tuple; using R1 = mp_clear; // std::tuple<> ``` ## mp_transform template class F, class... L> using mp_transform = /*...*/; `mp_transform, L2, ..., Ln>` applies `F` to each successive tuple of elements and returns `L1...>`. .Using mp_transform to produce a list of pointers from a list of pointees ``` template using add_pointer_t = typename std::add_pointer::type; // std::add_pointer_t in C++14 using L1 = std::tuple; using R1 = mp_transform; // std::tuple ``` .Using mp_transform to compare the contents of two lists of types ``` using L1 = std::tuple; using L2 = mp_list; using R1 = mp_all>; // mp_true ``` .Using mp_transform to compare the contents of two lists of integral constants ``` template using eq = mp_bool; using L1 = std::tuple, mp_int<2>, mp_int<3>>; using L2 = mp_list, mp_size_t<2>, mp_size_t<3>>; using R1 = mp_all>; // mp_true ``` .mp_transform on one list [cols="<.^4m,4*^.^1m",options="header",width=85%] |=== ^|List 4+|Contents 5+| |*L1*|A0|A1|...|An 5+| |*mp_transform*|F|F|...|F |=== .mp_transform on two lists [cols="<.^4m,4*^.^1m",options="header",width=85%] |=== ^|List 4+|Contents 5+| |*L1*|A0|A1|...|An 5+| |*L2*|B0|B1|...|Bn 5+| |*mp_transform*|F|F|...|F |=== ## mp_transform_q template using mp_transform_q = mp_transform; As `mp_transform`, but takes a quoted metafunction. .Using mp_transform_q to count the occurences of `void` in a list ``` using L1 = std::tuple; using R1 = mp_apply, L1>>; // mp_int\<2> ``` ## mp_transform_if template class P, template class F, class L...> using mp_transform_if = /*...*/; `mp_transform_if` replaces the elements of the list `L1` for which `mp_to_bool>` is `mp_true` with `F`, and returns the result, where `Ti` are the corresponding elements of `Li`. .Using mp_transform_if to replace the occurences of 'void' in a list with the corresponding elements of a second list ``` using L1 = std::tuple; using L2 = std::tuple; template using first_is_void = std::is_same; template using second = T2; using R1 = mp_transform_if; // std::tuple ``` ## mp_transform_if_q template using mp_transform_if_q = mp_transform_if; As `mp_transform_if`, but takes quoted metafunctions. .Using mp_transform_if_q to replace the occurences of 'void' in a list with the corresponding elements of a second list ``` using L1 = std::tuple; using L2 = std::tuple; using R1 = mp_transform_if_q, _2, L1, L2>; // std::tuple ``` ## mp_fill template using mp_fill = /*...*/; `mp_fill, V>` returns `L`, with the result having the same size as the input. .Using mp_fill with std::tuple ``` using L1 = std::tuple; using R1 = mp_fill; // std::tuple ``` .Using mp_fill with std::pair ``` using L1 = std::pair; using R1 = mp_fill; // std::pair ``` ## mp_count template using mp_count = /*...*/; `mp_count` returns `mp_size_t`, where `N` is the number of elements of `L` same as `V`. ## mp_count_if template class P> using mp_count_if = /*...*/; `mp_count_if` returns `mp_size_t`, where `N` is the number of elements `T` of `L` for which `mp_to_bool>` is `mp_true`. ## mp_contains template using mp_contains = mp_to_bool>; `mp_contains` is `mp_true` when `L` contains an element `V`, `mp_false` otherwise. ## mp_repeat_c template using mp_repeat_c = /*...*/; `mp_repeat_c` returns a list of the same type as `L` that consists of `N` concatenated copies of `L`. ## mp_repeat template using mp_repeat = /*...*/; Same as `mp_repeat_c` but with a type argument `N`. The number of copies is `N::value` and must be nonnegative. ## mp_product template class F, class... L> using mp_product = /*...*/; `mp_product, L2, ..., Ln>` evaluates `F` for values `Ui` taken from the Cartesian product of the lists, as if the elements `Ui` are formed by `n` nested loops, each traversing `Li`. It returns a list of type `L1` containing the results of the application of `F`. .mp_product on two lists [cols="<.^4m,4*^.^1m",options="header",width=85%] |=== ^|List 4+|Contents 5+| |*L1*|A0|A1|...|An 5+| |*L2*|B0|B1|...|Bm 5+| |*mp_product* |F F ... F |F F ... F |... |F F ... F |=== ## mp_product_q template using mp_product_q = mp_product; As `mp_product`, but takes a quoted metafunction. ## mp_drop_c template using mp_drop_c = /*...*/; `mp_drop_c` removes the first `N` elements of `L` and returns the result. ## mp_drop template using mp_drop = /*...*/; Same as `mp_drop_c`, but with a type argument `N`. `N::value` must be a nonnegative number. ## mp_iota_c template using mp_iota_c = /*...*/; `mp_iota_c` is an alias for `mp_list, mp_size_t<1>, ..., mp_size_t>`. ## mp_iota template using mp_iota = /*...*/; Same as `mp_iota_c`, but with a type argument `N`. `N::value` must be a nonnegative number. Returns `mp_list, std::integral_constant, ..., std::integral_constant>` where `T` is the type of `N::value`. ## mp_at_c template using mp_at_c = /*...*/; `mp_at_c` returns the `I`-th element of `L`, zero-based. ## mp_at template using mp_at = /*...*/; Same as `mp_at_c`, but with a type argument `I`. `I::value` must be a nonnegative number. ## mp_take_c template using mp_take_c = /*...*/; `mp_take_c` returns a list of the same type as `L` containing the first `N` elements of `L`. ## mp_take template using mp_take = /*...*/; Same as `mp_take_c`, but with a type argument `N`. `N::value` must be a nonnegative number. ## mp_insert_c template using mp_insert_c = mp_append, mp_push_front, T...>>; Inserts the elements `T...` into the list `L` at position `I` (a zero-based index). ## mp_insert template using mp_insert = mp_append, mp_push_front, T...>>; Same as `mp_insert_c`, but with a type argument `I`. ## mp_erase_c template using mp_erase_c = mp_append, mp_drop_c>; Removes from the list `L` the elements with indices from `I` (inclusive) to `J` (exclusive). ## mp_erase template using mp_erase = mp_append, mp_drop>; Same as `mp_erase_c`, but with a type arguments `I` and `J`. ## mp_replace template using mp_replace = /*...*/; Replaces all `V` elements of `L` with `W` and returns the result. ## mp_replace_if template class P, class W> using mp_replace_if = /*...*/; Replaces all `T` elements of `L` for which `mp_to_bool>` is `mp_true` with `W` and returns the result. ## mp_replace_at_c template using mp_replace_at_c = /*...*/; Replaces the element of `L` at zero-based index `I` with `W` and returns the result. ## mp_replace_at template using mp_replace_at = /*...*/; Same as `mp_replace_at_c`, but with a type argument `I`. `I::value` must be a nonnegative number. ## mp_copy_if template class P> using mp_copy_if = /*...*/; Copies the elements `T` of `L` for which `mp_to_bool>` is `mp_true` to a new list of the same type and returns it. ## mp_remove template using mp_remove = /*...*/; Removes all `V` elements of `L` and returns the result. ## mp_remove_if template class P> using mp_remove_if = /*...*/; Removes all elements `T` of `L` for which `mp_to_bool>` is `mp_true` and returns the result. ## mp_partition template class P> using mp_partition = /*...*/; `mp_partition, P>` partitions `L` into two lists `L` and `L` such that `mp_to_bool>` is `mp_true` for the elements of `L` and `mp_false` for the elements of `L`. Returns `L, L>`. ## mp_sort template class P> using mp_sort = /*...*/; `mp_sort` sorts the list `L` according to the strict weak ordering `mp_to_bool>`. .Using mp_sort to sort a list of std::ratio values ---- #include using L1 = mp_list, std::ratio<1,4>>; using R1 = mp_sort; // mp_list, ratio<1,2>> ---- ## mp_find template using mp_find = /*...*/; `mp_find` returns the index at which the type `V` is located in the list `L`. It's an alias for `mp_size_t`, where `I` is the zero-based index of the first occurence of `V` in `L`. If `L` does not contain `V`, `mp_find` is `mp_size`. ## mp_find_if template class P> using mp_find_if = /*...*/; `mp_find_f` is an alias for `mp_size_t`, where `I` is the zero-based index of the first element `T` in `L` for which `mp_to_bool>` is `mp_true`. If there is no such element, `mp_find_if` is `mp_size`. ## mp_reverse template using mp_reverse = /*...*/; `mp_reverse>` is `L`. ## mp_fold template class F> using mp_fold = /*...*/; `mp_fold, V, F>` is `F< F< F< F, T2>, ...>, Tn>`, or `V`, if `L` is empty. ## mp_reverse_fold template class F> using mp_reverse_fold = /*...*/; `mp_reverse_fold, V, F>` is `F>>>`, or `V`, if `L` is empty. ## mp_unique template using mp_unique = /*...*/; `mp_unique` returns a list of the same type as `L` with the duplicate elements removed. ## mp_all_of template class P> using mp_all_of = mp_bool< mp_count_if::value == mp_size::value >; `mp_all_of` is `mp_true` when `P` holds for all elements of `L`, `mp_false` otherwise. When `L` is empty, the result is `mp_true`. ## mp_none_of template class P> using mp_none_of = mp_bool< mp_count_if::value == 0 >; `mp_none_of` is `mp_true` when `P` holds for no element of `L`, `mp_false` otherwise. When `L` is empty, the result is `mp_true`. ## mp_any_of template class P> using mp_any_of = mp_bool< mp_count_if::value != 0 >; `mp_any_of` is `mp_true` when `P` holds for at least one element of `L`, `mp_false` otherwise. When `L` is empty, the result is `mp_false`. ## mp_for_each(f) template constexpr F mp_for_each(F&& f); `mp_for_each(f)` calls `f` with `T()` for each element `T` of the list `L`, in order. Returns `std::forward(f)`. .Using mp_for_each and a C++14 lambda to print a tuple ``` template void print( std::tuple const & tp ) { std::size_t const N = sizeof...(T); mp_for_each>( []( auto I ){ // I is mp_size_t<0>, mp_size_t<1>, ..., mp_size_t std::cout << std::get(tp) << std::endl; }); } ``` ## mp_with_index(i, f) template constexpr auto mp_with_index( std::size_t i, F && f ) -> decltype(std::declval()(std::declval>())); `mp_with_index(i, f)` calls `f` with `mp_size_t()` and returns the result. `i` must be less than `N`. Only `constexpr` on C++14 and higher. template constexpr auto mp_with_index( std::size_t i, F && f ) -> decltype(std::declval()(std::declval>())); Returns `mp_with_index(i, f)`. .Using mp_with_index and a C++14 lambda to print the active element of a variant ``` template void print( std::variant const& v ) { mp_with_index( v.index(), [&]( auto I ) { // I is mp_size_t here std::cout << std::get( v ) << std::endl; }); } ```