-
Notifications
You must be signed in to change notification settings - Fork 12
/
CorradeStridedArrayView.h
1313 lines (1015 loc) · 69.1 KB
/
CorradeStridedArrayView.h
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
/*
Corrade::Containers::StridedArrayView
— multidimensional strided array view
https://doc.magnum.graphics/corrade/classCorrade_1_1Containers_1_1StridedArrayView.html
Depends on CorradeArrayView.h.
This is a single-header library generated from the Corrade project. With
the goal being easy integration, it's deliberately free of all comments
to keep the file size small. More info, detailed changelogs and docs here:
- Project homepage — https://magnum.graphics/corrade/
- Documentation — https://doc.magnum.graphics/corrade/
- GitHub project page — https://github.com/mosra/corrade
- GitHub Singles repository — https://github.com/mosra/magnum-singles
Structured bindings for StridedDimensions on C++17 are opt-in due to
reliance on a potentially heavy STL header ---
`#define CORRADE_STRUCTURED_BINDINGS` before including the file. Including
it multiple times with different macros defined works too.
v2020.06-1687-g6b5f (2024-06-29)
- Structured bindings for StridedDimensions on C++17
v2020.06-1454-gfc3b7 (2023-08-27)
- New expanded() and collapsed() APIs
- Ability to slice to struct members and member functions
- New exceptPrefix() API, the except() API is renamed to exceptSuffix().
The suffix() API, which took an offset, is removed and will be
eventually reintroduced again but taking suffix size, consistently with
prefix() that takes prefix size.
- New sliceSize() API, taking a begin + size instead of begin + end
- New stridedArrayView() convenience helpers for creating 1D strided
array views from ArrayView instances and a pointer + size
- The Size and Stride member typedefs are moved to the Containers
namespace; Size1D, Size2D, Size3D, Size4D, Stride1D, Stride2D,
Stride3D and Stride4D convenience typedefs were added
- Renamed empty() to isEmpty() for consistency with other bool-returning
APIs
- MSVC 2022 compatibility
- Removed dependency on <utility>, resulting in about ~600 preprocessed
lines less
v2020.06-0-g61d1b58c (2020-06-27)
- Added mutable StridedDimensions::begin()/end()
- New cross-dimension arrayCast() overloads
- Added isContiguous() and asContiguous() overloads
- Similarly to ArrayView, there's now a StridedArrayView<void> and
StridedArrayView<const void> specialization usable for type-erased
storage in constexpr contexts
v2019.10-0-g162d6a7d (2019-10-24)
- Don't assert when creating arrays with non-zero stride but zero size
- Added a StridedArrayView4D convenience typedef
v2019.01-301-gefe8d740 (2019-08-05)
- MSVC 2019 compatibility
- New constructor taking just a size, with stride calculated implicitly
- Added except() for taking everything except last N elements
- Added every() for taking every Nth element
v2019.01-173-ge663b49c (2019-04-30)
- Initial release
Generated from Corrade v2020.06-1687-g6b5f (2024-06-29), 1313 / 2858 LoC
*/
/*
This file is part of Corrade.
Copyright © 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016,
2017, 2018, 2019, 2020, 2021, 2022, 2023
Vladimír Vondruš <[email protected]>
Copyright © 2022 Stanislaw Halik <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#include <type_traits>
#include "CorradeArrayView.h"
#ifdef __GNUC__
#define CORRADE_TARGET_GCC
#endif
#ifdef __clang__
#define CORRADE_TARGET_CLANG
#endif
#ifndef CORRADE_DEBUG_ASSERT
#define CORRADE_DEBUG_ASSERT(condition, message, returnValue) \
CORRADE_ASSERT(condition, message, returnValue)
#endif
#ifndef CorradeStridedArrayView_h
#define CorradeStridedArrayView_h
namespace Corrade { namespace Containers {
template<unsigned, class> class StridedDimensions;
template<unsigned dimensions> using Size = StridedDimensions<dimensions, std::size_t>;
template<unsigned dimensions> using Stride = StridedDimensions<dimensions, std::ptrdiff_t>;
template<unsigned, class> class StridedIterator;
template<unsigned, class> class StridedArrayView;
template<class T> using StridedArrayView1D = StridedArrayView<1, T>;
template<class T> using StridedArrayView2D = StridedArrayView<2, T>;
template<class T> using StridedArrayView3D = StridedArrayView<3, T>;
template<class T> using StridedArrayView4D = StridedArrayView<4, T>;
template<unsigned, class> class BasicStridedBitArrayView;
}}
#endif
#ifndef Corrade_Tags_h
#define Corrade_Tags_h
namespace Corrade {
struct DefaultInitT {
struct Init {};
constexpr explicit DefaultInitT(Init) {}
};
struct ValueInitT {
struct Init {};
constexpr explicit ValueInitT(Init) {}
};
struct NoInitT {
struct Init {};
constexpr explicit NoInitT(Init) {}
};
struct NoCreateT {
struct Init {};
constexpr explicit NoCreateT(Init) {}
};
struct DirectInitT {
struct Init {};
constexpr explicit DirectInitT(Init) {}
};
struct InPlaceInitT {
struct Init {};
constexpr explicit InPlaceInitT(Init) {}
};
constexpr DefaultInitT DefaultInit{DefaultInitT::Init{}};
constexpr ValueInitT ValueInit{ValueInitT::Init{}};
constexpr NoInitT NoInit{NoInitT::Init{}};
constexpr NoCreateT NoCreate{NoCreateT::Init{}};
constexpr DirectInitT DirectInit{DirectInitT::Init{}};
constexpr InPlaceInitT InPlaceInit{InPlaceInitT::Init{}};
}
#endif
#ifndef Corrade_Containers_sequenceHelpers_h
#define Corrade_Containers_sequenceHelpers_h
namespace Corrade { namespace Containers { namespace Implementation {
template<std::size_t ...> struct Sequence {};
template<class A, class B> struct SequenceConcat;
template<std::size_t ...first, std::size_t ...second> struct SequenceConcat<Sequence<first...>, Sequence<second...>> {
typedef Sequence<first..., (sizeof...(first) + second)...> Type;
};
template<std::size_t N> struct GenerateSequence:
SequenceConcat<typename GenerateSequence<N/2>::Type,
typename GenerateSequence<N - N/2>::Type> {};
template<> struct GenerateSequence<1> { typedef Sequence<0> Type; };
template<> struct GenerateSequence<0> { typedef Sequence<> Type; };
}}}
#endif
#ifndef Corrade_Utility_Math_h
#define Corrade_Utility_Math_h
namespace Corrade { namespace Utility {
template<class T> constexpr T min(T value, T min) {
return min < value ? min : value;
}
template<class T> constexpr T max(T value, T max) {
return value < max ? max : value;
}
template<class T> constexpr T abs(T a) {
return a < T{0} ? -a : a;
}
}}
#endif
#ifndef Corrade_Containers_StridedDimensions_h
#define Corrade_Containers_StridedDimensions_h
namespace Corrade { namespace Containers {
namespace Implementation {
template<unsigned, class, class> struct StridedDimensionsConverter;
template<unsigned, class> struct StridedElement;
template<unsigned, class> struct StridedBitElement;
template<int> struct ArrayCastFlattenOrInflate;
template<unsigned dimensions, class T> constexpr bool isAnyDimensionZero(const T(&)[dimensions], Sequence<>) {
return false;
}
template<unsigned dimensions, class T, std::size_t first, std::size_t ...next> constexpr bool isAnyDimensionZero(const T(&size)[dimensions], Sequence<first, next...>) {
return !size[first] || isAnyDimensionZero(size, Sequence<next...>{});
}
template<unsigned dimensions> constexpr std::size_t largestStride(const std::size_t(&)[dimensions], const std::ptrdiff_t(&)[dimensions], Sequence<>) {
return 0;
}
template<unsigned dimensions, std::size_t first, std::size_t ...next> constexpr std::size_t largestStride(const std::size_t(&size)[dimensions], const std::ptrdiff_t(&stride)[dimensions], Sequence<first, next...>) {
return Utility::max(size[first]*std::size_t(stride[first] < 0 ? -stride[first] : stride[first]),
largestStride(size, stride, Sequence<next...>{}));
}
template<unsigned dimensions> constexpr std::ptrdiff_t strideForSizeInternal(const std::size_t(&)[dimensions], std::size_t, Sequence<>) {
return 1;
}
template<unsigned dimensions, std::size_t first, std::size_t ...next> constexpr std::ptrdiff_t strideForSizeInternal(const std::size_t(&size)[dimensions], std::size_t index, Sequence<first, next...>) {
#if defined(CORRADE_TARGET_GCC) && !defined(CORRADE_TARGET_CLANG) && __GNUC__*100 + __GNUC_MINOR__ >= 1002
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wtype-limits"
#endif
return (first > index ? size[first] : 1)*strideForSizeInternal(size, index, Sequence<next...>{});
#if defined(CORRADE_TARGET_GCC) && !defined(CORRADE_TARGET_CLANG) && __GNUC__*100 + __GNUC_MINOR__ >= 1002
#pragma GCC diagnostic pop
#endif
}
template<unsigned dimensions, std::size_t ...index> constexpr Stride<dimensions> strideForSize(const std::size_t(&size)[dimensions], std::size_t typeSize, Sequence<index...>) {
return {std::ptrdiff_t(typeSize)*strideForSizeInternal(size, index, typename GenerateSequence<dimensions>::Type{})...};
}
}
template<unsigned dimensions, class T> class StridedDimensions {
public:
constexpr /*implicit*/ StridedDimensions() noexcept: _data{} {}
constexpr explicit StridedDimensions(Corrade::ValueInitT) noexcept: _data{} {}
explicit StridedDimensions(Corrade::NoInitT) noexcept {}
template<class ...Args> constexpr /*implicit*/ StridedDimensions(T first, Args... next) noexcept: _data{T(first), T(next)...} {
static_assert(sizeof...(Args) + 1 == dimensions, "wrong value count");
}
constexpr /*implicit*/ StridedDimensions(const T(&values)[dimensions]) noexcept: StridedDimensions{values, typename Implementation::GenerateSequence<dimensions>::Type{}} {}
template<class U, class = decltype(Implementation::StridedDimensionsConverter<dimensions, T, typename std::decay<U&&>::type>::from(std::declval<U&&>()))> constexpr /*implicit*/ StridedDimensions(U&& other) noexcept: StridedDimensions{Implementation::StridedDimensionsConverter<dimensions, T, typename std::decay<U&&>::type>::from(Utility::forward<U>(other))} {}
template<class U, class = decltype(Implementation::StridedDimensionsConverter<dimensions, T, U>::to(std::declval<StridedDimensions<dimensions, T>>()))> constexpr /*implicit*/ operator U() const noexcept {
return Implementation::StridedDimensionsConverter<dimensions, T, U>::to(*this);
}
template<unsigned d = dimensions, class = typename std::enable_if<d == 1>::type>
constexpr /*implicit*/ operator T() const { return _data[0]; }
bool operator==(const StridedDimensions<dimensions, T>& other) const {
for(std::size_t i = 0; i != dimensions; ++i)
if(_data[i] != other._data[i]) return false;
return true;
}
bool operator!=(const StridedDimensions<dimensions, T>& other) const {
for(std::size_t i = 0; i != dimensions; ++i)
if(_data[i] != other._data[i]) return true;
return false;
}
constexpr T operator[](std::size_t i) const {
return CORRADE_CONSTEXPR_DEBUG_ASSERT(i < dimensions,
"Containers::StridedDimensions::operator[](): dimension" << i << "out of range for" << dimensions << "dimensions"), _data[i];
}
T& operator[](std::size_t i) {
CORRADE_DEBUG_ASSERT(i < dimensions,
"Containers::StridedDimensions::operator[](): dimension" << i << "out of range for" << dimensions << "dimensions", _data[0]);
return _data[i];
}
T* begin() { return _data; }
constexpr const T* begin() const { return _data; }
constexpr const T* cbegin() const { return _data; }
T* end() { return _data + dimensions; }
constexpr const T* end() const { return _data + dimensions; }
constexpr const T* cend() const { return _data + dimensions; }
private:
template<unsigned, class> friend class StridedArrayView;
template<unsigned, class> friend class BasicStridedBitArrayView;
template<unsigned, class> friend struct Implementation::StridedElement;
template<unsigned, class> friend struct Implementation::StridedBitElement;
template<int> friend struct Implementation::ArrayCastFlattenOrInflate;
template<class U, unsigned dimensions_, class T_> friend StridedArrayView<dimensions_, U> arrayCast(const StridedArrayView<dimensions_, T_>&);
template<class U, unsigned dimensions_> friend StridedArrayView<dimensions_, U> arrayCast(const StridedArrayView<dimensions_, void>&);
template<class U, unsigned dimensions_> friend StridedArrayView<dimensions_, U> arrayCast(const StridedArrayView<dimensions_, const void>&);
template<unsigned newDimensions, class U, unsigned dimensions_> StridedArrayView<newDimensions, U> friend arrayCast(const StridedArrayView<dimensions_, void>&, std::size_t);
template<unsigned newDimensions, class U, unsigned dimensions_> StridedArrayView<newDimensions, U> friend arrayCast(const StridedArrayView<dimensions_, const void>&, std::size_t);
#if CORRADE_CXX_STANDARD > 201402
template<std::size_t index> constexpr friend const T& get(const StridedDimensions<dimensions, T>& value) {
return value._data[index];
}
template<std::size_t index> CORRADE_CONSTEXPR14 friend T& get(StridedDimensions<dimensions, T>& value) {
return value._data[index];
}
template<std::size_t index> CORRADE_CONSTEXPR14 friend T&& get(StridedDimensions<dimensions, T>&& value) {
return Utility::move(value._data[index]);
}
#endif
template<class U, std::size_t ...sequence> constexpr explicit StridedDimensions(const U* values, Implementation::Sequence<sequence...>) noexcept: _data{T(values[sequence])...} {}
T _data[dimensions];
};
namespace Implementation {
template<unsigned dimensions, class T> struct StridedDimensionsConverter<dimensions, T, StaticArrayView<std::size_t(dimensions), const T>> {
constexpr static StridedDimensions<dimensions, T> from(StaticArrayView<dimensions, const T> view) {
return fromInternal(view.data(), typename GenerateSequence<dimensions>::Type{});
}
constexpr static StaticArrayView<dimensions, const T> to(const StridedDimensions<dimensions, T>& dimensions_) {
return StaticArrayView<dimensions, const T>{dimensions_.begin()};
}
template<std::size_t ...sequence> constexpr static StridedDimensions<dimensions, T> fromInternal(const T* data, Sequence<sequence...>) {
return {data[sequence]...};
}
};
}
#ifndef CORRADE_MSVC2015_COMPATIBILITY /* Multiple definitions still broken */
template<unsigned dimensions> using Size = StridedDimensions<dimensions, std::size_t>;
#endif
typedef Size<1> Size1D;
typedef Size<2> Size2D;
typedef Size<3> Size3D;
typedef Size<4> Size4D;
#ifndef CORRADE_MSVC2015_COMPATIBILITY /* Multiple definitions still broken */
template<unsigned dimensions> using Stride = StridedDimensions<dimensions, std::ptrdiff_t>;
#endif
typedef Stride<1> Stride1D;
typedef Stride<2> Stride2D;
typedef Stride<3> Stride3D;
typedef Stride<4> Stride4D;
}}
#endif
#ifndef Corrade_Containers_StridedArrayView_h
#define Corrade_Containers_StridedArrayView_h
namespace Corrade { namespace Containers {
template<unsigned dimensions, class T> class StridedArrayView {
static_assert(dimensions, "can't have a zero-dimensional view");
public:
typedef T Type;
typedef typename std::conditional<dimensions == 1, T&, StridedArrayView<dimensions - 1, T>>::type ElementType;
typedef typename std::conditional<std::is_const<T>::value, const void, void>::type ErasedType;
typedef typename std::conditional<std::is_const<T>::value, const char, char>::type ArithmeticType;
enum: unsigned {
Dimensions = dimensions
};
template<class U, class = typename std::enable_if<std::is_same<std::nullptr_t, U>::value>::type> constexpr /*implicit*/ StridedArrayView(U) noexcept: _data{}, _size{}, _stride{} {}
constexpr /*implicit*/ StridedArrayView() noexcept: _data{}, _size{}, _stride{} {}
constexpr /*implicit*/ StridedArrayView(ArrayView<ErasedType> data, T* member, const Containers::Size<dimensions>& size, const Containers::Stride<dimensions>& stride) noexcept;
constexpr /*implicit*/ StridedArrayView(ArrayView<T> data, const Containers::Size<dimensions>& size, const Containers::Stride<dimensions>& stride) noexcept: StridedArrayView{data, data.data(), size, stride} {}
constexpr /*implicit*/ StridedArrayView(ArrayView<T> data, const Containers::Size<dimensions>& size) noexcept: StridedArrayView{data, data.data(), size, Implementation::strideForSize<dimensions>(size._data, sizeof(T), typename Implementation::GenerateSequence<dimensions>::Type{})} {}
template<unsigned d = dimensions, class = typename std::enable_if<d == 1>::type>
constexpr /*implicit*/ StridedArrayView(T* data, std::size_t size) noexcept: _data{data}, _size{size}, _stride{sizeof(T)} {}
template<class U, std::size_t size, unsigned d = dimensions, class = typename std::enable_if<d == 1 && std::is_convertible<U*, T*>::value>::type>
constexpr /*implicit*/ StridedArrayView(U(&data)[size]) noexcept: _data{data}, _size{size}, _stride{sizeof(T)} {
static_assert(sizeof(T) == sizeof(U), "type sizes are not compatible");
}
template<class U, class = typename std::enable_if<std::is_convertible<U*, T*>::value>::type>
constexpr /*implicit*/ StridedArrayView(const StridedArrayView<dimensions, U>& view) noexcept: _data{view._data}, _size{view._size}, _stride{view._stride} {
static_assert(sizeof(T) == sizeof(U), "type sizes are not compatible");
}
template<unsigned lessDimensions
, class = typename std::enable_if<(lessDimensions < dimensions)>::type
> /*implicit*/ StridedArrayView(const StridedArrayView<lessDimensions, T>& other) noexcept;
template<class U
, unsigned d = dimensions, class = typename std::enable_if<d == 1 && std::is_convertible<U*, T*>::value>::type
> constexpr /*implicit*/ StridedArrayView(ArrayView<U> view) noexcept: _data{view.data()}, _size{view.size()}, _stride{sizeof(T)} {
static_assert(sizeof(T) == sizeof(U), "type sizes are not compatible");
}
template<std::size_t size, class U
, unsigned d = dimensions, class = typename std::enable_if<d == 1 && std::is_convertible<U*, T*>::value>::type
> constexpr /*implicit*/ StridedArrayView(StaticArrayView<size, U> view) noexcept: _data{view.data()}, _size{size}, _stride{sizeof(T)} {
static_assert(sizeof(U) == sizeof(T), "type sizes are not compatible");
}
template<class U, unsigned d = dimensions, class = typename std::enable_if<d == 1, decltype(Implementation::ArrayViewConverter<T, typename std::decay<U&&>::type>::from(std::declval<U&&>()))>::type> constexpr /*implicit*/ StridedArrayView(U&& other) noexcept: StridedArrayView{Implementation::ArrayViewConverter<T, typename std::decay<U&&>::type>::from(Utility::forward<U>(other))} {}
constexpr explicit operator bool() const { return _data; }
constexpr ErasedType* data() const { return _data; }
constexpr typename std::conditional<dimensions == 1, std::size_t, const Containers::Size<dimensions>&>::type size() const { return _size; }
constexpr typename std::conditional<dimensions == 1, std::ptrdiff_t, const Containers::Stride<dimensions>&>::type stride() const { return _stride; }
constexpr StridedDimensions<dimensions, bool> isEmpty() const {
return isEmptyInternal(typename Implementation::GenerateSequence<dimensions>::Type{});
}
template<unsigned dimension = 0> bool isContiguous() const;
ArrayView<T> asContiguous() const;
template<unsigned dimension> StridedArrayView<dimension + 1, T> asContiguous() const;
StridedIterator<dimensions, T> begin() const { return {_data, _size, _stride, 0}; }
StridedIterator<dimensions, T> cbegin() const { return {_data, _size, _stride, 0}; }
StridedIterator<dimensions, T> end() const {
return {_data, _size, _stride, _size[0]};
}
StridedIterator<dimensions, T> cend() const {
return {_data, _size, _stride, _size[0]};
}
ElementType front() const;
ElementType back() const;
ElementType operator[](std::size_t i) const;
T& operator[](const Containers::Size<dimensions>& i) const;
StridedArrayView<dimensions, T> slice(std::size_t begin, std::size_t end) const;
template<unsigned newDimensions = dimensions> StridedArrayView<newDimensions, T> slice(const Containers::Size<dimensions>& begin, const Containers::Size<dimensions>& end) const;
StridedArrayView<dimensions, T> sliceSize(std::size_t begin, std::size_t size) const {
return slice(begin, begin + size);
}
template<unsigned newDimensions = dimensions> StridedArrayView<newDimensions, T> sliceSize(const Containers::Size<dimensions>& begin, const Containers::Size<dimensions>& size) const;
template<unsigned newDimensions = dimensions> StridedArrayView<newDimensions, T> slice() const {
return slice<newDimensions>({}, _size);
}
template<class U, class V = T> auto slice(U V::*member) const -> typename std::enable_if<(std::is_class<V>::value || std::is_union<V>::value) && !std::is_member_function_pointer<decltype(member)>::value, StridedArrayView<dimensions, typename std::conditional<std::is_const<T>::value, const U, U>::type>>::type {
return StridedArrayView<dimensions, typename std::conditional<std::is_const<T>::value, const U, U>::type>{_size, _stride, &(static_cast<T*>(_data)->*member)};
}
template<class U, class V = T> typename std::enable_if<(std::is_class<V>::value || std::is_union<V>::value) && !std::is_const<T>::value, StridedArrayView<dimensions, U>>::type slice(U&(V::*memberFunction)()) const;
template<class U, class V = T> typename std::enable_if<(std::is_class<V>::value || std::is_union<V>::value) && !std::is_const<T>::value, StridedArrayView<dimensions, U>>::type slice(U&(V::*memberFunction)() &) const;
template<class U, class V = T> typename std::enable_if<(std::is_class<V>::value || std::is_union<V>::value) && std::is_const<T>::value, StridedArrayView<dimensions, const U>>::type slice(const U&(V::*memberFunction)() const) const;
template<class U, class V = T> typename std::enable_if<(std::is_class<V>::value || std::is_union<V>::value) && std::is_const<T>::value, StridedArrayView<dimensions, const U>>::type slice(const U&(V::*memberFunction)() const &) const;
BasicStridedBitArrayView<dimensions, ArithmeticType> sliceBit(std::size_t index) const;
StridedArrayView<dimensions, T> prefix(std::size_t size) const {
return slice(0, size);
}
template<unsigned newDimensions = dimensions> StridedArrayView<newDimensions, T> prefix(const Containers::Size<dimensions>& size) const {
return slice<newDimensions>({}, size);
}
StridedArrayView<dimensions, T> exceptPrefix(std::size_t size) const {
return slice(size, _size._data[0]);
}
template<unsigned newDimensions = dimensions> StridedArrayView<newDimensions, T> exceptPrefix(const Containers::Size<dimensions>& size) const {
return slice<newDimensions>(size, _size);
}
StridedArrayView<dimensions, T> exceptSuffix(std::size_t size) const {
return slice({}, _size._data[0] - size);
}
template<unsigned newDimensions = dimensions> StridedArrayView<newDimensions, T> exceptSuffix(const Containers::Size<dimensions>& size) const;
StridedArrayView<dimensions, T> every(std::ptrdiff_t skip) const;
StridedArrayView<dimensions, T> every(const Containers::Stride<dimensions>& skip) const;
template<unsigned dimensionA, unsigned dimensionB> StridedArrayView<dimensions, T> transposed() const;
template<unsigned dimension> StridedArrayView<dimensions, T> flipped() const;
template<unsigned dimension> StridedArrayView<dimensions, T> broadcasted(std::size_t size) const;
template<unsigned dimension, unsigned count> StridedArrayView<dimensions + count - 1, T> expanded(const Containers::Size<count>& size) const;
template<unsigned dimension, unsigned count> StridedArrayView<dimensions - count + 1, T> collapsed() const;
private:
template<unsigned, class> friend class StridedArrayView;
template<unsigned, class> friend struct Implementation::StridedElement;
template<int> friend struct Implementation::ArrayCastFlattenOrInflate;
template<class U, unsigned dimensions_, class T_> friend StridedArrayView<dimensions_, U> arrayCast(const StridedArrayView<dimensions_, T_>&);
template<class U, unsigned dimensions_> friend StridedArrayView<dimensions_, U> arrayCast(const StridedArrayView<dimensions_, void>&);
template<class U, unsigned dimensions_> friend StridedArrayView<dimensions_, U> arrayCast(const StridedArrayView<dimensions_, const void>&);
template<unsigned newDimensions, class U, unsigned dimensions_> StridedArrayView<newDimensions, U> friend arrayCast(const StridedArrayView<dimensions_, void>&, std::size_t);
template<unsigned newDimensions, class U, unsigned dimensions_> StridedArrayView<newDimensions, U> friend arrayCast(const StridedArrayView<dimensions_, const void>&, std::size_t);
constexpr /*implicit*/ StridedArrayView(const Containers::Size<dimensions>& size, const Containers::Stride<dimensions>& stride, ErasedType* data) noexcept: _data{data}, _size{size}, _stride{stride} {}
template<std::size_t ...sequence> constexpr StridedDimensions<dimensions, bool> isEmptyInternal(Implementation::Sequence<sequence...>) const {
return StridedDimensions<dimensions, bool>{(_size._data[sequence] == 0)...};
}
ErasedType* _data;
Containers::Size<dimensions> _size;
Containers::Stride<dimensions> _stride;
};
template<unsigned dimensions> class StridedArrayView<dimensions, void> {
static_assert(dimensions, "can't have a zero-dimensional view");
public:
typedef void Type;
typedef void ErasedType;
enum: unsigned {
Dimensions = dimensions
};
template<class U, class = typename std::enable_if<std::is_same<std::nullptr_t, U>::value>::type> constexpr /*implicit*/ StridedArrayView(U) noexcept: _data{}, _size{}, _stride{} {}
constexpr /*implicit*/ StridedArrayView() noexcept: _data{}, _size{}, _stride{} {}
constexpr /*implicit*/ StridedArrayView(ArrayView<void> data, void* member, const Containers::Size<dimensions>& size, const Containers::Stride<dimensions>& stride) noexcept;
constexpr /*implicit*/ StridedArrayView(ArrayView<void> data, const Containers::Size<dimensions>& size, const Containers::Stride<dimensions>& stride) noexcept: StridedArrayView{data, data.data(), size, stride} {}
template<class T, unsigned d = dimensions, class = typename std::enable_if<d == 1>::type>
constexpr /*implicit*/ StridedArrayView(T* data, std::size_t size) noexcept: _data{data}, _size{size}, _stride{sizeof(T)} {}
template<class T, std::size_t size
, unsigned d = dimensions, class = typename std::enable_if<d == 1 && !std::is_const<T>::value>::type
> constexpr /*implicit*/ StridedArrayView(T(&data)[size]) noexcept: _data{data}, _size{size}, _stride{sizeof(T)} {}
template<class T
, class = typename std::enable_if<!std::is_const<T>::value>::type
> constexpr /*implicit*/ StridedArrayView(StridedArrayView<dimensions, T> view) noexcept: _data{view._data}, _size{view._size}, _stride{view._stride} {}
template<class T
, unsigned d = dimensions, class = typename std::enable_if<d == 1 && !std::is_const<T>::value>::type
> constexpr /*implicit*/ StridedArrayView(ArrayView<T> view) noexcept: _data{view.data()}, _size{view.size()}, _stride{sizeof(T)} {}
template<std::size_t size, class T
, unsigned d = dimensions, class = typename std::enable_if<d == 1 && !std::is_const<T>::value>::type
> constexpr /*implicit*/ StridedArrayView(StaticArrayView<size, T> view) noexcept: _data{view.data()}, _size{size}, _stride{sizeof(T)} {}
template<class T, unsigned d = dimensions, class = typename std::enable_if<d == 1, decltype(Implementation::ErasedArrayViewConverter<typename std::decay<T&&>::type>::from(std::declval<T&&>()))>::type> constexpr /*implicit*/ StridedArrayView(T&& other) noexcept: StridedArrayView{Implementation::ErasedArrayViewConverter<typename std::decay<T&&>::type>::from(other)} {}
constexpr explicit operator bool() const { return _data; }
constexpr void* data() const { return _data; }
constexpr typename std::conditional<dimensions == 1, std::size_t, const Containers::Size<dimensions>&>::type size() const { return _size; }
constexpr typename std::conditional<dimensions == 1, std::ptrdiff_t, const Containers::Stride<dimensions>&>::type stride() const { return _stride; }
constexpr StridedDimensions<dimensions, bool> isEmpty() const {
return isEmptyInternal(typename Implementation::GenerateSequence<dimensions>::Type{});
}
private:
template<unsigned, class> friend class StridedArrayView;
constexpr /*implicit*/ StridedArrayView(const Containers::Size<dimensions>& size, const Containers::Stride<dimensions>& stride, void* data) noexcept: _data{data}, _size{size}, _stride{stride} {}
template<std::size_t ...sequence> constexpr StridedDimensions<dimensions, bool> isEmptyInternal(Implementation::Sequence<sequence...>) const {
return StridedDimensions<dimensions, bool>{(_size._data[sequence] == 0)...};
}
void* _data;
Containers::Size<dimensions> _size;
Containers::Stride<dimensions> _stride;
};
template<unsigned dimensions> class StridedArrayView<dimensions, const void> {
static_assert(dimensions, "can't have a zero-dimensional view");
public:
typedef const void Type;
typedef const void ErasedType;
enum: unsigned {
Dimensions = dimensions
};
template<class U, class = typename std::enable_if<std::is_same<std::nullptr_t, U>::value>::type> constexpr /*implicit*/ StridedArrayView(U) noexcept: _data{}, _size{}, _stride{} {}
constexpr /*implicit*/ StridedArrayView() noexcept: _data{}, _size{}, _stride{} {}
constexpr /*implicit*/ StridedArrayView(ArrayView<const void> data, const void* member, const Containers::Size<dimensions>& size, const Containers::Stride<dimensions>& stride) noexcept;
constexpr /*implicit*/ StridedArrayView(ArrayView<const void> data, const Containers::Size<dimensions>& size, const Containers::Stride<dimensions>& stride) noexcept: StridedArrayView{data, data.data(), size, stride} {}
template<class T, unsigned d = dimensions, class = typename std::enable_if<d == 1>::type>
constexpr /*implicit*/ StridedArrayView(T* data, std::size_t size) noexcept: _data{data}, _size{size}, _stride{sizeof(T)} {}
template<class T, std::size_t size, unsigned d = dimensions, class = typename std::enable_if<d == 1>::type>
constexpr /*implicit*/ StridedArrayView(T(&data)[size]) noexcept: _data{data}, _size{size}, _stride{sizeof(T)} {}
template<class T> constexpr /*implicit*/ StridedArrayView(StridedArrayView<dimensions, T> view) noexcept: _data{view._data}, _size{view._size}, _stride{view._stride} {}
template<class T, unsigned d = dimensions, class = typename std::enable_if<d == 1>::type>
constexpr /*implicit*/ StridedArrayView(ArrayView<T> view) noexcept: _data{view.data()}, _size{view.size()}, _stride{sizeof(T)} {}
template<std::size_t size, class T, unsigned d = dimensions, class = typename std::enable_if<d == 1>::type>
constexpr /*implicit*/ StridedArrayView(StaticArrayView<size, T> view) noexcept: _data{view.data()}, _size{size}, _stride{sizeof(T)} {}
template<class T, unsigned d = dimensions, class = typename std::enable_if<d == 1, decltype(Implementation::ErasedArrayViewConverter<const T>::from(std::declval<const T&>()))>::type> constexpr /*implicit*/ StridedArrayView(const T& other) noexcept: StridedArrayView{Implementation::ErasedArrayViewConverter<const T>::from(other)} {}
constexpr explicit operator bool() const { return _data; }
constexpr const void* data() const { return _data; }
constexpr typename std::conditional<dimensions == 1, std::size_t, const Containers::Size<dimensions>&>::type size() const { return _size; }
constexpr typename std::conditional<dimensions == 1, std::ptrdiff_t, const Containers::Stride<dimensions>&>::type stride() const { return _stride; }
constexpr StridedDimensions<dimensions, bool> isEmpty() const {
return isEmptyInternal(typename Implementation::GenerateSequence<dimensions>::Type{});
}
private:
template<unsigned, class> friend class StridedArrayView;
template<class U, unsigned dimensions_> friend StridedArrayView<dimensions_, U> arrayCast(const StridedArrayView<dimensions_, const void>&);
template<unsigned newDimensions, class U, unsigned dimensions_> StridedArrayView<newDimensions, U> friend arrayCast(const StridedArrayView<dimensions_, const void>&, std::size_t);
constexpr /*implicit*/ StridedArrayView(const Containers::Size<dimensions>& size, const Containers::Stride<dimensions>& stride, const void* data) noexcept: _data{data}, _size{size}, _stride{stride} {}
template<std::size_t ...sequence> constexpr StridedDimensions<dimensions, bool> isEmptyInternal(Implementation::Sequence<sequence...>) const {
return StridedDimensions<dimensions, bool>{(_size._data[sequence] == 0)...};
}
const void* _data;
Containers::Size<dimensions> _size;
Containers::Stride<dimensions> _stride;
};
#ifndef CORRADE_MSVC2015_COMPATIBILITY /* Multiple definitions still broken */
template<class T> using StridedArrayView1D = StridedArrayView<1, T>;
template<class T> using StridedArrayView2D = StridedArrayView<2, T>;
template<class T> using StridedArrayView3D = StridedArrayView<3, T>;
template<class T> using StridedArrayView4D = StridedArrayView<4, T>;
#endif
template<class T> constexpr StridedArrayView1D<T> stridedArrayView(ArrayView<typename StridedArrayView1D<T>::ErasedType> data, T* member, std::size_t size, std::ptrdiff_t stride) {
return StridedArrayView1D<T>{data, member, size, stride};
}
#if defined(CORRADE_TARGET_GCC) && !defined(CORRADE_TARGET_CLANG) && __GNUC__ < 5
template<std::size_t size_, class T, class U> constexpr StridedArrayView1D<T> stridedArrayView(U(&data)[size_], T* member, std::size_t size, std::ptrdiff_t stride) {
return StridedArrayView1D<T>{data, member, size, stride};
}
#endif
template<class T> constexpr StridedArrayView1D<T> stridedArrayView(ArrayView<T> data, std::size_t size, std::ptrdiff_t stride) {
return {data, size, stride};
}
template<class T> constexpr StridedArrayView1D<T> stridedArrayView(T* data, std::size_t size) {
return {data, size};
}
template<std::size_t size, class T> constexpr StridedArrayView1D<T> stridedArrayView(T(&data)[size]) {
return StridedArrayView1D<T>{data};
}
template<class T> StridedArrayView1D<const T> stridedArrayView(std::initializer_list<T> list) {
return StridedArrayView1D<const T>{arrayView(list)};
}
template<class T> constexpr StridedArrayView1D<T> stridedArrayView(ArrayView<T> view) {
return view;
}
template<std::size_t size, class T> constexpr StridedArrayView1D<T> stridedArrayView(StaticArrayView<size, T> view) {
return ArrayView<T>{view};
}
template<unsigned dimensions, class T> constexpr StridedArrayView<dimensions, T> stridedArrayView(StridedArrayView<dimensions, T> view) {
return view;
}
template<class T, class U = decltype(stridedArrayView(Implementation::ErasedArrayViewConverter<typename std::remove_reference<T&&>::type>::from(std::declval<T&&>())))> constexpr U stridedArrayView(T&& other) {
return Implementation::ErasedArrayViewConverter<typename std::remove_reference<T&&>::type>::from(Utility::forward<T>(other));
}
template<class U, unsigned dimensions, class T> StridedArrayView<dimensions, U> arrayCast(const StridedArrayView<dimensions, T>& view) {
static_assert(std::is_standard_layout<T>::value, "the source type is not standard layout");
static_assert(std::is_standard_layout<U>::value, "the target type is not standard layout");
for(unsigned i = 0; i != dimensions; ++i) {
CORRADE_ASSERT(!view._stride._data[i] || sizeof(U) <= std::size_t(view._stride._data[i] < 0 ? -view._stride._data[i] : view._stride._data[i]),
"Containers::arrayCast(): can't fit a" << sizeof(U) << Utility::Debug::nospace << "-byte type into a stride of" << view._stride._data[i], {});
}
return StridedArrayView<dimensions, U>{view._size, view._stride, view._data};
}
template<class U, unsigned dimensions> StridedArrayView<dimensions, U> arrayCast(const StridedArrayView<dimensions, const void>& view) {
static_assert(std::is_standard_layout<U>::value, "the target type is not standard layout");
for(unsigned i = 0; i != dimensions; ++i) {
CORRADE_ASSERT(!view._stride._data[i] || sizeof(U) <= std::size_t(view._stride._data[i] < 0 ? -view._stride._data[i] : view._stride._data[i]),
"Containers::arrayCast(): can't fit a" << sizeof(U) << Utility::Debug::nospace << "-byte type into a stride of" << view._stride._data[i], {});
}
return StridedArrayView<dimensions, U>{view._size, view._stride, view._data};
}
template<class U, unsigned dimensions> StridedArrayView<dimensions, U> arrayCast(const StridedArrayView<dimensions, void>& view) {
auto out = arrayCast<const U, dimensions>(StridedArrayView<dimensions, const void>{view});
return StridedArrayView<dimensions, U>{out._size, out._stride, const_cast<void*>(out._data)};
}
namespace Implementation {
template<int dimensions> struct ArrayCastFlattenOrInflate {
static_assert(dimensions == 0, "can only inflate into one more dimension or flatten into the same / one less dimension");
};
template<> struct ArrayCastFlattenOrInflate<-1> {
template<class U, unsigned dimensions, class T> static StridedArrayView<dimensions - 1, U> cast(const StridedArrayView<dimensions, T>& view) {
for(unsigned i = 0; i != dimensions - 1; ++i) {
CORRADE_ASSERT(!view._stride._data[i] || sizeof(U) <= std::size_t(view._stride._data[i] < 0 ? -view._stride._data[i] : view._stride._data[i]),
"Containers::arrayCast(): can't fit a" << sizeof(U) << Utility::Debug::nospace << "-byte type into a stride of" << view._stride._data[i], {});
}
CORRADE_ASSERT(sizeof(T) == std::size_t(view._stride._data[dimensions - 1]),
"Containers::arrayCast(): last dimension needs to be contiguous in order to be flattened, expected stride" << sizeof(T) << "but got" << view.stride()[dimensions - 1], {});
CORRADE_ASSERT(sizeof(T)*view._size._data[dimensions - 1] == sizeof(U),
"Containers::arrayCast(): last dimension needs to have byte size equal to new type size in order to be flattened, expected" << sizeof(U) << "but got" << sizeof(T)*view._size._data[dimensions - 1], {});
return StridedArrayView<dimensions - 1, U>{
StaticArrayView<dimensions, const std::size_t>(view._size).template prefix<dimensions - 1>(),
StaticArrayView<dimensions, const std::ptrdiff_t>(view._stride).template prefix<dimensions - 1>(),
view._data};
}
};
template<> struct ArrayCastFlattenOrInflate<0> {
template<class U, unsigned dimensions, class T> static StridedArrayView<dimensions, U> cast(const StridedArrayView<dimensions, T>& view) {
for(unsigned i = 0; i != dimensions - 1; ++i) {
CORRADE_ASSERT(!view._stride._data[i] || sizeof(U) <= std::size_t(view._stride._data[i] < 0 ? -view._stride._data[i] : view._stride._data[i]),
"Containers::arrayCast(): can't fit a" << sizeof(U) << Utility::Debug::nospace << "-byte type into a stride of" << view._stride._data[i], {});
}
CORRADE_ASSERT(sizeof(T) == std::size_t(view._stride._data[dimensions - 1]),
"Containers::arrayCast(): last dimension needs to be contiguous in order to be flattened, expected stride" << sizeof(T) << "but got" << view.stride()[dimensions - 1], {});
CORRADE_ASSERT(sizeof(T)*view._size._data[dimensions - 1] % sizeof(U) == 0,
"Containers::arrayCast(): last dimension needs to have byte size divisible by new type size in order to be flattened, but for a" << sizeof(U) << Utility::Debug::nospace << "-byte type got" << sizeof(T)*view._size._data[dimensions - 1], {});
Size<dimensions> size;
Stride<dimensions> stride;
size._data[dimensions - 1] = sizeof(T)*view._size._data[dimensions - 1]/sizeof(U);
stride._data[dimensions - 1] = sizeof(U);
for(std::size_t i = 0; i != dimensions - 1; ++i) {
size._data[i] = view._size._data[i];
stride._data[i] = view._stride._data[i];
}
return StridedArrayView<dimensions, U>{size, stride, view._data};
}
};
template<> struct ArrayCastFlattenOrInflate<+1> {
template<class U, unsigned dimensions, class T> static StridedArrayView<dimensions + 1, U> cast(const StridedArrayView<dimensions, T>& view) {
constexpr std::size_t lastDimensionSize = sizeof(T)/sizeof(U);
static_assert(sizeof(T) % lastDimensionSize == 0, "original type not a multiply of inflated type");
Size<dimensions + 1> size;
Stride<dimensions + 1> stride;
size._data[dimensions] = lastDimensionSize;
stride._data[dimensions] = sizeof(U);
for(std::size_t i = 0; i != dimensions; ++i) {
size._data[i] = view._size._data[i];
stride._data[i] = view._stride._data[i];
}
return StridedArrayView<dimensions + 1, U>{size, stride, view._data};
}
};
}
template<unsigned newDimensions, class U, unsigned dimensions, class T> StridedArrayView<newDimensions, U> arrayCast(const StridedArrayView<dimensions, T>& view) {
static_assert(std::is_standard_layout<T>::value, "the source type is not standard layout");
static_assert(std::is_standard_layout<U>::value, "the target type is not standard layout");
return Implementation::ArrayCastFlattenOrInflate<int(newDimensions) - int(dimensions)>::template cast<U>(view);
}
template<unsigned newDimensions, class U, class T> inline StridedArrayView<newDimensions, U> arrayCast(const ArrayView<T>& view) {
return arrayCast<newDimensions, U, 1, T>(view);
}
template<unsigned newDimensions, class U, unsigned dimensions> StridedArrayView<newDimensions, U> arrayCast(const StridedArrayView<dimensions, const void>& view, std::size_t lastDimensionSize) {
static_assert(std::is_standard_layout<U>::value, "the target type is not standard layout");
static_assert(newDimensions == dimensions + 1, "can inflate only into one more dimension");
for(unsigned i = 0; i != dimensions - 1; ++i) {
CORRADE_ASSERT(!view._stride._data[i] || sizeof(U) <= std::size_t(view._stride._data[i] < 0 ? -view._stride._data[i] : view._stride._data[i]),
"Containers::arrayCast(): can't fit a" << sizeof(U) << Utility::Debug::nospace << "-byte type into a stride of" << view._stride._data[i], {});
}
CORRADE_ASSERT(!view._stride._data[dimensions - 1] || lastDimensionSize*sizeof(U) <= std::size_t(view._stride._data[dimensions - 1] < 0 ? -view._stride._data[dimensions - 1] : view._stride._data[dimensions - 1]),
"Containers::arrayCast(): can't fit" << lastDimensionSize << sizeof(U) << Utility::Debug::nospace << "-byte items into a stride of" << view._stride._data[dimensions - 1], {});
std::size_t size[newDimensions];
std::ptrdiff_t stride[newDimensions];
size[dimensions] = lastDimensionSize;
stride[dimensions] = sizeof(U);
for(std::size_t i = 0; i != dimensions; ++i) {
size[i] = view._size._data[i];
stride[i] = view._stride._data[i];
}
return StridedArrayView<newDimensions, U>{
StaticArrayView<newDimensions, const std::size_t>(size), StaticArrayView<newDimensions, const std::ptrdiff_t>(stride),
view._data};
}
template<unsigned newDimensions, class U, unsigned dimensions> StridedArrayView<newDimensions, U> arrayCast(const StridedArrayView<dimensions, void>& view, std::size_t lastDimensionSize) {
auto out = arrayCast<newDimensions, const U, dimensions>(StridedArrayView<dimensions, const void>{view}, lastDimensionSize);
return StridedArrayView<newDimensions, U>{out._size, out._stride, const_cast<void*>(out._data)};
}
template<unsigned dimensions, class T> class StridedIterator {
public:
typedef T Type;
typedef typename std::conditional<dimensions == 1, T&, StridedArrayView<dimensions - 1, T>>::type ElementType;
/*implicit*/ StridedIterator(typename std::conditional<std::is_const<T>::value, const void, void>::type* data, const Size<dimensions>& size, const Stride<dimensions>& stride, std::size_t i) noexcept: _data{data}, _size{size}, _stride{stride}, _i{i} {}
#ifdef CORRADE_MSVC2015_COMPATIBILITY
/*implicit*/ StridedIterator(): _data{}, _size{}, _stride{}, _i{} {}
#endif
bool operator==(const StridedIterator<dimensions, T>& other) const {
return _data == other._data && _stride == other._stride && _i == other._i;
}
bool operator!=(const StridedIterator<dimensions, T>& other) const {
return _data != other._data || _stride != other._stride || _i != other._i;
}
bool operator<(const StridedIterator<dimensions, T>& other) const {
return _data == other._data && _stride == other._stride && _i < other._i;
}
bool operator<=(const StridedIterator<dimensions, T>& other) const {
return _data == other._data && _stride == other._stride && _i <= other._i;
}
bool operator>(const StridedIterator<dimensions, T>& other) const {
return _data == other._data && _stride == other._stride && _i > other._i;
}
bool operator>=(const StridedIterator<dimensions, T>& other) const {
return _data == other._data && _stride == other._stride && _i >= other._i;
}
StridedIterator<dimensions, T> operator+(std::ptrdiff_t i) const {
return {_data, _size, _stride, _i + i};
}
StridedIterator<dimensions, T>& operator+=(std::ptrdiff_t i) {
_i += i;
return *this;
}
StridedIterator<dimensions, T> operator-(std::ptrdiff_t i) const {
return {_data, _size, _stride, _i - i};
}
StridedIterator<dimensions, T>& operator-=(std::ptrdiff_t i) {
_i -= i;
return *this;
}
std::ptrdiff_t operator-(const StridedIterator<dimensions, T>& it) const {
return _i - it._i;
}
StridedIterator<dimensions, T>& operator--() {
--_i;
return *this;
}
StridedIterator<dimensions, T>& operator++() {
++_i;
return *this;
}
ElementType operator*() const {
return Implementation::StridedElement<dimensions, T>::get(_data, _size, _stride, _i);
}
private:
typename std::conditional<std::is_const<T>::value, const void, void>::type* _data;
Size<dimensions> _size;
Stride<dimensions> _stride;
std::size_t _i;
};
template<unsigned dimensions, class T> inline StridedIterator<dimensions, T> operator+(std::ptrdiff_t i, StridedIterator<dimensions, T> it) {
return it + i;
}
template<unsigned dimensions, class T> constexpr StridedArrayView<dimensions, T>::StridedArrayView(ArrayView<ErasedType> data, T* member, const Containers::Size<dimensions>& size, const Containers::Stride<dimensions>& stride) noexcept: _data{(
CORRADE_CONSTEXPR_ASSERT(Implementation::isAnyDimensionZero(size._data, typename Implementation::GenerateSequence<dimensions>::Type{}) || Implementation::largestStride(size._data, stride._data, typename Implementation::GenerateSequence<dimensions>::Type{}) <= data.size(),
"Containers::StridedArrayView: data size" << data.size() << "is not enough for" << size << "elements of stride" << stride),
member)}, _size{size}, _stride{stride} {}
template<unsigned dimensions> constexpr StridedArrayView<dimensions, void>::StridedArrayView(ArrayView<void> data, void* member, const Containers::Size<dimensions>& size, const Containers::Stride<dimensions>& stride) noexcept: _data{(
CORRADE_CONSTEXPR_ASSERT(Implementation::isAnyDimensionZero(size._data, typename Implementation::GenerateSequence<dimensions>::Type{}) || Implementation::largestStride(size._data, stride._data, typename Implementation::GenerateSequence<dimensions>::Type{}) <= data.size(),
"Containers::StridedArrayView: data size" << data.size() << "is not enough for" << size << "elements of stride" << stride),
member)}, _size{size}, _stride{stride} {}
template<unsigned dimensions> constexpr StridedArrayView<dimensions, const void>::StridedArrayView(ArrayView<const void> data, const void* member, const Containers::Size<dimensions>& size, const Containers::Stride<dimensions>& stride) noexcept: _data{(
CORRADE_CONSTEXPR_ASSERT(Implementation::isAnyDimensionZero(size._data, typename Implementation::GenerateSequence<dimensions>::Type{}) || Implementation::largestStride(size._data, stride._data, typename Implementation::GenerateSequence<dimensions>::Type{}) <= data.size(),
"Containers::StridedArrayView: data size" << data.size() << "is not enough for" << size << "elements of stride" << stride),
member)}, _size{size}, _stride{stride} {}
template<unsigned dimensions, class T> template<unsigned lessDimensions, class> StridedArrayView<dimensions, T>::StridedArrayView(const StridedArrayView<lessDimensions, T>& other) noexcept: _data{other._data}, _size{Corrade::NoInit}, _stride{Corrade::NoInit} {
constexpr std::size_t extraDimensions = dimensions - lessDimensions;
const std::size_t stride = other._size._data[0]*other._stride._data[0];
for(std::size_t i = 0; i != extraDimensions; ++i) {
_size._data[i] = 1;
_stride._data[i] = stride;
}
for(std::size_t i = 0; i != lessDimensions; ++i) {
_size._data[extraDimensions + i] = other._size._data[i];
_stride._data[extraDimensions + i] = other._stride._data[i];
}
}
template<unsigned dimensions, class T> template<unsigned dimension> bool StridedArrayView<dimensions, T>::isContiguous() const {
static_assert(dimension < dimensions, "dimension out of range");
std::size_t nextDimensionSize = sizeof(T);
for(std::size_t i = dimensions; i != dimension; --i) {
if(std::size_t(_stride._data[i - 1]) != nextDimensionSize) return false;
nextDimensionSize *= _size._data[i - 1];
}
return true;
}
template<unsigned dimensions, class T> ArrayView<T> StridedArrayView<dimensions, T>::asContiguous() const {
CORRADE_DEBUG_ASSERT(isContiguous(),
"Containers::StridedArrayView::asContiguous(): the view is not contiguous", {});
std::size_t size = 1;
for(std::size_t i = 0; i != dimensions; ++i) size *= _size._data[i];
return {static_cast<T*>(_data), size};
}