FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
xtensor/include/xtensor/xaxis_iterator.hpp at stable · xtensor-stack/xtensor · GitHub
xtensor-stack
xtensor
Repository navigation
Code
Issues
381
(381)
Pull requests
44
(44)
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
xtensor
/
include
/
xtensor
/
xaxis_iterator.hpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
334 lines (291 loc) · 10.2 KB
Breadcrumbs
xtensor
/
include
/
xtensor
/
xaxis_iterator.hpp
Copy path
File metadata and controls
334 lines (291 loc) · 10.2 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*
**************************************************************************
* 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_AXIS_ITERATOR_HPP
#
define
XTENSOR_AXIS_ITERATOR_HPP
#
include
"
xstrided_view.hpp
"
namespace
xt
{
/*
*****************
* xaxis_iterator *
*****************
*/
/*
*
* @class xaxis_iterator
* @brief Class for iteration over (N-1)-dimensional slices, where
* N is the dimension of the underlying expression
*
* If N is the number of dimensions of an expression, the xaxis_iterator
* iterates over (N-1)-dimensional slices oriented along the specified axis.
*
* @tparam CT the closure type of the \ref xexpression
*/
template
<
class
CT
>
class
xaxis_iterator
{
public:
using
self_type = xaxis_iterator<
CT
>;
using
xexpression_type = std::
decay_t
<
CT
>;
using
size_type =
typename
xexpression_type::size_type;
using
difference_type =
typename
xexpression_type::difference_type;
using
shape_type =
typename
xexpression_type::shape_type;
using
value_type = xstrided_view<
CT
, shape_type>;
using
reference = std::
remove_reference_t
<
apply_cv_t
<
CT
, value_type>>;
using
pointer = xtl::xclosure_pointer<std::
remove_reference_t
<
apply_cv_t
<
CT
, value_type>>>;
using
iterator_category = std::forward_iterator_tag;
template
<
class
CTA
>
xaxis_iterator
(
CTA
&& e, size_type axis);
template
<
class
CTA
>
xaxis_iterator
(
CTA
&& e, size_type axis, size_type index, size_type offset);
self_type&
operator
++();
self_type
operator
++(
int
);
reference
operator
*()
const
;
pointer
operator
->()
const
;
bool
equal
(
const
self_type& rhs)
const
;
private:
using
storing_type = xtl::
ptr_closure_type_t
<
CT
>;
mutable
storing_type p_expression;
size_type m_index;
size_type m_add_offset;
value_type m_sv;
template
<
class
T
,
class
CTA
>
std::
enable_if_t
<std::is_pointer<T>::value, T>
get_storage_init
(
CTA
&& e)
const
;
template
<
class
T
,
class
CTA
>
std::
enable_if_t
<!std::is_pointer<T>::value, T>
get_storage_init
(
CTA
&& e)
const
;
};
template
<
class
CT
>
bool
operator
==(
const
xaxis_iterator<
CT
>& lhs,
const
xaxis_iterator<
CT
>& rhs);
template
<
class
CT
>
bool
operator
!=(
const
xaxis_iterator<
CT
>& lhs,
const
xaxis_iterator<
CT
>& rhs);
template
<
class
E
>
auto
axis_begin
(E&& e);
template
<
class
E
>
auto
axis_begin
(E&& e,
typename
std::
decay_t
<E>::size_type axis);
template
<
class
E
>
auto
axis_end
(E&& e);
template
<
class
E
>
auto
axis_end
(E&& e,
typename
std::
decay_t
<E>::size_type axis);
/*
********************************
* xaxis_iterator implementation *
********************************
*/
namespace
detail
{
template
<
class
CT
>
auto
derive_xstrided_view
(
CT
&& e,
typename
std::
decay_t
<
CT
>::size_type axis,
typename
std::
decay_t
<
CT
>::size_type offset)
{
using
xexpression_type = std::
decay_t
<
CT
>;
using
shape_type =
typename
xexpression_type::shape_type;
using
strides_type =
typename
xexpression_type::strides_type;
const
auto
& e_shape = e.
shape
();
shape_type
shape
(e_shape.
size
() -
1
);
auto
nxt =
std::copy
(e_shape.
cbegin
(), e_shape.
cbegin
() + axis, shape.
begin
());
std::copy
(e_shape.
cbegin
() + axis +
1
, e_shape.
end
(), nxt);
const
auto
& e_strides = e.
strides
();
strides_type
strides
(e_strides.
size
() -
1
);
auto
nxt_strides =
std::copy
(e_strides.
cbegin
(), e_strides.
cbegin
() + axis, strides.
begin
());
std::copy
(e_strides.
cbegin
() + axis +
1
, e_strides.
end
(), nxt_strides);
return
strided_view
(std::forward<
CT
>(e),
std::move
(shape),
std::move
(strides), offset, e.
layout
());
}
}
template
<
class
CT
>
template
<
class
T
,
class
CTA
>
inline
std::
enable_if_t
<std::is_pointer<T>::value, T>
xaxis_iterator<
CT
>::get_storage_init(
CTA
&& e)
const
{
return
&e;
}
template
<
class
CT
>
template
<
class
T
,
class
CTA
>
inline
std::
enable_if_t
<!std::is_pointer<T>::value, T>
xaxis_iterator<
CT
>::get_storage_init(
CTA
&& e)
const
{
return
e;
}
/*
*
* @name Constructors
*/
//
@{
/*
*
* Constructs an xaxis_iterator
*
* @param e the expression to iterate over
* @param axis the axis to iterate over taking N-1 dimensional slices
*/
template
<
class
CT
>
template
<
class
CTA
>
inline
xaxis_iterator<
CT
>::xaxis_iterator(
CTA
&& e, size_type axis)
: xaxis_iterator(std::forward<
CTA
>(e), axis,
0
, e.data_offset())
{
}
/*
*
* Constructs an xaxis_iterator starting at specified index and offset
*
* @param e the expression to iterate over
* @param axis the axis to iterate over taking N-1 dimensional slices
* @param index the starting index for the iterator
* @param offset the starting offset for the iterator
*/
template
<
class
CT
>
template
<
class
CTA
>
inline
xaxis_iterator<
CT
>::xaxis_iterator(
CTA
&& e, size_type axis, size_type index, size_type offset)
: p_expression(get_storage_init<storing_type>(std::forward<
CTA
>(e)))
, m_index(index)
, m_add_offset(
static_cast
<size_type>(e.strides()[axis]))
, m_sv(detail::derive_xstrided_view<
CTA
>(std::forward<
CTA
>(e), axis, offset))
{
}
//
@}
/*
*
* @name Increment
*/
//
@{
/*
*
* Increments the iterator to the next position and returns it.
*/
template
<
class
CT
>
inline
auto
xaxis_iterator<
CT
>::
operator
++() -> self_type&
{
m_sv.
set_offset
(m_sv.
data_offset
() + m_add_offset);
++m_index;
return
*
this
;
}
/*
*
* Makes a copy of the iterator, increments it to the next
* position, and returns the copy.
*/
template
<
class
CT
>
inline
auto
xaxis_iterator<
CT
>::
operator
++(
int
) -> self_type
{
self_type
tmp
(*
this
);
++(*
this
);
return
tmp;
}
//
@}
/*
*
* @name Reference
*/
//
@{
/*
*
* Returns the strided view at the current iteration position
*
* @return a strided_view
*/
template
<
class
CT
>
inline
auto
xaxis_iterator<
CT
>::
operator
*()
const
-> reference
{
return
m_sv;
}
/*
*
* Returns a pointer to the strided view at the current iteration position
*
* @return a pointer to a strided_view
*/
template
<
class
CT
>
inline
auto
xaxis_iterator<
CT
>::
operator
->()
const
-> pointer
{
return
xtl::closure_pointer
(
operator
*());
}
//
@}
/*
* @name Comparisons
*/
//
@{
/*
*
* Checks equality of the xaxis_slice_iterator and \c rhs.
*
* @param
* @return true if the iterators are equivalent, false otherwise
*/
template
<
class
CT
>
inline
bool
xaxis_iterator<
CT
>::equal(
const
self_type& rhs)
const
{
return
p_expression == rhs.
p_expression
&& m_index == rhs.
m_index
&& m_sv.
data_offset
() == rhs.
m_sv
.
data_offset
();
}
/*
*
* Checks equality of the iterators.
*
* @return true if the iterators are equivalent, false otherwise
*/
template
<
class
CT
>
inline
bool
operator
==(
const
xaxis_iterator<
CT
>& lhs,
const
xaxis_iterator<
CT
>& rhs)
{
return
lhs.
equal
(rhs);
}
/*
*
* Checks inequality of the iterators
* @return true if the iterators are different, true otherwise
*/
template
<
class
CT
>
inline
bool
operator
!=(
const
xaxis_iterator<
CT
>& lhs,
const
xaxis_iterator<
CT
>& rhs)
{
return
!(lhs == rhs);
}
//
@}
/*
*
* @name Iterators
*/
//
@{
/*
*
* Returns an iterator to the first element of the expression for axis 0
*
* @param e the expession to iterate over
* @return an instance of xaxis_iterator
*/
template
<
class
E
>
inline
auto
axis_begin
(E&& e)
{
using
return_type = xaxis_iterator<xtl::
closure_type_t
<E>>;
return
return_type
(std::forward<E>(e),
0
);
}
/*
*
* Returns an iterator to the first element of the expression for the specified axis
*
* @param e the expession to iterate over
* @param axis the axis to iterate over
* @return an instance of xaxis_iterator
*/
template
<
class
E
>
inline
auto
axis_begin
(E&& e,
typename
std::
decay_t
<E>::size_type axis)
{
using
return_type = xaxis_iterator<xtl::
closure_type_t
<E>>;
return
return_type
(std::forward<E>(e), axis);
}
/*
*
* Returns an iterator to the element following the last element of
* the expression for axis 0
*
* @param e the expession to iterate over
* @return an instance of xaxis_iterator
*/
template
<
class
E
>
inline
auto
axis_end
(E&& e)
{
using
size_type =
typename
std::
decay_t
<E>::size_type;
using
return_type = xaxis_iterator<xtl::
closure_type_t
<E>>;
return
return_type
(std::forward<E>(e),
0
, e.
shape
()[
0
],
static_cast
<size_type>(e.
strides
()[
0
])*e.
shape
()[
0
]);
}
/*
*
* Returns an iterator to the element following the last element of
* the expression for the specified axis
*
* @param e the expession to iterate over
* @param axis the axis to iterate over
* @return an instance of xaxis_iterator
*/
template
<
class
E
>
inline
auto
axis_end
(E&& e,
typename
std::
decay_t
<E>::size_type axis)
{
using
size_type =
typename
std::
decay_t
<E>::size_type;
using
return_type = xaxis_iterator<xtl::
closure_type_t
<E>>;
return
return_type
(std::forward<E>(e), axis, e.
shape
()[axis],
static_cast
<size_type>(e.
strides
()[axis])*e.
shape
()[axis]);
}
//
@}
}
#
endif
Back
|
FazBrowse Home
|
New Git URL