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
.
tao::seq
.tao::seq::integer_sequence
, etc. internally, therefore being compatible with C++11.tao::seq::make_integer_sequence
, etc. internally, therefore using the most scalable solution available.tao/seq/integer_sequence.hpp
Provides:
integer_sequence< typename T, T N >
index_sequence< std::size_t N >
Notes:
std::integer_sequence
and std::index_sequence
.tao/seq/make_integer_sequence.hpp
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
.
tao/seq/make_integer_range.hpp
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>
tao/seq/sum.hpp
Integral constant to provide the sum of Ns
.
If no Ns
are given, the result is T(0)
.
sum< typename T, T... Ns >
sum< typename S >
Examples:
sum<int,1,4,3,1>::value
➙ 9
sum<make_index_sequence<5>>::value
➙ 10
tao/seq/prod.hpp
Integral constant to provide the product of Ns
.
If no Ns
are given, the result is T(1)
.
prod< typename T, T... Ns >
prod< typename S >
Examples:
prod<int>::value
➙ 1
prod<int,1,4,3,-1>::value
➙ -12
tao/seq/partial_sum.hpp
Integral constant to provide the sum of the first I
elements.
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
tao/seq/partial_prod.hpp
Integral constant to provide the product of the first I
elements of Ns
.
partial_prod< std::size_t I, typename T, T... Ns >
partial_prod< std::size_t I, typename S >
Examples:
partial_prod<0,int,2,5,3,2>::value
➙ 1
partial_prod<1,int,2,5,3,2>::value
➙ 2
partial_prod<2,int,2,5,3,2>::value
➙ 10
partial_prod<4,int,2,5,3,2>::value
➙ 60
tao/seq/exclusive_scan.hpp
Provides a sequence with the exclusive scan of the input sequence.
exclusive_scan_t< typename OP, typename T, T Init, T... Ns >
exclusive_scan_t< typename OP, typename S, T Init >
Examples:
exclusive_scan_t<op::plus,int,0,1,4,0,3,1>
➙ integer_sequence<int,0,1,5,5,8>
exclusive_scan_t<op::multiplies,S,1>
➙ index_sequence<3,3,12,12,60,540,1080,6480>
tao/seq/inclusive_scan.hpp
Provides a sequence with the inclusive scan of the input sequence.
inclusive_scan_t< typename OP, typename T, T... Ns >
inclusive_scan_t< typename OP, typename S >
Examples:
inclusive_scan_t<op::plus,int,1,4,0,3,1>
➙ integer_sequence<int,1,5,5,8,9>
tao/seq/zip.hpp
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
.
tao/seq/plus.hpp
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>
tao/seq/minus.hpp
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>
tao/seq/multiply.hpp
Provides a sequence which is the element-wise product of its input sequences.
multiply_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,5,2,3,1>
using B = index_sequence<3,0,2,4,1>
multiply_t<A,B>
➙ index_sequence<3,0,4,12,1>
tao/seq/head.hpp
Integral constant to provide the first element of a non-empty sequence.
head< typename T, T... >
head< typename S >
tao/seq/tail.hpp
Removed the first element of a non-empty sequence.
tail_t< typename T, T... >
tail_t< typename S >
tao/seq/select.hpp
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 >
tao/seq/first.hpp
Sequence that contains only the first I
elements of a given sequence.
first_t< std::size_t I, typename T, T... >
first_t< std::size_t I, typename S >
tao/seq/concatenate.hpp
Concatenate the values of all sequences.
concatenate_t< typename... Ts >
Notes:
The sequences may have different element types, the resulting sequence's type is calculated with std::common_type_t
.
tao/seq/difference.hpp
Builds the difference of two sequences, i.e. a sequence that contains all elements of T
that are not in U
.
difference_t< typename T, typename U >
Examples:
using A = index_sequence<1,5,2,3,1,7>
using B = index_sequence<2,1>
difference_t<A,B>
➙ index_sequence<5,3,7>
Notes:
Both sequences may have a different element type, the resulting sequence's type is calculated with std::common_type_t
.
tao/seq/accumulate.hpp
Result of a left fold of the given values over OP
.
accumulate< typename OP, typename T, T... >
accumulate< typename OP, typename S >
tao/seq/reduce.hpp
Reduces the given values in an unspecified order over OP
.
reduce< typename OP, typename T, T... >
reduce< typename OP, typename S >
tao/seq/min.hpp
Integral constant to provide the minimum value.
min< typename T, T... >
min< typename S >
tao/seq/max.hpp
Integral constant to provide the maximum value.
max< typename T, T... >
max< typename S >
tao/seq/map.hpp
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>
tao/seq/is_all.hpp
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
tao/seq/is_any.hpp
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
tao/seq/contains.hpp
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
tao/seq/index_of.hpp
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
tao/seq/scale.hpp
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>
tao/seq/at_index.hpp
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
tao/seq/reverse.hpp
Reverses a sequence.
Examples:
reverse_t<int,1,4,0,3,2>
➙ integer_sequence<int,2,3,0,4,1>
reverse_t<index_sequence<1,4,0,3,2>>
➙ index_sequence<int,2,3,0,4,1>
tao/seq/sort.hpp
Sort a sequence according to a given predicate.
sort_t< typename OP, typename T, T... Ns >
sort_t< typename OP, typename S >
Examples:
Given a predicate less
...
struct less
{
template< typename T, T A, T B >
using apply = std::integral_constant< bool, ( A < B ) >;
};
sort_t<less,int,7,-2,3,0,4>
➙ integer_sequence<int,-2,0,3,4,7>
using S = index_sequence<39,10,2,4,10,2>
sort_t<less,S>
➙ index_sequence<2,2,4,10,10,39>
Released 2019-11-09
Released 2019-11-07
exclusive_scan
and inclusive_scan
.fold
into accumulate
and reduce
.first
, reverse
, prod
, partial_prod
, multiplies
, difference
, and sort
.at_index
.make_index_of_sequence
, permutate
, and sort_index
to contrib (unofficial).Released 2018-07-22
Released 2018-07-21
type_by_index
, use at_index
instead.Released 2018-06-29
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.
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.