Blaze 3.9
AlignedArray.h
Go to the documentation of this file.
1//=================================================================================================
33//=================================================================================================
34
35#ifndef _BLAZE_UTIL_ALIGNEDARRAY_H_
36#define _BLAZE_UTIL_ALIGNEDARRAY_H_
37
38
39//*************************************************************************************************
40// Includes
41//*************************************************************************************************
42
43#include <array>
51
52
53namespace blaze {
54
55//=================================================================================================
56//
57// CLASS DEFINITION
58//
59//=================================================================================================
60
61//*************************************************************************************************
97template< typename Type // Data type of the elements
98 , size_t N // Number of elements
99 , size_t Alignment = AlignmentOf_v<Type> > // Array alignment
101{
102 public:
103 //**Type definitions****************************************************************************
104 using ElementType = Type;
105 using Pointer = Type*;
106 using ConstPointer = const Type*;
107 using Reference = Type&;
108 using ConstReference = const Type&;
109 using Iterator = Type*;
110 using ConstIterator = const Type*;
111 //**********************************************************************************************
112
113 //**Constructors********************************************************************************
116 AlignedArray() = default;
117 AlignedArray( const AlignedArray& ) = default;
118 AlignedArray( AlignedArray&& ) = default;
119
120 template< typename... Ts >
121 constexpr AlignedArray( const Ts&... args );
122
123 template< typename T, size_t M >
124 constexpr AlignedArray( const T (&array)[M] );
125
126 template< typename T, size_t M >
127 constexpr AlignedArray( const std::array<T,M>& array );
128
129 template< typename T, size_t M >
130 constexpr AlignedArray( const AlignedArray<T,M>& array );
132 //**********************************************************************************************
133
134 //**Destructor**********************************************************************************
137 ~AlignedArray() = default;
139 //**********************************************************************************************
140
141 //**Conversion operators************************************************************************
144 constexpr operator Pointer () noexcept;
145 constexpr operator ConstPointer() const noexcept;
147 //**********************************************************************************************
148
149 //**Data access functions***********************************************************************
152 constexpr Reference operator[]( size_t index ) noexcept;
153 constexpr ConstReference operator[]( size_t index ) const noexcept;
154 inline Reference at( size_t index );
155 inline ConstReference at( size_t index ) const;
156 constexpr Pointer data() noexcept;
157 constexpr ConstPointer data() const noexcept;
158 constexpr Iterator begin () noexcept;
159 constexpr ConstIterator begin () const noexcept;
160 constexpr ConstIterator cbegin() const noexcept;
161 constexpr Iterator end () noexcept;
162 constexpr ConstIterator end () const noexcept;
163 constexpr ConstIterator cend () const noexcept;
165 //**********************************************************************************************
166
167 //**Assignment operators************************************************************************
170 AlignedArray& operator=( const AlignedArray& ) = default;
171 AlignedArray& operator=( AlignedArray&& ) = default;
172
173 template< typename T, size_t M >
174 constexpr AlignedArray& operator=( const T (&array)[M] );
175
176 template< typename T, size_t M >
177 constexpr AlignedArray& operator=( const std::array<T,M>& array );
178
179 template< typename T, size_t M >
180 constexpr AlignedArray& operator=( const AlignedArray<T,M>& array );
182 //**********************************************************************************************
183
184 //**Utility functions***************************************************************************
187 constexpr size_t size() const noexcept;
189 //**********************************************************************************************
190
191 //**Member variables****************************************************************************
195 alignas( Alignment ) Type v_[ N>0UL ? N : 1UL ];
202 //**********************************************************************************************
203
204 private:
205 //**Constructors********************************************************************************
208 template< typename T, size_t... Is >
209 constexpr AlignedArray( const T& array, std::index_sequence<Is...> );
211 //**********************************************************************************************
212
213 //**Compile time checks*************************************************************************
218 //**********************************************************************************************
219};
220//*************************************************************************************************
221
222
223
224
225//=================================================================================================
226//
227// DEDUCTION GUIDES
228//
229//=================================================================================================
230
231//*************************************************************************************************
232#if BLAZE_CPP17_MODE
233
234template< typename Type, typename... Ts >
235AlignedArray( Type, Ts... ) -> AlignedArray<Type,1+sizeof...(Ts)>;
236
237template< typename Type, size_t N >
238AlignedArray( Type (&)[N] ) -> AlignedArray< RemoveCV_t<Type>, N >;
239
240template< typename Type, size_t N >
241AlignedArray( std::array<Type,N> ) -> AlignedArray<Type,N>;
242
243#endif
244//*************************************************************************************************
245
246
247
248
249//=================================================================================================
250//
251// CONSTRUCTORS
252//
253//=================================================================================================
254
255//*************************************************************************************************
260template< typename Type // Data type of the elements
261 , size_t N // Number of elements
262 , size_t Alignment > // Array alignment
263template< typename... Ts > // Types of the array initializers
265 : v_{ args... } // The aligned array
266{}
267//*************************************************************************************************
268
269
270//*************************************************************************************************
278template< typename Type // Data type of the elements
279 , size_t N // Number of elements
280 , size_t Alignment > // Array alignment
281template< typename T // Data type of the static array
282 , size_t M > // Number of elements of the static array
283constexpr AlignedArray<Type,N,Alignment>::AlignedArray( const T (&array)[M] )
284 : AlignedArray( array, make_index_sequence<M>{} )
285{
286 BLAZE_STATIC_ASSERT( M <= N );
287}
288//*************************************************************************************************
289
290
291//*************************************************************************************************
299template< typename Type // Data type of the elements
300 , size_t N // Number of elements
301 , size_t Alignment > // Array alignment
302template< typename T // Data type of the std::array
303 , size_t M > // Number of elements of the std::array
304constexpr AlignedArray<Type,N,Alignment>::AlignedArray( const std::array<T,M>& array )
305 : AlignedArray( array, make_index_sequence<M>{} )
306{
307 BLAZE_STATIC_ASSERT( M <= N );
308}
309//*************************************************************************************************
310
311
312//*************************************************************************************************
320template< typename Type // Data type of the elements
321 , size_t N // Number of elements
322 , size_t Alignment > // Array alignment
323template< typename T // Data type of the aligned array
324 , size_t M > // Number of elements of the aligned array
326 : AlignedArray( array, make_index_sequence<M>{} )
327{
328 BLAZE_STATIC_ASSERT( M <= N );
329}
330//*************************************************************************************************
331
332
333//*************************************************************************************************
341template< typename Type // Data type of the elements
342 , size_t N // Number of elements
343 , size_t Alignment > // Array alignment
344template< typename T // Data type of the array
345 , size_t... Is > // Sequence of indices for the given array
346constexpr AlignedArray<Type,N,Alignment>::AlignedArray( const T& array, std::index_sequence<Is...> )
347 : v_{ array[Is]... }
348{}
349//*************************************************************************************************
350
351
352
353
354//=================================================================================================
355//
356// CONVERSION OPERATORS
357//
358//=================================================================================================
359
360//*************************************************************************************************
365template< typename Type // Data type of the elements
366 , size_t N // Number of elements
367 , size_t Alignment > // Array alignment
369{
370 return v_;
371}
372//*************************************************************************************************
373
374
375//*************************************************************************************************
380template< typename Type // Data type of the elements
381 , size_t N // Number of elements
382 , size_t Alignment > // Array alignment
384{
385 return v_;
386}
387//*************************************************************************************************
388
389
390
391
392//=================================================================================================
393//
394// DATA ACCESS FUNCTIONS
395//
396//=================================================================================================
397
398//*************************************************************************************************
406template< typename Type // Data type of the elements
407 , size_t N // Number of elements
408 , size_t Alignment > // Array alignment
411{
412 return v_[index];
413}
414//*************************************************************************************************
415
416
417//*************************************************************************************************
425template< typename Type // Data type of the elements
426 , size_t N // Number of elements
427 , size_t Alignment > // Array alignment
429 AlignedArray<Type,N,Alignment>::operator[]( size_t index ) const noexcept
430{
431 return v_[index];
432}
433//*************************************************************************************************
434
435
436//*************************************************************************************************
446template< typename Type // Data type of the elements
447 , size_t N // Number of elements
448 , size_t Alignment > // Array alignment
451{
452 if( index >= N ) {
453 BLAZE_THROW_OUT_OF_RANGE( "Invalid array access index" );
454 }
455 return v_[index];
456}
457//*************************************************************************************************
458
459
460//*************************************************************************************************
470template< typename Type // Data type of the elements
471 , size_t N // Number of elements
472 , size_t Alignment > // Array alignment
475{
476 if( index >= N ) {
477 BLAZE_THROW_OUT_OF_RANGE( "Invalid array access index" );
478 }
479 return v_[index];
480}
481//*************************************************************************************************
482
483
484//*************************************************************************************************
491template< typename Type // Data type of the elements
492 , size_t N // Number of elements
493 , size_t Alignment > // Array alignment
496{
497 return v_;
498}
499//*************************************************************************************************
500
501
502//*************************************************************************************************
509template< typename Type // Data type of the elements
510 , size_t N // Number of elements
511 , size_t Alignment > // Array alignment
514{
515 return v_;
516}
517//*************************************************************************************************
518
519
520//*************************************************************************************************
525template< typename Type // Data type of the elements
526 , size_t N // Number of elements
527 , size_t Alignment > // Array alignment
530{
531 return v_;
532}
533//*************************************************************************************************
534
535
536//*************************************************************************************************
541template< typename Type // Data type of the elements
542 , size_t N // Number of elements
543 , size_t Alignment > // Array alignment
546{
547 return v_;
548}
549//*************************************************************************************************
550
551
552//*************************************************************************************************
557template< typename Type // Data type of the elements
558 , size_t N // Number of elements
559 , size_t Alignment > // Array alignment
562{
563 return v_;
564}
565//*************************************************************************************************
566
567
568//*************************************************************************************************
573template< typename Type // Data type of the elements
574 , size_t N // Number of elements
575 , size_t Alignment > // Array alignment
578{
579 return v_ + N;
580}
581//*************************************************************************************************
582
583
584//*************************************************************************************************
589template< typename Type // Data type of the elements
590 , size_t N // Number of elements
591 , size_t Alignment > // Array alignment
594{
595 return v_ + N;
596}
597//*************************************************************************************************
598
599
600//*************************************************************************************************
605template< typename Type // Data type of the elements
606 , size_t N // Number of elements
607 , size_t Alignment > // Array alignment
610{
611 return v_ + N;
612}
613//*************************************************************************************************
614
615
616
617
618//=================================================================================================
619//
620// ASSIGNMENT OPERATORS
621//
622//=================================================================================================
623
624//*************************************************************************************************
633template< typename Type // Data type of the elements
634 , size_t N // Number of elements
635 , size_t Alignment > // Array alignment
636template< typename T // Data type of the static array
637 , size_t M > // Number of elements of the static array
640{
641 BLAZE_STATIC_ASSERT( M <= N );
642
643 for( size_t i=0UL; i<M; ++i )
644 v_[i] = array[i];
645
646 for( size_t i=M; i<N; ++i )
647 v_[i] = Type{};
648
649 return *this;
650}
651//*************************************************************************************************
652
653
654//*************************************************************************************************
663template< typename Type // Data type of the elements
664 , size_t N // Number of elements
665 , size_t Alignment > // Array alignment
666template< typename T // Data type of the std::array
667 , size_t M > // Number of elements of the std::array
669 AlignedArray<Type,N,Alignment>::operator=( const std::array<T,M>& array )
670{
671 BLAZE_STATIC_ASSERT( M <= N );
672
673 for( size_t i=0UL; i<M; ++i )
674 v_[i] = array[i];
675
676 for( size_t i=M; i<N; ++i )
677 v_[i] = Type{};
678
679 return *this;
680}
681//*************************************************************************************************
682
683
684//*************************************************************************************************
693template< typename Type // Data type of the elements
694 , size_t N // Number of elements
695 , size_t Alignment > // Array alignment
696template< typename T // Data type of the std::array
697 , size_t M > // Number of elements of the std::array
700{
701 BLAZE_STATIC_ASSERT( M <= N );
702
703 for( size_t i=0UL; i<M; ++i )
704 v_[i] = array[i];
705
706 for( size_t i=M; i<N; ++i )
707 v_[i] = Type{};
708
709 return *this;
710}
711//*************************************************************************************************
712
713
714
715
716//=================================================================================================
717//
718// UTILITY FUNCTIONS
719//
720//=================================================================================================
721
722//*************************************************************************************************
727template< typename Type // Data type of the elements
728 , size_t N // Number of elements
729 , size_t Alignment > // Array alignment
730constexpr size_t AlignedArray<Type,N,Alignment>::size() const noexcept
731{
732 return N;
733}
734//*************************************************************************************************
735
736} // namespace blaze
737
738#endif
Header file for the AlignmentOf type trait.
Constraint on the data type.
Header file for the integer_sequence and index_sequence aliases.
Header file for the RemoveCV type trait.
Compile time assertion.
Constraint on the data type.
Implementation of a static array with a fixed alignment.
Definition: AlignedArray.h:101
constexpr Pointer data() noexcept
Low-level data access to the array elements.
Definition: AlignedArray.h:495
const Type * ConstIterator
Iterator over constant elements.
Definition: AlignedArray.h:110
const Type & ConstReference
Reference to a constant array element.
Definition: AlignedArray.h:108
constexpr Iterator end() noexcept
Returns an iterator just past the last element of the aligned array.
Definition: AlignedArray.h:577
constexpr Reference operator[](size_t index) noexcept
Subscript operator for the direct access to the array elements.
Definition: AlignedArray.h:410
constexpr size_t size() const noexcept
Returns the current size/dimension of the aligned array.
Definition: AlignedArray.h:730
Type * Pointer
Pointer to a non-constant array element.
Definition: AlignedArray.h:105
constexpr AlignedArray(const std::array< T, M > &array)
Initialization of all aligned array elements from the given std::array.
Definition: AlignedArray.h:304
constexpr Iterator begin() noexcept
Returns an iterator to the first element of the aligned array.
Definition: AlignedArray.h:529
constexpr ConstIterator cbegin() const noexcept
Returns an iterator to the first element of the aligned array.
Definition: AlignedArray.h:561
Reference at(size_t index)
Checked access to the array elements.
Definition: AlignedArray.h:450
Type ElementType
Type of the array elements.
Definition: AlignedArray.h:104
constexpr AlignedArray(const Ts &... args)
Initialization constructor for AlignedArray.
Definition: AlignedArray.h:264
const Type * ConstPointer
Pointer to a constant array element.
Definition: AlignedArray.h:106
constexpr AlignedArray(const AlignedArray< T, M > &array)
Initialization of all aligned array elements from another aligned array.
Definition: AlignedArray.h:325
Type & Reference
Reference to a non-constant array element.
Definition: AlignedArray.h:107
constexpr ConstIterator cend() const noexcept
Returns an iterator just past the last element of the aligned array.
Definition: AlignedArray.h:609
Type * Iterator
Iterator over non-constant elements.
Definition: AlignedArray.h:109
constexpr AlignedArray(const T(&array)[M])
Initialization of all aligned array elements from the given static array.
Definition: AlignedArray.h:283
#define BLAZE_CONSTRAINT_MUST_NOT_BE_VOLATILE(T)
Constraint on the data type.
Definition: Volatile.h:79
#define BLAZE_CONSTRAINT_MUST_NOT_BE_CONST(T)
Constraint on the data type.
Definition: Const.h:79
#define BLAZE_STATIC_ASSERT(expr)
Compile time assertion macro.
Definition: StaticAssert.h:112
#define BLAZE_THROW_OUT_OF_RANGE(MESSAGE)
Macro for the emission of a std::out_of_range exception.
Definition: Exception.h:331
Header file for exception macros.