forked from boostorg/mp11
Add integer_sequence.hpp.
This commit is contained in:
85
include/boost/integer_sequence.hpp
Normal file
85
include/boost/integer_sequence.hpp
Normal file
@ -0,0 +1,85 @@
|
|||||||
|
#ifndef BOOST_INTEGER_SEQUENCE_HPP_INCLUDED
|
||||||
|
#define BOOST_INTEGER_SEQUENCE_HPP_INCLUDED
|
||||||
|
|
||||||
|
#include <cstddef>
|
||||||
|
|
||||||
|
namespace boost
|
||||||
|
{
|
||||||
|
|
||||||
|
// integer_sequence
|
||||||
|
template<class T, T... I> struct integer_sequence
|
||||||
|
{
|
||||||
|
};
|
||||||
|
|
||||||
|
// detail::make_integer_sequence_impl
|
||||||
|
namespace detail
|
||||||
|
{
|
||||||
|
|
||||||
|
// iseq_if_c
|
||||||
|
template<bool C, class T, class E> struct iseq_if_c_impl;
|
||||||
|
|
||||||
|
template<class T, class E> struct iseq_if_c_impl<true, T, E>
|
||||||
|
{
|
||||||
|
using type = T;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<class T, class E> struct iseq_if_c_impl<false, T, E>
|
||||||
|
{
|
||||||
|
using type = E;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<bool C, class T, class E> using iseq_if_c = typename iseq_if_c_impl<C, T, E>::type;
|
||||||
|
|
||||||
|
// iseq_identity
|
||||||
|
template<class T> struct iseq_identity
|
||||||
|
{
|
||||||
|
using type = T;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<class S1, class S2> struct append_integer_sequence;
|
||||||
|
|
||||||
|
template<class T, T... I, T... J> struct append_integer_sequence<integer_sequence<T, I...>, integer_sequence<T, J...>>
|
||||||
|
{
|
||||||
|
using type = integer_sequence< T, I..., ( J + sizeof...(I) )... >;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<class T, T N> struct make_integer_sequence_impl;
|
||||||
|
|
||||||
|
template<class T, T N> struct make_integer_sequence_impl_
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
|
||||||
|
static T const M = N / 2;
|
||||||
|
static T const R = N % 2;
|
||||||
|
|
||||||
|
using S1 = typename make_integer_sequence_impl<T, M>::type;
|
||||||
|
using S2 = typename append_integer_sequence<S1, S1>::type;
|
||||||
|
using S3 = typename make_integer_sequence_impl<T, R>::type;
|
||||||
|
using S4 = typename append_integer_sequence<S2, S3>::type;
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
using type = S4;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<class T, T N> struct make_integer_sequence_impl: iseq_if_c<N == 0, iseq_identity<integer_sequence<T>>, iseq_if_c<N == 1, iseq_identity<integer_sequence<T, 0>>, make_integer_sequence_impl_<T, N>>>
|
||||||
|
{
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace detail
|
||||||
|
|
||||||
|
// make_integer_sequence
|
||||||
|
template<class T, T N> using make_integer_sequence = typename detail::make_integer_sequence_impl<T, N>::type;
|
||||||
|
|
||||||
|
// index_sequence
|
||||||
|
template<std::size_t... I> using index_sequence = integer_sequence<std::size_t, I...>;
|
||||||
|
|
||||||
|
// make_index_sequence
|
||||||
|
template<std::size_t N> using make_index_sequence = make_integer_sequence<std::size_t, N>;
|
||||||
|
|
||||||
|
// index_sequence_for
|
||||||
|
template<class... T> using index_sequence_for = make_integer_sequence<std::size_t, sizeof...(T)>;
|
||||||
|
|
||||||
|
} // namespace boost
|
||||||
|
|
||||||
|
#endif // #ifndef BOOST_INTEGER_SEQUENCE_HPP_INCLUDED
|
@ -43,3 +43,6 @@ run mp_identity.cpp : : : $(REQ) ;
|
|||||||
run mp_inherit.cpp : : : $(REQ) ;
|
run mp_inherit.cpp : : : $(REQ) ;
|
||||||
run mp_if.cpp : : : $(REQ) ;
|
run mp_if.cpp : : : $(REQ) ;
|
||||||
run mp_eval_if.cpp : : : $(REQ) ;
|
run mp_eval_if.cpp : : : $(REQ) ;
|
||||||
|
|
||||||
|
# integer_sequence
|
||||||
|
run integer_sequence.cpp : : : $(REQ) ;
|
||||||
|
53
test/integer_sequence.cpp
Normal file
53
test/integer_sequence.cpp
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
|
||||||
|
// Copyright 2015 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
|
||||||
|
|
||||||
|
|
||||||
|
#include <boost/integer_sequence.hpp>
|
||||||
|
#include <boost/core/lightweight_test_trait.hpp>
|
||||||
|
#include <type_traits>
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
BOOST_TEST_TRAIT_TRUE((std::is_same<boost::make_integer_sequence<int, 0>, boost::integer_sequence<int>>));
|
||||||
|
BOOST_TEST_TRAIT_TRUE((std::is_same<boost::make_integer_sequence<int, 1>, boost::integer_sequence<int, 0>>));
|
||||||
|
BOOST_TEST_TRAIT_TRUE((std::is_same<boost::make_integer_sequence<int, 2>, boost::integer_sequence<int, 0, 1>>));
|
||||||
|
BOOST_TEST_TRAIT_TRUE((std::is_same<boost::make_integer_sequence<int, 3>, boost::integer_sequence<int, 0, 1, 2>>));
|
||||||
|
BOOST_TEST_TRAIT_TRUE((std::is_same<boost::make_integer_sequence<int, 4>, boost::integer_sequence<int, 0, 1, 2, 3>>));
|
||||||
|
|
||||||
|
BOOST_TEST_TRAIT_TRUE((std::is_same<boost::make_integer_sequence<char, 0>, boost::integer_sequence<char>>));
|
||||||
|
BOOST_TEST_TRAIT_TRUE((std::is_same<boost::make_integer_sequence<char, 1>, boost::integer_sequence<char, 0>>));
|
||||||
|
BOOST_TEST_TRAIT_TRUE((std::is_same<boost::make_integer_sequence<char, 2>, boost::integer_sequence<char, 0, 1>>));
|
||||||
|
BOOST_TEST_TRAIT_TRUE((std::is_same<boost::make_integer_sequence<char, 3>, boost::integer_sequence<char, 0, 1, 2>>));
|
||||||
|
BOOST_TEST_TRAIT_TRUE((std::is_same<boost::make_integer_sequence<char, 4>, boost::integer_sequence<char, 0, 1, 2, 3>>));
|
||||||
|
|
||||||
|
BOOST_TEST_TRAIT_TRUE((std::is_same<boost::make_integer_sequence<std::size_t, 0>, boost::integer_sequence<std::size_t>>));
|
||||||
|
BOOST_TEST_TRAIT_TRUE((std::is_same<boost::make_integer_sequence<std::size_t, 1>, boost::integer_sequence<std::size_t, 0>>));
|
||||||
|
BOOST_TEST_TRAIT_TRUE((std::is_same<boost::make_integer_sequence<std::size_t, 2>, boost::integer_sequence<std::size_t, 0, 1>>));
|
||||||
|
BOOST_TEST_TRAIT_TRUE((std::is_same<boost::make_integer_sequence<std::size_t, 3>, boost::integer_sequence<std::size_t, 0, 1, 2>>));
|
||||||
|
BOOST_TEST_TRAIT_TRUE((std::is_same<boost::make_integer_sequence<std::size_t, 4>, boost::integer_sequence<std::size_t, 0, 1, 2, 3>>));
|
||||||
|
|
||||||
|
BOOST_TEST_TRAIT_TRUE((std::is_same<boost::make_index_sequence<0>, boost::integer_sequence<std::size_t>>));
|
||||||
|
BOOST_TEST_TRAIT_TRUE((std::is_same<boost::make_index_sequence<1>, boost::integer_sequence<std::size_t, 0>>));
|
||||||
|
BOOST_TEST_TRAIT_TRUE((std::is_same<boost::make_index_sequence<2>, boost::integer_sequence<std::size_t, 0, 1>>));
|
||||||
|
BOOST_TEST_TRAIT_TRUE((std::is_same<boost::make_index_sequence<3>, boost::integer_sequence<std::size_t, 0, 1, 2>>));
|
||||||
|
BOOST_TEST_TRAIT_TRUE((std::is_same<boost::make_index_sequence<4>, boost::integer_sequence<std::size_t, 0, 1, 2, 3>>));
|
||||||
|
|
||||||
|
BOOST_TEST_TRAIT_TRUE((std::is_same<boost::make_index_sequence<0>, boost::index_sequence<>>));
|
||||||
|
BOOST_TEST_TRAIT_TRUE((std::is_same<boost::make_index_sequence<1>, boost::index_sequence<0>>));
|
||||||
|
BOOST_TEST_TRAIT_TRUE((std::is_same<boost::make_index_sequence<2>, boost::index_sequence<0, 1>>));
|
||||||
|
BOOST_TEST_TRAIT_TRUE((std::is_same<boost::make_index_sequence<3>, boost::index_sequence<0, 1, 2>>));
|
||||||
|
BOOST_TEST_TRAIT_TRUE((std::is_same<boost::make_index_sequence<4>, boost::index_sequence<0, 1, 2, 3>>));
|
||||||
|
|
||||||
|
BOOST_TEST_TRAIT_TRUE((std::is_same<boost::index_sequence_for<>, boost::index_sequence<>>));
|
||||||
|
BOOST_TEST_TRAIT_TRUE((std::is_same<boost::index_sequence_for<void>, boost::index_sequence<0>>));
|
||||||
|
BOOST_TEST_TRAIT_TRUE((std::is_same<boost::index_sequence_for<void, void>, boost::index_sequence<0, 1>>));
|
||||||
|
BOOST_TEST_TRAIT_TRUE((std::is_same<boost::index_sequence_for<void, void, void>, boost::index_sequence<0, 1, 2>>));
|
||||||
|
BOOST_TEST_TRAIT_TRUE((std::is_same<boost::index_sequence_for<void, void, void, void>, boost::index_sequence<0, 1, 2, 3>>));
|
||||||
|
|
||||||
|
return boost::report_errors();
|
||||||
|
}
|
Reference in New Issue
Block a user