-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy patharrayManipulation.hpp
More file actions
550 lines (494 loc) · 16.6 KB
/
arrayManipulation.hpp
File metadata and controls
550 lines (494 loc) · 16.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
/*
* 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 arrayManipulation.hpp
* @brief Contains functions for manipulating a contiguous array of values.
*/
#pragma once
// Source includes
#include "limits.hpp"
#include "Macros.hpp"
#include "typeManipulation.hpp"
// System includes
#include <cstring>
#ifdef LVARRAY_BOUNDS_CHECK
/**
* @brief Check that @p index is a valid into into the array.
* @param index The index to check.
* @note This is only active when LVARRAY_BOUNDS_CHECK is defined.
*/
#define ARRAYMANIPULATION_CHECK_BOUNDS( index ) \
LVARRAY_ERROR_IF( !isPositive( index ) || index >= size, \
"Array Bounds Check Failed: index=" << index << " size()=" << size )
/**
* @brief Check that @p index is a valid insertion position in the array.
* @param index The index to check.
* @note This is only active when LVARRAY_BOUNDS_CHECK is defined.
*/
#define ARRAYMANIPULATION_CHECK_INSERT_BOUNDS( index ) \
LVARRAY_ERROR_IF( !isPositive( index ) || index > size, \
"Array Bounds Insert Check Failed: index=" << index << " size()=" << size )
#else // LVARRAY_BOUNDS_CHECK
/**
* @brief Check that @p index is a valid into into the array.
* @param index The index to check.
* @note This is only active when LVARRAY_BOUNDS_CHECK is defined.
*/
#define ARRAYMANIPULATION_CHECK_BOUNDS( index )
/**
* @brief Check that @p index is a valid insertion position in the array.
* @param index The index to check.
* @note This is only active when LVARRAY_BOUNDS_CHECK is defined.
*/
#define ARRAYMANIPULATION_CHECK_INSERT_BOUNDS( index )
#endif // LVARRAY_BOUNDS_CHECK
namespace LvArray
{
/**
* @brief Contains functions for operating on a contiguous array of values.
* @details Most functions accept a pointer and a size as the first two arguments. Values
* in this range are expected to be in a valid state. Values past the end of the array
* are expected to be uninitialized. Functions that increase the size of the array
* expect the array to have a large enough capacity to handle the increase.
*/
namespace arrayManipulation
{
/**
* @tparam INDEX_TYPE the integral type to check.
* @return Return true iff @p i is greater than or equal to zero.
* @param i the value to check.
*/
template< typename INDEX_TYPE >
LVARRAY_HOST_DEVICE inline constexpr
typename std::enable_if< std::is_signed< INDEX_TYPE >::value, bool >::type
isPositive( INDEX_TYPE const i )
{ return i >= 0; }
/**
* @tparam INDEX_TYPE the integral type to check.
* @return Returns true. This specialization for unsigned types avoids compiler warnings.
*/
template< typename INDEX_TYPE >
LVARRAY_HOST_DEVICE inline constexpr
typename std::enable_if< !std::is_signed< INDEX_TYPE >::value, bool >::type
isPositive( INDEX_TYPE )
{ return true; }
/**
* @tparam ITER An iterator type.
* @return The distance between two non-random access iterators.
* @param first The iterator to the beginning of the range.
* @param last The iterator to the end of the range.
*/
DISABLE_HD_WARNING
template< typename ITER >
inline constexpr LVARRAY_HOST_DEVICE
typename std::iterator_traits< ITER >::difference_type
iterDistance( ITER first, ITER const last, std::input_iterator_tag )
{
typename std::iterator_traits< ITER >::difference_type n = 0;
while( first != last )
{
++first;
++n;
}
return n;
}
/**
* @tparam ITER An iterator type.
* @return The distance between two random access iterators.
* @param first The iterator to the beginning of the range.
* @param last The iterator to the end of the range.
*/
DISABLE_HD_WARNING
template< typename RandomAccessIterator >
inline constexpr LVARRAY_HOST_DEVICE
typename std::iterator_traits< RandomAccessIterator >::difference_type
iterDistance( RandomAccessIterator first, RandomAccessIterator last, std::random_access_iterator_tag )
{ return last - first; }
/**
* @tparam ITER An iterator type.
* @return The distance between two iterators.
* @param first The iterator to the beginning of the range.
* @param last The iterator to the end of the range.
*/
DISABLE_HD_WARNING
template< typename ITER >
inline constexpr LVARRAY_HOST_DEVICE
typename std::iterator_traits< ITER >::difference_type
iterDistance( ITER const first, ITER const last )
{ return iterDistance( first, last, typename std::iterator_traits< ITER >::iterator_category() ); }
/**
* @tparam T the storage type of the array.
* @brief Destory the values in the array.
* @param ptr Pointer to the array.
* @param size The size of the array.
*/
DISABLE_HD_WARNING
template< typename T >
LVARRAY_HOST_DEVICE inline
void destroy( T * const LVARRAY_RESTRICT ptr,
std::ptrdiff_t const size )
{
LVARRAY_ASSERT( ptr != nullptr || size == 0 );
if( !std::is_trivially_destructible< T >::value )
{
for( std::ptrdiff_t i = 0; i < size; ++i )
{
ptr[ i ].~T();
}
}
}
/**
* @tparam ITER An iterator type.
* @tparam T the storage type of the array.
* @brief Copy construct values from the source to the destination.
* @param first Iterator to the first value to copy.
* @param last Iterator to the end of the values to copy.
* @param dst Pointer to the destination array, must be uninitialized memory.
*/
DISABLE_HD_WARNING
template< typename ITER, typename T >
LVARRAY_HOST_DEVICE inline
void uninitializedCopy( ITER first,
ITER const & last,
T * LVARRAY_RESTRICT dst )
{
LVARRAY_ASSERT( dst != nullptr || first == last );
while( first != last )
{
new ( dst ) T( *first );
++dst;
++first;
}
}
/**
* @tparam T the storage type of the array.
* @brief Move construct values from the source to the destination.
* @param dst pointer to the destination array, must be uninitialized memory.
* @param size The number of values to copy.
* @param src pointer to the source array.
*/
DISABLE_HD_WARNING
template< typename T >
LVARRAY_HOST_DEVICE inline
void uninitializedMove( T * const LVARRAY_RESTRICT dst,
std::ptrdiff_t const size,
T * const LVARRAY_RESTRICT src )
{
LVARRAY_ASSERT( dst != nullptr || size == 0 );
LVARRAY_ASSERT( isPositive( size ) );
LVARRAY_ASSERT( src != nullptr || size == 0 );
for( std::ptrdiff_t i = 0; i < size; ++i )
{
new (dst + i) T( std::move( src[ i ] ) );
}
}
/**
* @tparam T the storage type of the array.
* @brief Shift values down into uninitialized memory.
* @param ptr Pointer to the begining of the shift, values before this must be uninitialized.
* @param size number of values to shift.
* @param amount the amount to shift by.
*/
DISABLE_HD_WARNING
template< typename T >
LVARRAY_HOST_DEVICE inline
void uninitializedShiftDown( T * const LVARRAY_RESTRICT ptr,
std::ptrdiff_t const size,
std::ptrdiff_t const amount )
{
LVARRAY_ASSERT( ptr != nullptr || size == 0 );
LVARRAY_ASSERT( isPositive( size ) );
LVARRAY_ASSERT( isPositive( amount ) );
if( amount == 0 )
return;
for( std::ptrdiff_t j = 0; j < size; ++j )
{
new ( ptr + j - amount ) T( std::move( ptr[ j ] ) );
ptr[ j ].~T();
}
}
/**
* @tparam T the storage type of the array.
* @brief Shift values up into uninitialized memory.
* @param ptr Pointer to the begining of the shift.
* @param size number of values to shift, values after this must be uninitialized.
* @param amount the amount to shift by.
*/
DISABLE_HD_WARNING
template< typename T >
LVARRAY_HOST_DEVICE inline
void uninitializedShiftUp( T * const LVARRAY_RESTRICT ptr,
std::ptrdiff_t const size,
std::ptrdiff_t const amount )
{
LVARRAY_ASSERT( ptr != nullptr || size == 0 );
LVARRAY_ASSERT( isPositive( size ) );
LVARRAY_ASSERT( isPositive( amount ) );
if( amount == 0 )
return;
for( std::ptrdiff_t j = size - 1; j >= 0; --j )
{
new ( ptr + amount + j ) T( std::move( ptr[ j ] ) );
ptr[ j ].~T();
}
}
/**
* @tparam T the storage type of the array.
* @tparam ARGS the types of the arguments to forward to the constructor.
* @brief Resize the give array.
* @param ptr Pointer to the array.
* @param size The size of the array.
* @param newSize the new size.
* @param args the arguments to forward to construct any new elements with.
*/
DISABLE_HD_WARNING
template< typename T, typename ... ARGS >
LVARRAY_HOST_DEVICE inline
void resize( T * const LVARRAY_RESTRICT ptr,
std::ptrdiff_t const size,
std::ptrdiff_t const newSize,
ARGS && ... args )
{
LVARRAY_ASSERT( ptr != nullptr || (size == 0 && newSize == 0) );
LVARRAY_ASSERT( isPositive( size ) );
LVARRAY_ASSERT( isPositive( newSize ) );
// Delete things between newSize and size.
destroy( ptr + newSize, size - newSize );
// Initialize things between size and newSize.
if( sizeof ... ( ARGS ) == 0 && std::is_trivially_default_constructible< T >::value )
{
if( newSize - size > 0 )
{
std::size_t const sizeDiff = integerConversion< std::size_t >( newSize - size );
#if !defined(__clang__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Warray-bounds"
#pragma GCC diagnostic ignored "-Wstringop-overflow="
#endif
memset( reinterpret_cast< void * >( ptr + size ), 0, ( sizeDiff ) * sizeof( T ) );
#if !defined(__clang__)
#pragma GCC diagnostic pop
#endif
}
}
else
{
// Use std::size_t so that when GCC optimizes this it doesn't produce sign warnings.
for( std::size_t i = size; i < std::size_t( newSize ); ++i )
{
new ( ptr + i ) T( std::forward< ARGS >( args )... );
}
}
}
/**
* @tparam T the storage type of the array.
* @brief Shift the values in the array at or above the given position up by the given amount.
* New uninitialized values take their place.
* @param ptr Pointer to the array.
* @param size The size of the array.
* @param index the index at which to begin the shift.
* @param n the number of places to shift.
*/
DISABLE_HD_WARNING
template< typename T >
LVARRAY_HOST_DEVICE inline
void shiftUp( T * const LVARRAY_RESTRICT ptr,
std::ptrdiff_t const size,
std::ptrdiff_t const index,
std::ptrdiff_t const n )
{
LVARRAY_ASSERT( ptr != nullptr || (size == 0 && n == 0) );
LVARRAY_ASSERT( isPositive( size ) );
LVARRAY_ASSERT( isPositive( n ) );
ARRAYMANIPULATION_CHECK_INSERT_BOUNDS( index );
if( n == 0 )
return;
// Move the existing values up by n.
for( std::ptrdiff_t i = size; i > index; --i )
{
std::ptrdiff_t const curIndex = i - 1;
new ( ptr + curIndex + n ) T( std::move( ptr[ curIndex ] ) );
}
// Delete the values moved out of.
std::ptrdiff_t const bounds = (index + n < size) ? index + n : size;
destroy( ptr + index, bounds - index );
}
/**
* @tparam T the storage type of the array.
* @brief Shift the values in the array at or above the given position down by the given amount overwriting
* the existing values. The n entries at the end of the array are not destroyed.
* @param ptr Pointer to the array.
* @param size The size of the array.
* @param index the index at which to begin the shift.
* @param n the number of places to shift.
*/
DISABLE_HD_WARNING
template< typename T >
LVARRAY_HOST_DEVICE inline
void shiftDown( T * const LVARRAY_RESTRICT ptr,
std::ptrdiff_t const size,
std::ptrdiff_t const index,
std::ptrdiff_t const n )
{
ARRAYMANIPULATION_CHECK_INSERT_BOUNDS( index );
LVARRAY_ASSERT( ptr != nullptr || (size == 0 && n == 0) );
LVARRAY_ASSERT( isPositive( size ) );
LVARRAY_ASSERT( isPositive( n ) );
LVARRAY_ASSERT( index >= n );
if( n == 0 )
return;
// Move the existing down by n.
for( std::ptrdiff_t i = index; i < size; ++i )
{
ptr[i - n] = std::move( ptr[i] );
}
}
/**
* @tparam T the storage type of the array.
* @brief Shift the values in the array at or above the given position down by the given amount overwriting
* the existing values. The n entries at the end of the array are then destroyed.
* @param ptr Pointer to the array.
* @param size The size of the array.
* @param index the index at which to begin the shift.
* @param n the number of places to shift.
*/
DISABLE_HD_WARNING
template< typename T >
LVARRAY_HOST_DEVICE inline
void erase( T * const LVARRAY_RESTRICT ptr,
std::ptrdiff_t const size,
std::ptrdiff_t const index,
std::ptrdiff_t const n=1 )
{
LVARRAY_ASSERT( isPositive( n ) );
if( n == 0 )
return;
ARRAYMANIPULATION_CHECK_BOUNDS( index );
ARRAYMANIPULATION_CHECK_BOUNDS( index + n - 1 );
shiftDown( ptr, size, std::ptrdiff_t( index + n ), n );
// Delete the values that were moved out of at the end of the array.
destroy( ptr + size - n, n );
}
/**
* @brief Append the to the array constructing the new value in place.
* @tparam T The storage type of the array.
* @tparam ARGS Variadic pack of types to construct T with, the types of @p args.
* @param ptr Pointer to the array.
* @param size The size of the array.
* @param args The arguments to forward to construct the new value.
*/
DISABLE_HD_WARNING
template< typename T, typename ... ARGS >
LVARRAY_HOST_DEVICE inline
void emplaceBack( T * const LVARRAY_RESTRICT ptr,
std::ptrdiff_t const size,
ARGS && ... args )
{
LVARRAY_ASSERT( ptr != nullptr );
LVARRAY_ASSERT( isPositive( size ) );
new ( ptr + size ) T( std::forward< ARGS >( args ) ... );
}
/**
* @brief Append the given values to the array.
* @tparam T The storage type of the array.
* @tparam ITER The type of the iterators @p first and @p last.
* @param ptr Pointer to the array.
* @param size The size of the array.
* @param first An iterator to the first value to append.
* @param last An iterator to the end of the values to append.
* @return The number of values appended.
*/
DISABLE_HD_WARNING
template< typename T, typename ITER >
LVARRAY_HOST_DEVICE inline
std::ptrdiff_t append( T * const LVARRAY_RESTRICT ptr,
std::ptrdiff_t const size,
ITER first,
ITER const last )
{
LVARRAY_ASSERT( ptr != nullptr || (size == 0 && first == last) );
LVARRAY_ASSERT( isPositive( size ) );
std::ptrdiff_t i = 0;
while( first != last )
{
new( ptr + size + i ) T( *first );
++first;
++i;
}
return i;
}
/**
* @brief Insert into the array constructing the new value in place.
* @tparam T The storage type of the array.
* @tparam ARGS Variadic pack of types to construct T with, the types of @p args.
* @param ptr Pointer to the array.
* @param size The size of the array.
* @param index The position to insert at.
* @param args The arguments to forward to construct the new value.
*/
DISABLE_HD_WARNING
template< typename T, typename ... ARGS >
LVARRAY_HOST_DEVICE inline
void emplace( T * const LVARRAY_RESTRICT ptr,
std::ptrdiff_t const size,
std::ptrdiff_t const index,
ARGS && ... args )
{
LVARRAY_ASSERT( ptr != nullptr );
LVARRAY_ASSERT( isPositive( size ) );
ARRAYMANIPULATION_CHECK_INSERT_BOUNDS( index );
// Create space for the new value.
shiftUp( ptr, size, index, std::ptrdiff_t( 1 ) );
new ( ptr + index ) T( std::forward< ARGS >( args ) ... );
}
/**
* @brief Insert the given values into the array at the given position.
* @tparam T The storage type of the array.
* @tparam ITERATOR An iterator type, the type of @p first.
* @param ptr Pointer to the array.
* @param size The size of the array.
* @param index The position to insert the value at.
* @param first Iterator to the first value to insert.
* @param n The number of values to insert.
*/
DISABLE_HD_WARNING
template< typename T, typename ITERATOR >
LVARRAY_HOST_DEVICE inline
void insert( T * const LVARRAY_RESTRICT ptr,
std::ptrdiff_t const size,
std::ptrdiff_t const index,
ITERATOR first,
std::ptrdiff_t const n )
{
LVARRAY_ASSERT( ptr != nullptr );
LVARRAY_ASSERT( isPositive( size ) );
ARRAYMANIPULATION_CHECK_INSERT_BOUNDS( index );
shiftUp( ptr, size, index, n );
for( std::ptrdiff_t i = 0; i < n; ++i )
{
new ( ptr + index + i ) T( *first );
++first;
}
}
/**
* @tparam T the storage type of the array.
* @brief Destroy the value at the end of the array.
* @param ptr Pointer to the array.
* @param size The size of the array.
*/
DISABLE_HD_WARNING
template< typename T >
LVARRAY_HOST_DEVICE inline
void popBack( T * const LVARRAY_RESTRICT ptr,
std::ptrdiff_t const size )
{
LVARRAY_ASSERT( ptr != nullptr );
LVARRAY_ASSERT( size > 0 );
ptr[ size - 1 ].~T();
}
} // namespace arrayManipulation
} // namespace LvArray