Cleaned up the compile_time_integer_list and make_compile_time_integer_range

types and put them into their own file.
This commit is contained in:
Davis King 2017-11-20 19:31:20 -05:00
parent 4d5c77cacb
commit af99cde18a
3 changed files with 84 additions and 35 deletions

View File

@ -18,6 +18,7 @@
#include <vector>
#include "tensor_tools.h"
#include <type_traits>
#include "../metaprogramming.h"
@ -143,33 +144,10 @@ namespace dlib
namespace impl
{
template <size_t... n>
struct ct_integers_list {
template <size_t m>
struct push_back
{
typedef ct_integers_list<n..., m> type;
};
};
template <size_t max>
struct ct_make_integer_range
{
// recursively call push_back on ct_integers_list to build a range from 1 to max
// inclusive.
typedef typename ct_make_integer_range<max-1>::type::template push_back<max>::type type;
};
template <>
struct ct_make_integer_range<0>
{
typedef ct_integers_list<> type;
};
template <size_t... indices, typename Tuple>
auto tuple_subset(
const Tuple& item,
ct_integers_list<indices...>
compile_time_integer_list<indices...>
) -> decltype(std::make_tuple(std::get<indices>(item)...))
{
return std::make_tuple(std::get<indices>(item)...);
@ -180,7 +158,7 @@ namespace dlib
const std::tuple<Head, Tail...>& item
)
{
return tuple_subset(item, typename ct_make_integer_range<sizeof...(Tail)>::type());
return tuple_subset(item, typename make_compile_time_integer_range<sizeof...(Tail)>::type());
}
template <typename T>
@ -192,15 +170,15 @@ namespace dlib
template <typename... T>
auto tuple_flatten(
const std::tuple<T...>& item
) -> decltype(tuple_flatten(item, typename ct_make_integer_range<sizeof...(T)>::type()))
) -> decltype(tuple_flatten(item, typename make_compile_time_integer_range<sizeof...(T)>::type()))
{
return tuple_flatten(item, typename ct_make_integer_range<sizeof...(T)>::type());
return tuple_flatten(item, typename make_compile_time_integer_range<sizeof...(T)>::type());
}
template <size_t... indices, typename... T>
auto tuple_flatten(
const std::tuple<T...>& item,
ct_integers_list<indices...>
compile_time_integer_list<indices...>
) -> decltype(std::tuple_cat(tuple_flatten(std::get<indices-1>(item))...))
{
return std::tuple_cat(tuple_flatten(std::get<indices-1>(item))...);

View File

@ -5,15 +5,14 @@
#include "global_function_search.h"
// TODO, move ct_make_integer_range into some other file so we don't have to include the
// dnn header. That thing is huge.
#include <dlib/dnn.h>
#include "../metaprogramming.h"
#include <utility>
namespace dlib
{
namespace gopt_impl
{
// ----------------------------------------------------------------------------------------
class disable_decay_to_scalar
@ -29,10 +28,11 @@ namespace dlib
auto _cwv (
T&& f,
const matrix<double,0,1>& a,
impl::ct_integers_list<indices...>
compile_time_integer_list<indices...>
) -> decltype(f(a(indices-1)...))
{
DLIB_CASSERT(a.size() == sizeof...(indices), "You invoked dlib::call_with_vect(f,a) but the number of arguments expected by f() doesn't match the size of 'a'. "
DLIB_CASSERT(a.size() == sizeof...(indices),
"You invoked dlib::call_with_vect(f,a) but the number of arguments expected by f() doesn't match the size of 'a'. "
<< "Expected " << sizeof...(indices) << " arguments but got " << a.size() << "."
);
return f(a(indices-1)...);
@ -45,9 +45,9 @@ namespace dlib
struct call_with_vect
{
template <typename T>
static auto go(T&& f, const matrix<double,0,1>& a) -> decltype(_cwv(std::forward<T>(f),a,typename impl::ct_make_integer_range<max_unpack>::type()))
static auto go(T&& f, const matrix<double,0,1>& a) -> decltype(_cwv(std::forward<T>(f),a,typename make_compile_time_integer_range<max_unpack>::type()))
{
return _cwv(std::forward<T>(f),a,typename impl::ct_make_integer_range<max_unpack>::type());
return _cwv(std::forward<T>(f),a,typename make_compile_time_integer_range<max_unpack>::type());
}
template <typename T>

71
dlib/metaprogramming.h Normal file
View File

@ -0,0 +1,71 @@
// Copyright (C) 2017 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#ifndef DLIB_METApROGRAMMING_Hh_
#define DLIB_METApROGRAMMING_Hh_
namespace dlib
{
// ----------------------------------------------------------------------------------------
template <size_t... n>
struct compile_time_integer_list
{
/*!
WHAT THIS OBJECT REPRESENTS
The point of this type is to, as the name suggests, hold a compile time list of integers.
As an example, here is something simple you could do with it:
template <size_t... ints>
void print_compile_time_ints (
compile_time_integer_list<ints...>
)
{
print(ints...);
}
int main()
{
print_compile_time_ints(compile_time_integer_list<0,4,9>());
}
Which just calls: print(0,4,9);
This is a simple example, but this kind of thing is useful in larger and
more complex template metaprogramming constructs.
!*/
template <size_t m>
struct push_back
{
typedef compile_time_integer_list<n..., m> type;
};
};
// ----------------------------------------------------------------------------------------
template <size_t max>
struct make_compile_time_integer_range
{
/*!
WHAT THIS OBJECT REPRESENTS
This object makes a compile_time_integer_list containing the integers in the range [1,max] inclusive.
For example:
make_compile_time_integer_range<4>::type
evaluates to:
compile_time_integer_list<1,2,3,4>
!*/
typedef typename make_compile_time_integer_range<max-1>::type::template push_back<max>::type type;
};
// base case
template <> struct make_compile_time_integer_range<0> { typedef compile_time_integer_list<> type; };
// ----------------------------------------------------------------------------------------
}
#endif // DLIB_METApROGRAMMING_Hh_