tuple/index: add some index sequence generators and manipulators
This commit is contained in:
parent
c570d6ad2f
commit
d69a567431
@ -384,6 +384,7 @@ list (
|
||||
time.hpp
|
||||
time/parse.hpp
|
||||
time/parse8601.cpp
|
||||
tuple/index.hpp
|
||||
tuple/type.hpp
|
||||
tuple/value.hpp
|
||||
typeidx.cpp
|
||||
@ -537,6 +538,7 @@ if (TESTS)
|
||||
thread/ticketlock
|
||||
time/8601
|
||||
traits
|
||||
tuple/index
|
||||
tuple/value
|
||||
tuple/type
|
||||
typeidx
|
||||
|
34
test/tuple/index.cpp
Normal file
34
test/tuple/index.cpp
Normal file
@ -0,0 +1,34 @@
|
||||
#include "tap.hpp"
|
||||
#include "tuple/index.hpp"
|
||||
|
||||
|
||||
int
|
||||
main ()
|
||||
{
|
||||
util::TAP::logger tap;
|
||||
|
||||
{
|
||||
using a_t = util::tuple::index::indices<0,1,2>;
|
||||
using b_t = util::tuple::index::indices<4,3>;
|
||||
|
||||
using result_t = util::tuple::index::cat<a_t,b_t>::type;
|
||||
using expected_t = util::tuple::index::indices<0,1,2,4,3>;
|
||||
|
||||
tap.expect (std::is_same_v<result_t, expected_t>, "index concatenation");
|
||||
}
|
||||
|
||||
{
|
||||
using indices_t = util::tuple::index::make_indices<4>::type;
|
||||
using expected_t = util::tuple::index::indices<0,1,2,3>;
|
||||
|
||||
tap.expect (std::is_same_v<indices_t,expected_t>, "index sequence creation");
|
||||
}
|
||||
|
||||
{
|
||||
using indices_t = util::tuple::index::make_reverse_t<4>;
|
||||
using expected_t = util::tuple::index::indices<3,2,1,0>;
|
||||
tap.expect (std::is_same_v<indices_t,expected_t>, "reverse index sequence creation");
|
||||
}
|
||||
|
||||
return tap.status ();
|
||||
}
|
126
tuple/index.hpp
Normal file
126
tuple/index.hpp
Normal file
@ -0,0 +1,126 @@
|
||||
/*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Copyright 2018 Danny Robson <danny@nerdcruft.net>
|
||||
*/
|
||||
|
||||
#ifndef CRUFT_UTIL_TUPLE_VALUE_HPP
|
||||
#define CRUFT_UTIL_TUPLE_VALUE_HPP
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// the implementation of all the below must be standalone as they are a
|
||||
// potential building block in other algorithms and we can't be sure we
|
||||
// won't create a dependency loop somewhere.
|
||||
namespace util::tuple::index {
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
template <size_t ...Indices>
|
||||
struct indices {};
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
template <typename A, typename B>
|
||||
struct cat;
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
template <
|
||||
size_t ...A,
|
||||
size_t ...B
|
||||
>
|
||||
struct cat<
|
||||
indices<A...>,
|
||||
indices<B...>
|
||||
> {
|
||||
using type = indices<A..., B...>;
|
||||
};
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
template <typename A, typename B>
|
||||
using cat_t = typename cat<A,B>::type;
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
namespace detail {
|
||||
template <size_t N>
|
||||
struct make_indices {
|
||||
using type = cat_t<
|
||||
typename make_indices<N-1>::type,
|
||||
indices<N-1>
|
||||
>;
|
||||
};
|
||||
|
||||
|
||||
template <>
|
||||
struct make_indices<0> {
|
||||
using type = indices<>;
|
||||
};
|
||||
};
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
// create a monotonically increasing index sequence over the range [0,N)
|
||||
template <size_t N>
|
||||
struct make_indices {
|
||||
using type = typename detail::make_indices<N>::type;
|
||||
};
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
template <size_t N>
|
||||
using make_indices_t = typename make_indices<N>::type;
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
namespace detail {
|
||||
template <typename Indices>
|
||||
struct make_reverse;
|
||||
|
||||
template <size_t Head, size_t ...Tail>
|
||||
struct make_reverse<
|
||||
indices<Head,Tail...>
|
||||
> {
|
||||
using type = cat_t<
|
||||
typename make_reverse<indices<Tail...>>::type,
|
||||
indices<Head>
|
||||
>;
|
||||
};
|
||||
|
||||
|
||||
template <>
|
||||
struct make_reverse<
|
||||
indices<>
|
||||
> {
|
||||
using type = indices<>;
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
// create a montonically decreasing index sequence over the range (N-1, 0].
|
||||
//
|
||||
// eg, make_reverse<3> is indices<3,2,1,0>
|
||||
template <size_t N>
|
||||
struct make_reverse {
|
||||
using type = typename detail::make_reverse<
|
||||
make_indices_t<N>
|
||||
>::type;
|
||||
};
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
template <size_t N>
|
||||
using make_reverse_t = typename make_reverse<N>::type;
|
||||
};
|
||||
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue
Block a user