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 
45 #include <blaze/util/Exception.h>
48 
49 
50 namespace blaze {
51 
52 //=================================================================================================
53 //
54 // CLASS DEFINITION
55 //
56 //=================================================================================================
57 
58 //*************************************************************************************************
94 template< typename Type // Data type of the elements
95  , size_t N // Number of elements
96  , size_t Alignment = AlignmentOf<Type>::value > // Array alignment
98 {
99  public:
100  //**Type definitions****************************************************************************
101  typedef Type ElementType;
102  typedef Type* Pointer;
103  typedef const Type* ConstPointer;
104  typedef Type& Reference;
105  typedef const Type& ConstReference;
106  typedef Type* Iterator;
107  typedef const Type* ConstIterator;
108  //**********************************************************************************************
109 
110  //**Constructors********************************************************************************
113  explicit inline AlignedArray();
114 
115  template< typename... Ts >
116  explicit inline constexpr AlignedArray( const Ts&... args );
118  //**********************************************************************************************
119 
120  //**Destructor**********************************************************************************
121  // No explicitly declared destructor.
122  //**********************************************************************************************
123 
124  //**Conversion operators************************************************************************
127  inline operator Pointer () noexcept;
128  inline constexpr operator ConstPointer() const noexcept;
130  //**********************************************************************************************
131 
132  //**Data access functions***********************************************************************
135  inline Reference operator[]( size_t index ) noexcept;
136  inline constexpr ConstReference operator[]( size_t index ) const noexcept;
137  inline Reference at( size_t index );
138  inline ConstReference at( size_t index ) const;
139  inline Pointer data() noexcept;
140  inline constexpr ConstPointer data() const noexcept;
141  inline Iterator begin () noexcept;
142  inline constexpr ConstIterator begin () const noexcept;
143  inline constexpr ConstIterator cbegin() const noexcept;
144  inline Iterator end () noexcept;
145  inline constexpr ConstIterator end () const noexcept;
146  inline constexpr ConstIterator cend () const noexcept;
148  //**********************************************************************************************
149 
150  //**Utility functions***************************************************************************
153  inline constexpr size_t size() const noexcept;
155  //**********************************************************************************************
156 
157  private:
158  //**Member variables****************************************************************************
162  alignas( Alignment ) Type v_[N];
163 
164 
165  //**********************************************************************************************
166 
167  //**Compile time checks*************************************************************************
172  //**********************************************************************************************
173 };
174 //*************************************************************************************************
175 
176 
177 
178 
179 //=================================================================================================
180 //
181 // CONSTRUCTORS
182 //
183 //=================================================================================================
184 
185 //*************************************************************************************************
188 template< typename Type // Data type of the elements
189  , size_t N // Number of elements
190  , size_t Alignment > // Array alignment
191 inline AlignedArray<Type,N,Alignment>::AlignedArray()
192 {}
193 //*************************************************************************************************
194 
195 
196 //*************************************************************************************************
201 template< typename Type // Data type of the elements
202  , size_t N // Number of elements
203  , size_t Alignment > // Array alignment
204 template< typename... Ts > // Types of the array initializers
205 inline constexpr AlignedArray<Type,N,Alignment>::AlignedArray( const Ts&... args )
206  : v_{ args... }
207 {
208  BLAZE_STATIC_ASSERT( sizeof...( Ts ) == N );
209 }
210 //*************************************************************************************************
211 
212 
213 
214 
215 //=================================================================================================
216 //
217 // CONVERSION OPERATORS
218 //
219 //=================================================================================================
220 
221 //*************************************************************************************************
226 template< typename Type // Data type of the elements
227  , size_t N // Number of elements
228  , size_t Alignment > // Array alignment
230 {
231  return v_;
232 }
233 //*************************************************************************************************
234 
235 
236 //*************************************************************************************************
241 template< typename Type // Data type of the elements
242  , size_t N // Number of elements
243  , size_t Alignment > // Array alignment
244 inline constexpr AlignedArray<Type,N,Alignment>::operator ConstPointer() const noexcept
245 {
246  return v_;
247 }
248 //*************************************************************************************************
249 
250 
251 
252 
253 //=================================================================================================
254 //
255 // DATA ACCESS FUNCTIONS
256 //
257 //=================================================================================================
258 
259 //*************************************************************************************************
267 template< typename Type // Data type of the elements
268  , size_t N // Number of elements
269  , size_t Alignment > // Array alignment
272 {
273  return v_[index];
274 }
275 //*************************************************************************************************
276 
277 
278 //*************************************************************************************************
286 template< typename Type // Data type of the elements
287  , size_t N // Number of elements
288  , size_t Alignment > // Array alignment
289 inline constexpr typename AlignedArray<Type,N,Alignment>::ConstReference
290  AlignedArray<Type,N,Alignment>::operator[]( size_t index ) const noexcept
291 {
292  return v_[index];
293 }
294 //*************************************************************************************************
295 
296 
297 //*************************************************************************************************
307 template< typename Type // Data type of the elements
308  , size_t N // Number of elements
309  , size_t Alignment > // Array alignment
312 {
313  if( index >= N ) {
314  BLAZE_THROW_OUT_OF_RANGE( "Invalid array access index" );
315  }
316  return v_[index];
317 }
318 //*************************************************************************************************
319 
320 
321 //*************************************************************************************************
331 template< typename Type // Data type of the elements
332  , size_t N // Number of elements
333  , size_t Alignment > // Array alignment
336 {
337  if( index >= N ) {
338  BLAZE_THROW_OUT_OF_RANGE( "Invalid array access index" );
339  }
340  return v_[index];
341 }
342 //*************************************************************************************************
343 
344 
345 //*************************************************************************************************
352 template< typename Type // Data type of the elements
353  , size_t N // Number of elements
354  , size_t Alignment > // Array alignment
357 {
358  return v_;
359 }
360 //*************************************************************************************************
361 
362 
363 //*************************************************************************************************
370 template< typename Type // Data type of the elements
371  , size_t N // Number of elements
372  , size_t Alignment > // Array alignment
373 inline constexpr typename AlignedArray<Type,N,Alignment>::ConstPointer
375 {
376  return v_;
377 }
378 //*************************************************************************************************
379 
380 
381 //*************************************************************************************************
386 template< typename Type // Data type of the elements
387  , size_t N // Number of elements
388  , size_t Alignment > // Array alignment
391 {
392  return v_;
393 }
394 //*************************************************************************************************
395 
396 
397 //*************************************************************************************************
402 template< typename Type // Data type of the elements
403  , size_t N // Number of elements
404  , size_t Alignment > // Array alignment
405 inline constexpr typename AlignedArray<Type,N,Alignment>::ConstIterator
407 {
408  return v_;
409 }
410 //*************************************************************************************************
411 
412 
413 //*************************************************************************************************
418 template< typename Type // Data type of the elements
419  , size_t N // Number of elements
420  , size_t Alignment > // Array alignment
421 inline constexpr typename AlignedArray<Type,N,Alignment>::ConstIterator
423 {
424  return v_;
425 }
426 //*************************************************************************************************
427 
428 
429 //*************************************************************************************************
434 template< typename Type // Data type of the elements
435  , size_t N // Number of elements
436  , size_t Alignment > // Array alignment
439 {
440  return v_ + N;
441 }
442 //*************************************************************************************************
443 
444 
445 //*************************************************************************************************
450 template< typename Type // Data type of the elements
451  , size_t N // Number of elements
452  , size_t Alignment > // Array alignment
453 inline constexpr typename AlignedArray<Type,N,Alignment>::ConstIterator
455 {
456  return v_ + N;
457 }
458 //*************************************************************************************************
459 
460 
461 //*************************************************************************************************
466 template< typename Type // Data type of the elements
467  , size_t N // Number of elements
468  , size_t Alignment > // Array alignment
469 inline constexpr typename AlignedArray<Type,N,Alignment>::ConstIterator
471 {
472  return v_ + N;
473 }
474 //*************************************************************************************************
475 
476 
477 
478 
479 //=================================================================================================
480 //
481 // UTILITY FUNCTIONS
482 //
483 //=================================================================================================
484 
485 //*************************************************************************************************
490 template< typename Type // Data type of the elements
491  , size_t N // Number of elements
492  , size_t Alignment > // Array alignment
493 inline constexpr size_t AlignedArray<Type,N,Alignment>::size() const noexcept
494 {
495  return N;
496 }
497 //*************************************************************************************************
498 
499 } // namespace blaze
500 
501 #endif
#define BLAZE_CONSTRAINT_MUST_NOT_BE_CONST(T)
Constraint on the data type.In case the given data type is a const-qualified type, a compilation error is created.
Definition: Const.h:79
Header file for the AlignmentOf type trait.
Header file for exception macros.
#define BLAZE_CONSTRAINT_MUST_NOT_BE_VOLATILE(T)
Constraint on the data type.In case the given data type is a volatile-qualified type, a compilation error is created.
Definition: Volatile.h:79
Type ElementType
Type of the array elements.
Definition: AlignedArray.h:101
const Type * ConstIterator
Iterator over constant elements.
Definition: AlignedArray.h:107
Iterator end() noexcept
Returns an iterator just past the last element of the aligned array.
Definition: AlignedArray.h:438
AlignedArray()
The default constructor for AlignedArray.
Definition: AlignedArray.h:191
constexpr ConstIterator cend() const noexcept
Returns an iterator just past the last element of the aligned array.
Definition: AlignedArray.h:470
Namespace of the Blaze C++ math library.
Definition: Blaze.h:57
Compile time assertion.
constexpr size_t size() const noexcept
Returns the current size/dimension of the aligned array.
Definition: AlignedArray.h:493
#define BLAZE_THROW_OUT_OF_RANGE(MESSAGE)
Macro for the emission of a std::out_of_range exception.This macro encapsulates the default way of Bl...
Definition: Exception.h:331
const Type * ConstPointer
Pointer to a constant array element.
Definition: AlignedArray.h:103
Constraint on the data type.
Reference at(size_t index)
Checked access to the array elements.
Definition: AlignedArray.h:311
Type * Pointer
Pointer to a non-constant array element.
Definition: AlignedArray.h:102
Reference operator[](size_t index) noexcept
Subscript operator for the direct access to the array elements.
Definition: AlignedArray.h:271
const Type & ConstReference
Reference to a constant array element.
Definition: AlignedArray.h:105
Constraint on the data type.
Type & Reference
Reference to a non-constant array element.
Definition: AlignedArray.h:104
Iterator begin() noexcept
Returns an iterator to the first element of the aligned array.
Definition: AlignedArray.h:390
Pointer data() noexcept
Low-level data access to the array elements.
Definition: AlignedArray.h:356
Type * Iterator
Iterator over non-constant elements.
Definition: AlignedArray.h:106
constexpr ConstIterator cbegin() const noexcept
Returns an iterator to the first element of the aligned array.
Definition: AlignedArray.h:422
Implementation of a static array with a fixed alignment.The AlignedArray class template represents a ...
Definition: AlignedArray.h:97
#define BLAZE_STATIC_ASSERT(expr)
Compile time assertion macro.In case of an invalid compile time expression, a compilation error is cr...
Definition: StaticAssert.h:112