forked from xtensor-stack/xtensor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxexception.hpp
More file actions
299 lines (259 loc) · 10.1 KB
/
Copy pathxexception.hpp
File metadata and controls
299 lines (259 loc) · 10.1 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/***************************************************************************
* Copyright (c) Johan Mabille, Sylvain Corlay and Wolf Vollprecht *
* Copyright (c) QuantStack *
* *
* Distributed under the terms of the BSD 3-Clause License. *
* *
* The full license is in the file LICENSE, distributed with this software. *
****************************************************************************/
#ifndef XTENSOR_EXCEPTION_HPP
#define XTENSOR_EXCEPTION_HPP
#include <iterator>
#include <sstream>
#include <stdexcept>
#include <string>
#include "xtensor_config.hpp"
namespace xt
{
/*******************
* broadcast_error *
*******************/
class broadcast_error : public std::runtime_error
{
public:
explicit broadcast_error(const char* msg)
: std::runtime_error(msg)
{
}
};
template <class S1, class S2>
[[noreturn]] void throw_broadcast_error(const S1& lhs, const S2& rhs);
/*********************
* concatenate_error *
*********************/
class concatenate_error : public std::runtime_error
{
public:
explicit concatenate_error(const char* msg)
: std::runtime_error(msg)
{
}
};
template <class S1, class S2>
[[noreturn]] void throw_concatenate_error(const S1& lhs, const S2& rhs);
/**********************************
* broadcast_error implementation *
**********************************/
namespace detail
{
template <class S1, class S2>
inline std::string shape_error_message(const S1& lhs, const S2& rhs)
{
std::ostringstream buf("Incompatible dimension of arrays:", std::ios_base::ate);
buf << "\n LHS shape = (";
using size_type1 = typename S1::value_type;
std::ostream_iterator<size_type1> iter1(buf, ", ");
std::copy(lhs.cbegin(), lhs.cend(), iter1);
buf << ")\n RHS shape = (";
using size_type2 = typename S2::value_type;
std::ostream_iterator<size_type2> iter2(buf, ", ");
std::copy(rhs.cbegin(), rhs.cend(), iter2);
buf << ")";
return buf.str();
}
}
#ifdef NDEBUG
// Do not inline this function
template <class S1, class S2>
[[noreturn]] void throw_broadcast_error(const S1&, const S2&)
{
XTENSOR_THROW(broadcast_error, "Incompatible dimension of arrays, compile in DEBUG for more info");
}
#else
template <class S1, class S2>
[[noreturn]] void throw_broadcast_error(const S1& lhs, const S2& rhs)
{
std::string msg = detail::shape_error_message(lhs, rhs);
XTENSOR_THROW(broadcast_error, msg.c_str());
}
#endif
/************************************
* concatenate_error implementation *
************************************/
#ifdef NDEBUG
// Do not inline this function
template <class S1, class S2>
[[noreturn]] void throw_concatenate_error(const S1&, const S2&)
{
XTENSOR_THROW(concatenate_error, "Incompatible dimension of arrays, compile in DEBUG for more info");
}
#else
template <class S1, class S2>
[[noreturn]] void throw_concatenate_error(const S1& lhs, const S2& rhs)
{
std::string msg = detail::shape_error_message(lhs, rhs);
XTENSOR_THROW(concatenate_error, msg.c_str());
}
#endif
/*******************
* transpose_error *
*******************/
class transpose_error : public std::runtime_error
{
public:
explicit transpose_error(const char* msg)
: std::runtime_error(msg)
{
}
};
/***************
* check_index *
***************/
template <class S, class... Args>
void check_index(const S& shape, Args... args);
template <class S, class It>
void check_element_index(const S& shape, It first, It last);
namespace detail
{
template <class S, std::size_t dim>
inline void check_index_impl(const S&)
{
}
template <class S, std::size_t dim, class T, class... Args>
inline void check_index_impl(const S& shape, T arg, Args... args)
{
if (sizeof...(Args) + 1 > shape.size())
{
check_index_impl<S, dim>(shape, args...);
}
else
{
if (std::size_t(arg) >= std::size_t(shape[dim]) && shape[dim] != 1)
{
XTENSOR_THROW(std::out_of_range,
"index " + std::to_string(arg) + " is out of bounds for axis " +
std::to_string(dim) + " with size " + std::to_string(shape[dim]));
}
check_index_impl<S, dim + 1>(shape, args...);
}
}
}
template <class S, class... Args>
inline void check_index(const S& shape, Args... args)
{
using value_type = typename S::value_type;
detail::check_index_impl<S, 0>(shape, static_cast<value_type>(args)...);
}
template <class S, class It>
inline void check_element_index(const S& shape, It first, It last)
{
using value_type = typename std::iterator_traits<It>::value_type;
using size_type = typename S::size_type;
auto dst = static_cast<size_type>(last - first);
It efirst = last - static_cast<std::ptrdiff_t>((std::min)(shape.size(), dst));
std::size_t axis = 0;
if(shape.empty())
{
if(first != last && *(--last) != value_type(0))
{
XTENSOR_THROW(std::out_of_range, "index out of bound (empty array)");
}
else
{
return;
}
}
else
{
while (efirst != last)
{
if (*efirst >= value_type(shape[axis]) && shape[axis] != 1)
{
XTENSOR_THROW(std::out_of_range,
"index " + std::to_string(*efirst) +
" is out of bounds for axis " +
std::to_string(axis) + " with size " +
std::to_string(shape[axis]));
}
++efirst, ++axis;
}
}
}
/*******************
* check_dimension *
*******************/
template <class S, class... Args>
inline void check_dimension(const S& shape, Args...)
{
if (sizeof...(Args) > shape.size())
{
XTENSOR_THROW(std::out_of_range,
"Number of arguments (" + std::to_string(sizeof...(Args)) +
") us greater than the number of dimensions (" +
std::to_string(shape.size()) + ")");
}
}
/****************
* check_access *
****************/
template <class S, class... Args>
inline void check_access(const S& shape, Args... args)
{
check_dimension(shape, args...);
check_index(shape, args...);
}
#if (defined(XTENSOR_ENABLE_ASSERT) && !defined(XTENSOR_DISABLE_EXCEPTIONS))
#define XTENSOR_TRY(expr) XTENSOR_TRY_IMPL(expr, __FILE__, __LINE__)
#define XTENSOR_TRY_IMPL(expr, file, line) \
try \
{ \
expr; \
} \
catch (std::exception& e) \
{ \
XTENSOR_THROW(std::runtime_error, \
std::string(file) + ':' + std::to_string(line) + \
": check failed\n\t" + std::string(e.what())); \
}
#else
#define XTENSOR_TRY(expr)
#endif
#ifdef XTENSOR_ENABLE_ASSERT
#define XTENSOR_ASSERT(expr) XTENSOR_ASSERT_IMPL(expr, __FILE__, __LINE__)
#define XTENSOR_ASSERT_IMPL(expr, file, line) \
if (!(expr)) \
{ \
XTENSOR_THROW(std::runtime_error, \
std::string(file) + ':' + std::to_string(line) + \
": assertion failed (" #expr ") \n\t"); \
}
#else
#define XTENSOR_ASSERT(expr)
#endif
#ifdef XTENSOR_ENABLE_CHECK_DIMENSION
#define XTENSOR_CHECK_DIMENSION(S, ARGS) XTENSOR_TRY(check_dimension(S, ARGS))
#else
#define XTENSOR_CHECK_DIMENSION(S, ARGS)
#endif
#ifdef XTENSOR_ENABLE_ASSERT
#define XTENSOR_ASSERT_MSG(expr, msg) \
if (!(expr)) \
{ \
XTENSOR_THROW(std::runtime_error, \
std::string("Assertion error!\n") + msg + \
"\n " + __FILE__ + '(' + std::to_string(__LINE__) + \
")\n"); \
}
#else
#define XTENSOR_ASSERT_MSG(expr, msg)
#endif
#define XTENSOR_PRECONDITION(expr, msg) \
if (!(expr)) \
{ \
XTENSOR_THROW(std::runtime_error, \
std::string("Precondition violation!\n") + msg + \
"\n " + __FILE__ + '(' + std::to_string(__LINE__) + \
")\n"); \
}
}
#endif // XEXCEPTION_HPP