Basic.hpp 3.81 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#pragma once

#include <cassert>
#include <tuple>
#include <map>
#include <list>

#include "IndexSeq.hpp"

namespace AMDiS
{  
    template <int I>
    using int_ = std::integral_constant<int, I>;

    template <bool B>
    using bool_ = std::integral_constant<bool, B>;

    template <size_t I>
    using index_ = std::integral_constant<size_t, I>;
    
    template <class T>
    using IdxPairList = std::map< std::pair<int, int>, std::list<std::shared_ptr<T> > >;
    
    template <class T>
    using IdxList = std::map< int, std::list<std::shared_ptr<T> > >;
  
  
    namespace Impl
    {
30
31
	template <class Tuple, size_t N>
	struct ConstructTuple
32
	{
33
34
35
36
37
38
39
40
	    // add arg to repeated constructor argument list
	    template <class Arg, class... Args>
	    static Tuple make(Arg&& arg, Args&&... args)
	    {
		return ConstructTuple<Tuple, N-1>::make( 
		    std::forward<Arg>(arg), std::forward<Arg>(arg), std::forward<Args>(args)...);
	    }
	};
41
	
42
43
	template <class Tuple>
	struct ConstructTuple<Tuple, 1>
44
	{
45
46
47
48
49
50
51
52
	    template <class... Args>
	    static Tuple make(Args&&... args)
	    {
		static_assert(std::tuple_size<Tuple>::value == sizeof...(args), 
		    "Nr. of argument != tuple-size");
		return Tuple{std::forward<Args>(args)...};
	    }
	};
53
	
54
55
	template <class Tuple>
	struct ConstructTuple<Tuple, 0>
56
	{
57
58
59
60
61
62
63
64
65
	    template <class... Args>
	    static Tuple make(Args&&... args)
	    {
		static_assert(std::tuple_size<Tuple>::value == 0, 
		    "Construction of empty tuples with empty argument list only!");
		return {};
	    }
	};
	
66
67
68
69
70
71
    } // end namespace Impl
    
    // construct a tuple with each element constructed by the same argument arg.
    template <class Tuple, class Arg>
    Tuple construct_tuple(Arg&& arg)
    {
72
73
	return Impl::ConstructTuple<Tuple, std::tuple_size<Tuple>::value>::make( 
	    std::forward<Arg>(arg));
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
    }
    
    // -----------
    
    template <template<class> class Base, class Tuple, class Indices> struct MakeTuple;
    
    template <template<class> class Base, class Tuple, size_t... I> 
    struct MakeTuple<Base, Tuple, Seq<I...>>
    {
	using type = std::tuple<Base<std::tuple_element_t<I, Tuple>>...>;
    };
    
    template <template<class> class Base, class Tuple> 
    using MakeTuple_t = 
	typename MakeTuple<Base, Tuple, MakeSeq_t<std::tuple_size<Tuple>::value>>::type;
    
    // -----------
    
    template <template<class,class> class Base, class Tuple1, class Tuple2, class Indices> struct MakeTuple2;
    
    template <template<class,class> class Base, class Tuple1, class Tuple2, size_t... I> 
    struct MakeTuple2<Base, Tuple1, Tuple2, Seq<I...>>
    {
	using type = std::tuple<Base<std::tuple_element_t<I, Tuple1>, std::tuple_element_t<I, Tuple2>>...>;
    };
    
    template <template<class,class> class Base, class Tuple1, class Tuple2> 
    using MakeTuple2_t = 
	typename MakeTuple2<Base, Tuple1, Tuple2, MakeSeq_t<std::tuple_size<Tuple1>::value>>::type;
    
    // -----------
    
    template <template<class...> class Base, class Tuple, class Indices> struct ExpandTuple;
    
    template <template<class...> class Base, class Tuple, size_t... I> 
    struct ExpandTuple<Base, Tuple, Seq<I...>>
    {
	using type = Base<std::tuple_element_t<I, Tuple>...>;
    };
    
    template <template<class...> class Base, class Tuple> 
    using ExpandTuple_t = 
	typename ExpandTuple<Base, Tuple, MakeSeq_t<std::tuple_size<Tuple>::value>>::type;
    
    
    // -----------
    
    template <class T, class Indices> struct RepeatedTuple;
    
    template <class T, size_t... I> 
    struct RepeatedTuple<T, Seq<I...>>
    {
	template <size_t, class U> using Id = U;	
	using type = std::tuple<Id<I, T>...>;
    };
    
    template <size_t N, class T> 
    using Repeat_t = 
	typename RepeatedTuple<T, MakeSeq_t<N>>::type;
    
  
135
136
137
138
139
140
    // -----------
	
    template <class T>
    using owner = T;
	
	
141
} // end namespace AMDiS