forked from altera-fpga/hls-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtuple.hpp
119 lines (100 loc) · 2.69 KB
/
tuple.hpp
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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
#ifndef __TUPLE_HPP__
#define __TUPLE_HPP__
#include <type_traits>
namespace fpga_tools {
//
// Generic tuple
//
// USAGE EXAMPLE:
// Tuple<char,short,int,long> my_tuple;
// char a = my_tuple.get<0>();
// short b = my_tuple.get<1>();
// int c = my_tuple.get<2>();
// long d = my_tuple.get<3>();
//
template <typename... Tys>
struct Tuple {
Tuple(Tys... Args) : values(Args...) {}
Tuple() {}
//
// get the index'th item in the tuple of values
//
template <int index>
auto& get() {
static_assert(index < NumTys, "index out of bounds");
return get_impl<index, Tys...>(values);
}
//
// helper to get the first element in the tuple
//
auto& first() { return get<0>(); }
//
// helper to get the last element in the tuple
//
auto& last() { return get<NumTys - 1>(); }
private:
//
// generic tuple implementation: recursive case
//
template <typename CurrentTy, typename... OtherTys>
struct tuple_impl {
tuple_impl(CurrentTy& current, OtherTys... others)
: value(current), other_values(others...) {}
tuple_impl() {}
using ValueTy = CurrentTy;
ValueTy value;
tuple_impl<OtherTys...> other_values;
};
//
// generic tuple implementation: base case
//
template <typename FinalTy>
struct tuple_impl<FinalTy> {
tuple_impl(FinalTy& current) : value(current) {}
tuple_impl() {}
using ValueTy = FinalTy;
ValueTy value;
};
// the tuple values
tuple_impl<Tys...> values;
// the number of tuple values
constexpr static auto NumTys = sizeof...(Tys);
//
// implementation of 'get' for general tuple
//
template <int index, typename HeadTy, typename... TailTys>
static auto& get_impl(tuple_impl<HeadTy, TailTys...>& sub_tuple) {
if constexpr (index == 0) {
// base case
return sub_tuple.value;
} else {
// recursive case
return get_impl<index - 1, TailTys...>(sub_tuple.other_values);
}
}
};
//
// NTuple implementation
// This is convenient way to have N elements of the same type
// somewhat like an std::array
//
template <int, typename Type>
using NTupleElem = Type;
template <typename Type, std::size_t... Idx>
static auto make_NTupleImpl(std::index_sequence<Idx...>)
-> Tuple<NTupleElem<Idx, Type>...>;
template <int N, typename Type>
using make_NTuple =
decltype(make_NTupleImpl<Type>(std::make_index_sequence<N>()));
//
// convenience alias for a tuple of N elements of the same type
// NOTE: for this alias, typename comes FIRST (to match std::array)
//
// USAGE EXAMPLE:
// NTuple<int, 10> elements;
// elements.get<3>() = 17;
//
template <typename Type, int N>
using NTuple = make_NTuple<N, Type>;
} // namespace fpga_tools
#endif /* __TUPLE_HPP__ */