/
DynamicArray.h
4171 lines (3580 loc) · 169 KB
/
DynamicArray.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
//=================================================================================================
/*!
// \file blaze_tensor/math/dense/DynamicArray.h
// \brief Header file for the implementation of a dynamic LxOxMxN array
//
// Copyright (C) 2012-2018 Klaus Iglberger - All Rights Reserved
// Copyright (C) 2018-2019 Hartmut Kaiser - All Rights Reserved
//
// This file is part of the Blaze library. You can redistribute it and/or modify it under
// the terms of the New (Revised) BSD License. Redistribution and use in source and binary
// forms, with or without modification, are permitted provided that the following conditions
// are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other materials
// provided with the distribution.
// 3. Neither the names of the Blaze development group nor the names of its contributors
// may be used to endorse or promote products derived from this software without specific
// prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
// SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
// BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
// DAMAGE.
*/
//=================================================================================================
#ifndef _BLAZE_TENSOR_MATH_DENSE_DYNAMIC_ARRAY_H_
#define _BLAZE_TENSOR_MATH_DENSE_DYNAMIC_ARRAY_H_
//*************************************************************************************************
// Includes
//*************************************************************************************************
#include <array>
#include <blaze/math/IntegerSequence.h>
#include <blaze/math/dense/DynamicMatrix.h>
#include <blaze/math/traits/DivTrait.h>
#include <blaze/math/traits/ExpandTrait.h>
#include <blaze/math/traits/MapTrait.h>
#include <blaze/math/traits/MultTrait.h>
#include <blaze/math/typetraits/HasMutableDataAccess.h>
#include <blaze/math/typetraits/IsAligned.h>
#include <blaze/math/typetraits/IsContiguous.h>
#include <blaze/math/typetraits/IsPadded.h>
#include <blaze/math/typetraits/IsResizable.h>
#include <blaze/math/typetraits/IsShrinkable.h>
#include <blaze/system/Optimizations.h>
#include <blaze/util/IntegralConstant.h>
#include <blaze/util/StaticAssert.h>
#include <blaze_tensor/math/Array.h>
//#include <blaze_tensor/math/CustomArray.h>
#include <blaze_tensor/math/Forward.h>
#include <blaze_tensor/math/InitFromValue.h>
#include <blaze_tensor/math/InitializerList.h>
#include <blaze_tensor/math/SMP.h>
#include <blaze_tensor/math/dense/DynamicTensor.h>
#include <blaze_tensor/math/dense/Transposition.h>
#include <blaze_tensor/math/expressions/DenseArray.h>
#include <blaze_tensor/math/traits/QuatSliceTrait.h>
#include <blaze_tensor/math/typetraits/IsNdArray.h>
#include <blaze_tensor/math/typetraits/IsDenseArray.h>
#include <blaze_tensor/math/typetraits/IsRowMajorArray.h>
#include <blaze_tensor/util/ArrayForEach.h>
namespace blaze {
//=================================================================================================
//
// CLASS DEFINITION
//
//=================================================================================================
//*************************************************************************************************
/*!\defgroup dynamic_array DynamicArray
// \ingroup dense_array
*/
/*!\brief Efficient implementation of a dynamic \f$ M \times N \f$ array.
// \ingroup dynamic_array
//
// The DynamicArray class template is the representation of an arbitrary sized array with
// \f$ M \times N \f$ dynamically allocated elements of arbitrary type. The type of the elements
// and the storage order of the array can be specified via the two template parameters:
\code
template< typename Type >
class DynamicArray;
\endcode
// - Type: specifies the type of the array elements. DynamicArray can be used with any
// non-cv-qualified, non-reference, non-pointer element type.
//
// The use of DynamicArray is very natural and intuitive. All operations (addition, subtraction,
// multiplication, scaling, ...) can be performed on all possible combinations of matrices with
// fitting element types. The following example an impression of the use of DynamicArray:
\code
using blaze::DynamicArray;
using blaze::CompressedArray;
DynamicArray<4, double> A( 2, 3, 4, 5 ); // Default constructed, non-initialized, 2x3x4x5 array
A(0,0,0,0) = 1.0; A(0,0,0,1) = 2.0; A(0,0,0,2) = 3.0; // Initialization of the first row
A(0,0,1,0) = 4.0; A(0,0,1,1) = 5.0; A(0,0,1,2) = 6.0; // Initialization of the second row
DynamicArray<2, float> B( 2, 3 ); // Default constructed column-major single precision 2x3 array
B(0,0) = 1.0; B(0,1) = 3.0; B(0,2) = 5.0; // Initialization of the first row
B(1,0) = 2.0; B(1,1) = 4.0; B(1,2) = 6.0; // Initialization of the second row
CompressedArray<float> C( 2, 3 ); // Empty sparse single precision array
DynamicArray<2, float> D( 3, 2, 4.0F ); // Directly, homogeneously initialized single precision 3x2 array
DynamicArray<2, double> E( A ); // Creation of a new array as a copy of A
DynamicArray<2, double> F; // Creation of a default column-major array
E = A + B; // Array addition and assignment to a array
F = A - C; // Array subtraction and assignment to a column-major array
F = A * D; // Array multiplication between two matrices of different element types
A *= 2.0; // In-place scaling of array A
E = 2.0 * B; // Scaling of array B
F = D * 2.0; // Scaling of array D
E += A - B; // Addition assignment
E -= A + C; // Subtraction assignment
F *= A * D; // Multiplication assignment
\endcode
*/
template< size_t N, typename Type > // Data type of the array
class DynamicArray
: public DenseArray< DynamicArray<N, Type> >
{
public:
//**Type definitions****************************************************************************
using This = DynamicArray<N, Type>; //!< Type of this DynamicArray instance.
using BaseType = DenseArray<This>; //!< Base type of this DynamicArray instance.
using ResultType = This; //!< Result type for expression template evaluations.
using OppositeType = DynamicArray<N, Type>; //!< Result type with opposite storage order for expression template evaluations.
using TransposeType = DynamicArray<N, Type>; //!< Transpose type for expression template evaluations.
using ElementType = Type; //!< Type of the array elements.
using SIMDType = SIMDTrait_t<ElementType>; //!< SIMD type of the array elements.
using ReturnType = const Type&; //!< Return type for expression template evaluations.
using CompositeType = const This&; //!< Data type for composite expression templates.
using Reference = Type&; //!< Reference to a non-constant array value.
using ConstReference = const Type&; //!< Reference to a constant array value.
using Pointer = Type*; //!< Pointer to a non-constant array value.
using ConstPointer = const Type*; //!< Pointer to a constant array value.
using Iterator = DenseIterator<Type,usePadding>; //!< Iterator over non-constant elements.
using ConstIterator = DenseIterator<const Type,usePadding>; //!< Iterator over constant elements.
//**********************************************************************************************
//**Rebind struct definition********************************************************************
/*!\brief Rebind mechanism to obtain a DynamicArray with different data/element type.
*/
template< typename NewType > // Data type of the other array
struct Rebind {
using Other = DynamicArray<N, NewType>; //!< The type of the other DynamicArray.
};
//**********************************************************************************************
//**Resize struct definition********************************************************************
/*!\brief Resize mechanism to obtain a DynamicArray with different fixed dimensions.
*/
// template< size_t... NewDims > // Dimensions of the other array
// struct Resize {
// BLAZE_STATIC_ASSERT_MSG(N == sizeof...(NewDims), "incompatible dimensionality of other array");
// using Other = DynamicArray<N, Type>; //!< The type of the other DynamicArray.
// };
//**********************************************************************************************
//**Compilation flags***************************************************************************
//! Compilation flag for SIMD optimization.
/*! The \a simdEnabled compilation flag indicates whether expressions the array is involved
in can be optimized via SIMD operations. In case the element type of the array is a
vectorizable data type, the \a simdEnabled compilation flag is set to \a true, otherwise
it is set to \a false. */
static constexpr bool simdEnabled = IsVectorizable_v<Type>;
//! Compilation flag for SMP assignments.
/*! The \a smpAssignable compilation flag indicates whether the array can be used in SMP
(shared memory parallel) assignments (both on the left-hand and right-hand side of the
assignment). */
static constexpr bool smpAssignable = !IsSMPAssignable_v<Type>;
//**********************************************************************************************
static constexpr size_t num_dimensions = N; // Dimensionality of the array
//**********************************************************************************************
//**Constructors********************************************************************************
/*!\name Constructors */
//@{
explicit inline DynamicArray() noexcept;
template< typename... Dims, typename = EnableIf_t< sizeof...(Dims) == N - 1 > >
explicit inline DynamicArray( size_t dim0, Dims... dims );
explicit inline DynamicArray( std::array< size_t, N> const& dims );
template< typename... Dims >
explicit inline DynamicArray( InitFromValue, const Type& init, Dims... dims );
explicit inline DynamicArray( nested_initializer_list< N, Type > list );
template< typename Other, typename... Dims, typename = EnableIf_t< sizeof...(Dims) == N > >
explicit inline DynamicArray( const Other* array, Dims... dims );
inline DynamicArray( const DynamicArray& m );
inline DynamicArray( DynamicArray&& m ) noexcept;
template< typename MT > inline DynamicArray( const Array<MT>& m );
template< typename MT, bool TF, size_t M = N, typename = EnableIf_t< M == 1 > >
inline DynamicArray( const Vector<MT, TF>& m );
template< typename MT, bool SO, size_t M = N, typename = EnableIf_t< M == 2 > >
inline DynamicArray( const Matrix<MT, SO>& m );
template< typename MT, size_t M = N, typename = EnableIf_t< M == 3 > >
inline DynamicArray( const Tensor<MT>& m );
//@}
//**********************************************************************************************
//**Destructor**********************************************************************************
/*!\name Destructor */
//@{
inline ~DynamicArray();
//@}
//**********************************************************************************************
//**Data access functions***********************************************************************
/*!\name Data access functions */
//@{
template< typename... Dims >
inline Reference operator()( Dims... dims ) noexcept;
template< typename... Dims >
inline ConstReference operator()( Dims... dims ) const noexcept;
inline Reference operator()( std::array< size_t, N > const& indices ) noexcept;
inline ConstReference operator()( std::array< size_t, N > const& indices ) const noexcept;
template< typename... Dims >
inline Reference at( Dims... dims );
template< typename... Dims >
inline ConstReference at( Dims... dims ) const;
inline Reference at( std::array< size_t, N > const& indices );
inline ConstReference at( std::array< size_t, N > const& indices ) const;
inline Pointer data () noexcept;
inline ConstPointer data () const noexcept;
template< typename... Dims >
inline Pointer data ( size_t i, Dims... subdims ) noexcept;
template< typename... Dims >
inline ConstPointer data ( size_t i, Dims... subdims ) const noexcept;
template< typename... Dims >
inline Iterator begin ( size_t i, Dims... subdims ) noexcept;
template< typename... Dims >
inline ConstIterator begin ( size_t i, Dims... subdims ) const noexcept;
template< typename... Dims >
inline ConstIterator cbegin( size_t i, Dims... subdims ) const noexcept;
template< typename... Dims >
inline Iterator end ( size_t i, Dims... subdims ) noexcept;
template< typename... Dims >
inline ConstIterator end ( size_t i, Dims... subdims ) const noexcept;
template< typename... Dims >
inline ConstIterator cend ( size_t i, Dims... subdims ) const noexcept;
//@}
//**********************************************************************************************
//**Assignment operators************************************************************************
/*!\name Assignment operators */
//@{
inline DynamicArray& operator=( const Type& rhs );
inline DynamicArray& operator=( nested_initializer_list< N, Type > list );
inline DynamicArray& operator=( const DynamicArray& rhs );
inline DynamicArray& operator=( DynamicArray&& rhs ) noexcept;
template< typename MT > inline DynamicArray& operator= ( const Array<MT>& rhs );
template< typename MT > inline DynamicArray& operator+=( const Array<MT>& rhs );
template< typename MT > inline DynamicArray& operator-=( const Array<MT>& rhs );
template< typename MT > inline DynamicArray& operator%=( const Array<MT>& rhs );
template< typename MT, bool TF, size_t M = N, typename = EnableIf_t< M == 1 > >
inline DynamicArray& operator= ( const Vector<MT, TF>& m );
template< typename MT, bool TF, size_t M = N, typename = EnableIf_t< M == 1 > >
inline DynamicArray& operator+=( const Vector<MT, TF>& m );
template< typename MT, bool TF, size_t M = N, typename = EnableIf_t< M == 1 > >
inline DynamicArray& operator-=( const Vector<MT, TF>& m );
template< typename MT, bool TF, size_t M = N, typename = EnableIf_t< M == 1 > >
inline DynamicArray& operator%=( const Vector<MT, TF>& m );
template< typename MT, bool SO, size_t M = N, typename = EnableIf_t< M == 2 > >
inline DynamicArray& operator= ( const Matrix<MT, SO>& m );
template< typename MT, bool SO, size_t M = N, typename = EnableIf_t< M == 2 > >
inline DynamicArray& operator+=( const Matrix<MT, SO>& m );
template< typename MT, bool SO, size_t M = N, typename = EnableIf_t< M == 2 > >
inline DynamicArray& operator-=( const Matrix<MT, SO>& m );
template< typename MT, bool SO, size_t M = N, typename = EnableIf_t< M == 2 > >
inline DynamicArray& operator%=( const Matrix<MT, SO>& m );
template< typename MT, size_t M = N, typename = EnableIf_t< M == 3 > >
inline DynamicArray& operator= ( const Tensor<MT>& m );
template< typename MT, size_t M = N, typename = EnableIf_t< M == 3 > >
inline DynamicArray& operator+=( const Tensor<MT>& m );
template< typename MT, size_t M = N, typename = EnableIf_t< M == 3 > >
inline DynamicArray& operator-=( const Tensor<MT>& m );
template< typename MT, size_t M = N, typename = EnableIf_t< M == 3 > >
inline DynamicArray& operator%=( const Tensor<MT>& m );
//@}
//**********************************************************************************************
//**Utility functions***************************************************************************
/*!\name Utility functions */
//@{
inline constexpr std::array< size_t, N > const& dimensions() const noexcept;
inline size_t quats() const noexcept;
inline size_t pages() const noexcept;
inline size_t rows() const noexcept;
inline size_t columns() const noexcept;
template < size_t Dim >
inline size_t dimension() const noexcept;
inline size_t spacing() const noexcept;
inline size_t capacity() const noexcept;
template< typename... Dims >
inline size_t capacity( size_t i, Dims... subdims ) const noexcept;
inline size_t nonZeros() const;
template< typename... Dims >
inline size_t nonZeros( size_t i, Dims... subdims ) const;
inline void reset();
template< typename... Dims >
inline void reset( size_t i, Dims... subdims );
inline void clear();
void resize( std::array< size_t, N > const& dims, bool preserve = true );
inline void extend( std::array< size_t, N > const& dims, bool preserve = true );
inline void reserve( size_t elements );
inline void shrinkToFit();
inline void swap( DynamicArray& m ) noexcept;
//@}
//**********************************************************************************************
//**Numeric functions***************************************************************************
/*!\name Numeric functions */
//@{
inline DynamicArray& transpose();
inline DynamicArray& ctranspose();
template < typename T >
inline DynamicArray& transpose( const T* indices, size_t n );
template < typename T >
inline DynamicArray& ctranspose( const T* indices, size_t n );
template< typename Other > inline DynamicArray& scale( const Other& scalar );
//@}
//**********************************************************************************************
private:
//**********************************************************************************************
/*! \cond BLAZE_INTERNAL */
//! Helper variable template for the explicit application of the SFINAE principle.
template< typename MT >
static constexpr bool VectorizedAssign_v =
( useOptimizedKernels &&
simdEnabled && MT::simdEnabled &&
IsSIMDCombinable_v< Type, ElementType_t<MT> > &&
IsRowMajorArray_v< MT >);
/*! \endcond */
//**********************************************************************************************
//**********************************************************************************************
/*! \cond BLAZE_INTERNAL */
//! Helper variable template for the explicit application of the SFINAE principle.
template< typename MT >
static constexpr bool VectorizedAddAssign_v =
( useOptimizedKernels &&
simdEnabled && MT::simdEnabled &&
IsSIMDCombinable_v< Type, ElementType_t<MT> > &&
HasSIMDAdd_v< Type, ElementType_t<MT> > &&
IsRowMajorArray_v< MT >);
/*! \endcond */
//**********************************************************************************************
//**********************************************************************************************
/*! \cond BLAZE_INTERNAL */
//! Helper variable template for the explicit application of the SFINAE principle.
template< typename MT >
static constexpr bool VectorizedSubAssign_v =
( useOptimizedKernels &&
simdEnabled && MT::simdEnabled &&
IsSIMDCombinable_v< Type, ElementType_t<MT> > &&
HasSIMDSub_v< Type, ElementType_t<MT> > &&
IsRowMajorArray_v< MT >);
/*! \endcond */
//**********************************************************************************************
//**********************************************************************************************
/*! \cond BLAZE_INTERNAL */
//! Helper variable template for the explicit application of the SFINAE principle.
template< typename MT >
static constexpr bool VectorizedSchurAssign_v =
( useOptimizedKernels &&
simdEnabled && MT::simdEnabled &&
IsSIMDCombinable_v< Type, ElementType_t<MT> > &&
HasSIMDMult_v< Type, ElementType_t<MT> > &&
IsRowMajorArray_v< MT >);
/*! \endcond */
//**********************************************************************************************
//**********************************************************************************************
//! The number of elements packed within a single SIMD element.
static constexpr size_t SIMDSIZE = SIMDTrait<ElementType>::size;
//**********************************************************************************************
public:
//**Debugging functions*************************************************************************
/*!\name Debugging functions */
//@{
inline bool isIntact() const noexcept;
//@}
//**********************************************************************************************
//**Expression template evaluation functions****************************************************
/*!\name Expression template evaluation functions */
//@{
template< typename Other > inline bool canAlias ( const Other* alias ) const noexcept;
template< typename Other > inline bool isAliased( const Other* alias ) const noexcept;
inline bool isAligned () const noexcept;
inline bool canSMPAssign() const noexcept;
template< typename... Dims >
BLAZE_ALWAYS_INLINE SIMDType load ( Dims... dims ) const noexcept;
template< typename... Dims >
BLAZE_ALWAYS_INLINE SIMDType loada( Dims... dims ) const noexcept;
template< typename... Dims >
BLAZE_ALWAYS_INLINE SIMDType loadu( Dims... dims ) const noexcept;
template< typename... Dims >
BLAZE_ALWAYS_INLINE void store ( const SIMDType& value, Dims... dims ) noexcept;
template< typename... Dims >
BLAZE_ALWAYS_INLINE void storea( const SIMDType& value, Dims... dims ) noexcept;
template< typename... Dims >
BLAZE_ALWAYS_INLINE void storeu( const SIMDType& value, Dims... dims ) noexcept;
template< typename... Dims >
BLAZE_ALWAYS_INLINE void stream( const SIMDType& value, Dims... dims ) noexcept;
template< typename MT >
inline auto assign( const DenseArray<MT>& rhs ) /*-> DisableIf_t< VectorizedAssign_v<MT> >*/;
//template< typename MT >
//inline auto assign( const DenseArray<MT>& rhs ) -> EnableIf_t< VectorizedAssign_v<MT> >;
template< typename MT >
inline auto addAssign( const DenseArray<MT>& rhs ) /*-> DisableIf_t< VectorizedAddAssign_v<MT> >*/;
//template< typename MT >
//inline auto addAssign( const DenseArray<MT>& rhs ) -> EnableIf_t< VectorizedAddAssign_v<MT> >;
template< typename MT >
inline auto subAssign( const DenseArray<MT>& rhs ) /*-> DisableIf_t< VectorizedSubAssign_v<MT> >*/;
//template< typename MT >
//inline auto subAssign( const DenseArray<MT>& rhs ) -> EnableIf_t< VectorizedSubAssign_v<MT> >;
template< typename MT >
inline auto schurAssign( const DenseArray<MT>& rhs ) /*-> DisableIf_t< VectorizedSchurAssign_v<MT> >*/;
//template< typename MT >
//inline auto schurAssign( const DenseArray<MT>& rhs ) -> EnableIf_t< VectorizedSchurAssign_v<MT> >;
//@}
//**********************************************************************************************
private:
//**Utility functions***************************************************************************
/*!\name Utility functions */
//@{
template< typename... Dims >
inline static std::array< size_t, N > initDimensions( Dims... dims ) noexcept;
inline static size_t addPadding( size_t value ) noexcept;
inline size_t calcCapacity() const noexcept;
template< typename... Dims >
inline size_t index( Dims... dims ) const noexcept;
inline size_t index( std::array< size_t, N > const& indices ) const noexcept;
template< typename... Dims >
inline size_t row_index( size_t i, Dims... subdims ) const noexcept;
//@}
//**********************************************************************************************
//**Member variables****************************************************************************
/*!\name Member variables */
//@{
std::array< size_t, N > dims_; //!< The current dimensions of the array (dims[0]...dims_[N-1]) .
size_t nn_; //!< The alignment adjusted number of columns.
size_t capacity_; //!< The maximum capacity of the array.
Type* BLAZE_RESTRICT v_; //!< The dynamically allocated array elements.
//@}
//**********************************************************************************************
//**Compile time checks*************************************************************************
/*! \cond BLAZE_INTERNAL */
BLAZE_CONSTRAINT_MUST_NOT_BE_POINTER_TYPE ( Type );
BLAZE_CONSTRAINT_MUST_NOT_BE_REFERENCE_TYPE( Type );
BLAZE_CONSTRAINT_MUST_NOT_BE_CONST ( Type );
BLAZE_CONSTRAINT_MUST_NOT_BE_VOLATILE ( Type );
/*! \endcond */
//**********************************************************************************************
};
//*************************************************************************************************
//=================================================================================================
//
// CONSTRUCTORS
//
//=================================================================================================
//*************************************************************************************************
/*!\brief The default constructor for DynamicArray.
*/
template< size_t N // The dimensionality of the array
, typename Type > // Data type of the array
inline DynamicArray<N, Type>::DynamicArray() noexcept
: dims_ ( ) // The current dimensions of the array
, nn_ ( 0UL ) // The length of a padded row
, capacity_( 0UL ) // The maximum capacity of the array
, v_ ( nullptr ) // The array elements
{}
//*************************************************************************************************
//*************************************************************************************************
/*!\brief Constructor for a array of size \f$ m \times n \f$. No element initialization is performed!
//
// \param m The number of rows of the array.
// \param n The number of columns of the array.
//
// \note This constructor is only responsible to allocate the required dynamic memory. No
// element initialization is performed!
*/
template< size_t N // The dimensionality of the array
, typename Type > // Data type of the array
template< typename... Dims, typename Enable >
inline DynamicArray< N, Type >::DynamicArray( size_t dim0, Dims... dims )
: dims_ ( initDimensions( dim0, dims... ) ) // The current dimensions of the array
, nn_ ( addPadding( dims_[0] ) ) // The length of a padded row
, capacity_( calcCapacity() ) // The maximum capacity of the array
, v_( allocate< Type >( capacity_ ) ) // The array elements
{
BLAZE_STATIC_ASSERT( N - 1 == sizeof...( dims ) );
if( IsVectorizable_v<Type> ) {
ArrayForEachPadded( dims_, nn_, [&]( size_t i ) { v_[i] = Type(); } );
}
BLAZE_INTERNAL_ASSERT( isIntact(), "Invariant violation detected" );
}
//*************************************************************************************************
//*************************************************************************************************
/*!\brief Constructor for a homogeneous initialization of all \f$ m \times n \f$ array elements.
//
// \param m The number of rows of the array.
// \param n The number of columns of the array.
// \param init The initial value of the array elements.
//
// All array elements are initialized with the specified value.
*/
template< size_t N // The dimensionality of the array
, typename Type > // Data type of the array
template< typename... Dims >
inline DynamicArray<N, Type>::DynamicArray( InitFromValue, const Type& init, Dims...dims )
: DynamicArray( dims... )
{
BLAZE_STATIC_ASSERT( N == sizeof...( dims ) );
ArrayForEach( dims_, nn_, [&]( size_t i ) { v_[i] = init; } );
BLAZE_INTERNAL_ASSERT( isIntact(), "Invariant violation detected" );
}
//*************************************************************************************************
//*************************************************************************************************
/*!\brief List initialization of all array elements.
//
// \param list The initializer list.
//
// This constructor provides the option to explicitly initialize the elements of the array by
// means of an initializer list:
\code
using blaze::rowMajor;
blaze::DynamicArray<3, int> A{ { { 1, 2, 3 },
{ 4, 5 },
{ 7, 8, 9 } },
{ { 1, 2, 3 },
{ 4, 5 },
{ 7, 8, 9 } } };
\endcode
// The array is sized according to the size of the initializer list and all its elements are
// initialized by the values of the given initializer list. Missing values are initialized as
// default (as e.g. the value 6 in the example).
*/
template< size_t N // The dimensionality of the array
, typename Type > // Data type of the array
inline DynamicArray<N, Type>::DynamicArray( nested_initializer_list< N, Type > list )
: DynamicArray( list.dimensions() )
{
list.transfer_data( *this );
BLAZE_INTERNAL_ASSERT( isIntact(), "Invariant violation detected" );
}
//*************************************************************************************************
//*************************************************************************************************
/*!\brief Array initialization of all array elements.
//
// \param m The number of rows of the array.
// \param n The number of columns of the array.
// \param array Dynamic array for the initialization.
//
// This constructor offers the option to directly initialize the elements of the array with
// a dynamic array:
\code
using blaze::rowMajor;
int* array = new int[20];
// ... Initialization of the dynamic array
blaze::DynamicArray<3, int> v( array, 6UL, 4UL, 5UL );
delete[] array;
\endcode
// The array is sized according to the given size of the array and initialized with the values
// from the given array. Note that it is expected that the given \a array has at least \a m by
// \a n elements. Providing an array with less elements results in undefined behavior!
*/
template< size_t N // The dimensionality of the array
, typename Type > // Data type of the array
template< typename Other, typename... Dims, typename Enable > // Data type of the initialization array
inline DynamicArray<N, Type>::DynamicArray( const Other* array, Dims... dims )
: DynamicArray( dims... )
{
BLAZE_STATIC_ASSERT( N == sizeof...( dims ) );
if( IsNothrowMoveAssignable_v< Type > ) {
ArrayForEach2( dims_, nn_, [&]( size_t i, size_t j ) {
v_[j] = std::move( array[i] );
} );
}
else {
ArrayForEach2( dims_, nn_, [&]( size_t i, size_t j ) {
v_[j] = array[i];
} );
}
BLAZE_INTERNAL_ASSERT( isIntact(), "Invariant violation detected" );
}
//*************************************************************************************************
//*************************************************************************************************
/*!\brief The copy constructor for DynamicArray.
//
// \param m Array to be copied.
//
// The copy constructor is explicitly defined due to the required dynamic memory management
// and in order to enable/facilitate NRV optimization.
*/
template< size_t N // The dimensionality of the array
, typename Type > // Data type of the array
inline DynamicArray<N, Type>::DynamicArray( const DynamicArray& m )
: dims_ ( m.dims_ ) // The current dimensions of the array
, nn_ ( m.nn_ ) // The length of a padded row
, capacity_( m.capacity_ ) // The maximum capacity of the array
, v_( allocate< Type >( capacity_ ) ) // The array elements
{
smpAssign( *this, m );
BLAZE_INTERNAL_ASSERT( isIntact(), "Invariant violation detected" );
}
//*************************************************************************************************
//*************************************************************************************************
/*!\brief The move constructor for DynamicArray.
//
// \param m The array to be move into this instance.
*/
template< size_t N // The dimensionality of the array
, typename Type > // Data type of the array
inline DynamicArray<N, Type>::DynamicArray( DynamicArray&& m ) noexcept
: dims_ ( std::move(m.dims_) ) // The current dimensions of the array
, nn_ ( m.nn_ ) // The length of a padded row
, capacity_( m.capacity_ ) // The maximum capacity of the array
, v_ ( m.v_ ) // The array elements
{
m.nn_ = 0UL;
m.capacity_ = 0UL;
m.v_ = nullptr;
}
//*************************************************************************************************
//*************************************************************************************************
/*!\brief Conversion constructor from different arrays.
//
// \param m Array to be copied.
*/
template< size_t N // The dimensionality of the array
, typename Type > // Data type of the array
template< typename MT > // Type of the foreign array
inline DynamicArray<N, Type>::DynamicArray( const Array<MT>& rhs )
: DynamicArray( (~rhs).dimensions() )
{
if( (~rhs).canAlias( this ) ) {
const ResultType_t<MT> tmp( ~rhs );
smpAssign( *this, tmp );
}
else {
smpAssign( *this, ~rhs );
}
BLAZE_INTERNAL_ASSERT( isIntact(), "Invariant violation detected" );
}
//*************************************************************************************************
//*************************************************************************************************
/*!\brief Conversion constructor from different arrays.
//
// \param m Array to be copied.
*/
template< size_t N // The dimensionality of the array
, typename Type > // Data type of the array
template< typename MT, bool TF, size_t M, typename Enable >
inline DynamicArray<N, Type>::DynamicArray( const Vector<MT, TF>& rhs )
{
using ET = ElementType_t<MT>;
using custom_array = CustomArray<1, ET, false, true>;
if( (~rhs).canAlias( this ) ) {
const ResultType_t<MT> tmp( ~rhs );
smpAssign( *this, custom_array( tmp.data(), tmp.size(), tmp.spacing() ) );
}
else {
auto const& tmp = ~rhs;
smpAssign( *this, custom_array( tmp.data(), tmp.size(), tmp.spacing() ) );
}
BLAZE_INTERNAL_ASSERT( isIntact(), "Invariant violation detected" );
}
//*************************************************************************************************
//*************************************************************************************************
/*!\brief Conversion constructor from different arrays.
//
// \param m Array to be copied.
*/
template< size_t N // The dimensionality of the array
, typename Type > // Data type of the array
template< typename MT, bool SO, size_t M, typename Enable >
inline DynamicArray<N, Type>::DynamicArray( const Matrix<MT,SO>& rhs )
{
using ET = ElementType_t<MT>;
using custom_array = CustomArray<2, ET, false, true>;
if( (~rhs).canAlias( this ) ) {
const ResultType_t<MT> tmp( ~rhs );
smpAssign( *this,
custom_array( tmp.data(), tmp.rows(), tmp.columns(), tmp.spacing() ) );
}
else {
auto const& tmp = ~rhs;
smpAssign( *this,
custom_array( tmp.data(), tmp.rows(), tmp.columns(), tmp.spacing() ) );
}
BLAZE_INTERNAL_ASSERT( isIntact(), "Invariant violation detected" );
}
//*************************************************************************************************
//*************************************************************************************************
/*!\brief Conversion constructor from different arrays.
//
// \param m Array to be copied.
*/
template< size_t N // The dimensionality of the array
, typename Type > // Data type of the array
template< typename MT, size_t M, typename Enable >
inline DynamicArray<N, Type>::DynamicArray( const Tensor<MT>& rhs )
{
using ET = ElementType_t<MT>;
using custom_array = CustomArray<2, ET, false, true>;
if( (~rhs).canAlias( this ) ) {
const ResultType_t<MT> tmp( ~rhs );
smpAssign( *this,
custom_array( tmp.data(), tmp.pages(), tmp.rows(), tmp.columns(),
tmp.spacing() ) );
}
else {
auto const& tmp = ~rhs;
smpAssign( *this,
custom_array( tmp.data(), tmp.pages(), tmp.rows(), tmp.columns(),
tmp.spacing() ) );
}
BLAZE_INTERNAL_ASSERT( isIntact(), "Invariant violation detected" );
}
//*************************************************************************************************
//*************************************************************************************************
/*!\brief Constructor from array bounds.
//
// \param m Array to be copied.
*/
template< size_t N // The dimensionality of the array
, typename Type > // Data type of the array
inline DynamicArray<N, Type>::DynamicArray( const std::array<size_t, N>& dims )
: dims_ ( dims ) // The current dimensions of the array
, nn_ ( addPadding( dims_[0] ) ) // The length of a padded row
, capacity_( calcCapacity() ) // The maximum capacity of the array
, v_( allocate< Type >( capacity_ ) ) // The array elements
{
if( IsVectorizable_v<Type> ) {
ArrayForEachPadded( dims_, nn_, [&]( size_t i ) { v_[i] = Type(); } );
}
BLAZE_INTERNAL_ASSERT( isIntact(), "Invariant violation detected" );
}
//*************************************************************************************************
//=================================================================================================
//
// DESTRUCTOR
//
//=================================================================================================
//*************************************************************************************************
/*!\brief The destructor for DynamicArray.
*/
template< size_t N // The dimensionality of the array
, typename Type > // Data type of the array
inline DynamicArray<N, Type>::~DynamicArray()
{
deallocate( v_ );
}
//*************************************************************************************************
//=================================================================================================
//
// DATA ACCESS FUNCTIONS
//
//=================================================================================================
//*************************************************************************************************
/*!\brief ND-access to the array elements.
//
// \param i Access index for the row. The index has to be in the range \f$[0..M-1]\f$.
// \param j Access index for the column. The index has to be in the range \f$[0..N-1]\f$.
// \param k Access index for the page. The index has to be in the range \f$[0..O-1]\f$.
// \return Reference to the accessed value.
//
// This function only performs an index check in case BLAZE_USER_ASSERT() is active. In contrast,
// the at() function is guaranteed to perform a check of the given access indices.
*/
template< size_t N // The dimensionality of the array
, typename Type > // Data type of the array
template< typename... Dims >
inline typename DynamicArray<N, Type>::Reference
DynamicArray<N, Type>::operator()( Dims... dims ) noexcept
{
BLAZE_STATIC_ASSERT( N == sizeof...( dims ) );
#if defined(BLAZE_USER_ASSERTION)
size_t indices[] = { size_t(dims)... };
ArrayDimForEach( dims_, [&]( size_t i, size_t dim ) {
BLAZE_USER_ASSERT( indices[N - i - 1] < dim, "Invalid array access index" );
} );
MAYBE_UNUSED( indices );
#endif
return v_[index( dims... )];
}
//*************************************************************************************************
//*************************************************************************************************
/*!\brief ND-access to the array elements.
//
// \param i Access index for the row. The index has to be in the range \f$[0..M-1]\f$.
// \param j Access index for the column. The index has to be in the range \f$[0..N-1]\f$.
// \param k Access index for the page. The index has to be in the range \f$[0..O-1]\f$.
// \return Reference to the accessed value.
//
// This function only performs an index check in case BLAZE_USER_ASSERT() is active. In contrast,
// the at() function is guaranteed to perform a check of the given access indices.
*/
template< size_t N // The dimensionality of the array
, typename Type > // Data type of the array
template< typename... Dims >
inline typename DynamicArray<N, Type>::ConstReference
DynamicArray<N, Type>::operator()( Dims... dims ) const noexcept
{
BLAZE_STATIC_ASSERT( N == sizeof...( dims ) );
#if defined(BLAZE_USER_ASSERTION)
size_t indices[] = { size_t(dims)... };
ArrayDimForEach( dims_, [&]( size_t i, size_t dim ) {
BLAZE_USER_ASSERT( indices[N - i - 1] < dim, "Invalid array access index" );
} );
MAYBE_UNUSED( indices );
#endif
return v_[index( dims... )];
}
//*************************************************************************************************
//*************************************************************************************************
/*!\brief ND-access to the array elements.
//
// \param i Access index for the row. The index has to be in the range \f$[0..M-1]\f$.
// \param j Access index for the column. The index has to be in the range \f$[0..N-1]\f$.
// \param k Access index for the page. The index has to be in the range \f$[0..O-1]\f$.
// \return Reference to the accessed value.
//
// This function only performs an index check in case BLAZE_USER_ASSERT() is active. In contrast,
// the at() function is guaranteed to perform a check of the given access indices.
*/
template< size_t N // The dimensionality of the array
, typename Type > // Data type of the array
inline typename DynamicArray<N, Type>::Reference
DynamicArray<N, Type>::operator()( std::array< size_t, N > const& indices ) noexcept
{
#if defined(BLAZE_USER_ASSERTION)
ArrayDimForEach( dims_, [&]( size_t i, size_t dim ) {
BLAZE_USER_ASSERT( indices[i] < dim, "Invalid array access index" );
} );
#endif
return v_[index( indices )];
}
//*************************************************************************************************
//*************************************************************************************************
/*!\brief ND-access to the array elements.
//
// \param i Access index for the row. The index has to be in the range \f$[0..M-1]\f$.
// \param j Access index for the column. The index has to be in the range \f$[0..N-1]\f$.
// \param k Access index for the page. The index has to be in the range \f$[0..O-1]\f$.
// \return Reference to the accessed value.
//
// This function only performs an index check in case BLAZE_USER_ASSERT() is active. In contrast,
// the at() function is guaranteed to perform a check of the given access indices.
*/
template< size_t N // The dimensionality of the array
, typename Type > // Data type of the array
inline typename DynamicArray<N, Type>::ConstReference
DynamicArray<N, Type>::operator()( std::array< size_t, N > const& indices ) const noexcept
{
#if defined(BLAZE_USER_ASSERTION)
ArrayDimForEach( dims_, [&]( size_t i, size_t dim ) {
BLAZE_USER_ASSERT( indices[i] < dim, "Invalid array access index" );
} );
#endif
return v_[index( indices )];
}
//*************************************************************************************************
//*************************************************************************************************
/*!\brief Checked access to the array elements.
//
// \param i Access index for the row. The index has to be in the range \f$[0..M-1]\f$.
// \param j Access index for the column. The index has to be in the range \f$[0..N-1]\f$.
// \param k Access index for the page. The index has to be in the range \f$[0..O-1]\f$.
// \return Reference to the accessed value.
// \exception std::out_of_range Invalid array access index.
//
// In contrast to the subscript operator this function always performs a check of the given
// access indices.
*/