forked from xtensor-stack/xtensor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxfixed.hpp
More file actions
949 lines (811 loc) · 36.3 KB
/
Copy pathxfixed.hpp
File metadata and controls
949 lines (811 loc) · 36.3 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
/***************************************************************************
* 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_FIXED_HPP
#define XTENSOR_FIXED_HPP
#include <algorithm>
#include <array>
#include <cstddef>
#include <utility>
#include <vector>
#include "xcontainer.hpp"
#include "xstrides.hpp"
#include "xstorage.hpp"
#include "xsemantic.hpp"
#include "xtensor_config.hpp"
namespace xtl
{
namespace detail
{
template <class T, std::size_t N>
struct sequence_builder<xt::const_array<T, N>>
{
using sequence_type = xt::const_array<T, N>;
using value_type = typename sequence_type::value_type;
using size_type = typename sequence_type::size_type;
inline static sequence_type make(size_type /*size*/, value_type /*v*/)
{
return sequence_type();
}
};
}
}
namespace xt
{
/**********************
* xfixed declaration *
**********************/
template <class ET, class S, layout_type L, bool SH, class Tag>
class xfixed_container;
namespace detail
{
/**************************************************************************************
The following is something we can currently only dream about -- for when we drop
support for a lot of the old compilers (e.g. GCC 4.9, MSVC 2017 ;)
template <class T>
constexpr std::size_t calculate_stride(T& shape, std::size_t idx, layout_type L)
{
if (shape[idx] == 1)
{
return std::size_t(0);
}
std::size_t data_size = 1;
std::size_t stride = 1;
if (L == layout_type::row_major)
{
// because we have a integer sequence that counts
// from 0 to sz - 1, we need to "invert" idx here
idx = shape.size() - idx;
for (std::size_t i = idx; i != 0; --i)
{
stride = data_size;
data_size = stride * shape[i - 1];
}
}
else
{
for (std::size_t i = 0; i < idx + 1; ++i)
{
stride = data_size;
data_size = stride * shape[i];
}
}
return stride;
}
*****************************************************************************************/
template <layout_type L, std::size_t I, std::size_t... X>
struct calculate_stride;
template <std::size_t I, std::size_t Y, std::size_t... X>
struct calculate_stride<layout_type::column_major, I, Y, X...>
{
constexpr static std::ptrdiff_t value = Y * calculate_stride<layout_type::column_major, I - 1, X...>::value;
};
template <std::size_t Y, std::size_t... X>
struct calculate_stride<layout_type::column_major, 0, Y, X...>
{
constexpr static std::ptrdiff_t value = 1;
};
template <std::size_t I, std::size_t... X>
struct calculate_stride_row_major
{
constexpr static std::ptrdiff_t value = at<sizeof...(X) - I, X...>::value * calculate_stride_row_major<I - 1, X...>::value;
};
template <std::size_t... X>
struct calculate_stride_row_major<0, X...>
{
constexpr static std::ptrdiff_t value = 1;
};
template <std::size_t I, std::size_t... X>
struct calculate_stride<layout_type::row_major, I, X...>
{
constexpr static std::ptrdiff_t value = calculate_stride_row_major<sizeof...(X) - I - 1, X...>::value;
};
namespace workaround
{
template <layout_type L, size_t I, class SEQ>
struct computed_strides;
template <layout_type L, size_t I, size_t... X>
struct computed_strides<L, I, std::index_sequence<X...>>
{
constexpr static std::ptrdiff_t value = calculate_stride<L, I, X...>::value;
};
template <layout_type L, size_t I, class SEQ >
constexpr std::ptrdiff_t get_computed_strides(bool cond)
{
return cond ? 0 : computed_strides<L, I, SEQ>::value;
}
}
template <layout_type L, class R, std::size_t... X, std::size_t... I>
constexpr R get_strides_impl(const xt::fixed_shape<X...>& shape, std::index_sequence<I...>)
{
static_assert((L == layout_type::row_major) || (L == layout_type::column_major),
"Layout not supported for fixed array");
#if (_MSC_VER >= 1910)
using temp_type = std::index_sequence<X...>;
return R({ workaround::get_computed_strides<L, I, temp_type>(shape[I] == 1)... });
#else
return R({ shape[I] == 1 ? 0 : calculate_stride<L, I, X...>::value... });
#endif
}
template <class S, class T, std::size_t... I>
constexpr T get_backstrides_impl(const S& shape, const T& strides, std::index_sequence<I...>)
{
return T({(strides[I] * std::ptrdiff_t(shape[I] - 1))...});
}
template <std::size_t... X>
struct fixed_compute_size_impl;
template <std::size_t Y, std::size_t... X>
struct fixed_compute_size_impl<Y, X...>
{
constexpr static std::size_t value = Y * fixed_compute_size_impl<X...>::value;
};
template <std::size_t X>
struct fixed_compute_size_impl<X>
{
constexpr static std::size_t value = X;
};
template <>
struct fixed_compute_size_impl<>
{
// support for 0D xtensor fixed (empty shape = xshape<>)
constexpr static std::size_t value = 1;
};
// TODO unify with constexpr compute_size when dropping MSVC 2015
template <class T>
struct fixed_compute_size;
template <std::size_t... X>
struct fixed_compute_size<xt::fixed_shape<X...>>
{
constexpr static std::size_t value = fixed_compute_size_impl<X...>::value;
};
template <class V, std::size_t... X>
struct get_init_type_impl;
template <class V, std::size_t Y>
struct get_init_type_impl<V, Y>
{
using type = V[Y];
};
template <class V>
struct get_init_type_impl<V>
{
using type = V[1];
};
template <class V, std::size_t Y, std::size_t... X>
struct get_init_type_impl<V, Y, X...>
{
using tmp_type = typename get_init_type_impl<V, X...>::type;
using type = tmp_type[Y];
};
}
template <layout_type L, class R, std::size_t... X>
constexpr R get_strides(const fixed_shape<X...>& shape) noexcept
{
return detail::get_strides_impl<L, R>(shape, std::make_index_sequence<sizeof...(X)>{});
}
template <class S, class T>
constexpr T get_backstrides(const S& shape, const T& strides) noexcept
{
return detail::get_backstrides_impl(shape, strides,
std::make_index_sequence<std::tuple_size<T>::value>{});
}
template <class V, class S>
struct get_init_type;
template <class V, std::size_t... X>
struct get_init_type<V, fixed_shape<X...>>
{
using type = typename detail::get_init_type_impl<V, X...>::type;
};
template <class V, class S>
using get_init_type_t = typename get_init_type<V, S>::type;
template <class ET, class S, layout_type L, bool SH, class Tag>
struct xcontainer_inner_types<xfixed_container<ET, S, L, SH, Tag>>
{
using shape_type = S;
using inner_shape_type = typename S::cast_type;
using strides_type = get_strides_t<inner_shape_type>;
using inner_strides_type = strides_type;
using backstrides_type = inner_strides_type;
using inner_backstrides_type = backstrides_type;
// NOTE: 0D (S::size() == 0) results in storage for 1 element (scalar)
#if defined(_MSC_VER) && _MSC_VER < 1910 && !defined(_WIN64)
// WORKAROUND FOR MSVC 2015 32 bit, fallback to unaligned container for 0D scalar case
using storage_type = std::array<ET, detail::fixed_compute_size<S>::value>;
#else
using storage_type = aligned_array<ET, detail::fixed_compute_size<S>::value>;
#endif
using reference = typename storage_type::reference;
using const_reference = typename storage_type::const_reference;
using size_type = typename storage_type::size_type;
using temporary_type = xfixed_container<ET, S, L, SH, Tag>;
static constexpr layout_type layout = L;
};
template <class ET, class S, layout_type L, bool SH, class Tag>
struct xiterable_inner_types<xfixed_container<ET, S, L, SH, Tag>>
: xcontainer_iterable_types<xfixed_container<ET, S, L, SH, Tag>>
{
};
/**
* @class xfixed_container
* @brief Dense multidimensional container with tensor semantic and fixed
* dimension.
*
* The xfixed_container class implements a dense multidimensional container
* with tensor semantic and fixed dimension
*
* @tparam ET The type of the elements.
* @tparam S The xshape template paramter of the container.
* @tparam L The layout_type of the tensor.
* @tparam SH Wether the tensor can be used as a shared expression.
* @tparam Tag The expression tag.
* @sa xtensor_fixed
*/
template <class ET, class S, layout_type L, bool SH, class Tag>
class xfixed_container : public xcontainer<xfixed_container<ET, S, L, SH, Tag>>,
public xcontainer_semantic<xfixed_container<ET, S, L, SH, Tag>>
{
public:
using self_type = xfixed_container<ET, S, L, SH, Tag>;
using base_type = xcontainer<self_type>;
using semantic_base = xcontainer_semantic<self_type>;
using storage_type = typename base_type::storage_type;
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 shape_type = typename base_type::shape_type;
using inner_shape_type = typename base_type::inner_shape_type;
using strides_type = typename base_type::strides_type;
using backstrides_type = typename base_type::backstrides_type;
using inner_backstrides_type = typename base_type::inner_backstrides_type;
using inner_strides_type = typename base_type::inner_strides_type;
using temporary_type = typename semantic_base::temporary_type;
using expression_tag = Tag;
constexpr static std::size_t N = std::tuple_size<shape_type>::value;
xfixed_container() = default;
xfixed_container(const value_type& v);
explicit xfixed_container(const inner_shape_type& shape, layout_type l = L);
explicit xfixed_container(const inner_shape_type& shape, value_type v, layout_type l = L);
// remove this enable_if when removing the other value_type constructor
template <class IX = std::integral_constant<std::size_t, N>, class EN = std::enable_if_t<IX::value != 0, int>>
xfixed_container(nested_initializer_list_t<value_type, N> t);
~xfixed_container() = default;
xfixed_container(const xfixed_container&) = default;
xfixed_container& operator=(const xfixed_container&) = default;
xfixed_container(xfixed_container&&) = default;
xfixed_container& operator=(xfixed_container&&) = default;
template <class E>
xfixed_container(const xexpression<E>& e);
template <class E>
xfixed_container& operator=(const xexpression<E>& e);
template <class ST = std::array<std::size_t, N>>
static xfixed_container from_shape(ST&& /*s*/);
template <class ST = std::array<std::size_t, N>>
void resize(ST&& shape, bool force = false) const;
template <class ST = shape_type>
void resize(ST&& shape, layout_type l) const;
template <class ST = shape_type>
void resize(ST&& shape, const strides_type& strides) const;
template <class ST = std::array<std::size_t, N>>
auto const& reshape(ST&& shape, layout_type layout = L) const;
template <class ST>
bool broadcast_shape(ST& s, bool reuse_cache = false) const;
constexpr layout_type layout() const noexcept;
bool is_contiguous() const noexcept;
private:
storage_type m_storage;
XTENSOR_CONSTEXPR_ENHANCED_STATIC inner_shape_type m_shape = S();
XTENSOR_CONSTEXPR_ENHANCED_STATIC inner_strides_type m_strides = get_strides<L, inner_strides_type>(S());
XTENSOR_CONSTEXPR_ENHANCED_STATIC inner_backstrides_type m_backstrides = get_backstrides(m_shape, m_strides);
storage_type& storage_impl() noexcept;
const storage_type& storage_impl() const noexcept;
XTENSOR_CONSTEXPR_RETURN const inner_shape_type& shape_impl() const noexcept;
XTENSOR_CONSTEXPR_RETURN const inner_strides_type& strides_impl() const noexcept;
XTENSOR_CONSTEXPR_RETURN const inner_backstrides_type& backstrides_impl() const noexcept;
friend class xcontainer<xfixed_container<ET, S, L, SH, Tag>>;
};
#ifdef XTENSOR_HAS_CONSTEXPR_ENHANCED
// Out of line definitions to prevent linker errors prior to C++17
template <class ET, class S, layout_type L, bool SH, class Tag>
constexpr typename xfixed_container<ET, S, L, SH, Tag>::inner_shape_type xfixed_container<ET, S, L, SH, Tag>::m_shape;
template <class ET, class S, layout_type L, bool SH, class Tag>
constexpr typename xfixed_container<ET, S, L, SH, Tag>::inner_strides_type xfixed_container<ET, S, L, SH, Tag>::m_strides;
template <class ET, class S, layout_type L, bool SH, class Tag>
constexpr typename xfixed_container<ET, S, L, SH, Tag>::inner_backstrides_type xfixed_container<ET, S, L, SH, Tag>::m_backstrides;
#endif
/****************************************
* xfixed_container_adaptor declaration *
****************************************/
template <class EC, class S, layout_type L, bool SH, class Tag>
class xfixed_adaptor;
template <class EC, class S, layout_type L, bool SH, class Tag>
struct xcontainer_inner_types<xfixed_adaptor<EC, S, L, SH, Tag>>
{
using storage_type = std::remove_reference_t<EC>;
using reference = typename storage_type::reference;
using const_reference = typename storage_type::const_reference;
using size_type = typename storage_type::size_type;
using shape_type = S;
using inner_shape_type = typename S::cast_type;
using strides_type = get_strides_t<inner_shape_type>;
using backstrides_type = strides_type;
using inner_strides_type = strides_type;
using inner_backstrides_type = backstrides_type;
using temporary_type = xfixed_container<typename storage_type::value_type, S, L, SH, Tag>;
static constexpr layout_type layout = L;
};
template <class EC, class S, layout_type L, bool SH, class Tag>
struct xiterable_inner_types<xfixed_adaptor<EC, S, L, SH, Tag>>
: xcontainer_iterable_types<xfixed_adaptor<EC, S, L, SH, Tag>>
{
};
/**
* @class xfixed_adaptor
* @brief Dense multidimensional container adaptor with tensor semantic
* and fixed dimension.
*
* The xfixed_adaptor class implements a dense multidimensional
* container adaptor with tensor semantic and fixed dimension. It
* is used to provide a multidimensional container semantic and a
* tensor semantic to stl-like containers.
*
* @tparam EC The closure for the container type to adapt.
* @tparam S The xshape template parameter for the fixed shape of the adaptor
* @tparam L The layout_type of the adaptor.
* @tparam SH Wether the adaptor can be used as a shared expression.
* @tparam Tag The expression tag.
*/
template <class EC, class S, layout_type L, bool SH, class Tag>
class xfixed_adaptor : public xcontainer<xfixed_adaptor<EC, S, L, SH, Tag>>,
public xcontainer_semantic<xfixed_adaptor<EC, S, L, SH, Tag>>
{
public:
using container_closure_type = EC;
using self_type = xfixed_adaptor<EC, S, L, SH, Tag>;
using base_type = xcontainer<self_type>;
using semantic_base = xcontainer_semantic<self_type>;
using storage_type = typename base_type::storage_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 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 temporary_type = typename semantic_base::temporary_type;
using expression_tag = Tag;
constexpr static std::size_t N = S::size();
xfixed_adaptor(storage_type&& data);
xfixed_adaptor(const storage_type& data);
template <class D>
xfixed_adaptor(D&& data);
~xfixed_adaptor() = default;
xfixed_adaptor(const xfixed_adaptor&) = default;
xfixed_adaptor& operator=(const xfixed_adaptor&);
xfixed_adaptor(xfixed_adaptor&&) = default;
xfixed_adaptor& operator=(xfixed_adaptor&&);
xfixed_adaptor& operator=(temporary_type&&);
template <class E>
xfixed_adaptor& operator=(const xexpression<E>& e);
template <class ST = std::array<std::size_t, N>>
void resize(ST&& shape, bool force = false) const;
template <class ST = shape_type>
void resize(ST&& shape, layout_type l) const;
template <class ST = shape_type>
void resize(ST&& shape, const strides_type& strides) const;
template <class ST = std::array<std::size_t, N>>
const auto& reshape(ST&& shape, layout_type layout = L) const;
template <class ST>
bool broadcast_shape(ST& s, bool reuse_cache = false) const;
constexpr layout_type layout() const noexcept;
bool is_contiguous() const noexcept;
private:
container_closure_type m_storage;
XTENSOR_CONSTEXPR_ENHANCED_STATIC inner_shape_type m_shape = S();
XTENSOR_CONSTEXPR_ENHANCED_STATIC inner_strides_type m_strides = get_strides<L, inner_strides_type>(S());
XTENSOR_CONSTEXPR_ENHANCED_STATIC inner_backstrides_type m_backstrides = get_backstrides(m_shape, m_strides);
storage_type& storage_impl() noexcept;
const storage_type& storage_impl() const noexcept;
XTENSOR_CONSTEXPR_RETURN const inner_shape_type& shape_impl() const noexcept;
XTENSOR_CONSTEXPR_RETURN const inner_strides_type& strides_impl() const noexcept;
XTENSOR_CONSTEXPR_RETURN const inner_backstrides_type& backstrides_impl() const noexcept;
friend class xcontainer<xfixed_adaptor<EC, S, L, SH, Tag>>;
};
#ifdef XTENSOR_HAS_CONSTEXPR_ENHANCED
// Out of line definitions to prevent linker errors prior to C++17
template <class EC, class S, layout_type L, bool SH, class Tag>
constexpr typename xfixed_adaptor<EC, S, L, SH, Tag>::inner_shape_type xfixed_adaptor<EC, S, L, SH, Tag>::m_shape;
template <class EC, class S, layout_type L, bool SH, class Tag>
constexpr typename xfixed_adaptor<EC, S, L, SH, Tag>::inner_strides_type xfixed_adaptor<EC, S, L, SH, Tag>::m_strides;
template <class EC, class S, layout_type L, bool SH, class Tag>
constexpr typename xfixed_adaptor<EC, S, L, SH, Tag>::inner_backstrides_type xfixed_adaptor<EC, S, L, SH, Tag>::m_backstrides;
#endif
/************************************
* xfixed_container implementation *
************************************/
/**
* @name Constructors
*/
//@{
/**
* Create an uninitialized xfixed_container.
* Note this function is only provided for homogenity, and the shape & layout argument is
* disregarded (the template shape is always used).
*
* @param shape the shape of the xfixed_container (unused!)
* @param l the layout_type of the xfixed_container (unused!)
*/
template <class ET, class S, layout_type L, bool SH, class Tag>
inline xfixed_container<ET, S, L, SH, Tag>::xfixed_container(const inner_shape_type& shape, layout_type l)
{
(void)(shape);
(void)(l);
XTENSOR_ASSERT(shape.size() == N && std::equal(shape.begin(), shape.end(), m_shape.begin()));
XTENSOR_ASSERT(L == l);
}
template <class ET, class S, layout_type L, bool SH, class Tag>
inline xfixed_container<ET, S, L, SH, Tag>::xfixed_container(const value_type& v)
{
if (this->size() != 1)
{
XTENSOR_THROW(std::runtime_error,
"wrong shape for scalar assignment (has to be xshape<>).");
}
m_storage[0] = v;
}
/**
* Create an xfixed_container, and initialize with the value of v.
* Note, the shape argument to this function is only provided for homogenity,
* and the shape argument is disregarded (the template shape is always used).
*
* @param shape the shape of the xfixed_container (unused!)
* @param v the fill value
* @param l the layout_type of the xfixed_container (unused!)
*/
template <class ET, class S, layout_type L, bool SH, class Tag>
inline xfixed_container<ET, S, L, SH, Tag>::xfixed_container(const inner_shape_type& shape, value_type v, layout_type l)
{
(void)(shape);
(void)(l);
XTENSOR_ASSERT(shape.size() == N && std::equal(shape.begin(), shape.end(), m_shape.begin()));
XTENSOR_ASSERT(L == l);
std::fill(m_storage.begin(), m_storage.end(), v);
}
namespace detail
{
template <std::size_t X>
struct check_initializer_list_shape
{
template <class T, class S>
static bool run(const T& t, const S& shape)
{
std::size_t IX = shape.size() - X;
bool result = (shape[IX] == t.size());
for (std::size_t i = 0; i < shape[IX]; ++i)
{
result = result && check_initializer_list_shape<X - 1>::run(t.begin()[i], shape);
}
return result;
}
};
template <>
struct check_initializer_list_shape<0>
{
template <class T, class S>
static bool run(const T& /*t*/, const S& /*shape*/)
{
return true;
}
};
}
template <class ET, class S, layout_type L, bool SH, class Tag>
template <class ST>
inline xfixed_container<ET, S, L, SH, Tag> xfixed_container<ET, S, L, SH, Tag>::from_shape(ST&& shape)
{
(void) shape;
self_type tmp;
XTENSOR_ASSERT(shape.size() == N && std::equal(shape.begin(), shape.end(), tmp.shape().begin()));
return tmp;
}
/**
* Allocates an xfixed_container with shape S with values from a C array.
* The type returned by get_init_type_t is raw C array ``value_type[X][Y][Z]`` for ``xt::xshape<X, Y, Z>``.
* C arrays can be initialized with the initializer list syntax, but the size is checked at compile
* time to prevent errors.
* Note: for clang < 3.8 this is an initializer_list and the size is not checked at compile-or runtime.
*/
template <class ET, class S, layout_type L, bool SH, class Tag>
template <class IX, class EN>
inline xfixed_container<ET, S, L, SH, Tag>::xfixed_container(nested_initializer_list_t<value_type, N> t)
{
XTENSOR_ASSERT_MSG(detail::check_initializer_list_shape<N>::run(t, this->shape()) == true, "initializer list shape does not match fixed shape");
L == layout_type::row_major ? nested_copy(m_storage.begin(), t) : nested_copy(this->template begin<layout_type::row_major>(), t);
}
//@}
/**
* @name Extended copy semantic
*/
//@{
/**
* The extended copy constructor.
*/
template <class ET, class S, layout_type L, bool SH, class Tag>
template <class E>
inline xfixed_container<ET, S, L, SH, Tag>::xfixed_container(const xexpression<E>& e)
{
semantic_base::assign(e);
}
/**
* The extended assignment operator.
*/
template <class ET, class S, layout_type L, bool SH, class Tag>
template <class E>
inline auto xfixed_container<ET, S, L, SH, Tag>::operator=(const xexpression<E>& e) -> self_type&
{
return semantic_base::operator=(e);
}
//@}
/**
* Note that the xfixed_container **cannot** be resized. Attempting to resize with a different
* size throws an assert in debug mode.
*/
template <class ET, class S, layout_type L, bool SH, class Tag>
template <class ST>
inline void xfixed_container<ET, S, L, SH, Tag>::resize(ST&& shape, bool) const
{
(void)(shape); // remove unused parameter warning if XTENSOR_ASSERT undefined
XTENSOR_ASSERT(std::equal(shape.begin(), shape.end(), m_shape.begin()) && shape.size() == m_shape.size());
}
/**
* Note that the xfixed_container **cannot** be resized. Attempting to resize with a different
* size throws an assert in debug mode.
*/
template <class ET, class S, layout_type L, bool SH, class Tag>
template <class ST>
inline void xfixed_container<ET, S, L, SH, Tag>::resize(ST&& shape, layout_type l) const
{
(void)(shape); // remove unused parameter warning if XTENSOR_ASSERT undefined
(void)(l);
XTENSOR_ASSERT(std::equal(shape.begin(), shape.end(), m_shape.begin()) && shape.size() == m_shape.size() && L == l);
}
/**
* Note that the xfixed_container **cannot** be resized. Attempting to resize with a different
* size throws an assert in debug mode.
*/
template <class ET, class S, layout_type L, bool SH, class Tag>
template <class ST>
inline void xfixed_container<ET, S, L, SH, Tag>::resize(ST&& shape, const strides_type& strides) const
{
(void)(shape); // remove unused parameter warning if XTENSOR_ASSERT undefined
(void)(strides);
XTENSOR_ASSERT(std::equal(shape.begin(), shape.end(), m_shape.begin()) && shape.size() == m_shape.size());
XTENSOR_ASSERT(std::equal(strides.begin(), strides.end(), m_strides.begin()) && strides.size() == m_strides.size());
}
/**
* Note that the xfixed_container **cannot** be reshaped to a shape different from ``S``.
*/
template <class ET, class S, layout_type L, bool SH, class Tag>
template <class ST>
inline auto const& xfixed_container<ET, S, L, SH, Tag>::reshape(ST&& shape, layout_type layout) const
{
if (!(std::equal(shape.begin(), shape.end(), m_shape.begin()) && shape.size() == m_shape.size() && layout == L))
{
XTENSOR_THROW(std::runtime_error, "Trying to reshape xtensor_fixed with different shape or layout.");
}
return *this;
}
template <class ET, class S, layout_type L, bool SH, class Tag>
template <class ST>
inline bool xfixed_container<ET, S, L, SH, Tag>::broadcast_shape(ST& shape, bool) const
{
return xt::broadcast_shape(m_shape, shape);
}
template <class ET, class S, layout_type L, bool SH, class Tag>
constexpr layout_type xfixed_container<ET, S, L, SH, Tag>::layout() const noexcept
{
return base_type::static_layout;
}
template <class ET, class S, layout_type L, bool SH, class Tag>
inline bool xfixed_container<ET, S, L, SH, Tag>::is_contiguous() const noexcept
{
using str_type = typename inner_strides_type::value_type;
return m_strides.empty()
|| (layout() == layout_type::row_major && m_strides.back() == str_type(1))
|| (layout() == layout_type::column_major && m_strides.front() == str_type(1));
}
template <class ET, class S, layout_type L, bool SH, class Tag>
inline auto xfixed_container<ET, S, L, SH, Tag>::storage_impl() noexcept -> storage_type&
{
return m_storage;
}
template <class ET, class S, layout_type L, bool SH, class Tag>
inline auto xfixed_container<ET, S, L, SH, Tag>::storage_impl() const noexcept -> const storage_type&
{
return m_storage;
}
template <class ET, class S, layout_type L, bool SH, class Tag>
XTENSOR_CONSTEXPR_RETURN auto xfixed_container<ET, S, L, SH, Tag>::shape_impl() const noexcept -> const inner_shape_type&
{
return m_shape;
}
template <class ET, class S, layout_type L, bool SH, class Tag>
XTENSOR_CONSTEXPR_RETURN auto xfixed_container<ET, S, L, SH, Tag>::strides_impl() const noexcept -> const inner_strides_type&
{
return m_strides;
}
template <class ET, class S, layout_type L, bool SH, class Tag>
XTENSOR_CONSTEXPR_RETURN auto xfixed_container<ET, S, L, SH, Tag>::backstrides_impl() const noexcept -> const inner_backstrides_type&
{
return m_backstrides;
}
/*******************
* xfixed_adaptor *
*******************/
/**
* @name Constructors
*/
//@{
/**
* Constructs an xfixed_adaptor of the given stl-like container.
* @param data the container to adapt
*/
template <class EC, class S, layout_type L, bool SH, class Tag>
inline xfixed_adaptor<EC, S, L, SH, Tag>::xfixed_adaptor(storage_type&& data)
: base_type(), m_storage(std::move(data))
{
}
/**
* Constructs an xfixed_adaptor of the given stl-like container.
* @param data the container to adapt
*/
template <class EC, class S, layout_type L, bool SH, class Tag>
inline xfixed_adaptor<EC, S, L, SH, Tag>::xfixed_adaptor(const storage_type& data)
: base_type(), m_storage(data)
{
}
/**
* Constructs an xfixed_adaptor of the given stl-like container,
* with the specified shape and layout_type.
* @param data the container to adapt
*/
template <class EC, class S, layout_type L, bool SH, class Tag>
template <class D>
inline xfixed_adaptor<EC, S, L, SH, Tag>::xfixed_adaptor(D&& data)
: base_type(), m_storage(std::forward<D>(data))
{
}
//@}
template <class EC, class S, layout_type L, bool SH, class Tag>
inline auto xfixed_adaptor<EC, S, L, SH, Tag>::operator=(const xfixed_adaptor& rhs) -> self_type&
{
base_type::operator=(rhs);
m_storage = rhs.m_storage;
return *this;
}
template <class EC, class S, layout_type L, bool SH, class Tag>
inline auto xfixed_adaptor<EC, S, L, SH, Tag>::operator=(xfixed_adaptor&& rhs) -> self_type&
{
base_type::operator=(std::move(rhs));
m_storage = rhs.m_storage;
return *this;
}
template <class EC, class S, layout_type L, bool SH, class Tag>
inline auto xfixed_adaptor<EC, S, L, SH, Tag>::operator=(temporary_type&& rhs) -> self_type&
{
m_storage.resize(rhs.storage().size());
std::copy(rhs.storage().cbegin(), rhs.storage().cend(), m_storage.begin());
return *this;
}
/**
* @name Extended copy semantic
*/
//@{
/**
* The extended assignment operator.
*/
template <class EC, class S, layout_type L, bool SH, class Tag>
template <class E>
inline auto xfixed_adaptor<EC, S, L, SH, Tag>::operator=(const xexpression<E>& e) -> self_type&
{
return semantic_base::operator=(e);
}
//@}
/**
* Note that the xfixed_adaptor **cannot** be resized. Attempting to resize with a different
* size throws an assert in debug mode.
*/
template <class ET, class S, layout_type L, bool SH, class Tag>
template <class ST>
inline void xfixed_adaptor<ET, S, L, SH, Tag>::resize(ST&& shape, bool) const
{
(void)(shape); // remove unused parameter warning if XTENSOR_ASSERT undefined
XTENSOR_ASSERT(std::equal(shape.begin(), shape.end(), m_shape.begin()) && shape.size() == m_shape.size());
}
/**
* Note that the xfixed_adaptor **cannot** be resized. Attempting to resize with a different
* size throws an assert in debug mode.
*/
template <class ET, class S, layout_type L, bool SH, class Tag>
template <class ST>
inline void xfixed_adaptor<ET, S, L, SH, Tag>::resize(ST&& shape, layout_type l) const
{
(void)(shape); // remove unused parameter warning if XTENSOR_ASSERT undefined
(void)(l);
XTENSOR_ASSERT(std::equal(shape.begin(), shape.end(), m_shape.begin()) && shape.size() == m_shape.size() && L == l);
}
/**
* Note that the xfixed_adaptor **cannot** be resized. Attempting to resize with a different
* size throws an assert in debug mode.
*/
template <class ET, class S, layout_type L, bool SH, class Tag>
template <class ST>
inline void xfixed_adaptor<ET, S, L, SH, Tag>::resize(ST&& shape, const strides_type& strides) const
{
(void)(shape); // remove unused parameter warning if XTENSOR_ASSERT undefined
(void)(strides);
XTENSOR_ASSERT(std::equal(shape.begin(), shape.end(), m_shape.begin()) && shape.size() == m_shape.size());
XTENSOR_ASSERT(std::equal(strides.begin(), strides.end(), m_strides.begin()) && strides.size() == m_strides.size());
}
/**
* Note that the xfixed_container **cannot** be reshaped to a shape different from ``S``.
*/
template <class ET, class S, layout_type L, bool SH, class Tag>
template <class ST>
inline auto const& xfixed_adaptor<ET, S, L, SH, Tag>::reshape(ST&& shape, layout_type layout) const
{
if (!(std::equal(shape.begin(), shape.end(), m_shape.begin()) && shape.size() == m_shape.size() && layout == L))
{
XTENSOR_THROW(std::runtime_error, "Trying to reshape xtensor_fixed with different shape or layout.");
}
return *this;
}
template <class ET, class S, layout_type L, bool SH, class Tag>
template <class ST>
inline bool xfixed_adaptor<ET, S, L, SH, Tag>::broadcast_shape(ST& shape, bool) const
{
return xt::broadcast_shape(m_shape, shape);
}
template <class EC, class S, layout_type L, bool SH, class Tag>
inline auto xfixed_adaptor<EC, S, L, SH, Tag>::storage_impl() noexcept -> storage_type&
{
return m_storage;
}
template <class EC, class S, layout_type L, bool SH, class Tag>
inline auto xfixed_adaptor<EC, S, L, SH, Tag>::storage_impl() const noexcept -> const storage_type&
{
return m_storage;
}
template <class EC, class S, layout_type L, bool SH, class Tag>
constexpr layout_type xfixed_adaptor<EC, S, L, SH, Tag>::layout() const noexcept
{
return base_type::static_layout;
}
template <class EC, class S, layout_type L, bool SH, class Tag>
inline bool xfixed_adaptor<EC, S, L, SH, Tag>::is_contiguous() const noexcept
{
using str_type = typename inner_strides_type::value_type;
return m_strides.empty()
|| (layout() == layout_type::row_major && m_strides.back() == str_type(1))
|| (layout() == layout_type::column_major && m_strides.front() == str_type(1));
}
template <class EC, class S, layout_type L, bool SH, class Tag>
XTENSOR_CONSTEXPR_RETURN auto xfixed_adaptor<EC, S, L, SH, Tag>::shape_impl() const noexcept -> const inner_shape_type&
{
return m_shape;
}
template <class EC, class S, layout_type L, bool SH, class Tag>
XTENSOR_CONSTEXPR_RETURN auto xfixed_adaptor<EC, S, L, SH, Tag>::strides_impl() const noexcept -> const inner_strides_type&
{
return m_strides;
}
template <class EC, class S, layout_type L, bool SH, class Tag>
XTENSOR_CONSTEXPR_RETURN auto xfixed_adaptor<EC, S, L, SH, Tag>::backstrides_impl() const noexcept -> const inner_backstrides_type&
{
return m_backstrides;
}
}
#endif