-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathArray.hpp
More file actions
759 lines (657 loc) · 26.6 KB
/
Array.hpp
File metadata and controls
759 lines (657 loc) · 26.6 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
/*
* Copyright (c) 2021, Lawrence Livermore National Security, LLC and LvArray contributors.
* All rights reserved.
* See the LICENSE file for details.
* SPDX-License-Identifier: (BSD-3-Clause)
*/
/**
* @file Array.hpp
* @brief Contains the implementation of LvArray::Array.
*/
#pragma once
// Source includes
#include "indexing.hpp"
#include "ArrayView.hpp"
#include "bufferManipulation.hpp"
#include "StackBuffer.hpp"
/**
* @brief The top level namespace.
*/
namespace LvArray
{
/**
* @class Array
* @brief This class provides a fixed dimensional resizeable array interface in addition to an
* interface similar to std::vector for a 1D array.
* @tparam T The type of data that is contained by the array.
* @tparam NDIM The number of dimensions in array (e.g. NDIM=1->vector, NDIM=2->Matrix, etc. ).
* Some methods such as push_back are only available for a 1D array.
* @tparam PERMUTATION A camp::idx_seq containing the values in [0, NDIM) which describes how the
* data is to be laid out in memory. Given an 3-dimensional array A of dimension (L, M, N)
* the standard layout is A( i, j, k ) = A.data()[ i * M * N + j * N + k ], this layout is
* the given by the identity permutation (0, 1, 2) which says that the first dimension in
* memory corresponds to the first index (i), and similarly for the other dimensions. The
* same array with a layout of A( i, j, k ) = A.data()[ L * N * j + L * k + i ] would have
* a permutation of (1, 2, 0) because the first dimension in memory corresponds to the second
* index (j), the second dimension in memory corresponds to the third index (k) and the last
* dimension in memory goes with the first index (i).
* @note RAJA provides aliases for every valid permutations up to 5D, the two permutations
* mentioned above can be used via RAJA::PERM_IJK and RAJA::PERM_JKI.
* @note The dimension with unit stride is the last dimension in the permutation.
* @tparam INDEX_TYPE the integer to use for indexing.
* @tparam BUFFER_TYPE A class that defines how to actually allocate memory for the Array. Must take
* one template argument that describes the type of the data being stored (T).
*/
template< typename T,
int NDIM,
typename PERMUTATION,
typename INDEX_TYPE,
template< typename > class BUFFER_TYPE >
class Array : public ArrayView< T,
NDIM,
typeManipulation::getStrideOneDimension( PERMUTATION {} ),
INDEX_TYPE,
BUFFER_TYPE >
{
public:
// Check that the template arguments are valid.
static_assert( NDIM >= 0, "The dimension of the Array must be positive." );
static_assert( typeManipulation::isValidPermutation( PERMUTATION {} ), "The permutation must be valid." );
static_assert( typeManipulation::getDimension< PERMUTATION > == NDIM,
"The dimension of the permutation must match the dimension of the Array." );
static_assert( std::is_integral< INDEX_TYPE >::value, "INDEX_TYPE must be integral." );
/// The permutation of the array.
using Permutation = PERMUTATION;
/// Alias for the parent class.
using ParentClass = ArrayView< T, NDIM, typeManipulation::getStrideOneDimension( Permutation {} ), INDEX_TYPE, BUFFER_TYPE >;
using ParentClass::USD;
using typename ParentClass::NestedViewType;
using typename ParentClass::NestedViewTypeConst;
/**
* @name Constructors, destructor and assignment operators.
*/
///@{
/**
* @brief default constructor
*/
LVARRAY_HOST_DEVICE
inline Array():
ParentClass( true )
{
this->m_strides = indexing::calculateStrides< PERMUTATION >( this->m_dims );
#if !defined(LVARRAY_DEVICE_COMPILE)
setName( "" );
#endif
#if defined(LVARRAY_USE_TOTALVIEW_OUTPUT) && !defined(LVARRAY_DEVICE_COMPILE)
Array::TV_ttf_display_type( nullptr );
#endif
}
/**
* @brief Constructor that takes in the dimensions as a variadic parameter list
* @param dims the dimensions of the array in form ( n0, n1,..., n(NDIM-1) )
*/
template< typename ... DIMS,
typename=std::enable_if_t< sizeof ... ( DIMS ) == NDIM &&
typeManipulation::all_of_t< std::is_integral< DIMS > ... >::value > >
LVARRAY_HOST_DEVICE
inline explicit Array( DIMS const ... dims ):
Array()
{ resize( dims ... ); }
/**
* @brief Construct an Array from @p buffer, taking ownership of its contents.
* @param buffer The buffer to construct the Array from.
* @note The Array is empty and @p buffer is expected to be empty as well.
*/
Array( BUFFER_TYPE< T > && buffer ):
ParentClass( std::move( buffer ) )
{
this->m_strides = indexing::calculateStrides< PERMUTATION >( this->m_dims );
#if !defined(LVARRAY_DEVICE_COMPILE)
setName( "" );
#endif
#if defined(LVARRAY_USE_TOTALVIEW_OUTPUT) && !defined(LVARRAY_DEVICE_COMPILE)
Array::TV_ttf_display_type( nullptr );
#endif
}
/**
* @brief Copy constructor.
* @param source object to copy.
* @note Performs a deep copy of source
*/
LVARRAY_HOST_DEVICE
Array( Array const & source ):
Array()
{ *this = source; }
/**
* @brief Move constructor.
* @param source object to move.
* @note Moves the source into *this. This calls BUFFER_TYPE( BUFFER_TYPE && ) which usually is a
* shallow copy that invalidates the contents of source. However this depends on the
* implementation of BUFFER_TYPE.
*/
LVARRAY_HOST_DEVICE
Array( Array && source ):
ParentClass( std::move( source ) )
{
for( int i = 0; i < NDIM; ++i )
{
source.m_dims[ i ] = 0;
source.m_strides[ i ] = 0;
}
}
/**
* @brief Destructor, free's the data.
*/
LVARRAY_HOST_DEVICE
~Array()
{ bufferManipulation::free( this->m_dataBuffer, this->size() ); }
/**
* @brief Copy assignment operator, performs a deep copy of rhs.
* @param rhs Source for the assignment.
* @return *this.
*/
LVARRAY_HOST_DEVICE
Array & operator=( Array const & rhs )
{
bufferManipulation::copyInto( this->m_dataBuffer, this->size(), rhs.m_dataBuffer, rhs.size() );
for( int i = 0; i < NDIM; ++i )
{
this->m_dims[ i ] = rhs.m_dims[ i ];
this->m_strides[ i ] = rhs.m_strides[ i ];
}
return *this;
}
/**
* @brief Copy assignment operator, performs a deep copy of rhs.
* @param rhs Source for the assignment.
* @return *this.
*/
LVARRAY_HOST_DEVICE
Array & operator=( typename ParentClass::ViewTypeConst const & rhs )
{
bufferManipulation::copyInto( this->m_dataBuffer, this->size(), rhs.dataBuffer(), rhs.size() );
INDEX_TYPE const * const dims = rhs.dims();
INDEX_TYPE const * const strides = rhs.strides();
for( int i = 0; i < NDIM; ++i )
{
this->m_dims[ i ] = dims[ i ];
this->m_strides[ i ] = strides[ i ];
}
return *this;
}
/**
* @brief Move assignment operator, performs a shallow copy of rhs.
* @param rhs Source for the assignment.
* @return *this.
*/
LVARRAY_HOST_DEVICE
Array & operator=( Array && rhs )
{
bufferManipulation::free( this->m_dataBuffer, this->size() );
ParentClass::operator=( std::move( rhs ) );
for( int i = 0; i < NDIM; ++i )
{
rhs.m_dims[ i ] = 0;
rhs.m_strides[ i ] = 0;
}
return *this;
}
///@}
/**
* @name ArrayView and ArraySlice creation methods and user defined conversions.
*/
///@{
using ParentClass::toView;
/**
* @brief Overload for rvalues that is deleted.
* @return None.
* @note This cannot be called on a rvalue since the @c ArrayView would
* contain the buffer of the current @c Array that is about to be destroyed.
* This overload prevents that from happening.
*/
inline LVARRAY_HOST_DEVICE constexpr
ArrayView< T, NDIM, USD, INDEX_TYPE, BUFFER_TYPE > toView() const && = delete;
using ParentClass::toViewConst;
/**
* @brief Overload for rvalues that is deleted.
* @return None.
* @note This cannot be called on a rvalue since the @c ArrayView would
* contain the buffer of the current @c Array that is about to be destroyed.
* This overload prevents that from happening.
*/
inline LVARRAY_HOST_DEVICE constexpr
ArrayView< T const, NDIM, USD, INDEX_TYPE, BUFFER_TYPE > toViewConst() const && = delete;
using ParentClass::toNestedView;
/**
* @brief Overload for rvalues that is deleted.
* @return None.
* @note This cannot be called on a rvalue since the @c ArrayView would
* contain the buffer of the current @c Array that is about to be destroyed.
* This overload prevents that from happening.
*/
inline LVARRAY_HOST_DEVICE constexpr
NestedViewType toNestedView() const && = delete;
using ParentClass::toNestedViewConst;
/**
* @brief Overload for rvalues that is deleted.
* @return None.
* @note This cannot be called on a rvalue since the @c ArrayView would
* contain the buffer of the current @c Array that is about to be destroyed.
* This overload prevents that from happening.
*/
inline LVARRAY_HOST_DEVICE constexpr
NestedViewTypeConst toNestedViewConst() const && = delete;
/**
* @brief A user defined conversion operator (UDC) to an ArrayView< T const, ... >.
* @return A new ArrayView where @c T is @c const.
*/
inline LVARRAY_HOST_DEVICE constexpr
operator ArrayView< T const, NDIM, USD, INDEX_TYPE, BUFFER_TYPE >() const & noexcept
{ return toViewConst(); }
/**
* @brief Overload for rvalues that is deleted.
* @return None.
* @note This cannot be called on a rvalue since the @c ArrayView would
* contain the buffer of the current @c Array that is about to be destroyed.
* This overload prevents that from happening.
*/
template< typename _T=T >
inline LVARRAY_HOST_DEVICE constexpr
operator std::enable_if_t< !std::is_const< _T >::value,
ArrayView< T const, NDIM, USD, INDEX_TYPE, BUFFER_TYPE > >() const && noexcept = delete;
///@}
/**
* @name Resizing methods.
*/
///@{
/**
* @brief Resize the dimensions of the Array to match the given dimensions.
* @param numDims must equal NDIMS.
* @param dims the new size of the dimensions, must be of length NDIM.
* @note This does not preserve the values in the Array NDIM == 1.
*/
template< typename DIMS_TYPE >
LVARRAY_HOST_DEVICE
void resize( int const numDims, DIMS_TYPE const * const dims )
{
LVARRAY_ERROR_IF_NE( numDims, NDIM );
INDEX_TYPE const oldSize = this->size();
for( int i = 0; i < NDIM; ++i )
{
this->m_dims[ i ] = LvArray::integerConversion< INDEX_TYPE >( dims[ i ] );
LVARRAY_ERROR_IF_LT( this->m_dims[ i ], 0 );
}
this->m_strides = indexing::calculateStrides< PERMUTATION >( this->m_dims );
bufferManipulation::resize( this->m_dataBuffer, oldSize, this->size() );
}
/**
* @brief function to resize the array.
* @tparam DIMS Variadic list of integral types.
* @param newDims The new dimensions, must be of length NDIM.
* @note This does not preserve the values in the Array unless NDIM == 1.
* @return void.
*/
template< typename ... DIMS >
LVARRAY_HOST_DEVICE
std::enable_if_t< sizeof ... ( DIMS ) == NDIM && typeManipulation::all_of_t< std::is_integral< DIMS > ... >::value >
resize( DIMS const ... newDims )
{
static_assert( sizeof ... ( DIMS ) == NDIM, "The number of arguments provided does not equal NDIM!" );
INDEX_TYPE const oldSize = this->size();
int curDim = 0;
typeManipulation::forEachArg( [&]( auto const newDim )
{
this->m_dims[ curDim ] = LvArray::integerConversion< INDEX_TYPE >( newDim );
LVARRAY_ERROR_IF_LT( this->m_dims[ curDim ], 0 );
++curDim;
}, newDims ... );
this->m_strides = indexing::calculateStrides< PERMUTATION >( this->m_dims );
bufferManipulation::resize( this->m_dataBuffer, oldSize, this->size() );
}
/**
* @brief Resize the array without initializing any new values or destroying any old values.
* Only safe on POD data, however it is much faster for large allocations.
* @tparam DIMS Variadic list of integral types.
* @param newDims The new dimensions, must be of length NDIM.
* @note This does not preserve the values in the Array unless NDIM == 1.
*/
template< typename ... DIMS >
LVARRAY_HOST_DEVICE
void resizeWithoutInitializationOrDestruction( DIMS const ... newDims )
{
return resizeWithoutInitializationOrDestruction( MemorySpace::host, newDims ... );
}
/**
* @brief Resize the array without initializing any new values or destroying any old values.
* Only safe on POD data, however it is much faster for large allocations.
* @tparam DIMS Variadic list of integral types.
* @param space The space to perform the resize in.
* @param newDims The new dimensions, must be of length NDIM.
* @note This does not preserve the values in the Array unless NDIM == 1.
*/
template< typename ... DIMS >
LVARRAY_HOST_DEVICE
void resizeWithoutInitializationOrDestruction( MemorySpace const space, DIMS const ... newDims )
{
static_assert( sizeof ... ( DIMS ) == NDIM, "The number of arguments provided does not equal NDIM!" );
static_assert( std::is_trivially_destructible< T >::value,
"This function is only safe if T is trivially destructable." );
INDEX_TYPE const oldSize = this->size();
int i = 0;
typeManipulation::forEachArg( [&]( auto const newDim )
{
this->m_dims[ i ] = LvArray::integerConversion< INDEX_TYPE >( newDim );
LVARRAY_ERROR_IF_LT( this->m_dims[ i ], 0 );
++i;
}, newDims ... );
this->m_strides = indexing::calculateStrides< PERMUTATION >( this->m_dims );
bufferManipulation::reserve( this->m_dataBuffer, oldSize, space, this->size() );
}
/**
* @brief Resize specific dimensions of the array.
* @tparam INDICES the indices of the dimensions to resize, should be sorted an unique.
* @tparam DIMS variadic pack containing the dimension types
* @param newDims the new dimensions. newDims[ 0 ] will be the new size of
* dimensions INDICES[ 0 ].
* @note This does not preserve the values in the Array unless NDIM == 1.
*/
template< INDEX_TYPE... INDICES, typename ... DIMS >
LVARRAY_HOST_DEVICE
void resizeDimension( DIMS const ... newDims )
{
static_assert( sizeof ... (INDICES) <= NDIM, "Too many arguments provided." );
static_assert( sizeof ... (INDICES) == sizeof ... (DIMS),
"The number of indices must match the number of dimensions." );
static_assert( typeManipulation::all_of< ( 0 <= INDICES ) ... >::value, "INDICES must all be positive." );
static_assert( typeManipulation::all_of< ( INDICES < NDIM ) ... >::value, "INDICES must all be less than NDIM." );
INDEX_TYPE const oldSize = this->size();
typeManipulation::forEachArg( [&]( auto const & pair )
{
this->m_dims[ camp::get< 0 >( pair ) ] = LvArray::integerConversion< INDEX_TYPE >( camp::get< 1 >( pair ) );
LVARRAY_ERROR_IF_LT( this->m_dims[ camp::get< 0 >( pair ) ], 0 );
}, camp::make_tuple( INDICES, newDims )... );
this->m_strides = indexing::calculateStrides< PERMUTATION >( this->m_dims );
bufferManipulation::resize( this->m_dataBuffer, oldSize, this->size() );
}
/**
* @brief Resize the first dimension of the Array.
* @param newdim the new size of the first dimension.
* @note This preserves the values in the Array.
*/
LVARRAY_HOST_DEVICE
void resize( INDEX_TYPE const newdim )
{ resizeDefaultDimension( newdim ); }
/**
* @brief Resize the first dimension of the Array.
* @param newdim the new size of the first dimension.
* @param defaultValue the value to initialize the new values with.
* @note This preserves the values in the Array.
*/
LVARRAY_HOST_DEVICE
void resizeDefault( INDEX_TYPE const newdim, T const & defaultValue )
{ resizeDefaultDimension( newdim, defaultValue ); }
/**
* @brief Sets the size of the Array to zero and destroys all the values.
* @details Sets the size of the first dimension to 0 but leaves the
* size of the other dimensions untouched. Equivalent to resize( 0 ).
*/
void clear()
{
bufferManipulation::resize( this->m_dataBuffer, this->size(), 0 );
this->m_dims[ 0 ] = 0;
this->m_strides = indexing::calculateStrides< PERMUTATION >( this->m_dims );
}
///@}
/**
* @name Methods to modify the capacity.
*/
///@{
/**
* @brief Reserve space in the Array to hold at least the given number of values.
* @param newCapacity the number of values to reserve space for. After this call
* capacity() >= newCapacity.
*/
void reserve( INDEX_TYPE const newCapacity )
{ bufferManipulation::reserve( this->m_dataBuffer, this->size(), MemorySpace::host, newCapacity ); }
///@}
/**
* @name One dimensional interface.
* @note These methods are only enabled for one dimensional arrays (NDIM == 1).
*/
///@{
/**
* @brief Construct a value in place at the end of the array.
* @tparam ARGS A variadic pack of the types to construct the new value from.
* @param args A variadic pack of values to construct the new value from.
* @note This method is only available on 1D arrays.
* @return void.
*/
template< typename ... ARGS, int _NDIM=NDIM >
std::enable_if_t< _NDIM == 1 >
emplace_back( ARGS && ... args )
{
bufferManipulation::emplaceBack( this->m_dataBuffer, this->size(), std::forward< ARGS >( args ) ... );
++this->m_dims[ 0 ];
}
/**
* @brief Insert a value into the array constructing it in place.
* @tparam ARGS A variadic pack of the types to construct the new value from.
* @param pos the position to insert at.
* @param args A variadic pack of values to construct the new value from.
* @note This method is only available on 1D arrays.
* @return void.
*/
template< typename ... ARGS, int _NDIM=NDIM >
std::enable_if_t< _NDIM == 1 >
emplace( INDEX_TYPE const pos, ARGS && ... args )
{
bufferManipulation::emplace( this->m_dataBuffer, this->size(), pos, std::forward< ARGS >( args ) ... );
++this->m_dims[ 0 ];
}
/**
* @brief Insert values into the array at the given position.
* @tparam ITER An iterator type, they type of @p first and @p last.
* @param pos The position to insert at.
* @param first An iterator to the first value to insert.
* @param last An iterator to the last value to insert.
* @note This method is only available on 1D arrays.
* @return None.
*/
template< typename ITER, int _NDIM=NDIM >
std::enable_if_t< _NDIM == 1 >
insert( INDEX_TYPE const pos, ITER const first, ITER const last )
{ this->m_dims[ 0 ] += bufferManipulation::insert( this->m_dataBuffer, this->size(), pos, first, last ); }
/**
* @brief Remove the last value in the array.
* @note This method is only available on 1D arrays.
* @return void.
*/
template< int _NDIM=NDIM >
std::enable_if_t< _NDIM == 1 >
pop_back()
{
bufferManipulation::popBack( this->m_dataBuffer, this->size() );
--this->m_dims[ 0 ];
}
/**
* @brief Remove the value at the given position.
* @param pos the position of the value to remove.
* @note This method is only available on 1D arrays.
* @return void.
*/
template< int _NDIM=NDIM >
std::enable_if_t< _NDIM == 1 >
erase( INDEX_TYPE const pos )
{
bufferManipulation::erase( this->m_dataBuffer, this->size(), pos );
--this->m_dims[ 0 ];
}
///@}
/**
* @brief Set the name to be displayed whenever the underlying Buffer's user call back is called.
* @param name the name of the Array.
*/
void setName( std::string const & name )
{ this->m_dataBuffer.template setName< decltype(*this) >( name ); }
#if defined(LVARRAY_USE_TOTALVIEW_OUTPUT) && !defined(LVARRAY_DEVICE_COMPILE)
/**
* @brief Static function that will be used by Totalview to display the array contents.
* @param av A pointer to the array that is being displayed.
* @return 0 if everything went OK
*/
static int TV_ttf_display_type( Array const * av )
{
return ParentClass::TV_ttf_display_type( av );
}
#endif
private:
/**
* @brief Resize the default dimension of the Array.
* @tparam ARGS variadic pack containing the types to initialize the new values with.
* @param newDimLength the new size of the default dimension.
* @param args arguments to initialize the new values with.
* @note This preserves the values in the Array.
*/
DISABLE_HD_WARNING
template< typename ... ARGS >
LVARRAY_HOST_DEVICE
void resizeDefaultDimension( INDEX_TYPE const newDimLength, ARGS && ... args )
{
LVARRAY_ERROR_IF_LT( newDimLength, 0 );
// If the first dimension in memory is 0 then a simple 1D resizing is sufficient. The
// check if NDIM == 1 is to give the compiler compile time knowledge that this path is always taken for 1D arrays.
if( NDIM == 1 || typeManipulation::asArray( PERMUTATION {} )[ 0 ] == 0 )
{
INDEX_TYPE const oldSize = this->size();
this->m_dims[ 0 ] = newDimLength;
this->m_strides = indexing::calculateStrides< PERMUTATION >( this->m_dims );
bufferManipulation::resize( this->m_dataBuffer, oldSize, this->size(), std::forward< ARGS >( args )... );
return;
}
// Get the current length and stride of the dimension as well as the size of the whole Array.
INDEX_TYPE const curDimLength = this->m_dims[ 0 ];
INDEX_TYPE const curDimStride = this->m_strides[ 0 ];
INDEX_TYPE const curSize = this->size();
// Set the size of the dimension, recalculate the strides and get the new total size.
this->m_dims[ 0 ] = newDimLength;
this->m_strides = indexing::calculateStrides< PERMUTATION >( this->m_dims );
INDEX_TYPE const newSize = this->size();
// If we aren't changing the total size then we can return early.
if( newSize == curSize ) return;
// If the size is increasing do one thing, if it's decreasing do another.
if( newDimLength > curDimLength )
{
// Reserve space in the buffer but don't initialize the values.
bufferManipulation::reserve( this->m_dataBuffer, curSize, MemorySpace::host, newSize );
T * const ptr = this->data();
// The resizing consists of iterations where each iteration consists of the addition of a
// contiguous segment of new values.
INDEX_TYPE const valuesToAddPerIteration = curDimStride * ( newDimLength - curDimLength );
INDEX_TYPE const valuesToShiftPerIteration = curDimStride * curDimLength;
INDEX_TYPE const numIterations = ( newSize - curSize ) / valuesToAddPerIteration;
// Iterate backwards over the iterations.
for( INDEX_TYPE i = numIterations - 1; i >= 0; --i )
{
// First shift the values up to make remove for the values of subsequent iterations.
// This step is a no-op on the last iteration (i=0).
INDEX_TYPE const valuesLeftToInsert = valuesToAddPerIteration * i;
T * const startOfShift = ptr + valuesToShiftPerIteration * i;
arrayManipulation::shiftUp( startOfShift, valuesToShiftPerIteration, INDEX_TYPE( 0 ), valuesLeftToInsert );
// Initialize the new values.
T * const startOfNewValues = startOfShift + valuesToShiftPerIteration + valuesLeftToInsert;
for( INDEX_TYPE j = 0; j < valuesToAddPerIteration; ++j )
{
new ( startOfNewValues + j ) T( args ... );
}
}
}
else
{
T * const ptr = this->data();
// The resizing consists of iterations where each iteration consists of the removal of a
// contiguous segment of new values.
INDEX_TYPE const valuesToRemovePerIteration = curDimStride * ( curDimLength - newDimLength );
INDEX_TYPE const valuesToShiftPerIteration = curDimStride * newDimLength;
INDEX_TYPE const numIterations = ( curSize - newSize ) / valuesToRemovePerIteration;
// Iterate over the iterations, skipping the first.
for( INDEX_TYPE i = 1; i < numIterations; ++i )
{
INDEX_TYPE const amountToShift = valuesToRemovePerIteration * i;
T * const startOfShift = ptr + valuesToShiftPerIteration * i;
arrayManipulation::shiftDown( startOfShift,
valuesToShiftPerIteration + amountToShift,
amountToShift, amountToShift );
}
// After the iterations are complete all the values to remove have been moved to the end of the array.
// We destroy them here.
INDEX_TYPE const totalValuesToRemove = valuesToRemovePerIteration * numIterations;
for( INDEX_TYPE i = 0; i < totalValuesToRemove; ++i )
{
ptr[ newSize + i ].~T();
}
}
}
};
/**
* @brief True if the template type is an Array.
*/
template< typename >
constexpr bool isArray = false;
/**
* @tparam T The type contained in the Array.
* @tparam NDIM The number of dimensions in the Array.
* @tparam PERMUTATION The way that the data is layed out in memory.
* @tparam INDEX_TYPE The integral type used as an index.
* @tparam BUFFER_TYPE The type used to manage the underlying allocation.
* @brief Specialization of isArray for the Array class.
*/
template< typename T,
int NDIM,
typename PERMUTATION,
typename INDEX_TYPE,
template< typename > class BUFFER_TYPE >
constexpr bool isArray< Array< T, NDIM, PERMUTATION, INDEX_TYPE, BUFFER_TYPE > > = true;
namespace internal
{
// Since Array expects the BUFFER to only except a single template parameter we need to
// create an alias for a StackBuffer with a given length.
/**
* @struct StackArrayHelper
* @tparam T The type stored in the Array.
* @tparam NDIM The number of dimensions in the Array.
* @tparam PERMUTATION The dimension and permutation of the Array.
* @tparam INDEX_TYPE The integer used to index the Array.
* @tparam LENGTH The capacity of the underlying StackBuffer.
*/
template< typename T,
int NDIM,
typename PERMUTATION,
typename INDEX_TYPE,
int LENGTH >
struct StackArrayHelper
{
/**
* @brief An alias for a StackBuffer with the given length.
* @tparam U The type contained in the StackBuffer.
*/
template< typename U >
using BufferType = StackBuffer< U, LENGTH >;
/// An alias for the Array type.
using type = Array< T, NDIM, PERMUTATION, INDEX_TYPE, BufferType >;
};
} // namespace internal
/**
* @tparam T The type of the values stored in the Array.
* @tparam NDIM The number of dimensions in the Array.
* @tparam PERMUTATION The layout of the data in memory.
* @tparam INDEX_TYPE The integer used for indexing.
* @tparam LENGTH The capacity of the backing c-array.
* @brief An alias for a Array backed by a StackBuffer.
*/
template< typename T,
int NDIM,
typename PERMUTATION,
typename INDEX_TYPE,
int LENGTH >
using StackArray = typename internal::StackArrayHelper< T, NDIM, PERMUTATION, INDEX_TYPE, LENGTH >::type;
} /* namespace LvArray */