The Art of C++ / Sequences is a zero-dependency C++11 header-only library that provides efficient algorithms to generate and work on variadic templates and std::integer_sequence
.
- Requires C++11 or newer.
- Tested with GCC 4.8+, Clang 3.4+, Xcode 6+ and Visual Studio 2017.
- All provided templates are in the nested namespace
tao::seq
. - All templates don't use C++14 features, therefore being compatible with C++11. Sometimes, C++14/C++17 features are used conditionally, taking advantage of newer language features when available but providing C++11-compatible implementations otherwise.
- All templates use
tao::seq::integer_sequence
, etc. internally, therefore being compatible with C++11. - All templates use
tao::seq::make_integer_sequence
, etc. internally, therefore using the most scalable solution available.
Provides:
integer_sequence< typename T, T N >
index_sequence< std::size_t N >
Notes:
- When available (C++14 or newer), the above are type-aliases for
std::integer_sequence
andstd::index_sequence
.
Efficient versions of sequence generators.
make_integer_sequence< typename T, T N >
make_index_sequence< std::size_t N >
index_sequence_for< typename... Ts >
Examples:
make_integer_sequence<int,0>
➙integer_sequence<int>
make_integer_sequence<int,1>
➙integer_sequence<int,0>
make_integer_sequence<int,3>
➙integer_sequence<int,0,1,2>
make_index_sequence<0>
➙index_sequence<>
make_index_sequence<1>
➙index_sequence<0>
make_index_sequence<5>
➙index_sequence<0,1,2,3,4>
index_sequence_for<int,void,long>
➙index_sequence<0,1,2>
Notes:
libc++ already has very efficient versions for the above, so they are pulled in with a using-declaration. Only if we don't know if the STL's versions are at least O(log N) we provide our own implementations.
Our own implementation has O(log N) instantiation depth. This allows for very large sequences without the need to increase the compiler's default instantiation depth limits. For example, GCC and Clang generate index_sequence<10000>
in ~0.15s (on my machine, of course). The standard library version from libstdc++, when trying to create index_sequence<5000>
and with its O(N) implementation, requires ~30s, >3GB of RAM and -ftemplate-depth=5100
.
Generate half-open ranges of integers.
make_integer_range< typename T, T N, T M >
make_index_range< std::size_t N, std::size_t M >
Examples:
make_integer_range<int,3,7>
➙integer_sequence<int,3,4,5,6>
make_integer_range<int,7,3>
➙integer_sequence<int,7,6,5,4>
make_integer_sequence<int,-2,2>
➙integer_sequence<int,-2,-1,0,1>
make_index_range<5,5>
➙index_sequence<>
make_index_range<2,5>
➙index_sequence<2,3,4>
Integral constant to provide the sum of Ns
.
sum< typename T, T... Ns >
sum< typename S >
Examples:
sum<int,1,4,3,1>::value
➙9
sum<make_index_sequence<5>>::value
➙10
Integral constant to provide the sum of the first I
elements of Ns
.
partial_sum< std::size_t I, typename T, T... Ns >
partial_sum< std::size_t I, typename S >
Examples:
partial_sum<0,int,1,4,3,1>::value
➙0
partial_sum<2,int,1,4,3,1>::value
➙5
partial_sum<4,make_index_sequence<5>>::value
➙6
Provides a sequence with the exclusive scan of the input sequence.
exclusive_scan_t< typename T, T... Ns >
exclusive_scan_t< typename S >
Examples:
exclusive_scan_t<int,1,4,0,3,1>
➙integer_sequence<int,0,1,5,5,8>
Provides a sequence with the inclusive scan of the input sequence.
inclusive_scan_t< typename T, T... Ns >
inclusive_scan_t< typename S >
Examples:
inclusive_scan_t<int,1,4,0,3,1>
➙integer_sequence<int,1,5,5,8,9>
Applies a binary operation to elements from two sequences.
zip_t< typename OP, typename L, typename R >
Notes:
Both sequences may have a different element type, the resulting sequence's type is calculated with std::common_type_t
.
Provides a sequence which is the element-wise sum of its input sequences.
plus_t< typename L, typename R >
Notes:
Both sequences may have a different element type, the resulting sequence's type is calculated with std::common_type_t
.
Examples:
using A = index_sequence<1,4,0,3,1>
using B = make_index_sequence<5>
plus_t<A,B>
➙index_sequence<1,5,2,6,5>
Provides a sequence which is the element-wise sum of its input sequences.
minus_t< typename L, typename R >
Notes:
Both sequences may have a different element type, the resulting sequence's type is calculated with std::common_type_t
.
Examples:
using A = integer_sequence<int,1,4,0,3,1>
using B = integer_sequence<int,0,1,2,3,4>
minus_t<A,B>
➙integer_sequence<int,1,3,-2,0,-3>
minus_t<B,A>
➙integer_sequence<int,-1,-3,2,0,3>
Integral constant to provide the first element of a non-empty sequence.
head< typename T, T... >
head< typename S >
Removed the first element of a non-empty sequence.
tail_t< typename T, T... >
tail_t< typename S >
Integral constant to provide the I
th element of a non-empty sequence.
select< std::size_t I, typename T, T... >
select< std::size_t I, typename S >
Concatenate the values.
concatenate_t< typename T, typename U >
Notes:
Both sequences may have a different element type, the resulting sequence's type is calculated with std::common_type_t
.
Integral constant calculated by "folding" a sequence of values with a given binary operation.
fold< typename OP, typename T, T... >
fold< typename OP, typename S >
Integral constant to provide the minimum value.
min< typename T, T... >
min< typename S >
Notes:
Implemented with fold
like this:
namespace impl
{
struct min
{
template< typename T, T A, T B >
using apply = std::integral_constant< T, ( ( A < B ) ? A : B ) >;
};
}
template< typename T, T... Ns >
using min = fold< impl::min, T, Ns... >;
Integral constant to provide the maximum value.
max< typename T, T... >
max< typename S >
Map a sequence of indices to a sequence of values.
map_t< typename I, typename M >
Examples:
using I = index_sequence<1,0,3,2,1,1,3>
using M = integer_sequence<int,5,6,-7,8,9>
map_t<I,M>
➙integer_sequence<int,6,5,8,-7,6,6,8>
Integral constant which is true if all boolean parameters are true (logical and).
is_all< bool... >
Examples:
is_all<true,true,true,true>::value
➙true
is_all<true,true,false,true>::value
➙false
is_all<>::value
➙true
Integral constant which is true if any boolean parameter is true (logical or).
is_any< bool... >
Examples:
is_any<false,true,false,false>::value
➙true
is_any<false,false,false,false>::value
➙false
is_any<>::value
➙false
Integral constant which is true if an element N
is part of a list of elements Ns...
.
contains< typename T, T N, T... Ns>
contains< typename S, T N>
Examples:
contains<int,0>
➙false
contains<int,0,0>
➙true
contains<int,0,1>
➙false
contains<int,0,1,2,3,4,5>
➙false
contains<int,3,1,2,3,4,5>
➙true
using A = integer_sequence<int,1,2,3,4,5>
contains<A,0>
➙false
contains<A,3>
➙true
Integral constant which is the smallest index of an element N
in a list of elements Ns...
.
index_of< typename T, T N, T... Ns>
index_of< typename S, T N>
Note: Ns...
must contain N
, otherwise a static_assert
is triggered.
Examples:
index_of<int,0,0>
➙0
index_of<int,3,1,2,3,4,5>
➙2
using A = integer_sequence<int,1,2,3,4,5>
index_of<A,3>
➙2
Scales a sequence by a factor F
.
scale< typename T, T F, T... Ns>
scale< typename S, T F>
Examples:
scale<int,0,0>
➙integer_sequence<int,0>
scale<int,2,-1,2,0,1,5>
➙integer_sequence<int,-2,4,0,2,10>
using A = integer_sequence<int,-1,2,4>
scale<A,3>
➙integer_sequence<int,-3,6,12>
Returns the I
th type from a list of types Ts...
.
at_index_t< std::size_t I, typename... Ts >
Examples:
at_index<0,bool,int,void,char*>
➙bool
at_index<2,bool,int,void,char*>
➙void
Returns the element-wise product of two sequences.
Examples :
using A = index_sequence< 0, 5, 7, 1 >
using B = index_sequence< 4, 5, 6, 7 >
prod_t<A,B>
➙index_sequence< 0, 25, 42, 7 >
Integral constant to provide the product of a sequence (the empty sequence counting as 1).
Examples :
prod_red_t< integer_sequence<int> >
➙integral_constant<int,1>
prod_red_t< integer_sequence< int, -1, 4, 5 > >
➙integral_constant<int,-20>
Reverse cumulative product of a sequence
Example :
cum_prod_t< index_sequence< 4, 5, 6, 7 > >
➙index_sequence< 210, 42, 7, 1 >
Filters out elements of the first sequence in the second sequence.
Example :
filter_out_seq_t< index_sequence< 4, 6 >, index_sequence< 4, 5, 6, 7 > >
➙index_sequence< 5, 7 >
Given a sequence S and U a permutation of S, returns the indices of elements of U in S.
Example :
using S = integer_sequence< int, 7, -2, 3, 0, 4 >
using U = integer_sequence< int, 4, 7, -2, 0, 3 >
index_of_seq_t< S, U >
➙index_sequence< 4, 0, 1, 3, 2 >
Apply a index permutation on a sequence.
Example :
using S = integer_sequence< int, 7, -2, 3, 0, 4 >
permute_t< index_sequence< 3, 0, 4, 1, 2 >, S >
➙integer_sequence< int, -2, 0, 4, 7, 3 >
Sort a sequence with a comparison (meta)function.
Example :
template<int a, int b>
struct ord
{
static const bool value = a < b;
};
using S = integer_sequence< int, 7, -2, 3, 0, 4 >
sort_t< S, ord >
➙integer_sequence< int, -2, 0, 3, 4, 7 >
Same as sort
, but returns the indices of the sorted sequence instead.
using U = index_sequence< 39, 2, 4, 10 >
sort_index_t< U, ord >
➙index_sequence< 3, 0, 1, 2 >
Released 2018-07-22
- Added documentation for the remaining headers.
Released 2018-07-21
- Removed
type_by_index
, useat_index
instead.
Released 2018-06-29
- Initial release.
The Art of C++ is certified Open Source software. It may be used for any purpose, including commercial purposes, at absolutely no cost. It is distributed under the terms of the MIT license reproduced here.
Copyright (c) 2015-2019 Daniel Frey
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.