-
Notifications
You must be signed in to change notification settings - Fork 390
/
xstrided_view.hpp
921 lines (785 loc) · 32.9 KB
/
xstrided_view.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
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
/***************************************************************************
* 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_STRIDED_VIEW_HPP
#define XTENSOR_STRIDED_VIEW_HPP
#include <algorithm>
#include <cstddef>
#include <tuple>
#include <type_traits>
#include <utility>
#include <xtl/xsequence.hpp>
#include <xtl/xvariant.hpp>
#include "xexpression.hpp"
#include "xiterable.hpp"
#include "xlayout.hpp"
#include "xsemantic.hpp"
#include "xstorage.hpp"
#include "xstrided_view_base.hpp"
#include "xutils.hpp"
namespace xt
{
/***************************
* xstrided_view extension *
***************************/
namespace extension
{
template <class Tag, class CT, class S, layout_type L, class FST>
struct xstrided_view_base_impl;
template <class CT, class S, layout_type L, class FST>
struct xstrided_view_base_impl<xtensor_expression_tag, CT, S, L, FST>
{
using type = xtensor_empty_base;
};
template <class CT, class S, layout_type L, class FST>
struct xstrided_view_base : xstrided_view_base_impl<xexpression_tag_t<CT>, CT, S, L, FST>
{
};
template <class CT, class S, layout_type L, class FST>
using xstrided_view_base_t = typename xstrided_view_base<CT, S, L, FST>::type;
}
template <layout_type L1, layout_type L2, class T>
struct select_iterable_base
{
using type = std::conditional_t<L1 == L2 && L1 != layout_type::dynamic, xcontiguous_iterable<T>, xiterable<T>>;
};
template <layout_type L1, layout_type L2, class T>
using select_iterable_base_t = typename select_iterable_base<L1, L2, T>::type;
template <class CT, class S, layout_type L, class FST>
class xstrided_view;
template <class CT, class S, layout_type L, class FST>
struct xcontainer_inner_types<xstrided_view<CT, S, L, FST>>
{
using xexpression_type = std::decay_t<CT>;
using undecay_expression = CT;
using reference = inner_reference_t<undecay_expression>;
using const_reference = typename xexpression_type::const_reference;
using size_type = typename xexpression_type::size_type;
using shape_type = std::decay_t<S>;
using undecay_shape = S;
using storage_getter = FST;
using inner_storage_type = typename storage_getter::type;
using temporary_type = typename detail::xtype_for_shape<
S>::template type<typename xexpression_type::value_type, xexpression_type::static_layout>;
using storage_type = std::remove_reference_t<inner_storage_type>;
static constexpr layout_type layout = L;
};
template <class CT, class S, layout_type L, class FST>
struct xiterable_inner_types<xstrided_view<CT, S, L, FST>>
{
using inner_shape_type = std::decay_t<S>;
using inner_strides_type = get_strides_t<inner_shape_type>;
using inner_backstrides_type_type = inner_strides_type;
using const_stepper = std::conditional_t<
is_indexed_stepper<typename std::decay_t<CT>::stepper>::value,
xindexed_stepper<const xstrided_view<CT, S, L, FST>, true>,
xstepper<const xstrided_view<CT, S, L, FST>>>;
using stepper = std::conditional_t<
is_indexed_stepper<typename std::decay_t<CT>::stepper>::value,
xindexed_stepper<xstrided_view<CT, S, L, FST>, false>,
xstepper<xstrided_view<CT, S, L, FST>>>;
};
template <class CT, class S, layout_type L, class FST, class RHS>
struct can_assign<xstrided_view<CT, S, L, FST>, RHS> : can_assign<CT, RHS>
{
};
/*****************
* xstrided_view *
*****************/
/**
* @class xstrided_view
* @brief View of an xexpression using strides
*
* The xstrided_view class implements a view utilizing an initial offset
* and strides.
*
* @tparam CT the closure type of the \ref xexpression type underlying this view
* @tparam L the layout of the strided view
* @tparam S the strides type of the strided view
* @tparam FST the flat storage type used for the strided view
*
* @sa strided_view, transpose
*/
template <class CT, class S, layout_type L = layout_type::dynamic, class FST = detail::flat_storage_getter<CT, XTENSOR_DEFAULT_TRAVERSAL>>
class xstrided_view
: public xview_semantic<xstrided_view<CT, S, L, FST>>,
public select_iterable_base_t<L, std::decay_t<CT>::static_layout, xstrided_view<CT, S, L, FST>>,
private xstrided_view_base<xstrided_view<CT, S, L, FST>>,
public extension::xstrided_view_base_t<CT, S, L, FST>
{
public:
using self_type = xstrided_view<CT, S, L, FST>;
using base_type = xstrided_view_base<self_type>;
using semantic_base = xview_semantic<self_type>;
using extension_base = extension::xstrided_view_base_t<CT, S, L, FST>;
using expression_tag = typename extension_base::expression_tag;
using xexpression_type = typename base_type::xexpression_type;
using base_type::is_const;
using value_type = typename base_type::value_type;
using reference = typename base_type::reference;
using const_reference = typename base_type::const_reference;
using pointer = typename base_type::pointer;
using const_pointer = typename base_type::const_pointer;
using size_type = typename base_type::size_type;
using difference_type = typename base_type::difference_type;
using inner_storage_type = typename base_type::inner_storage_type;
using storage_type = typename base_type::storage_type;
using linear_iterator = typename storage_type::iterator;
using const_linear_iterator = typename storage_type::const_iterator;
using reverse_linear_iterator = std::reverse_iterator<linear_iterator>;
using const_reverse_linear_iterator = std::reverse_iterator<const_linear_iterator>;
using iterable_base = select_iterable_base_t<L, xexpression_type::static_layout, self_type>;
using inner_shape_type = typename base_type::inner_shape_type;
using inner_strides_type = typename base_type::inner_strides_type;
using inner_backstrides_type = typename base_type::inner_backstrides_type;
using shape_type = typename base_type::shape_type;
using strides_type = typename base_type::strides_type;
using backstrides_type = typename base_type::backstrides_type;
using stepper = typename iterable_base::stepper;
using const_stepper = typename iterable_base::const_stepper;
using base_type::contiguous_layout;
using base_type::static_layout;
using temporary_type = typename xcontainer_inner_types<self_type>::temporary_type;
using base_index_type = xindex_type_t<shape_type>;
using data_alignment = xt_simd::container_alignment_t<storage_type>;
using simd_type = xt_simd::simd_type<value_type>;
using simd_value_type = xt_simd::simd_type<value_type>;
using bool_load_type = typename base_type::bool_load_type;
template <class CTA, class SA>
xstrided_view(CTA&& e, SA&& shape, strides_type&& strides, std::size_t offset, layout_type layout) noexcept;
xstrided_view(const xstrided_view& rhs) = default;
self_type& operator=(const self_type&);
template <class E>
self_type& operator=(const xexpression<E>& e);
template <class E>
disable_xexpression<E, self_type>& operator=(const E& e);
using base_type::backstrides;
using base_type::dimension;
using base_type::is_contiguous;
using base_type::layout;
using base_type::shape;
using base_type::size;
using base_type::strides;
using base_type::operator();
using base_type::at;
using base_type::unchecked;
using base_type::operator[];
using base_type::data;
using base_type::data_offset;
using base_type::element;
using base_type::expression;
using base_type::storage;
using base_type::broadcast_shape;
using base_type::has_linear_assign;
template <class T>
void fill(const T& value);
linear_iterator linear_begin();
linear_iterator linear_end();
const_linear_iterator linear_begin() const;
const_linear_iterator linear_end() const;
const_linear_iterator linear_cbegin() const;
const_linear_iterator linear_cend() const;
reverse_linear_iterator linear_rbegin();
reverse_linear_iterator linear_rend();
const_reverse_linear_iterator linear_rbegin() const;
const_reverse_linear_iterator linear_rend() const;
const_reverse_linear_iterator linear_crbegin() const;
const_reverse_linear_iterator linear_crend() const;
template <class ST, class STEP = stepper>
disable_indexed_stepper_t<STEP> stepper_begin(const ST& shape);
template <class ST, class STEP = stepper>
disable_indexed_stepper_t<STEP> stepper_end(const ST& shape, layout_type l);
template <class ST, class STEP = stepper>
enable_indexed_stepper_t<STEP> stepper_begin(const ST& shape);
template <class ST, class STEP = stepper>
enable_indexed_stepper_t<STEP> stepper_end(const ST& shape, layout_type l);
template <class ST, class STEP = const_stepper>
disable_indexed_stepper_t<STEP> stepper_begin(const ST& shape) const;
template <class ST, class STEP = const_stepper>
disable_indexed_stepper_t<STEP> stepper_end(const ST& shape, layout_type l) const;
template <class ST, class STEP = const_stepper>
enable_indexed_stepper_t<STEP> stepper_begin(const ST& shape) const;
template <class ST, class STEP = const_stepper>
enable_indexed_stepper_t<STEP> stepper_end(const ST& shape, layout_type l) const;
template <class requested_type>
using simd_return_type = xt_simd::simd_return_type<value_type, requested_type>;
template <class T, class R>
using enable_simd_interface = std::enable_if_t<has_simd_interface<T>::value && L != layout_type::dynamic, R>;
template <class align, class simd, class T = xexpression_type>
enable_simd_interface<T, void> store_simd(size_type i, const simd& e);
template <
class align,
class requested_type = value_type,
std::size_t N = xt_simd::simd_traits<requested_type>::size,
class T = xexpression_type>
enable_simd_interface<T, simd_return_type<requested_type>> load_simd(size_type i) const;
reference data_element(size_type i);
const_reference data_element(size_type i) const;
reference flat(size_type i);
const_reference flat(size_type i) const;
using container_iterator = std::
conditional_t<is_const, typename storage_type::const_iterator, typename storage_type::iterator>;
using const_container_iterator = typename storage_type::const_iterator;
template <class E>
using rebind_t = xstrided_view<E, S, L, typename FST::template rebind_t<E>>;
template <class E>
rebind_t<E> build_view(E&& e) const;
private:
container_iterator data_xbegin() noexcept;
const_container_iterator data_xbegin() const noexcept;
container_iterator data_xend(layout_type l, size_type offset) noexcept;
const_container_iterator data_xend(layout_type l, size_type offset) const noexcept;
template <class It>
It data_xbegin_impl(It begin) const noexcept;
template <class It>
It data_xend_impl(It end, layout_type l, size_type offset) const noexcept;
void assign_temporary_impl(temporary_type&& tmp);
using base_type::set_offset;
template <class C>
friend class xstepper;
friend class xview_semantic<self_type>;
friend class xaccessible<self_type>;
friend class xconst_accessible<self_type>;
template <class D>
friend class xaxis_iterator;
template <class D>
friend class xaxis_slice_iterator;
};
/**************************
* xstrided_view builders *
**************************/
template <class T>
using xstrided_slice = xtl::variant<
T,
xrange_adaptor<placeholders::xtuph, T, T>,
xrange_adaptor<T, placeholders::xtuph, T>,
xrange_adaptor<T, T, placeholders::xtuph>,
xrange_adaptor<T, placeholders::xtuph, placeholders::xtuph>,
xrange_adaptor<placeholders::xtuph, T, placeholders::xtuph>,
xrange_adaptor<placeholders::xtuph, placeholders::xtuph, T>,
xrange_adaptor<T, T, T>,
xrange_adaptor<placeholders::xtuph, placeholders::xtuph, placeholders::xtuph>,
xrange<T>,
xstepped_range<T>,
xall_tag,
xellipsis_tag,
xnewaxis_tag>;
/**
* @typedef xstrided_slice_vector
* @brief vector of slices used to build a `xstrided_view`
*/
using xstrided_slice_vector = std::vector<xstrided_slice<std::ptrdiff_t>>;
template <layout_type L = layout_type::dynamic, class E, class S, class X>
auto strided_view(E&& e, S&& shape, X&& stride, std::size_t offset = 0, layout_type layout = L) noexcept;
template <class E>
auto strided_view(E&& e, const xstrided_slice_vector& slices);
/********************************
* xstrided_view implementation *
********************************/
/**
* @name Constructor
*/
//@{
/**
* Constructs an xstrided_view
*
* @param e the underlying xexpression for this view
* @param shape the shape of the view
* @param strides the strides of the view
* @param offset the offset of the first element in the underlying container
* @param layout the layout of the view
*/
template <class CT, class S, layout_type L, class FST>
template <class CTA, class SA>
inline xstrided_view<CT, S, L, FST>::xstrided_view(
CTA&& e,
SA&& shape,
strides_type&& strides,
std::size_t offset,
layout_type layout
) noexcept
: base_type(std::forward<CTA>(e), std::forward<SA>(shape), std::move(strides), offset, layout)
{
}
//@}
template <class CT, class S, layout_type L, class FST>
inline auto xstrided_view<CT, S, L, FST>::operator=(const self_type& rhs) -> self_type&
{
temporary_type tmp(rhs);
return this->assign_temporary(std::move(tmp));
}
/**
* @name Extended copy semantic
*/
//@{
/**
* The extended assignment operator.
*/
template <class CT, class S, layout_type L, class FST>
template <class E>
inline auto xstrided_view<CT, S, L, FST>::operator=(const xexpression<E>& e) -> self_type&
{
return semantic_base::operator=(e);
}
//@}
template <class CT, class S, layout_type L, class FST>
template <class E>
inline auto xstrided_view<CT, S, L, FST>::operator=(const E& e) -> disable_xexpression<E, self_type>&
{
this->fill(e);
return *this;
}
namespace xstrided_view_detail
{
template <class V, class T>
inline void run_assign_temporary_impl(V& v, const T& t, std::true_type /* enable strided assign */)
{
strided_loop_assigner<true>::run(v, t);
}
template <class V, class T>
inline void
run_assign_temporary_impl(V& v, const T& t, std::false_type /* fallback to iterator assign */)
{
std::copy(t.cbegin(), t.cend(), v.begin());
}
}
template <class CT, class S, layout_type L, class FST>
inline void xstrided_view<CT, S, L, FST>::assign_temporary_impl(temporary_type&& tmp)
{
constexpr bool
fast_assign = xassign_traits<xstrided_view<CT, S, L, FST>, temporary_type>::simd_strided_assign();
xstrided_view_detail::run_assign_temporary_impl(*this, tmp, std::integral_constant<bool, fast_assign>{});
}
/**
* @name Data
*/
//@{
/**
* Fills the view with the given value.
* @param value the value to fill the view with.
*/
template <class CT, class S, layout_type L, class FST>
template <class T>
inline void xstrided_view<CT, S, L, FST>::fill(const T& value)
{
if (layout() != layout_type::dynamic)
{
std::fill(this->linear_begin(), this->linear_end(), value);
}
else
{
std::fill(this->begin(), this->end(), value);
}
}
//@}
template <class CT, class S, layout_type L, class FST>
inline auto xstrided_view<CT, S, L, FST>::data_element(size_type i) -> reference
{
return storage()[i];
}
template <class CT, class S, layout_type L, class FST>
inline auto xstrided_view<CT, S, L, FST>::data_element(size_type i) const -> const_reference
{
return storage()[i];
}
template <class CT, class S, layout_type L, class FST>
inline auto xstrided_view<CT, S, L, FST>::flat(size_type i) -> reference
{
return storage()[i];
}
template <class CT, class S, layout_type L, class FST>
inline auto xstrided_view<CT, S, L, FST>::flat(size_type i) const -> const_reference
{
return storage()[i];
}
template <class CT, class S, layout_type L, class FST>
inline auto xstrided_view<CT, S, L, FST>::linear_begin() -> linear_iterator
{
return this->storage().begin() + static_cast<std::ptrdiff_t>(data_offset());
}
template <class CT, class S, layout_type L, class FST>
inline auto xstrided_view<CT, S, L, FST>::linear_end() -> linear_iterator
{
return this->storage().begin() + static_cast<std::ptrdiff_t>(data_offset() + size());
}
template <class CT, class S, layout_type L, class FST>
inline auto xstrided_view<CT, S, L, FST>::linear_begin() const -> const_linear_iterator
{
return this->linear_cbegin();
}
template <class CT, class S, layout_type L, class FST>
inline auto xstrided_view<CT, S, L, FST>::linear_end() const -> const_linear_iterator
{
return this->linear_cend();
}
template <class CT, class S, layout_type L, class FST>
inline auto xstrided_view<CT, S, L, FST>::linear_cbegin() const -> const_linear_iterator
{
return this->storage().cbegin() + static_cast<std::ptrdiff_t>(data_offset());
}
template <class CT, class S, layout_type L, class FST>
inline auto xstrided_view<CT, S, L, FST>::linear_cend() const -> const_linear_iterator
{
return this->storage().cbegin() + static_cast<std::ptrdiff_t>(data_offset() + size());
}
template <class CT, class S, layout_type L, class FST>
inline auto xstrided_view<CT, S, L, FST>::linear_rbegin() -> reverse_linear_iterator
{
return reverse_linear_iterator(this->linear_begin());
}
template <class CT, class S, layout_type L, class FST>
inline auto xstrided_view<CT, S, L, FST>::linear_rend() -> reverse_linear_iterator
{
return reverse_linear_iterator(this->linear_end());
}
template <class CT, class S, layout_type L, class FST>
inline auto xstrided_view<CT, S, L, FST>::linear_rbegin() const -> const_reverse_linear_iterator
{
return this->linear_crbegin();
}
template <class CT, class S, layout_type L, class FST>
inline auto xstrided_view<CT, S, L, FST>::linear_rend() const -> const_reverse_linear_iterator
{
return this->linear_crend();
}
template <class CT, class S, layout_type L, class FST>
inline auto xstrided_view<CT, S, L, FST>::linear_crbegin() const -> const_reverse_linear_iterator
{
return const_reverse_linear_iterator(this->linear_cbegin());
}
template <class CT, class S, layout_type L, class FST>
inline auto xstrided_view<CT, S, L, FST>::linear_crend() const -> const_reverse_linear_iterator
{
return const_reverse_linear_iterator(this->linear_cend());
}
/***************
* stepper api *
***************/
template <class CT, class S, layout_type L, class FST>
template <class ST, class STEP>
inline auto xstrided_view<CT, S, L, FST>::stepper_begin(const ST& shape) -> disable_indexed_stepper_t<STEP>
{
size_type offset = shape.size() - dimension();
return stepper(this, data_xbegin(), offset);
}
template <class CT, class S, layout_type L, class FST>
template <class ST, class STEP>
inline auto xstrided_view<CT, S, L, FST>::stepper_end(const ST& shape, layout_type l)
-> disable_indexed_stepper_t<STEP>
{
size_type offset = shape.size() - dimension();
return stepper(this, data_xend(l, offset), offset);
}
template <class CT, class S, layout_type L, class FST>
template <class ST, class STEP>
inline auto xstrided_view<CT, S, L, FST>::stepper_begin(const ST& shape) -> enable_indexed_stepper_t<STEP>
{
size_type offset = shape.size() - dimension();
return stepper(this, offset);
}
template <class CT, class S, layout_type L, class FST>
template <class ST, class STEP>
inline auto xstrided_view<CT, S, L, FST>::stepper_end(const ST& shape, layout_type /*l*/)
-> enable_indexed_stepper_t<STEP>
{
size_type offset = shape.size() - dimension();
return stepper(this, offset, true);
}
template <class CT, class S, layout_type L, class FST>
template <class ST, class STEP>
inline auto xstrided_view<CT, S, L, FST>::stepper_begin(const ST& shape) const
-> disable_indexed_stepper_t<STEP>
{
size_type offset = shape.size() - dimension();
return const_stepper(this, data_xbegin(), offset);
}
template <class CT, class S, layout_type L, class FST>
template <class ST, class STEP>
inline auto xstrided_view<CT, S, L, FST>::stepper_end(const ST& shape, layout_type l) const
-> disable_indexed_stepper_t<STEP>
{
size_type offset = shape.size() - dimension();
return const_stepper(this, data_xend(l, offset), offset);
}
template <class CT, class S, layout_type L, class FST>
template <class ST, class STEP>
inline auto xstrided_view<CT, S, L, FST>::stepper_begin(const ST& shape) const
-> enable_indexed_stepper_t<STEP>
{
size_type offset = shape.size() - dimension();
return const_stepper(this, offset);
}
template <class CT, class S, layout_type L, class FST>
template <class ST, class STEP>
inline auto xstrided_view<CT, S, L, FST>::stepper_end(const ST& shape, layout_type /*l*/) const
-> enable_indexed_stepper_t<STEP>
{
size_type offset = shape.size() - dimension();
return const_stepper(this, offset, true);
}
template <class CT, class S, layout_type L, class FST>
template <class It>
inline It xstrided_view<CT, S, L, FST>::data_xbegin_impl(It begin) const noexcept
{
return begin + static_cast<std::ptrdiff_t>(this->data_offset());
}
template <class CT, class S, layout_type L, class FST>
template <class It>
inline It
xstrided_view<CT, S, L, FST>::data_xend_impl(It begin, layout_type l, size_type offset) const noexcept
{
return strided_data_end(*this, begin + std::ptrdiff_t(this->data_offset()), l, offset);
}
template <class CT, class S, layout_type L, class FST>
inline auto xstrided_view<CT, S, L, FST>::data_xbegin() noexcept -> container_iterator
{
return data_xbegin_impl(this->storage().begin());
}
template <class CT, class S, layout_type L, class FST>
inline auto xstrided_view<CT, S, L, FST>::data_xbegin() const noexcept -> const_container_iterator
{
return data_xbegin_impl(this->storage().cbegin());
}
template <class CT, class S, layout_type L, class FST>
inline auto xstrided_view<CT, S, L, FST>::data_xend(layout_type l, size_type offset) noexcept
-> container_iterator
{
return data_xend_impl(this->storage().begin(), l, offset);
}
template <class CT, class S, layout_type L, class FST>
inline auto xstrided_view<CT, S, L, FST>::data_xend(layout_type l, size_type offset) const noexcept
-> const_container_iterator
{
return data_xend_impl(this->storage().cbegin(), l, offset);
}
template <class CT, class S, layout_type L, class FST>
template <class alignment, class simd, class T>
inline auto xstrided_view<CT, S, L, FST>::store_simd(size_type i, const simd& e)
-> enable_simd_interface<T, void>
{
using align_mode = driven_align_mode_t<alignment, data_alignment>;
xt_simd::store_as(&(storage()[i]), e, align_mode());
}
template <class CT, class S, layout_type L, class FST>
template <class alignment, class requested_type, std::size_t N, class T>
inline auto xstrided_view<CT, S, L, FST>::load_simd(size_type i) const
-> enable_simd_interface<T, simd_return_type<requested_type>>
{
using align_mode = driven_align_mode_t<alignment, data_alignment>;
return xt_simd::load_as<requested_type>(&(storage()[i]), align_mode());
}
template <class CT, class S, layout_type L, class FST>
template <class E>
inline auto xstrided_view<CT, S, L, FST>::build_view(E&& e) const -> rebind_t<E>
{
inner_shape_type sh(this->shape());
inner_strides_type str(this->strides());
return rebind_t<E>(
std::forward<E>(e),
std::move(sh),
std::move(str),
base_type::data_offset(),
this->layout()
);
}
/*****************************************
* xstrided_view builders implementation *
*****************************************/
/**
* Construct a strided view from an xexpression, shape, strides and offset.
*
* @param e xexpression
* @param shape the shape of the view
* @param strides the new strides of the view
* @param offset the offset of the first element in the underlying container
* @param layout the new layout of the expression
*
* @tparam L the static layout type of the view (default: dynamic)
* @tparam E type of xexpression
* @tparam S strides type
* @tparam X strides type
*
* @return the view
*/
template <layout_type L, class E, class S, class X>
inline auto strided_view(E&& e, S&& shape, X&& strides, std::size_t offset, layout_type layout) noexcept
{
using view_type = xstrided_view<xclosure_t<E>, S, L>;
return view_type(std::forward<E>(e), std::forward<S>(shape), std::forward<X>(strides), offset, layout);
}
namespace detail
{
struct no_adj_strides_policy
{
protected:
inline void resize(std::size_t)
{
}
inline void set_fake_slice(std::size_t)
{
}
template <class ST, class S>
bool fill_args(
const xstrided_slice_vector& /*slices*/,
std::size_t /*sl_idx*/,
std::size_t /*i*/,
std::size_t /*old_shape*/,
const ST& /*old_stride*/,
S& /*shape*/,
get_strides_t<S>& /*strides*/
)
{
return false;
}
};
}
/**
* Function to create a dynamic view from
* an xexpression and an xstrided_slice_vector.
*
* @param e xexpression
* @param slices the slice vector
*
* @return initialized strided_view according to slices
*
* @code{.cpp}
* xt::xarray<double> a = {{1, 2, 3}, {4, 5, 6}};
* xt::xstrided_slice_vector sv({xt::range(0, 1)});
* sv.push_back(xt::range(0, 3, 2));
* auto v = xt::strided_view(a, sv);
* // ==> {{1, 3}}
* @endcode
*
* You can also achieve the same with the following short-hand syntax:
*
* @code{.cpp}
* xt::xarray<double> a = {{1, 2, 3}, {4, 5, 6}};
* auto v = xt::strided_view(a, {xt::range(0, 1), xt::range(0, 3, 2)});
* // ==> {{1, 3}}
* @endcode
*/
template <class E>
inline auto strided_view(E&& e, const xstrided_slice_vector& slices)
{
detail::strided_view_args<detail::no_adj_strides_policy> args;
args.fill_args(
e.shape(),
detail::get_strides<XTENSOR_DEFAULT_TRAVERSAL>(e),
detail::get_offset<XTENSOR_DEFAULT_TRAVERSAL>(e),
e.layout(),
slices
);
using view_type = xstrided_view<xclosure_t<E>, decltype(args.new_shape)>;
return view_type(
std::forward<E>(e),
std::move(args.new_shape),
std::move(args.new_strides),
args.new_offset,
args.new_layout
);
}
namespace detail
{
template <typename S>
struct rebind_shape;
template <std::size_t... X>
struct rebind_shape<xt::fixed_shape<X...>>
{
using type = xt::fixed_shape<X...>;
};
template <class S>
struct rebind_shape
{
using type = rebind_container_t<size_t, S>;
};
template <
class S,
std::enable_if_t<std::is_signed<get_value_type_t<typename std::decay<S>::type>>::value, bool> = true>
inline void recalculate_shape_impl(S& shape, size_t size)
{
using value_type = get_value_type_t<typename std::decay_t<S>>;
XTENSOR_ASSERT(std::count(shape.cbegin(), shape.cend(), -1) <= 1);
auto iter = std::find(shape.begin(), shape.end(), -1);
if (iter != std::end(shape))
{
const auto total = std::accumulate(shape.cbegin(), shape.cend(), -1, std::multiplies<int>{});
const auto missing_dimension = size / total;
(*iter) = static_cast<value_type>(missing_dimension);
}
}
template <
class S,
std::enable_if_t<!std::is_signed<get_value_type_t<typename std::decay<S>::type>>::value, bool> = true>
inline void recalculate_shape_impl(S&, size_t)
{
}
template <class S>
inline auto recalculate_shape(S&& shape, size_t size)
{
return recalculate_shape_impl(shape, size);
}
}
template <layout_type L = XTENSOR_DEFAULT_TRAVERSAL, class E, class S>
inline auto reshape_view(E&& e, S&& shape)
{
static_assert(
L == layout_type::row_major || L == layout_type::column_major,
"traversal has to be row or column major"
);
using shape_type = std::decay_t<decltype(shape)>;
using unsigned_shape_type = typename detail::rebind_shape<shape_type>::type;
get_strides_t<unsigned_shape_type> strides;
detail::recalculate_shape(shape, e.size());
xt::resize_container(strides, shape.size());
compute_strides(shape, L, strides);
constexpr auto computed_layout = std::decay_t<E>::static_layout == L ? L : layout_type::dynamic;
using view_type = xstrided_view<
xclosure_t<E>,
unsigned_shape_type,
computed_layout,
detail::flat_adaptor_getter<xclosure_t<E>, L>>;
return view_type(
std::forward<E>(e),
xtl::forward_sequence<unsigned_shape_type, S>(shape),
std::move(strides),
0,
e.layout()
);
}
/**
* @deprecated
* @brief Return a view on a container with a new shape
*
* Note: if you resize the underlying container, this view becomes
* invalidated.
*
* @param e xexpression to reshape
* @param shape new shape
* @param order traversal order (optional)
*
* @return view on xexpression with new shape
*/
template <layout_type L = XTENSOR_DEFAULT_TRAVERSAL, class E, class S>
inline auto reshape_view(E&& e, S&& shape, layout_type /*order*/)
{
return reshape_view<L>(std::forward<E>(e), std::forward<S>(shape));
}
template <layout_type L = XTENSOR_DEFAULT_TRAVERSAL, class E, class I, std::size_t N>
inline auto reshape_view(E&& e, const I (&shape)[N], layout_type order)
{
using shape_type = std::array<std::size_t, N>;
return reshape_view<L>(std::forward<E>(e), xtl::forward_sequence<shape_type, decltype(shape)>(shape), order);
}
template <layout_type L = XTENSOR_DEFAULT_TRAVERSAL, class E, class I, std::size_t N>
inline auto reshape_view(E&& e, const I (&shape)[N])
{
using shape_type = std::array<I, N>;
return reshape_view<L>(std::forward<E>(e), xtl::forward_sequence<shape_type, decltype(shape)>(shape));
}
}
#endif