GEOSX
ArrayOfSets.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020, 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 
38  using ParentClass = ArrayOfSetsView< T, INDEX_TYPE, BUFFER_TYPE >;
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  inline
131  {
133  ParentClass::assimilate( reinterpret_cast< ParentClass && >( src ) );
134 
135  INDEX_TYPE const numSets = size();
136  if( desc == sortedArrayManipulation::UNSORTED_NO_DUPLICATES )
137  {
138  for( INDEX_TYPE i = 0; i < numSets; ++i )
139  {
140  T * const setValues = getSetValues( i );
141  INDEX_TYPE const numValues = sizeOfSet( i );
142  std::sort( setValues, setValues + numValues );
143  }
144  }
145  if( desc == sortedArrayManipulation::SORTED_WITH_DUPLICATES )
146  {
147  for( INDEX_TYPE i = 0; i < numSets; ++i )
148  {
149  T * const setValues = getSetValues( i );
150  INDEX_TYPE const numValues = sizeOfSet( i );
151 
152  INDEX_TYPE const numUniqueValues = sortedArrayManipulation::removeDuplicates( setValues, setValues + numValues );
153  arrayManipulation::resize( setValues, numValues, numUniqueValues );
154  this->m_sizes[ i ] = numUniqueValues;
155  }
156  }
157  if( desc == sortedArrayManipulation::UNSORTED_WITH_DUPLICATES )
158  {
159  for( INDEX_TYPE i = 0; i < numSets; ++i )
160  {
161  T * const setValues = getSetValues( i );
162  INDEX_TYPE const numValues = sizeOfSet( i );
163 
164  INDEX_TYPE const numUniqueValues = sortedArrayManipulation::makeSortedUnique( setValues, setValues + numValues );
165  arrayManipulation::resize( setValues, numValues, numUniqueValues );
166  this->m_sizes[ i ] = numUniqueValues;
167  }
168  }
169 
170 #ifdef ARRAY_BOUNDS_CHECK
172 #endif
173  }
174 
176 
178 
182 
190  constexpr inline
192  toView() const &
193  { return ParentClass::toView(); }
194 
202  constexpr inline
204  toView() const && = delete;
205 
212  constexpr inline
214  toViewConst() const &
215  { return ParentClass::toViewConst(); }
216 
224  constexpr inline
226  toViewConst() const && = delete;
227 
234  constexpr inline
238 
246  constexpr inline
248  toArrayOfArraysView() const && = delete;
249 
251 
255 
263  inline
264  INDEX_TYPE size() const
265  { return ParentClass::size(); }
266 
268  using ParentClass::capacity;
271  using ParentClass::contains;
273 
275 
276 
280 
282  using ParentClass::operator[];
283  using ParentClass::operator();
284 
286 
290 
292  using ParentClass::reserve;
294 
301  void resize( INDEX_TYPE const newSize, INDEX_TYPE const defaultArrayCapacity=0 )
302  { return ParentClass::resize( newSize, defaultArrayCapacity ); }
303 
304  using ParentClass::compress;
305 
307 
311 
317  inline
318  void appendSet( INDEX_TYPE const setCapacity=0 )
319  {
320  INDEX_TYPE const maxOffset = this->m_offsets[ this->m_numArrays ];
321  bufferManipulation::emplaceBack( this->m_offsets, this->m_numArrays + 1, maxOffset );
323  ++this->m_numArrays;
324 
326  }
327 
333  inline
334  void insertSet( INDEX_TYPE const i, INDEX_TYPE const setCapacity=0 )
335  {
338 
339  // Insert an set of capacity zero at the given location
340  INDEX_TYPE const offset = this->m_offsets[i];
341  bufferManipulation::emplace( this->m_offsets, this->m_numArrays + 1, i + 1, offset );
342  bufferManipulation::emplace( this->m_sizes, this->m_numArrays, i, 0 );
343  ++this->m_numArrays;
344 
345  // Set the capacity of the new set
347  }
348 
353  inline
354  void eraseSet( INDEX_TYPE const i )
355  {
357 
358  setCapacityOfSet( i, 0 );
359  bufferManipulation::erase( this->m_offsets, this->m_numArrays + 1, i + 1 );
360  bufferManipulation::erase( this->m_sizes, this->m_numArrays, i );
361  --this->m_numArrays;
362  }
363 
365 
369 
377  inline
378  bool insertIntoSet( INDEX_TYPE const i, T const & val )
379  { return ParentClass::insertIntoSetImpl( i, val, CallBacks( *this, i ) ); }
380 
390  template< typename ITER >
391  inline
392  INDEX_TYPE insertIntoSet( INDEX_TYPE const i, ITER const first, ITER const last )
393  { return ParentClass::insertIntoSetImpl( i, first, last, CallBacks( *this, i ) ); }
394 
399  LVARRAY_HOST_DEVICE inline
400  bool removeFromSet( INDEX_TYPE const i, T const & value ) const
401  { return ParentClass::removeFromSet( i, value ); }
402 
414  template< typename ITER >
415  LVARRAY_HOST_DEVICE inline
416  INDEX_TYPE removeFromSet( INDEX_TYPE const i, ITER const first, ITER const last ) const
417  { return ParentClass::removeFromSet( i, first, last ); }
418 
423  void clearSet( INDEX_TYPE const i )
424  {
426 
427  INDEX_TYPE const prevSize = sizeOfSet( i );
428  T * const values = getSetValues( i );
429  arrayManipulation::resize( values, prevSize, INDEX_TYPE( 0 ) );
430  this->m_sizes[ i ] = 0;
431  }
432 
438  inline
439  void setCapacityOfSet( INDEX_TYPE const i, INDEX_TYPE newCapacity )
440  { ParentClass::setCapacityOfArray( i, newCapacity ); }
441 
447  inline
448  void reserveCapacityOfSet( INDEX_TYPE const i, INDEX_TYPE newCapacity )
449  {
451  if( newCapacity > capacityOfSet( i ) ) setCapacityOfSet( i, newCapacity );
452  }
453 
455 
459 
467  void move( MemorySpace const space, bool const touch=true ) const
468  { return ParentClass::move( space, touch ); }
469 
471 
476  void setName( std::string const & name )
477  { ParentClass::template setName< decltype( *this ) >( name ); }
478 
479 private:
480 
482 
487  class CallBacks : public sortedArrayManipulation::CallBacks< T >
488  {
489 public:
490 
496  inline
497  CallBacks( ArrayOfSets & aos, INDEX_TYPE const i ):
498  m_aos( aos ),
499  m_i( i )
500  {}
501 
510  inline
511  T * incrementSize( T * const curPtr, INDEX_TYPE const nToAdd ) const
512  {
513  LVARRAY_UNUSED_VARIABLE( curPtr );
514  INDEX_TYPE const newNNZ = m_aos.sizeOfSet( m_i ) + nToAdd;
515  if( newNNZ > m_aos.capacityOfSet( m_i ) )
516  {
517  m_aos.setCapacityOfSet( m_i, 2 * newNNZ );
518  }
519 
520  return m_aos.getSetValues( m_i );
521  }
522 
523 private:
525  ArrayOfSets & m_aos;
526 
528  INDEX_TYPE const m_i;
529  };
530 };
531 
532 } /* namespace LvArray */
constexpr ArrayOfSetsView< T const, INDEX_TYPE const, BUFFER_TYPE > toViewConst() const &
#define LVARRAY_UNUSED_VARIABLE(X)
Mark X as an unused variable, used to silence compiler warnings.
Definition: Macros.hpp:51
void emplace(BUFFER &buf, std::ptrdiff_t const size, std::ptrdiff_t const pos, ARGS &&... args)
Construct a new value at position pos.
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:142
constexpr ArrayOfArraysView< T const, INDEX_TYPE const, true, BUFFER_TYPE > toArrayOfArraysView() const &
bool insertIntoSet(INDEX_TYPE const i, T const &val)
Insert a value into the given set.
void setCapacityOfSet(INDEX_TYPE const i, INDEX_TYPE newCapacity)
Set the capacity of a set.
void emplaceBack(BUFFER &buf, std::ptrdiff_t const size, ARGS &&... args)
Construct a new value at the end of the buffer.
#define ARRAYOFARRAYS_CHECK_BOUNDS(i)
Check that i is a valid array index.
LvArray::ArrayOfSets< T, localIndex, LvArray::ChaiBuffer > ArrayOfSets
Array of variable-sized sets. See LvArray::ArrayOfSets for details.
Definition: DataTypes.hpp:311
void eraseSet(INDEX_TYPE const i)
Erase a set.
bool removeFromSet(INDEX_TYPE const i, T const &value) const
Remove a value from the given set.
INDEX_TYPE size_type
The integer type used for indexing, here for stl compatability.
bool insertIntoSetImpl(INDEX_TYPE const i, T const &value, CALLBACKS &&cbacks) const
Helper function to insert a value into the given set.
void reserve(INDEX_TYPE const newCapacity)
Reserve space for the given number of arrays.
constexpr INDEX_TYPE_NC sizeOfSet(INDEX_TYPE const i) const
constexpr ArrayOfSetsView< T, INDEX_TYPE const, BUFFER_TYPE > toView() const
ArrayOfSets(ArrayOfSets const &src)
Copy constructor, performs a deep copy.
Definition: ArrayOfSets.hpp:69
void setName(std::string const &name)
Set the name to be displayed whenever the underlying Buffer&#39;s user call back is called.
T value_type
An alias for the type contained in the inner arrays, here for stl compatability.
void compress(BUFFERS &... buffers)
Compress the arrays so that the values of each array are contiguous with no extra capacity in between...
void move(MemorySpace const space, bool const touch=true) const
Move this ArrayOfSets to the given memory space.
This class implements an array of arrays like object with contiguous storage.
constexpr std::enable_if< std::is_signed< INDEX_TYPE >::value, bool >::type isPositive(INDEX_TYPE const i)
void resize(INDEX_TYPE const newSize, INDEX_TYPE const defaultArrayCapacity=0)
Set the number of arrays.
void reserveCapacityOfSet(INDEX_TYPE const i, INDEX_TYPE newCapacity)
Reserve space in a set.
constexpr ArraySlice< T, 1, 0, INDEX_TYPE_NC > getSetValues(INDEX_TYPE const i) const
constexpr ArrayOfSetsView< T const, INDEX_TYPE const, BUFFER_TYPE > toViewConst() const
This class provides a no-op callbacks interface for the ArrayManipulation sorted routines.
This class provides a view into an array of sets like object.
This class provides a view into an array of arrays like object.
INDEX_TYPE insertIntoSet(INDEX_TYPE const i, ITER const first, ITER const last)
Inserts multiple values into the given set.
bool contains(INDEX_TYPE const i, T const &value) const
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.
void clearSet(INDEX_TYPE const i)
Clear a set.
constexpr INDEX_TYPE_NC capacityOfSet(INDEX_TYPE const i) const
LvArray::ArrayOfArrays< T, localIndex, LvArray::ChaiBuffer > ArrayOfArrays
Array of variable-sized arrays. See LvArray::ArrayOfArrays for details.
Definition: DataTypes.hpp:303
void insertSet(INDEX_TYPE const i, INDEX_TYPE const setCapacity=0)
Insert a set at position i with capacity setCapacity.
void move(MemorySpace const space, bool const touch=true) const
Move this ArrayOfSets to the given memory space.
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...
#define ARRAYOFARRAYS_CHECK_INSERT_BOUNDS(i)
Check that i is a valid index to insert an array at.
MemorySpace
An enum containing the available memory spaces.
void setCapacity(BUFFER &buf, std::ptrdiff_t const size, std::ptrdiff_t const newCapacity)
Set the capacity of the buffer.
The top level namespace.
Definition: Array.hpp:24
constexpr ArrayOfSetsView< T, INDEX_TYPE const, BUFFER_TYPE > toView() const &
This class implements an array of sets like object with contiguous storage.
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.
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...
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.
void appendSet(INDEX_TYPE const setCapacity=0)
Append a set with capacity setCapacity.
void resize(T *const LVARRAY_RESTRICT ptr, std::ptrdiff_t const size, std::ptrdiff_t const newSize, ARGS &&... args)
Resize the give array.
void free(BUFFERS &... buffers)
Destroy all the objects held by this array and free all associated memory.
INDEX_TYPE size() const
~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.
void assimilate(ArrayOfArraysView< T, INDEX_TYPE, CONST_SIZES, BUFFER_TYPE > &&src)
Steal the resources of src, clearing it in the process.
Contains the implementation of LvArray::ArrayOfSetsView.
bool removeFromSet(INDEX_TYPE const i, T const &value) const
Remove a value from the given set.
INDEX_TYPE removeFromSet(INDEX_TYPE const i, ITER const first, ITER const last) const
Removes multiple values from the given set.
LvArray::ArrayOfSetsView< T, localIndex const, LvArray::ChaiBuffer > ArrayOfSetsView
View of array of variable-sized sets. See LvArray::ArrayOfSetsView for details.
Definition: DataTypes.hpp:315
std::string string
String type.
Definition: DataTypes.hpp:131
void resize(INDEX_TYPE const newSize, INDEX_TYPE const defaultArrayCapacity=0)
Set the number of arrays.
constexpr ArrayOfArraysView< T const, INDEX_TYPE const, true, BUFFER_TYPE > toArrayOfArraysView() const
ArrayOfSets & operator=(ArrayOfSets &&src)
Default move assignment operator, performs a shallow copy.
void reserveValues(INDEX_TYPE const newValueCapacity, BUFFERS &... buffers)
Reserve space for the given number of values.
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...
void erase(BUFFER &buf, std::ptrdiff_t const size, std::ptrdiff_t const pos)
Erase a value from the buffer.
Description
Describes an as some combination of sorted/unsorted and unique/with duplicates.
#define LVARRAY_HOST_DEVICE
Mark a function for both host and device usage.
Definition: Macros.hpp:389