LvArray
ArrayOfSets.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2021, Lawrence Livermore National Security, LLC and LvArray contributors.
3  * All rights reserved.
4  * See the LICENSE file for details.
5  * SPDX-License-Identifier: (BSD-3-Clause)
6  */
7 
13 #pragma once
14 
15 #include "ArrayOfSetsView.hpp"
16 
17 namespace LvArray
18 {
19 
20 // Forward declaration of the ArrayOfArrays class so that we can define the assimilate method.
21 template< typename T, typename INDEX_TYPE, template< typename > class BUFFER_TYPE >
22 class ArrayOfArrays;
23 
24 
31 template< typename T,
32  typename INDEX_TYPE,
33  template< typename > class BUFFER_TYPE >
34 class ArrayOfSets : protected ArrayOfSetsView< T, INDEX_TYPE, BUFFER_TYPE >
35 {
36 
39 
40 public:
41  using typename ParentClass::ValueType;
42  using typename ParentClass::IndexType;
43  using typename ParentClass::value_type;
44  using typename ParentClass::size_type;
45 
49 
56  inline
57  ArrayOfSets( INDEX_TYPE const nsets=0, INDEX_TYPE defaultSetCapacity=0 ):
58  ParentClass( true )
59  {
60  resize( nsets, defaultSetCapacity );
61  setName( "" );
62  }
63 
68  inline
69  ArrayOfSets( ArrayOfSets const & src ):
70  ParentClass( true )
71  { *this = src; }
72 
76  inline
77  ArrayOfSets( ArrayOfSets && ) = default;
78 
82  inline
84  { ParentClass::free(); }
85 
87 
91 
98  inline
100  {
101  ParentClass::setEqualTo( src.m_numArrays,
102  src.m_offsets[ src.m_numArrays ],
103  src.m_offsets,
104  src.m_sizes,
105  src.m_values );
106  return *this;
107  }
108 
114  inline
116  {
118  ParentClass::operator=( std::move( src ) );
119  return *this;
120  }
121 
128  template< typename POLICY >
129  inline
132  {
134  ParentClass::assimilate( reinterpret_cast< ParentClass && >( src ) );
135 
136  INDEX_TYPE const numSets = size();
139  this->m_offsets,
140  this->m_sizes,
141  this->m_values );
142  BUFFER_TYPE< INDEX_TYPE > const sizes = this->m_sizes;
143 
144  if( desc == sortedArrayManipulation::UNSORTED_NO_DUPLICATES )
145  {
146  RAJA::forall< POLICY >( RAJA::TypedRangeSegment< INDEX_TYPE >( 0, numSets ),
147  [view] LVARRAY_HOST_DEVICE ( INDEX_TYPE const i )
148  {
149  ArraySlice< T, 1, 0, INDEX_TYPE > const setValues = view[i];
150  sortedArrayManipulation::makeSorted( setValues.begin(), setValues.end() );
151  } );
152  }
153  else if( desc == sortedArrayManipulation::SORTED_WITH_DUPLICATES )
154  {
155  RAJA::forall< POLICY >( RAJA::TypedRangeSegment< INDEX_TYPE >( 0, numSets ),
156  [view, sizes] LVARRAY_HOST_DEVICE ( INDEX_TYPE const i )
157  {
158  ArraySlice< T, 1, 0, INDEX_TYPE > const setValues = view[i];
159  INDEX_TYPE const numUniqueValues = sortedArrayManipulation::removeDuplicates( setValues.begin(), setValues.end() );
160  arrayManipulation::resize< T >( setValues, setValues.size(), numUniqueValues );
161  sizes[ i ] = numUniqueValues;
162  } );
163  }
164  else if( desc == sortedArrayManipulation::UNSORTED_WITH_DUPLICATES )
165  {
166  RAJA::forall< POLICY >( RAJA::TypedRangeSegment< INDEX_TYPE >( 0, numSets ),
167  [view, sizes] LVARRAY_HOST_DEVICE ( INDEX_TYPE const i )
168  {
169  ArraySlice< T, 1, 0, INDEX_TYPE > const setValues = view[ i ];
170  INDEX_TYPE const numUniqueValues = sortedArrayManipulation::makeSortedUnique( setValues.begin(), setValues.end() );
171  arrayManipulation::resize< T >( setValues, setValues.size(), numUniqueValues );
172  sizes[ i ] = numUniqueValues;
173  } );
174  }
175 
176 #ifdef ARRAY_BOUNDS_CHECK
178 #endif
179  }
180 
182 
184 
188 
196  constexpr inline
198  toView() const &
199  { return ParentClass::toView(); }
200 
208  constexpr inline
210  toView() const && = delete;
211 
218  constexpr inline
220  toViewConst() const &
221  { return ParentClass::toViewConst(); }
222 
230  constexpr inline
232  toViewConst() const && = delete;
233 
240  constexpr inline
244 
252  constexpr inline
254  toArrayOfArraysView() const && = delete;
255 
257 
261 
269  inline
270  INDEX_TYPE size() const
271  { return ParentClass::size(); }
272 
274  using ParentClass::capacity;
277  using ParentClass::contains;
279 
281 
282 
286 
288  using ParentClass::operator[];
289  using ParentClass::operator();
290 
292 
296 
298  using ParentClass::reserve;
300 
307  void resize( INDEX_TYPE const newSize, INDEX_TYPE const defaultArrayCapacity=0 )
308  { return ParentClass::resize( newSize, defaultArrayCapacity ); }
309 
310  using ParentClass::compress;
311 
313 
317 
323  inline
324  void appendSet( INDEX_TYPE const setCapacity=0 )
325  {
326  INDEX_TYPE const maxOffset = this->m_offsets[ this->m_numArrays ];
327  bufferManipulation::emplaceBack( this->m_offsets, this->m_numArrays + 1, maxOffset );
329  ++this->m_numArrays;
330 
332  }
333 
339  inline
340  void insertSet( INDEX_TYPE const i, INDEX_TYPE const setCapacity=0 )
341  {
344 
345  // Insert an set of capacity zero at the given location
346  INDEX_TYPE const offset = this->m_offsets[i];
347  bufferManipulation::emplace( this->m_offsets, this->m_numArrays + 1, i + 1, offset );
348  bufferManipulation::emplace( this->m_sizes, this->m_numArrays, i, 0 );
349  ++this->m_numArrays;
350 
351  // Set the capacity of the new set
353  }
354 
359  inline
360  void eraseSet( INDEX_TYPE const i )
361  {
363 
364  setCapacityOfSet( i, 0 );
365  bufferManipulation::erase( this->m_offsets, this->m_numArrays + 1, i + 1 );
366  bufferManipulation::erase( this->m_sizes, this->m_numArrays, i );
367  --this->m_numArrays;
368  }
369 
371 
375 
383  inline
384  bool insertIntoSet( INDEX_TYPE const i, T const & val )
385  { return ParentClass::insertIntoSetImpl( i, val, CallBacks( *this, i ) ); }
386 
396  template< typename ITER >
397  inline
398  INDEX_TYPE insertIntoSet( INDEX_TYPE const i, ITER const first, ITER const last )
399  { return ParentClass::insertIntoSetImpl( i, first, last, CallBacks( *this, i ) ); }
400 
405  LVARRAY_HOST_DEVICE inline
406  bool removeFromSet( INDEX_TYPE const i, T const & value ) const
407  { return ParentClass::removeFromSet( i, value ); }
408 
420  template< typename ITER >
421  LVARRAY_HOST_DEVICE inline
422  INDEX_TYPE removeFromSet( INDEX_TYPE const i, ITER const first, ITER const last ) const
423  { return ParentClass::removeFromSet( i, first, last ); }
424 
429  void clearSet( INDEX_TYPE const i )
430  {
432 
433  INDEX_TYPE const prevSize = sizeOfSet( i );
434  T * const values = getSetValues( i );
435  arrayManipulation::resize( values, prevSize, INDEX_TYPE( 0 ) );
436  this->m_sizes[ i ] = 0;
437  }
438 
444  inline
445  void setCapacityOfSet( INDEX_TYPE const i, INDEX_TYPE newCapacity )
446  { ParentClass::setCapacityOfArray( i, newCapacity ); }
447 
453  inline
454  void reserveCapacityOfSet( INDEX_TYPE const i, INDEX_TYPE newCapacity )
455  {
457  if( newCapacity > capacityOfSet( i ) ) setCapacityOfSet( i, newCapacity );
458  }
459 
461 
465 
473  void move( MemorySpace const space, bool const touch=true ) const
474  { return ParentClass::move( space, touch ); }
475 
477 
482  void setName( std::string const & name )
483  { ParentClass::template setName< decltype( *this ) >( name ); }
484 
485 private:
486 
488 
494  {
495 public:
496 
502  inline
503  CallBacks( ArrayOfSets & aos, INDEX_TYPE const i ):
504  m_aos( aos ),
505  m_i( i )
506  {}
507 
516  inline
517  T * incrementSize( T * const curPtr, INDEX_TYPE const nToAdd ) const
518  {
519  LVARRAY_UNUSED_VARIABLE( curPtr );
520  INDEX_TYPE const newNNZ = m_aos.sizeOfSet( m_i ) + nToAdd;
521  if( newNNZ > m_aos.capacityOfSet( m_i ) )
522  {
523  m_aos.setCapacityOfSet( m_i, 2 * newNNZ );
524  }
525 
526  return m_aos.getSetValues( m_i );
527  }
528 
529 private:
532 
534  INDEX_TYPE const m_i;
535  };
536 };
537 
538 } /* namespace LvArray */
constexpr ArrayOfSetsView< T const, INDEX_TYPE const, BUFFER_TYPE > toViewConst() const &
Definition: ArrayOfSets.hpp:220
#define LVARRAY_UNUSED_VARIABLE(X)
Mark X as an unused variable, used to silence compiler warnings.
Definition: Macros.hpp:79
void emplace(BUFFER &buf, std::ptrdiff_t const size, std::ptrdiff_t const pos, ARGS &&... args)
Construct a new value at position pos.
Definition: bufferManipulation.hpp:315
ArrayOfSets & operator=(ArrayOfSets const &src)
Copy assignment operator, performs a deep copy.
Definition: ArrayOfSets.hpp:99
#define LVARRAY_ASSERT(EXP)
Assert EXP is true with no message.
Definition: Macros.hpp:223
constexpr ArrayOfArraysView< T const, INDEX_TYPE const, true, BUFFER_TYPE > toArrayOfArraysView() const &
Definition: ArrayOfSets.hpp:242
bool insertIntoSet(INDEX_TYPE const i, T const &val)
Insert a value into the given set.
Definition: ArrayOfSets.hpp:384
void setCapacityOfSet(INDEX_TYPE const i, INDEX_TYPE newCapacity)
Set the capacity of a set.
Definition: ArrayOfSets.hpp:445
void assimilate(ArrayOfArrays< T, INDEX_TYPE, BUFFER_TYPE > &&src, sortedArrayManipulation::Description const desc)
Steal the resources from an ArrayOfArrays and convert it to an ArrayOfSets.
Definition: ArrayOfSets.hpp:130
void emplaceBack(BUFFER &buf, std::ptrdiff_t const size, ARGS &&... args)
Construct a new value at the end of the buffer.
Definition: bufferManipulation.hpp:297
#define ARRAYOFARRAYS_CHECK_BOUNDS(i)
Check that i is a valid array index.
Definition: ArrayOfArraysView.hpp:106
void eraseSet(INDEX_TYPE const i)
Erase a set.
Definition: ArrayOfSets.hpp:360
INDEX_TYPE size_type
The integer type used for indexing, here for stl compatability.
Definition: ArrayOfArraysView.hpp:194
void reserve(INDEX_TYPE const newCapacity)
Reserve space for the given number of arrays.
Definition: ArrayOfArraysView.hpp:638
This class serves to provide a sliced multidimensional interface to the family of LvArray classes...
Definition: ArraySlice.hpp:89
INDEX_TYPE IndexType
The integer type used for indexing.
Definition: ArrayOfArraysView.hpp:188
BUFFER_TYPE< INDEX_TYPE > m_offsets
Definition: ArrayOfArraysView.hpp:965
LVARRAY_HOST_DEVICE constexpr ArraySlice< T, 1, 0, INDEX_TYPE_NC > getSetValues(INDEX_TYPE const i) const
Definition: ArrayOfSetsView.hpp:346
INDEX_TYPE const m_i
The index of the associated set.
Definition: ArrayOfSets.hpp:534
LVARRAY_HOST_DEVICE CONSTEXPR_WITH_NDEBUG INDEX_TYPE_NC capacity() const
Definition: ArrayOfArraysView.hpp:373
BUFFER_TYPE< T > m_values
Definition: ArrayOfArraysView.hpp:972
LVARRAY_HOST_DEVICE constexpr INDEX_TYPE_NC capacityOfSet(INDEX_TYPE const i) const
Definition: ArrayOfSetsView.hpp:181
ArrayOfSets(ArrayOfSets const &src)
Copy constructor, performs a deep copy.
Definition: ArrayOfSets.hpp:69
LVARRAY_HOST_DEVICE constexpr INDEX_TYPE size() const noexcept
Definition: ArraySlice.hpp:170
BUFFER_TYPE< SIZE_TYPE > m_sizes
Holds the size of each array.
Definition: ArrayOfArraysView.hpp:968
LVARRAY_HOST_DEVICE constexpr T * begin() const
Definition: ArraySlice.hpp:331
void setName(std::string const &name)
Set the name to be displayed whenever the underlying Buffer&#39;s user call back is called.
Definition: ArrayOfSets.hpp:482
DISABLE_HD_WARNING LVARRAY_HOST_DEVICE void resize(T *const LVARRAY_RESTRICT ptr, std::ptrdiff_t const size, std::ptrdiff_t const newSize, ARGS &&... args)
Resize the give array.
Definition: arrayManipulation.hpp:283
T value_type
An alias for the type contained in the inner arrays, here for stl compatability.
Definition: ArrayOfArraysView.hpp:191
void compress(BUFFERS &... buffers)
Compress the arrays so that the values of each array are contiguous with no extra capacity in between...
Definition: ArrayOfArraysView.hpp:669
void move(MemorySpace const space, bool const touch=true) const
Move this ArrayOfSets to the given memory space.
Definition: ArrayOfSets.hpp:473
LVARRAY_HOST_DEVICE INDEX_TYPE removeFromSet(INDEX_TYPE const i, ITER const first, ITER const last) const
Removes multiple values from the given set.
Definition: ArrayOfSets.hpp:422
This class implements an array of arrays like object with contiguous storage.
Definition: ArrayOfArrays.hpp:33
LVARRAY_HOST_DEVICE constexpr ArrayOfArraysView< T const, INDEX_TYPE const, true, BUFFER_TYPE > toArrayOfArraysView() const
Definition: ArrayOfSetsView.hpp:149
LVARRAY_HOST_DEVICE constexpr std::enable_if< std::is_signed< INDEX_TYPE >::value, bool >::type isPositive(INDEX_TYPE const i)
Definition: arrayManipulation.hpp:82
DISABLE_HD_WARNING LVARRAY_HOST_DEVICE std::ptrdiff_t removeDuplicates(ITER first, ITER const last, Compare &&comp=Compare())
Remove duplicates from the array, duplicates aren&#39;t destroyed but they&#39;re moved out of...
Definition: sortedArrayManipulation.hpp:307
void resize(INDEX_TYPE const newSize, INDEX_TYPE const defaultArrayCapacity=0)
Set the number of arrays.
Definition: ArrayOfArraysView.hpp:631
LVARRAY_HOST_DEVICE bool removeFromSet(INDEX_TYPE const i, T const &value) const
Remove a value from the given set.
Definition: ArrayOfSetsView.hpp:287
LVARRAY_HOST_DEVICE constexpr ArrayOfSetsView< T, INDEX_TYPE const, BUFFER_TYPE > toView() const
Definition: ArrayOfSetsView.hpp:123
void reserveCapacityOfSet(INDEX_TYPE const i, INDEX_TYPE newCapacity)
Reserve space in a set.
Definition: ArrayOfSets.hpp:454
This class provides a no-op callbacks interface for the ArrayManipulation sorted routines.
Definition: sortedArrayManipulation.hpp:72
This class provides a view into an array of sets like object.
Definition: ArrayOfSetsView.hpp:40
This class provides a view into an array of arrays like object.
Definition: ArrayOfArraysView.hpp:170
INDEX_TYPE insertIntoSet(INDEX_TYPE const i, ITER const first, ITER const last)
Inserts multiple values into the given set.
Definition: ArrayOfSets.hpp:398
LVARRAY_HOST_DEVICE constexpr INDEX_TYPE_NC valueCapacity() const
Definition: ArrayOfArraysView.hpp:394
void clearSet(INDEX_TYPE const i)
Clear a set.
Definition: ArrayOfSets.hpp:429
void insertSet(INDEX_TYPE const i, INDEX_TYPE const setCapacity=0)
Insert a set at position i with capacity setCapacity.
Definition: ArrayOfSets.hpp:340
void move(MemorySpace const space, bool const touch=true) const
Move this ArrayOfSets to the given memory space.
Definition: ArrayOfSetsView.hpp:320
#define ARRAYOFARRAYS_CHECK_INSERT_BOUNDS(i)
Check that i is a valid index to insert an array at.
Definition: ArrayOfArraysView.hpp:121
camp::resources::Platform MemorySpace
an alias for camp::resources::Platform.
Definition: bufferManipulation.hpp:31
LVARRAY_HOST_DEVICE bool contains(INDEX_TYPE const i, T const &value) const
Definition: ArrayOfSetsView.hpp:192
LVARRAY_HOST_DEVICE bool removeFromSet(INDEX_TYPE const i, T const &value) const
Remove a value from the given set.
Definition: ArrayOfSets.hpp:406
The top level namespace.
Definition: Array.hpp:24
constexpr ArrayOfSetsView< T, INDEX_TYPE const, BUFFER_TYPE > toView() const &
Definition: ArrayOfSets.hpp:198
This class implements an array of sets like object with contiguous storage.
Definition: ArrayOfArrays.hpp:22
ArrayOfSets & m_aos
A reference to the associated ArrayOfSets.
Definition: ArrayOfSets.hpp:531
LVARRAY_HOST_DEVICE constexpr ArrayOfSetsView< T const, INDEX_TYPE const, BUFFER_TYPE > toViewConst() const
Definition: ArrayOfSetsView.hpp:136
void resizeFromCapacities(INDEX_TYPE const numSubArrays, INDEX_TYPE const *const capacities, BUFFERS &... buffers)
Clears the array and creates a new array with the given number of sub-arrays.
Definition: ArrayOfArraysView.hpp:725
ArrayOfSets(INDEX_TYPE const nsets=0, INDEX_TYPE defaultSetCapacity=0)
Constructor.
Definition: ArrayOfSets.hpp:57
void consistencyCheck() const
Verify that the capacity of each set is greater than or equal to the size and that each set is sorted...
Definition: ArrayOfSetsView.hpp:207
LVARRAY_HOST_DEVICE constexpr INDEX_TYPE_NC sizeOfSet(INDEX_TYPE const i) const
Definition: ArrayOfSetsView.hpp:171
void setEqualTo(INDEX_TYPE const srcNumArrays, INDEX_TYPE const srcMaxOffset, BUFFER_TYPE< INDEX_TYPE > const &srcOffsets, BUFFER_TYPE< INDEX_TYPE > const &srcSizes, BUFFER_TYPE< T > const &srcValues, PAIRS_OF_BUFFERS &&... pairs)
Set this ArrayOfArraysView equal to the provided arrays.
Definition: ArrayOfArraysView.hpp:830
void appendSet(INDEX_TYPE const setCapacity=0)
Append a set with capacity setCapacity.
Definition: ArrayOfSets.hpp:324
INDEX_TYPE_NC m_numArrays
The number of arrays contained.
Definition: ArrayOfArraysView.hpp:961
DISABLE_HD_WARNING LVARRAY_HOST_DEVICE void makeSorted(RandomAccessIterator const first, RandomAccessIterator const last, Compare &&comp=Compare())
Sort the given values in place using the given comparator.
Definition: sortedArrayManipulation.hpp:210
void free(BUFFERS &... buffers)
Destroy all the objects held by this array and free all associated memory.
Definition: ArrayOfArraysView.hpp:806
INDEX_TYPE size() const
Definition: ArrayOfSets.hpp:270
~ArrayOfSets()
Destructor, frees the values, sizes and offsets Buffers.
Definition: ArrayOfSets.hpp:83
ArrayOfSetsView & operator=(ArrayOfSetsView const &)=default
Default copy assignment operator, this does a shallow copy.
void setCapacityOfArray(INDEX_TYPE const i, INDEX_TYPE const newCapacity, BUFFERS &... buffers)
Set the capacity of the given array.
Definition: ArrayOfArraysView.hpp:877
void assimilate(ArrayOfArraysView< T, INDEX_TYPE, CONST_SIZES, BUFFER_TYPE > &&src)
Steal the resources of src, clearing it in the process.
Definition: ArrayOfArraysView.hpp:749
T * incrementSize(T *const curPtr, INDEX_TYPE const nToAdd) const
Callback signaling that the size of the set has increased.
Definition: ArrayOfSets.hpp:517
Contains the implementation of LvArray::ArrayOfSetsView.
DISABLE_HD_WARNING LVARRAY_HOST_DEVICE void setCapacity(BUFFER &buf, std::ptrdiff_t const size, MemorySpace const space, std::ptrdiff_t const newCapacity)
Set the capacity of the buffer.
Definition: bufferManipulation.hpp:214
void resize(INDEX_TYPE const newSize, INDEX_TYPE const defaultArrayCapacity=0)
Set the number of arrays.
Definition: ArrayOfSets.hpp:307
This class provides the callbacks for the sortedArrayManipulation routines.
Definition: ArrayOfSets.hpp:493
LVARRAY_HOST_DEVICE constexpr INDEX_TYPE_NC size() const
Definition: ArrayOfArraysView.hpp:324
ArrayOfSets & operator=(ArrayOfSets &&src)
Default move assignment operator, performs a shallow copy.
Definition: ArrayOfSets.hpp:115
LVARRAY_HOST_DEVICE constexpr T * end() const
Definition: ArraySlice.hpp:339
CallBacks(ArrayOfSets &aos, INDEX_TYPE const i)
Constructor.
Definition: ArrayOfSets.hpp:503
DISABLE_HD_WARNING LVARRAY_HOST_DEVICE std::ptrdiff_t makeSortedUnique(ITER const first, ITER const last, Compare &&comp=Compare())
Sort and remove duplicates from the array, duplicates aren&#39;t destroyed but they&#39;re moved out of...
Definition: sortedArrayManipulation.hpp:356
void reserveValues(INDEX_TYPE const newValueCapacity, BUFFERS &... buffers)
Reserve space for the given number of values.
Definition: ArrayOfArraysView.hpp:652
LVARRAY_HOST_DEVICE bool insertIntoSetImpl(INDEX_TYPE const i, T const &value, CALLBACKS &&cbacks) const
Helper function to insert a value into the given set.
Definition: ArrayOfSetsView.hpp:364
void erase(BUFFER &buf, std::ptrdiff_t const size, std::ptrdiff_t const pos)
Erase a value from the buffer.
Definition: bufferManipulation.hpp:373
Description
Describes an as some combination of sorted/unsorted and unique/with duplicates.
Definition: sortedArrayManipulation.hpp:42
#define LVARRAY_HOST_DEVICE
Mark a function for both host and device usage.
Definition: Macros.hpp:600
T ValueType
An alias for the type contained in the inner arrays.
Definition: ArrayOfArraysView.hpp:185