forked from xtensor-stack/xtensor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxview.hpp
More file actions
2092 lines (1790 loc) · 77.4 KB
/
Copy pathxview.hpp
File metadata and controls
2092 lines (1790 loc) · 77.4 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
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
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************
* 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_VIEW_HPP
#define XTENSOR_VIEW_HPP
#include <algorithm>
#include <array>
#include <cstddef>
#include <tuple>
#include <type_traits>
#include <utility>
#include <xtl/xclosure.hpp>
#include <xtl/xsequence.hpp>
#include <xtl/xmeta_utils.hpp>
#include <xtl/xtype_traits.hpp>
#include "xaccessible.hpp"
#include "xarray.hpp"
#include "xbroadcast.hpp"
#include "xcontainer.hpp"
#include "xiterable.hpp"
#include "xsemantic.hpp"
#include "xslice.hpp"
#include "xtensor.hpp"
#include "xtensor_config.hpp"
#include "xtensor_forward.hpp"
#include "xview_utils.hpp"
namespace xt
{
/*******************
* xview extension *
*******************/
namespace extension
{
template <class Tag, class CT, class... S>
struct xview_base_impl;
template <class CT, class... S>
struct xview_base_impl<xtensor_expression_tag, CT, S...>
{
using type = xtensor_empty_base;
};
template <class CT, class... S>
struct xview_base : xview_base_impl<xexpression_tag_t<CT>, CT, S...>
{
};
template <class CT, class... S>
using xview_base_t = typename xview_base<CT, S...>::type;
}
/*********************
* xview declaration *
*********************/
template <bool is_const, class CT, class... S>
class xview_stepper;
template <class ST, class... S>
struct xview_shape_type;
namespace detail
{
template <class T>
struct is_xrange
: std::false_type
{
};
template <class T>
struct is_xrange<xrange<T>>
: std::true_type
{
};
template <class S>
struct is_xall_slice
: std::false_type
{
};
template <class T>
struct is_xall_slice<xall<T>>
: std::true_type
{
};
template <layout_type L, bool valid, bool all_seen, bool range_seen, class V>
struct is_contiguous_view_impl
{
static constexpr bool value = false;
};
template <class T>
struct static_dimension
{
static constexpr std::ptrdiff_t value = -1;
};
template <class T, std::size_t N>
struct static_dimension<std::array<T, N>>
{
static constexpr std::ptrdiff_t value = static_cast<std::ptrdiff_t>(N);
};
template <class T, std::size_t N>
struct static_dimension<xt::const_array<T, N>>
{
static constexpr std::ptrdiff_t value = static_cast<std::ptrdiff_t>(N);
};
template <std::size_t... I>
struct static_dimension<xt::fixed_shape<I...>>
{
static constexpr std::ptrdiff_t value = sizeof...(I);
};
// if we have the same number of integers as we have static dimensions
// this can be interpreted like a xscalar
template <class CT, class... S>
struct is_xscalar_impl<xview<CT, S...>>
{
static constexpr bool value = static_cast<std::ptrdiff_t>(integral_count<S...>()) == static_dimension<typename std::decay_t<CT>::shape_type>::value ? true : false;
};
template <class S>
struct is_strided_slice_impl
: std::true_type
{
};
template <class T>
struct is_strided_slice_impl<xkeep_slice<T>>
: std::false_type
{
};
template <class T>
struct is_strided_slice_impl<xdrop_slice<T>>
: std::false_type
{
};
// If we have no discontiguous slices, we can calculate strides for this view.
template <class E, class... S>
struct is_strided_view
: std::integral_constant<bool, xtl::conjunction<has_data_interface<E>, is_strided_slice_impl<S>...>::value>
{
};
// if row major the view can only be (statically) computed as contiguous if:
// any number of integers is followed by either one or no range which
// are followed by explicit (or implicit) all's
//
// e.g.
// (i, j, all(), all()) == contiguous
// (i, range(0, 2), all()) == contiguous
// (i) == contiguous (implicit all slices)
// (i, all(), j) == *not* contiguous
// (i, range(0, 2), range(0, 2)) == *not* contiguous etc.
template <bool valid, bool all_seen, bool range_seen, class V>
struct is_contiguous_view_impl<layout_type::row_major, valid, all_seen, range_seen, V>
{
using slice = xtl::mpl::front_t<V>;
static constexpr bool is_range_slice = is_xrange<slice>::value;
static constexpr bool is_int_slice = std::is_integral<slice>::value;
static constexpr bool is_all_slice = is_xall_slice<slice>::value;
static constexpr bool have_all_seen = all_seen || is_all_slice;
static constexpr bool have_range_seen = is_range_slice;
static constexpr bool is_valid = valid && (have_all_seen ? is_all_slice : (!range_seen && (is_int_slice || is_range_slice)));
static constexpr bool value = is_contiguous_view_impl<layout_type::row_major,
is_valid,
have_all_seen,
range_seen || is_range_slice,
xtl::mpl::pop_front_t<V>>::value;
};
template <bool valid, bool all_seen, bool range_seen>
struct is_contiguous_view_impl<layout_type::row_major, valid, all_seen, range_seen, xtl::mpl::vector<>>
{
static constexpr bool value = valid;
};
// For column major the *same* but reverse is true -- with the additional
// constraint that we have to know the dimension at compile time otherwise
// we cannot make the decision as there might be implicit all's following.
template <bool valid, bool int_seen, bool range_seen, class V>
struct is_contiguous_view_impl<layout_type::column_major, valid, int_seen, range_seen, V>
{
using slice = xtl::mpl::front_t<V>;
static constexpr bool is_range_slice = is_xrange<slice>::value;
static constexpr bool is_int_slice = std::is_integral<slice>::value;
static constexpr bool is_all_slice = is_xall_slice<slice>::value;
static constexpr bool have_int_seen = int_seen || is_int_slice;
static constexpr bool is_valid = valid && (have_int_seen ? is_int_slice : (!range_seen && (is_all_slice || is_range_slice)));
static constexpr bool value = is_contiguous_view_impl<layout_type::column_major,
is_valid,
have_int_seen,
is_range_slice || range_seen,
xtl::mpl::pop_front_t<V>>::value;
};
template <bool valid, bool int_seen, bool range_seen>
struct is_contiguous_view_impl<layout_type::column_major, valid, int_seen, range_seen, xtl::mpl::vector<>>
{
static constexpr bool value = valid;
};
// TODO relax has_data_interface constraint here!
template <class E, class... S>
struct is_contiguous_view
: std::integral_constant<bool,
has_data_interface<E>::value &&
!(E::static_layout == layout_type::column_major && static_dimension<typename E::shape_type>::value != sizeof...(S)) &&
is_contiguous_view_impl<E::static_layout, true, false, false, xtl::mpl::vector<S...>>::value
>
{
};
template <layout_type L, class T, std::ptrdiff_t offset>
struct unwrap_offset_container
{
using type = void;
};
template <class T, std::ptrdiff_t offset>
struct unwrap_offset_container<layout_type::row_major, T, offset>
{
using type = sequence_view<T, offset, static_dimension<T>::value>;
};
template <class T, std::ptrdiff_t start, std::ptrdiff_t end, std::ptrdiff_t offset>
struct unwrap_offset_container<layout_type::row_major, sequence_view<T, start, end>, offset>
{
using type = sequence_view<T, start + offset, end>;
};
template <class T, std::ptrdiff_t offset>
struct unwrap_offset_container<layout_type::column_major, T, offset>
{
using type = sequence_view<T, 0, static_dimension<T>::value - offset>;
};
template <class T, std::ptrdiff_t start, std::ptrdiff_t end, std::ptrdiff_t offset>
struct unwrap_offset_container<layout_type::column_major, sequence_view<T, start, end>, offset>
{
using type = sequence_view<T, start, end - offset>;
};
template <class E, class... S>
struct get_contigous_shape_type
{
// if we have no `range` in the slices we can re-use the shape with an offset
using type = std::conditional_t<xtl::disjunction<is_xrange<S>...>::value,
typename xview_shape_type<typename E::shape_type, S...>::type,
// In the false branch we know that we have only integers at the front OR end, and NO range
typename unwrap_offset_container<E::static_layout, typename E::inner_shape_type, integral_count<S...>()>::type>;
};
template <class T>
struct is_sequence_view
: std::integral_constant<bool, false>
{
};
template <class T, std::ptrdiff_t S, std::ptrdiff_t E>
struct is_sequence_view<sequence_view<T, S, E>>
: std::integral_constant<bool, true>
{
};
}
template <class CT, class... S>
struct xcontainer_inner_types<xview<CT, S...>>
{
using xexpression_type = std::decay_t<CT>;
using reference = inner_reference_t<CT>;
using const_reference = typename xexpression_type::const_reference;
using size_type = typename xexpression_type::size_type;
using temporary_type = view_temporary_type_t<xexpression_type, S...>;
static constexpr layout_type layout =
detail::is_contiguous_view<xexpression_type, S...>::value ?
xexpression_type::static_layout : layout_type::dynamic;
static constexpr bool is_const = std::is_const<std::remove_reference_t<CT>>::value;
using extract_storage_type = xtl::mpl::eval_if_t<has_data_interface<xexpression_type>,
detail::expr_storage_type<xexpression_type>,
make_invalid_type<>>;
using storage_type = std::conditional_t<is_const, const extract_storage_type, extract_storage_type>;
};
template <class CT, class... S>
struct xiterable_inner_types<xview<CT, S...>>
{
using xexpression_type = std::decay_t<CT>;
static constexpr bool is_strided_view = detail::is_strided_view<xexpression_type, S...>::value;
static constexpr bool is_contiguous_view = detail::is_contiguous_view<xexpression_type, S...>::value;
using inner_shape_type = std::conditional_t<is_contiguous_view,
typename detail::get_contigous_shape_type<xexpression_type, S...>::type,
typename xview_shape_type<typename xexpression_type::shape_type, S...>::type>;
using stepper = std::conditional_t<is_strided_view,
xstepper<xview<CT, S...>>,
xview_stepper<std::is_const<std::remove_reference_t<CT>>::value, CT, S...>>;
using const_stepper = std::conditional_t<is_strided_view,
xstepper<const xview<CT, S...>>,
xview_stepper<true, std::remove_cv_t<CT>, S...>>;
};
/**
* @class xview
* @brief Multidimensional view with tensor semantic.
*
* The xview class implements a multidimensional view with tensor
* semantic. It is used to adapt the shape of an xexpression without
* changing it. xview is not meant to be used directly, but
* only with the \ref view helper functions.
*
* @tparam CT the closure type of the \ref xexpression to adapt
* @tparam S the slices type describing the shape adaptation
*
* @sa view, range, all, newaxis, keep, drop
*/
template <class CT, class... S>
class xview : public xview_semantic<xview<CT, S...>>,
public std::conditional_t<
detail::is_contiguous_view<std::decay_t<CT>, S...>::value,
xcontiguous_iterable<xview<CT, S...>>,
xiterable<xview<CT, S...>>>,
public xaccessible<xview<CT, S...>>,
public extension::xview_base_t<CT, S...>
{
public:
using self_type = xview<CT, S...>;
using inner_types = xcontainer_inner_types<self_type>;
using xexpression_type = std::decay_t<CT>;
using semantic_base = xview_semantic<self_type>;
using temporary_type = typename xcontainer_inner_types<self_type>::temporary_type;
using accessible_base = xaccessible<self_type>;
using extension_base = extension::xview_base_t<CT, S...>;
using expression_tag = typename extension_base::expression_tag;
static constexpr bool is_const = std::is_const<std::remove_reference_t<CT>>::value;
using value_type = typename xexpression_type::value_type;
using simd_value_type = xt_simd::simd_type<value_type>;
using bool_load_type = typename xexpression_type::bool_load_type;
using reference = typename inner_types::reference;
using const_reference = typename inner_types::const_reference;
using pointer = std::conditional_t<is_const,
typename xexpression_type::const_pointer,
typename xexpression_type::pointer>;
using const_pointer = typename xexpression_type::const_pointer;
using size_type = typename inner_types::size_type;
using difference_type = typename xexpression_type::difference_type;
static constexpr layout_type static_layout = inner_types::layout;
static constexpr bool contiguous_layout = static_layout != layout_type::dynamic;
static constexpr bool is_strided_view = detail::is_strided_view<xexpression_type, S...>::value;
static constexpr bool is_contiguous_view = contiguous_layout;
using iterable_base = xiterable<self_type>;
using inner_shape_type = typename iterable_base::inner_shape_type;
using shape_type = typename xview_shape_type<typename xexpression_type::shape_type, S...>::type;
using xexpression_inner_strides_type = xtl::mpl::eval_if_t<has_strides<xexpression_type>,
detail::expr_inner_strides_type<xexpression_type>,
get_strides_type<shape_type>>;
using xexpression_inner_backstrides_type = xtl::mpl::eval_if_t<has_strides<xexpression_type>,
detail::expr_inner_backstrides_type<xexpression_type>,
get_strides_type<shape_type>>;
using storage_type = typename inner_types::storage_type;
static constexpr bool has_trivial_strides = is_contiguous_view && !xtl::disjunction<detail::is_xrange<S>...>::value;
using inner_strides_type = std::conditional_t<has_trivial_strides,
typename detail::unwrap_offset_container<xexpression_type::static_layout,
xexpression_inner_strides_type,
integral_count<S...>()>::type,
get_strides_t<shape_type>>;
using inner_backstrides_type = std::conditional_t<has_trivial_strides,
typename detail::unwrap_offset_container<xexpression_type::static_layout,
xexpression_inner_backstrides_type,
integral_count<S...>()>::type,
get_strides_t<shape_type>>;
using strides_type = get_strides_t<shape_type>;
using back_strides_type = strides_type;
using slice_type = std::tuple<S...>;
using stepper = typename iterable_base::stepper;
using const_stepper = typename iterable_base::const_stepper;
using storage_iterator = std::conditional_t<has_data_interface<xexpression_type>::value && is_strided_view,
std::conditional_t<is_const,
typename xexpression_type::const_storage_iterator,
typename xexpression_type::storage_iterator>,
typename iterable_base::storage_iterator>;
using const_storage_iterator = std::conditional_t<has_data_interface<xexpression_type>::value && is_strided_view,
typename xexpression_type::const_storage_iterator,
typename iterable_base::const_storage_iterator>;
using container_iterator = pointer;
using const_container_iterator = const_pointer;
// The FSL argument prevents the compiler from calling this constructor
// instead of the copy constructor when sizeof...(SL) == 0.
template <class CTA, class FSL, class... SL>
explicit xview(CTA&& e, FSL&& first_slice, SL&&... slices) noexcept;
xview(const xview&) = default;
self_type& operator=(const xview& rhs);
template <class E>
self_type& operator=(const xexpression<E>& e);
template <class E>
disable_xexpression<E, self_type>& operator=(const E& e);
const inner_shape_type& shape() const noexcept;
const slice_type& slices() const noexcept;
layout_type layout() const noexcept;
bool is_contiguous() const noexcept;
using accessible_base::shape;
template <class T>
void fill(const T& value);
template <class... Args>
reference operator()(Args... args);
template <class... Args>
reference unchecked(Args... args);
template <class It>
reference element(It first, It last);
template <class... Args>
const_reference operator()(Args... args) const;
template <class... Args>
const_reference unchecked(Args... args) const;
template <class It>
const_reference element(It first, It last) const;
xexpression_type& expression() noexcept;
const xexpression_type& expression() const noexcept;
template <class ST>
bool broadcast_shape(ST& shape, bool reuse_cache = false) const;
template <class ST>
bool has_linear_assign(const ST& strides) const;
template <class ST, bool Enable = is_strided_view>
std::enable_if_t<!Enable, stepper>
stepper_begin(const ST& shape);
template <class ST, bool Enable = is_strided_view>
std::enable_if_t<!Enable, stepper>
stepper_end(const ST& shape, layout_type l);
template <class ST, bool Enable = is_strided_view>
std::enable_if_t<!Enable, const_stepper>
stepper_begin(const ST& shape) const;
template <class ST, bool Enable = is_strided_view>
std::enable_if_t<!Enable, const_stepper>
stepper_end(const ST& shape, layout_type l) const;
template <class ST, bool Enable = is_strided_view>
std::enable_if_t<Enable, stepper>
stepper_begin(const ST& shape);
template <class ST, bool Enable = is_strided_view>
std::enable_if_t<Enable, stepper>
stepper_end(const ST& shape, layout_type l);
template <class ST, bool Enable = is_strided_view>
std::enable_if_t<Enable, const_stepper>
stepper_begin(const ST& shape) const;
template <class ST, bool Enable = is_strided_view>
std::enable_if_t<Enable, const_stepper>
stepper_end(const ST& shape, layout_type l) const;
template <class T = xexpression_type>
std::enable_if_t<has_data_interface<T>::value, storage_type&>
storage();
template <class T = xexpression_type>
std::enable_if_t<has_data_interface<T>::value, const storage_type&>
storage() const;
template <class T = xexpression_type>
std::enable_if_t<has_data_interface<T>::value && is_strided_view, storage_iterator>
storage_begin();
template <class T = xexpression_type>
std::enable_if_t<has_data_interface<T>::value && is_strided_view, storage_iterator>
storage_end();
template <class T = xexpression_type>
std::enable_if_t<has_data_interface<T>::value && is_strided_view, const_storage_iterator>
storage_cbegin() const;
template <class T = xexpression_type>
std::enable_if_t<has_data_interface<T>::value && is_strided_view, const_storage_iterator>
storage_cend() const;
template <class T = xexpression_type>
std::enable_if_t<has_data_interface<T>::value && is_strided_view, const inner_strides_type&>
strides() const;
template <class T = xexpression_type>
std::enable_if_t<has_data_interface<T>::value && is_strided_view, const inner_strides_type&>
backstrides() const;
template <class T = xexpression_type>
std::enable_if_t<has_data_interface<T>::value && is_strided_view, const_pointer>
data() const;
template <class T = xexpression_type>
std::enable_if_t<has_data_interface<T>::value && is_strided_view, pointer>
data();
template <class T = xexpression_type>
std::enable_if_t<has_data_interface<T>::value && is_strided_view, std::size_t>
data_offset() const noexcept;
template <class It>
inline It data_xbegin_impl(It begin) const noexcept;
template <class It>
inline It data_xend_impl(It begin, layout_type l, size_type offset) const noexcept;
inline container_iterator data_xbegin() noexcept;
inline const_container_iterator data_xbegin() const noexcept;
inline container_iterator data_xend(layout_type l, size_type offset) noexcept;
inline const_container_iterator data_xend(layout_type l, size_type offset) const noexcept;
// Conversion operator enabled for statically "scalar" views
template <class ST = self_type, class = std::enable_if_t<is_xscalar<std::decay_t<ST>>::value, int>>
operator reference()
{
return (*this)();
}
template <class ST = self_type, class = std::enable_if_t<is_xscalar<std::decay_t<ST>>::value, int>>
operator const_reference() const
{
return (*this)();
}
size_type underlying_size(size_type dim) const;
xtl::xclosure_pointer<self_type&> operator&() &;
xtl::xclosure_pointer<const self_type&> operator&() const &;
xtl::xclosure_pointer<self_type> operator&() &&;
template <class E, class T = xexpression_type,
class = std::enable_if_t<has_data_interface<T>::value && is_contiguous_view, int>>
void assign_to(xexpression<E>& e, bool force_resize) const;
template <class E>
using rebind_t = xview<E, S...>;
template <class E>
rebind_t<E> build_view(E&& e) const;
//
// SIMD interface
//
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 && is_strided_view, 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;
template <class T = xexpression_type>
enable_simd_interface<T, reference> data_element(size_type i);
template <class T = xexpression_type>
enable_simd_interface<T, const_reference> data_element(size_type i) const;
private:
// VS 2015 workaround (yes, really)
template <std::size_t I>
struct lesser_condition
{
static constexpr bool value = (I + newaxis_count_before<S...>(I + 1) < sizeof...(S));
};
CT m_e;
slice_type m_slices;
inner_shape_type m_shape;
mutable inner_strides_type m_strides;
mutable inner_backstrides_type m_backstrides;
mutable std::size_t m_data_offset;
mutable bool m_strides_computed;
template <class CTA, class FSL, class... SL>
explicit xview(std::true_type, CTA&& e, FSL&& first_slice, SL&&... slices) noexcept;
template <class CTA, class FSL, class... SL>
explicit xview(std::false_type, CTA&& e, FSL&& first_slice, SL&&... slices) noexcept;
template <class... Args>
auto make_index_sequence(Args... args) const noexcept;
void compute_strides(std::true_type) const;
void compute_strides(std::false_type) const;
reference access();
template <class Arg, class... Args>
reference access(Arg arg, Args... args);
const_reference access() const;
template <class Arg, class... Args>
const_reference access(Arg arg, Args... args) const;
template <typename std::decay_t<CT>::size_type... I, class... Args>
reference unchecked_impl(std::index_sequence<I...>, Args... args);
template <typename std::decay_t<CT>::size_type... I, class... Args>
const_reference unchecked_impl(std::index_sequence<I...>, Args... args) const;
template <typename std::decay_t<CT>::size_type... I, class... Args>
reference access_impl(std::index_sequence<I...>, Args... args);
template <typename std::decay_t<CT>::size_type... I, class... Args>
const_reference access_impl(std::index_sequence<I...>, Args... args) const;
template <typename std::decay_t<CT>::size_type I, class... Args>
std::enable_if_t<lesser_condition<I>::value, size_type> index(Args... args) const;
template <typename std::decay_t<CT>::size_type I, class... Args>
std::enable_if_t<!lesser_condition<I>::value, size_type> index(Args... args) const;
template <typename std::decay_t<CT>::size_type, class T>
size_type sliced_access(const xslice<T>& slice) const;
template <typename std::decay_t<CT>::size_type I, class T, class Arg, class... Args>
size_type sliced_access(const xslice<T>& slice, Arg arg, Args... args) const;
template <typename std::decay_t<CT>::size_type I, class T, class... Args>
disable_xslice<T, size_type> sliced_access(const T& squeeze, Args...) const;
using base_index_type = xindex_type_t<typename xexpression_type::shape_type>;
template <class It>
base_index_type make_index(It first, It last) const;
void assign_temporary_impl(temporary_type&& tmp);
template <std::size_t... I>
std::size_t data_offset_impl(std::index_sequence<I...>) const noexcept;
template <std::size_t... I>
auto compute_strides_impl(std::index_sequence<I...>) const noexcept;
inner_shape_type compute_shape(std::true_type) const;
inner_shape_type compute_shape(std::false_type) const;
template <class E, std::size_t... I>
rebind_t<E> build_view_impl(E&& e, std::index_sequence<I...>) const;
friend class xview_semantic<xview<CT, S...>>;
};
template <class E, class... S>
auto view(E&& e, S&&... slices);
template <class E>
auto row(E&& e, const int index);
template <class E>
auto col(E&& e, const int index);
/*****************************
* xview_stepper declaration *
*****************************/
namespace detail
{
template <class V>
struct get_stepper_impl
{
using xexpression_type = typename V::xexpression_type;
using type = typename xexpression_type::stepper;
};
template <class V>
struct get_stepper_impl<const V>
{
using xexpression_type = typename V::xexpression_type;
using type = typename xexpression_type::const_stepper;
};
}
template <class V>
using get_stepper = typename detail::get_stepper_impl<V>::type;
template <bool is_const, class CT, class... S>
class xview_stepper
{
public:
using view_type = std::conditional_t<is_const,
const xview<CT, S...>,
xview<CT, S...>>;
using substepper_type = get_stepper<view_type>;
using value_type = typename substepper_type::value_type;
using reference = typename substepper_type::reference;
using pointer = typename substepper_type::pointer;
using difference_type = typename substepper_type::difference_type;
using size_type = typename view_type::size_type;
using shape_type = typename substepper_type::shape_type;
xview_stepper() = default;
xview_stepper(view_type* view, substepper_type it,
size_type offset, bool end = false, layout_type l = XTENSOR_DEFAULT_TRAVERSAL);
reference operator*() const;
void step(size_type dim);
void step_back(size_type dim);
void step(size_type dim, size_type n);
void step_back(size_type dim, size_type n);
void reset(size_type dim);
void reset_back(size_type dim);
void to_begin();
void to_end(layout_type l);
private:
bool is_newaxis_slice(size_type index) const noexcept;
void to_end_impl(layout_type l);
template <class F>
void common_step_forward(size_type dim, F f);
template <class F>
void common_step_backward(size_type dim, F f);
template <class F>
void common_step_forward(size_type dim, size_type n, F f);
template <class F>
void common_step_backward(size_type dim, size_type n, F f);
template <class F>
void common_reset(size_type dim, F f, bool backwards);
view_type* p_view;
substepper_type m_it;
size_type m_offset;
std::array<std::size_t, sizeof...(S)> m_index_keeper;
};
// meta-function returning the shape type for an xview
template <class ST, class... S>
struct xview_shape_type
{
using type = ST;
};
template <class I, std::size_t L, class... S>
struct xview_shape_type<std::array<I, L>, S...>
{
using type = std::array<I, L - integral_count<S...>() + newaxis_count<S...>()>;
};
template <std::size_t... I, class... S>
struct xview_shape_type<fixed_shape<I...>, S...>
{
using type = typename xview_shape_type<std::array<std::size_t, sizeof...(I)>, S...>::type;
};
/************************
* xview implementation *
************************/
/**
* @name Constructor
*/
//@{
/**
* Constructs a view on the specified xexpression.
* Users should not call directly this constructor but
* use the view function instead.
* @param e the xexpression to adapt
* @param first_slice the first slice describing the view
* @param slices the slices list describing the view
* @sa view
*/
template <class CT, class... S>
template <class CTA, class FSL, class... SL>
xview<CT, S...>::xview(CTA&& e, FSL&& first_slice, SL&&... slices) noexcept
: xview(
std::integral_constant<bool, has_trivial_strides>{},
std::forward<CTA>(e),
std::forward<FSL>(first_slice),
std::forward<SL>(slices)...
)
{
}
// trivial strides initializer
template <class CT, class... S>
template <class CTA, class FSL, class... SL>
xview<CT, S...>::xview(std::true_type, CTA&& e, FSL&& first_slice, SL&&... slices) noexcept
: m_e(std::forward<CTA>(e)),
m_slices(std::forward<FSL>(first_slice), std::forward<SL>(slices)...),
m_shape(compute_shape(detail::is_sequence_view<inner_shape_type>{})),
m_strides(m_e.strides()),
m_backstrides(m_e.backstrides()),
m_data_offset(data_offset_impl(std::make_index_sequence<sizeof...(S)>())),
m_strides_computed(true)
{
}
template <class CT, class... S>
template <class CTA, class FSL, class... SL>
xview<CT, S...>::xview(std::false_type, CTA&& e, FSL&& first_slice, SL&&... slices) noexcept
: m_e(std::forward<CTA>(e)),
m_slices(std::forward<FSL>(first_slice), std::forward<SL>(slices)...),
m_shape(compute_shape(std::false_type{})),
m_strides_computed(false)
{
}
//@}
template <class CT, class... S>
inline auto xview<CT, S...>::operator=(const xview& 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>
template <class E>
inline auto xview<CT, S...>::operator=(const xexpression<E>& e) -> self_type&
{
return semantic_base::operator=(e);
}
//@}
template <class CT, class... S>
template <class E>
inline auto xview<CT, S...>::operator=(const E& e) -> disable_xexpression<E, self_type>&
{
this->fill(e);
return *this;
}
/**
* @name Size and shape
*/
//@{
/**
* Returns the shape of the view.
*/
template <class CT, class... S>
inline auto xview<CT, S...>::shape() const noexcept -> const inner_shape_type&
{
return m_shape;
}
/**
* Returns the slices of the view.
*/
template <class CT, class... S>
inline auto xview<CT, S...>::slices() const noexcept -> const slice_type&
{
return m_slices;
}
/**
* Returns the slices of the view.
*/
template <class CT, class... S>
inline layout_type xview<CT, S...>::layout() const noexcept
{
return xtl::mpl::static_if<is_strided_view>([&](auto self)
{
if (static_layout != layout_type::dynamic)
{
return static_layout;
}
else
{
bool strides_match = do_strides_match(self(this)->shape(), self(this)->strides(), self(this)->m_e.layout(), true);
return strides_match ? self(this)->m_e.layout() : layout_type::dynamic;
}
},
/* else */ [&](auto /*self*/)
{
return layout_type::dynamic;
});
}
template <class CT, class... S>
inline bool xview<CT, S...>::is_contiguous() const noexcept
{
return layout() != layout_type::dynamic;
}
//@}
/**
* @name Data
*/
//@{
/**
* Fills the view with the given value.
* @param value the value to fill the view with.
*/
template <class CT, class... S>
template <class T>
inline void xview<CT, S...>::fill(const T& value)
{
xtl::mpl::static_if<static_layout != layout_type::dynamic>([&](auto self)
{
std::fill(self(this)->storage_begin(), self(this)->storage_end(), value);
}, /*else*/ [&](auto self)
{
std::fill(self(this)->begin(), self(this)->end(), value);
});
}
/**
* Returns a reference to the element at the specified position in the view.
* @param args a list of indices specifying the position in the view. Indices
* must be unsigned integers, the number of indices should be equal or greater
* than the number of dimensions of the view.
*/
template <class CT, class... S>
template <class... Args>
inline auto xview<CT, S...>::operator()(Args... args) -> reference
{
XTENSOR_TRY(check_index(shape(), args...));
XTENSOR_CHECK_DIMENSION(shape(), args...);
// The static cast prevents the compiler from instantiating the template methods with signed integers,
// leading to warning about signed/unsigned conversions in the deeper layers of the access methods
return access(static_cast<size_type>(args)...);
}
/**
* Returns a reference to the element at the specified position in the view.
* @param args a list of indices specifying the position in the view. Indices
* must be unsigned integers, the number of indices must be equal to the number of
* dimensions of the view, else the behavior is undefined.
*
* @warning This method is meant for performance, for expressions with a dynamic
* number of dimensions (i.e. not known at compile time). Since it may have