-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathArrayOfSetsView.hpp
More file actions
515 lines (442 loc) · 17.4 KB
/
ArrayOfSetsView.hpp
File metadata and controls
515 lines (442 loc) · 17.4 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
/*
* 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 ArrayOfSetsView.hpp
* @brief Contains the implementation of LvArray::ArrayOfSetsView
*/
#pragma once
// Source includes
#include "ArrayOfArraysView.hpp"
#include "arrayManipulation.hpp"
#include "sortedArrayManipulation.hpp"
#include "ArraySlice.hpp"
#include "typeManipulation.hpp"
namespace LvArray
{
/**
* @class ArrayOfSetsView
* @brief This class provides a view into an array of sets like object.
* @tparam T the type stored in the arrays.
* @tparam INDEX_TYPE the integer to use for indexing.
*
* When INDEX_TYPE is const m_offsets is not touched when copied between memory spaces.
* INDEX_TYPE should always be const since ArrayOfSetsview is not allowed to modify the offsets.
*
* When T is const and INDEX_TYPE is const you cannot insert or remove from the View
* and neither the offsets, sizes, or values are touched when copied between memory spaces.
*/
template< typename T,
typename INDEX_TYPE,
template< typename > class BUFFER_TYPE >
class ArrayOfSetsView : protected ArrayOfArraysView< T, INDEX_TYPE, std::is_const< T >::value, BUFFER_TYPE >
{
protected:
/// Alias for the parent class
using ParentClass = ArrayOfArraysView< T, INDEX_TYPE, std::is_const< T >::value, BUFFER_TYPE >;
/// Since INDEX_TYPE should always be const we need an alias for the non const version.
using INDEX_TYPE_NC = typename ParentClass::INDEX_TYPE_NC;
using typename ParentClass::SIZE_TYPE;
public:
using typename ParentClass::ValueType;
using typename ParentClass::IndexType;
using typename ParentClass::value_type;
using typename ParentClass::size_type;
/**
* @name Constructors, destructor and assignment operators
*/
///@{
/**
* @brief A constructor to create an uninitialized ArrayOfSetsView.
* @note An uninitialized ArrayOfSetsView should not be used until it is assigned to.
*/
ArrayOfSetsView() = default;
/**
* @brief Default copy constructor. Performs a shallow copy and calls the
* chai::ManagedArray copy constructor.
*/
inline
ArrayOfSetsView( ArrayOfSetsView const & ) = default;
/**
* @brief Default move constructor.
*/
inline
ArrayOfSetsView( ArrayOfSetsView && ) = default;
/**
* @brief Construct a new ArrayOfArraysView from the given buffers.
* @param numArrays The number of arrays.
* @param offsets The offsets buffer, of size @p numArrays + 1.
* @param sizes The sizes buffer, of size @p numArrays.
* @param values The values buffer, of size @p offsets[ numArrays ].
*/
LVARRAY_HOST_DEVICE constexpr inline
ArrayOfSetsView( INDEX_TYPE const numArrays,
BUFFER_TYPE< INDEX_TYPE > const & offsets,
BUFFER_TYPE< SIZE_TYPE > const & sizes,
BUFFER_TYPE< T > const & values ):
ParentClass( numArrays, offsets, sizes, values )
{}
/**
* @brief Default copy assignment operator, this does a shallow copy.
* @return *this.
*/
inline
ArrayOfSetsView & operator=( ArrayOfSetsView const & ) = default;
/**
* @brief Default move assignment operator, this does a shallow copy.
* @return *this.
*/
inline
ArrayOfSetsView & operator=( ArrayOfSetsView && ) = default;
///@}
/**
* @name ArrayOfSetsView and ArrayOfArraysView creation methods
*/
///@{
/**
* @return Return a new ArrayOfSetsView< T, INDEX_TYPE const >.
*/
LVARRAY_HOST_DEVICE constexpr inline
ArrayOfSetsView< T, INDEX_TYPE const, BUFFER_TYPE >
toView() const
{
return ArrayOfSetsView< T, INDEX_TYPE const, BUFFER_TYPE >( size(),
this->m_offsets,
this->m_sizes,
this->m_values );
}
/**
* @return Return a new ArrayOfSetsView< T const, INDEX_TYPE const >.
*/
LVARRAY_HOST_DEVICE constexpr inline
ArrayOfSetsView< T const, INDEX_TYPE const, BUFFER_TYPE >
toViewConst() const
{
return ArrayOfSetsView< T const, INDEX_TYPE const, BUFFER_TYPE >( size(),
this->m_offsets,
this->m_sizes,
this->m_values );
}
/**
* @return Return a new ArrayOfArraysView< T const, INDEX_TYPE const, true >.
*/
LVARRAY_HOST_DEVICE constexpr inline
ArrayOfArraysView< T const, INDEX_TYPE const, true, BUFFER_TYPE >
toArrayOfArraysView() const
{
return ArrayOfArraysView< T const, INDEX_TYPE const, true, BUFFER_TYPE >( size(),
this->m_offsets,
this->m_sizes,
this->m_values );
}
///@}
/**
* @name Attribute querying methods
*/
///@{
using ParentClass::size;
/**
* @return Return the size of the given set.
* @param i The set to get the size of.
*/
LVARRAY_HOST_DEVICE constexpr inline
INDEX_TYPE_NC sizeOfSet( INDEX_TYPE const i ) const
{ return ParentClass::sizeOfArray( i ); }
using ParentClass::capacity;
/**
* @return Return the capacity of the given set.
* @param i The set to get the capacity of.
*/
LVARRAY_HOST_DEVICE constexpr inline
INDEX_TYPE_NC capacityOfSet( INDEX_TYPE const i ) const
{ return ParentClass::capacityOfArray( i ); }
using ParentClass::valueCapacity;
/**
* @return Return true iff the given set contains the given value.
* @param i the set to search.
* @param value the value to search for.
*/
LVARRAY_HOST_DEVICE inline
bool contains( INDEX_TYPE const i, T const & value ) const
{
ARRAYOFARRAYS_CHECK_BOUNDS( i );
INDEX_TYPE const setSize = sizeOfSet( i );
T const * const setValues = (*this)[ i ];
return sortedArrayManipulation::contains( setValues, setSize, value );
}
/**
* @brief Verify that the capacity of each set is greater than or equal to the
* size and that each set is sorted unique.
* @note The is intended for debugging.
*/
void consistencyCheck() const
{
INDEX_TYPE const numSets = size();
for( INDEX_TYPE_NC i = 0; i < numSets; ++i )
{
LVARRAY_ERROR_IF_GT( sizeOfSet( i ), capacityOfSet( i ) );
T * const setValues = getSetValues( i );
INDEX_TYPE const numValues = sizeOfSet( i );
LVARRAY_ERROR_IF( !sortedArrayManipulation::isSortedUnique( setValues, setValues + numValues ),
"Values should be sorted and unique!" );
}
}
///@}
/**
* @name Methods that provide access to the data
*/
///@{
/**
* @return Return an ArraySlice1d<T const> (pointer to const) to the values of the given array.
* @param i The set to access.
*/
LVARRAY_HOST_DEVICE constexpr inline
ArraySlice< T const, 1, 0, INDEX_TYPE_NC > operator[]( INDEX_TYPE const i ) const
{ return ParentClass::operator[]( i ); }
/**
* @return Return a const reference to the value at the given position in the given array.
* @param i The set to access.
* @param j The index within the set to access.
*/
LVARRAY_HOST_DEVICE constexpr inline
T const & operator()( INDEX_TYPE const i, INDEX_TYPE const j ) const
{ return ParentClass::operator()( i, j ); }
///@}
/**
* @name Methods that modify the size of an inner set.
*/
///@{
/**
* @brief Insert a value into the given set.
* @param i the set to insert into.
* @param value the value to insert.
* @return True iff the value was inserted (the set did not already contain the value).
* @pre Since the ArrayOfSetsview can't do reallocation or shift the offsets it is
* up to the user to ensure that the given set has enough space for the new entries.
*/
LVARRAY_HOST_DEVICE inline
bool insertIntoSet( INDEX_TYPE const i, T const & value ) const
{ return insertIntoSetImpl( i, value, CallBacks( *this, i ) ); }
/**
* @tparam ITER An iterator type.
* @brief Inserts multiple values into the given set.
* @param i The set to insert into.
* @param first An iterator to the first value to insert.
* @param last An iterator to the end of the values to insert.
* @return The number of values inserted.
* @pre The values to insert [ @p first, @p last ) must be sorted and contain no duplicates.
* @pre Since the ArrayOfSetsView can't do reallocation or shift the offsets it is
* up to the user to ensure that the given set has enough space for the new entries.
*/
template< typename ITER >
LVARRAY_HOST_DEVICE inline
INDEX_TYPE_NC insertIntoSet( INDEX_TYPE const i, ITER const first, ITER const last ) const
{ return insertIntoSetImpl( i, first, last, CallBacks( *this, i ) ); }
/**
* @brief Remove a value from the given set.
* @param i The set to remove from.
* @param value The value to remove.
* @return True iff the value was removed (the set previously contained the value).
*/
LVARRAY_HOST_DEVICE inline
bool removeFromSet( INDEX_TYPE const i, T const & value ) const
{ return removeFromSetImpl( i, value, CallBacks( *this, i ) ); }
/**
* @tparam ITER An iterator type.
* @brief Removes multiple values from the given set.
* @param i The set to remove from.
* @param first An iterator to the first value to remove.
* @param last An iterator to the end of the values to remove.
* @return The number of values removed.
* @pre The values to remove [ @p first, @p last ) must be sorted and contain no duplicates.
*/
template< typename ITER >
LVARRAY_HOST_DEVICE inline
INDEX_TYPE_NC removeFromSet( INDEX_TYPE const i, ITER const first, ITER const last ) const
{ return removeFromSetImpl( i, first, last, CallBacks( *this, i ) ); }
///@}
/**
* @name Methods dealing with memory spaces
*/
///@{
/**
* @brief Move this ArrayOfSets to the given memory space.
* @param space The memory space to move to.
* @param touch If true touch the values, sizes and offsets in the new space.
* @note When moving to the GPU since the offsets can't be modified on device they are not touched.
* @note This is just a wrapper around the ArrayOfArraysView method. The reason
* it isn't pulled in with a @c using statement is that it is detected using
* IS_VALID_EXPRESSION and this fails with NVCC.
*/
void move( MemorySpace const space, bool const touch=true ) const
{ return ParentClass::move( space, touch ); }
///@}
using ParentClass::getSizes;
using ParentClass::getOffsets;
using ParentClass::getValues;
protected:
/**
* @brief Protected constructor to be used by parent classes.
* @note The unused boolean parameter is to distinguish this from the default constructor.
*/
ArrayOfSetsView( bool ):
ParentClass( true )
{}
/**
* @return Return an ArraySlice1d to the values of the given array.
* @param i the array to access.
* @note Protected because it returns a non-const pointer.
*/
LVARRAY_HOST_DEVICE constexpr inline
ArraySlice< T, 1, 0, INDEX_TYPE_NC > getSetValues( INDEX_TYPE const i ) const
{ return ParentClass::operator[]( i ); }
/**
* @name Methods to be used by derived classes
*/
///@{
/**
* @brief Helper function to insert a value into the given set.
* @tparam CALLBACKS type of the call-back helper class.
* @param i the set to insert into.
* @param value the value to insert.
* @param cbacks call-back helper class used with the sortedArrayManipulation routines.
* @return True iff the value was inserted (the set did not already contain the value).
*/
template< typename CALLBACKS >
LVARRAY_HOST_DEVICE inline
bool insertIntoSetImpl( INDEX_TYPE const i, T const & value, CALLBACKS && cbacks ) const
{
ARRAYOFARRAYS_CHECK_BOUNDS( i );
INDEX_TYPE const setSize = sizeOfSet( i );
T * const setValues = getSetValues( i );
bool const success = sortedArrayManipulation::insert( setValues, setSize, value, std::move( cbacks ) );
this->m_sizes[i] += success;
return success;
}
/**
* @tparam ITER An iterator type.
* @tparam CALLBACKS type of the call-back helper class.
* @brief Inserts multiple values into the given set.
* @param i The set to insert into.
* @param first An iterator to the first value to insert.
* @param last An iterator to the end of the values to insert.
* @param cbacks Helper class used with the sortedArrayManipulation routines.
* @return The number of values inserted.
* @note The values to insert [ @p first, @p last ) must be sorted and contain no duplicates.
*/
template< typename ITER, typename CALLBACKS >
LVARRAY_HOST_DEVICE inline
INDEX_TYPE_NC insertIntoSetImpl( INDEX_TYPE const i,
ITER const first,
ITER const last,
CALLBACKS && cbacks ) const
{
ARRAYOFARRAYS_CHECK_BOUNDS( i );
INDEX_TYPE const setSize = sizeOfSet( i );
T * const setValues = getSetValues( i );
INDEX_TYPE const nInserted = sortedArrayManipulation::insert( setValues,
setSize,
first,
last,
std::forward< CALLBACKS >( cbacks ) );
this->m_sizes[i] += nInserted;
return nInserted;
}
/**
* @brief Helper function to remove a value from the given set.
* @tparam CALLBACKS type of the call-back helper class.
* @param i the set to remove from.
* @param value the value to remove.
* @param cbacks call-back helper class used with the sortedArrayManipulation routines.
* @return True iff the value was removed (the set contained the value).
*/
template< typename CALLBACKS >
LVARRAY_HOST_DEVICE inline
bool removeFromSetImpl( INDEX_TYPE const i, T const & value, CALLBACKS && cbacks ) const
{
ARRAYOFARRAYS_CHECK_BOUNDS( i );
INDEX_TYPE const setSize = sizeOfSet( i );
T * const setValues = getSetValues( i );
bool const success = sortedArrayManipulation::remove( setValues, setSize, value, std::move( cbacks ) );
this->m_sizes[i] -= success;
return success;
}
/**
* @tparam ITER An iterator type.
* @tparam CALLBACKS type of the call-back helper class.
* @brief Removes multiple values from the given set.
* @param i The set to remove from.
* @param first An iterator to the first value to remove.
* @param last An iterator to the end of the values to remove.
* @param cbacks Helper class used with the sortedArrayManipulation routines.
* @return The number of values removed.
* @note The values to remove [ @p first, @p last ) must be sorted and contain no duplicates.
*/
template< typename ITER, typename CALLBACKS >
LVARRAY_HOST_DEVICE inline
INDEX_TYPE_NC removeFromSetImpl( INDEX_TYPE const i,
ITER const first,
ITER const last,
CALLBACKS && cbacks ) const
{
ARRAYOFARRAYS_CHECK_BOUNDS( i );
INDEX_TYPE const setSize = sizeOfSet( i );
T * const setValues = getSetValues( i );
INDEX_TYPE const nRemoved = sortedArrayManipulation::remove( setValues,
setSize,
first,
last,
std::forward< CALLBACKS >( cbacks ) );
this->m_sizes[i] -= nRemoved;
return nRemoved;
}
///@}
private:
/**
* @class CallBacks
* @brief This class provides the callbacks for the sortedArrayManipulation routines.
*/
class CallBacks : public sortedArrayManipulation::CallBacks< T >
{
public:
/**
* @brief Constructor.
* @param aos the ArrayOfSetsView this CallBacks is associated with.
* @param i the set this CallBacks is associated with.
*/
LVARRAY_HOST_DEVICE inline
CallBacks( ArrayOfSetsView const & aos, INDEX_TYPE const i ):
m_aos( aos ),
m_indexOfSet( i )
{}
/**
* @brief Callback signaling that the size of the set has increased.
* @param curPtr the current pointer to the array.
* @param nToAdd the increase in the size.
* @note This method doesn't actually change the size, it just checks that the new
* size doesn't exceed the capacity since the ArrayOfSetsView can't do allocation.
* @return a pointer to the sets values.
*/
LVARRAY_HOST_DEVICE inline
T * incrementSize( T * const curPtr, INDEX_TYPE const nToAdd ) const
{
LVARRAY_UNUSED_VARIABLE( curPtr );
#ifdef LVARRAY_BOUNDS_CHECK
LVARRAY_ERROR_IF_GT_MSG( m_aos.sizeOfSet( m_indexOfSet ) + nToAdd, m_aos.capacityOfSet( m_indexOfSet ),
"ArrayOfSetsView cannot do reallocation." );
#else
LVARRAY_DEBUG_VAR( nToAdd );
#endif
return m_aos.getSetValues( m_indexOfSet );
}
private:
/// The ArrayOfSetsView the call back is associated with.
ArrayOfSetsView const & m_aos;
/// The index of the set the call back is associated with.
INDEX_TYPE const m_indexOfSet;
};
};
} // namespace LvArray