GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — C++11 compatibility · hmenke/cpp-patterns@3f13fbd

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • multi_array
    • CMakeLists.txt
    • example.cpp
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎multi_array/CMakeLists.txt‎

Copy file name to clipboardExpand all lines: multi_array/CMakeLists.txt
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
cmake_minimum_required(VERSION 3.1 FATAL_ERROR)
22

33
project(multi_array CXX)
4-
set(CMAKE_CXX_STANDARD 14)
4+
set(CMAKE_CXX_STANDARD 11)
55

66
# Add the executable
77

Collapse file

‎multi_array/example.cpp‎

Copy file name to clipboardExpand all lines: multi_array/example.cpp
+58-59Lines changed: 58 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,53 @@ struct all<> {
6868
static constexpr bool const value = true;
6969
};
7070

71-
// index_sequence
71+
// linearized_index
7272

73-
// https://stackoverflow.com/a/24481400/1944004
74-
template <std::size_t... I>
75-
struct index_sequence {};
73+
template <size_t n, size_t... N>
74+
struct linearized_index {
75+
template <typename... Idx>
76+
constexpr std::size_t operator()(Idx... idx) const {
77+
using unpack = std::size_t[];
78+
return unpack{std::size_t(idx)...}[n] +
79+
unpack{std::size_t(N)...}[n] *
80+
linearized_index<n - 1, N...>{}(idx...);
81+
}
82+
};
7683

77-
template <std::size_t N, std::size_t... I>
78-
struct make_index_sequence : public make_index_sequence<N - 1, N - 1, I...> {};
84+
template <size_t... N>
85+
struct linearized_index<0, N...> {
86+
template <typename... Idx>
87+
constexpr std::size_t operator()(Idx... idx) const {
88+
using unpack = std::size_t[];
89+
return unpack{std::size_t(idx)...}[0];
90+
}
91+
};
7992

80-
template <std::size_t... I>
81-
struct make_index_sequence<0, I...> : public index_sequence<I...> {};
93+
// check_bounds
94+
95+
template <size_t n, size_t... N>
96+
struct check_bounds {
97+
template <typename... Idx>
98+
constexpr bool operator()(Idx... idx) const {
99+
using unpack = std::size_t[];
100+
return unpack{std::size_t(idx)...}[n] < unpack{std::size_t(N)...}[n]
101+
? check_bounds<n - 1, N...>{}(idx...)
102+
: throw std::out_of_range("index out of bounds: " +
103+
std::to_string(n));
104+
}
105+
};
106+
107+
template <size_t... N>
108+
struct check_bounds<0, N...> {
109+
template <typename... Idx>
110+
constexpr bool operator()(Idx... idx) const {
111+
using unpack = std::size_t[];
112+
return unpack{std::size_t(idx)...}[0] < unpack{std::size_t(N)...}[0]
113+
? false
114+
: throw std::out_of_range("index out of bounds: " +
115+
std::to_string(0));
116+
}
117+
};
82118

83119
} // namespace meta
84120

@@ -106,37 +142,6 @@ class multi_array {
106142
template <size_type i, size_type... pack>
107143
using size_element = meta::pack_element<i, size_type, pack...>;
108144

109-
template <size_type... I, typename... Idx>
110-
CXX14_CONSTEXPR size_type linearized_index(meta::index_sequence<I...>,
111-
Idx... idx) const {
112-
size_type index = 0;
113-
using unpack = size_type[];
114-
(void)unpack{0UL,
115-
((void)(index = (index + unpack{size_type(idx)...}[I]) *
116-
size_element<I + 1, N...>::value),
117-
0UL)...};
118-
return index + unpack{size_type(idx)...}[sizeof...(idx) - 1];
119-
}
120-
121-
#ifndef NDEBUG
122-
#define check_bounds(a, b) check_bounds_impl(a, b)
123-
#else
124-
#define check_bounds(a, b)
125-
#endif // NDEBUG
126-
127-
template <size_type... I, typename... Idx>
128-
CXX14_CONSTEXPR void check_bounds_impl(meta::index_sequence<I...>,
129-
Idx... idx) const {
130-
using unpack = size_type[];
131-
(void)unpack{
132-
0UL,
133-
((void)(unpack{size_type(idx)...}[I] < size_element<I, N...>::value
134-
? 0
135-
: throw std::out_of_range("index out of bounds: " +
136-
std::to_string(I))),
137-
0UL)...};
138-
}
139-
140145
// Storage
141146
value_type m_data[size_product<N...>::value];
142147

@@ -177,44 +182,40 @@ class multi_array {
177182
static_assert(
178183
meta::all<std::is_convertible<Idx, size_type>::value...>::value,
179184
"type mismatch");
180-
check_bounds(meta::make_index_sequence<sizeof...(idx)>{}, idx...);
181-
size_type index = linearized_index(
182-
meta::make_index_sequence<sizeof...(idx) - 1>{}, idx...);
183-
return m_data[index];
185+
return
186+
#ifndef NDEBUG
187+
meta::check_bounds<sizeof...(idx) - 1, N...>{}(idx...),
188+
#endif
189+
m_data[meta::linearized_index<sizeof...(idx) - 1, N...>{}(idx...)];
184190
}
185191

186192
template <typename... Idx>
187-
CXX14_CONSTEXPR value_type operator()(Idx... idx) const noexcept {
193+
constexpr value_type operator()(Idx... idx) const noexcept {
188194
static_assert(sizeof...(idx) == sizeof...(N), "dimension mismatch");
189195
static_assert(
190196
meta::all<std::is_convertible<Idx, size_type>::value...>::value,
191197
"type mismatch");
192-
check_bounds(meta::make_index_sequence<sizeof...(idx)>{}, idx...);
193-
size_type index = linearized_index(
194-
meta::make_index_sequence<sizeof...(idx) - 1>{}, idx...);
195-
return m_data[index];
198+
return
199+
#ifndef NDEBUG
200+
meta::check_bounds<sizeof...(idx) - 1, N...>{}(idx...),
201+
#endif
202+
m_data[meta::linearized_index<sizeof...(idx) - 1, N...>{}(idx...)];
196203
}
197204

198205
template <size_type... idx>
199206
CXX14_CONSTEXPR reference get() noexcept {
200207
static_assert(sizeof...(idx) == sizeof...(N), "dimension mismatch");
201208
static_assert(meta::all<size_less<idx, N>::value...>::value,
202209
"index out of bounds");
203-
check_bounds(meta::make_index_sequence<sizeof...(idx)>{}, idx...);
204-
size_type index = linearized_index(
205-
meta::make_index_sequence<sizeof...(idx) - 1>{}, idx...);
206-
return m_data[index];
210+
return this->operator()(idx...);
207211
}
208212

209213
template <size_type... idx>
210-
CXX14_CONSTEXPR value_type get() const noexcept {
214+
constexpr value_type get() const noexcept {
211215
static_assert(sizeof...(idx) == sizeof...(N), "dimension mismatch");
212216
static_assert(meta::all<size_less<idx, N>::value...>::value,
213217
"index out of bounds");
214-
check_bounds(meta::make_index_sequence<sizeof...(idx)>{}, idx...);
215-
size_type index = linearized_index(
216-
meta::make_index_sequence<sizeof...(idx) - 1>{}, idx...);
217-
return m_data[index];
218+
return this->operator()(idx...);
218219
}
219220

220221
template <size_type i>
@@ -226,8 +227,6 @@ class multi_array {
226227
constexpr size_type size() const noexcept {
227228
return size_product<N...>::value;
228229
}
229-
230-
#undef check_bounds
231230
};
232231

233232
} // namespace math

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.