-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
static_view
163 lines (144 loc) · 7.08 KB
/
static_view
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
Copyright (C) 2018-2024 Geoffrey Daniels. https://gpdaniels.com/
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 3 of the License only.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#ifndef GTL_CONTAINER_STATIC_VIEW_HPP
#define GTL_CONTAINER_STATIC_VIEW_HPP
// Summary: A non-owning static_view into multi-dimensional memory. [wip]
#ifndef NDEBUG
# if defined(_MSC_VER)
# define __builtin_trap() __debugbreak()
# endif
/// @brief A simple assert macro to break the program if the static_view is misused.
# define GTL_STATIC_VIEW_ASSERT(ASSERTION, MESSAGE) static_cast<void>((ASSERTION) || (__builtin_trap(), 0))
#else
/// @brief At release time the assert macro is implemented as a nop.
# define GTL_STATIC_VIEW_ASSERT(ASSERTION, MESSAGE) static_cast<void>(0)
#endif
namespace gtl {
template <typename type, unsigned int... dimension_sizes>
class static_view final {
private:
template <unsigned int... all_dimension_sizes>
struct slice_type_generator final {
using slice_type = type*;
};
template <unsigned int first_dimension_size, unsigned int... remaining_dimension_sizes>
struct slice_type_generator<first_dimension_size, remaining_dimension_sizes...> final {
using slice_type = static_view<type, remaining_dimension_sizes...>;
};
using slice_type = typename slice_type_generator<dimension_sizes...>::slice_type;
private:
type* slice;
public:
/// @brief Constructor that takes a pointer to the memory to static_view.
static_view(type* memory)
: slice(memory) {
}
public:
/// @brief Get the number of dimensions of the static_view.
/// @return The number of dimensions of the static_view.
constexpr static unsigned int dimensions() {
return (sizeof...(dimension_sizes));
}
/// @brief Get the total size of the static_view.
/// @return The size of memory in the static_view.
constexpr static unsigned int size() {
if constexpr (dimensions() == 0) {
return 0;
}
else {
return ((dimensions() > 0) * ... * dimension_sizes);
}
}
/// @brief Get the size of a specified dimension in the static_view.
/// @param dimension_index The index of the dimension we want the size of.
/// @return The size of dimension in the static_view specified by index.
constexpr static unsigned int size(unsigned int dimension_index) {
if constexpr (dimensions() == 0) {
static_cast<void>(dimension_index);
return 0;
}
else {
GTL_STATIC_VIEW_ASSERT(dimension_index < dimensions(), "Dimension index must be within number of dimensions of the array");
const unsigned int dimension_array[dimensions()] = { dimension_sizes... };
return dimension_array[dimension_index];
}
}
public:
constexpr type* data() {
return this->slice;
}
public:
/// @brief Get a const reference to either the array or value of this level.
/// @param index Used to specify the location to get within the current dimension size.
/// @return A const reference to either the array or value of this level.
constexpr decltype(auto) operator[](unsigned int index) const {
GTL_STATIC_VIEW_ASSERT(index < size(0), "Array index must be less than the maximum size of the dimension.");
if constexpr (dimensions() == 0) {
return nullptr;
}
else if constexpr (dimensions() == 1) {
return static_cast<const type&>(*(this->data + index));
}
else {
return slice_type(this->slice + index * slice_type::size());
}
}
/// @brief Get a non-const reference to either the array or value of this level.
/// @param index Used to specify the location to get within the current dimension size.
/// @return A non-const reference to either the array or value of this level.
constexpr decltype(auto) operator[](unsigned int index) {
GTL_STATIC_VIEW_ASSERT(index < size(0), "Array index must be less than the maximum size of the dimension.");
if constexpr (dimensions() == 0) {
return nullptr;
}
else if constexpr (dimensions() == 1) {
return static_cast<type&>(*(this->slice + index));
}
else {
return slice_type(this->slice + index * slice_type::size());
}
}
public:
/// @brief Get a const reference to the slice_type of the level argument-count below.
/// @param first_index Used to specify the location to get within the current dimension size.
/// @param remaining_indexes Used to specify the locations for the argument-count dimension sizes below.
/// @return A const reference to either the array or value of the level argument-count below.
template <typename... index_types>
constexpr const type& operator()(unsigned int first_index, index_types... remaining_indexes) const {
static_assert(1 + sizeof...(remaining_indexes) == sizeof...(dimension_sizes), "Number of indexes must be equal to the number of dimensions.");
if constexpr (dimensions() == 1) {
return this->operator[](first_index);
}
else {
return this->operator[](first_index)(remaining_indexes...);
}
}
/// @brief Get a non-const reference to the slice_type of the level argument-count below.
/// @param first_index Used to specify the location to get within the current dimension size.
/// @param remaining_indexes Used to specify the locations for the argument-count dimension sizes below.
/// @return A non-const reference to either the array or value of the level argument-count below.
template <typename... index_types>
constexpr type& operator()(unsigned int first_index, index_types... remaining_indexes) {
static_assert(1 + sizeof...(remaining_indexes) == sizeof...(dimension_sizes), "Number of indexes must be equal to the number of dimensions.");
if constexpr (dimensions() == 1) {
return this->operator[](first_index);
}
else {
return this->operator[](first_index).operator()(remaining_indexes...);
}
}
};
}
#undef GTL_STATIC_VIEW_ASSERT
#endif // GTL_CONTAINER_STATIC_VIEW_HPP