LvArray
ArrayOfSetsView.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 // Source includes
16 #include "ArrayOfArraysView.hpp"
17 #include "arrayManipulation.hpp"
19 #include "ArraySlice.hpp"
20 #include "typeManipulation.hpp"
21 
22 namespace LvArray
23 {
24 
37 template< typename T,
38  typename INDEX_TYPE,
39  template< typename > class BUFFER_TYPE >
40 class ArrayOfSetsView : protected ArrayOfArraysView< T, INDEX_TYPE, std::is_const< T >::value, BUFFER_TYPE >
41 {
42 protected:
45 
48 
49  using typename ParentClass::SIZE_TYPE;
50 
51 public:
52  using typename ParentClass::ValueType;
53  using typename ParentClass::IndexType;
54  using typename ParentClass::value_type;
55  using typename ParentClass::size_type;
56 
57 
61 
67  ArrayOfSetsView() = default;
68 
73  inline
74  ArrayOfSetsView( ArrayOfSetsView const & ) = default;
75 
79  inline
80  ArrayOfSetsView( ArrayOfSetsView && ) = default;
81 
89  LVARRAY_HOST_DEVICE constexpr inline
90  ArrayOfSetsView( INDEX_TYPE const numArrays,
91  BUFFER_TYPE< INDEX_TYPE > const & offsets,
92  BUFFER_TYPE< SIZE_TYPE > const & sizes,
93  BUFFER_TYPE< T > const & values ):
94  ParentClass( numArrays, offsets, sizes, values )
95  {}
96 
101  inline
102  ArrayOfSetsView & operator=( ArrayOfSetsView const & ) = default;
103 
108  inline
109  ArrayOfSetsView & operator=( ArrayOfSetsView && ) = default;
110 
112 
116 
121  LVARRAY_HOST_DEVICE constexpr inline
123  toView() const
124  {
126  this->m_offsets,
127  this->m_sizes,
128  this->m_values );
129  }
130 
134  LVARRAY_HOST_DEVICE constexpr inline
136  toViewConst() const
137  {
139  this->m_offsets,
140  this->m_sizes,
141  this->m_values );
142  }
143 
147  LVARRAY_HOST_DEVICE constexpr inline
150  {
152  this->m_offsets,
153  this->m_sizes,
154  this->m_values );
155  }
156 
158 
162 
164  using ParentClass::size;
165 
170  LVARRAY_HOST_DEVICE constexpr inline
171  INDEX_TYPE_NC sizeOfSet( INDEX_TYPE const i ) const
172  { return ParentClass::sizeOfArray( i ); }
173 
174  using ParentClass::capacity;
175 
180  LVARRAY_HOST_DEVICE constexpr inline
181  INDEX_TYPE_NC capacityOfSet( INDEX_TYPE const i ) const
182  { return ParentClass::capacityOfArray( i ); }
183 
185 
191  LVARRAY_HOST_DEVICE inline
192  bool contains( INDEX_TYPE const i, T const & value ) const
193  {
195 
196  INDEX_TYPE const setSize = sizeOfSet( i );
197  T const * const setValues = (*this)[ i ];
198 
199  return sortedArrayManipulation::contains( setValues, setSize, value );
200  }
201 
207  void consistencyCheck() const
208  {
209  INDEX_TYPE const numSets = size();
210  for( INDEX_TYPE_NC i = 0; i < numSets; ++i )
211  {
213 
214  T * const setValues = getSetValues( i );
215  INDEX_TYPE const numValues = sizeOfSet( i );
216  LVARRAY_ERROR_IF( !sortedArrayManipulation::isSortedUnique( setValues, setValues + numValues ),
217  "Values should be sorted and unique!" );
218  }
219  }
220 
222 
226 
232  LVARRAY_HOST_DEVICE constexpr inline
234  { return ParentClass::operator[]( i ); }
235 
241  LVARRAY_HOST_DEVICE constexpr inline
242  T const & operator()( INDEX_TYPE const i, INDEX_TYPE const j ) const
243  { return ParentClass::operator()( i, j ); }
244 
246 
250 
260  LVARRAY_HOST_DEVICE inline
261  bool insertIntoSet( INDEX_TYPE const i, T const & value ) const
262  { return insertIntoSetImpl( i, value, CallBacks( *this, i ) ); }
263 
275  template< typename ITER >
276  LVARRAY_HOST_DEVICE inline
277  INDEX_TYPE_NC insertIntoSet( INDEX_TYPE const i, ITER const first, ITER const last ) const
278  { return insertIntoSetImpl( i, first, last, CallBacks( *this, i ) ); }
279 
286  LVARRAY_HOST_DEVICE inline
287  bool removeFromSet( INDEX_TYPE const i, T const & value ) const
288  { return removeFromSetImpl( i, value, CallBacks( *this, i ) ); }
289 
299  template< typename ITER >
300  LVARRAY_HOST_DEVICE inline
301  INDEX_TYPE_NC removeFromSet( INDEX_TYPE const i, ITER const first, ITER const last ) const
302  { return removeFromSetImpl( i, first, last, CallBacks( *this, i ) ); }
303 
305 
309 
320  void move( MemorySpace const space, bool const touch=true ) const
321  { return ParentClass::move( space, touch ); }
322 
324 
325 
326  using ParentClass::getSizes;
329 
330 protected:
331 
337  ParentClass( true )
338  {}
339 
345  LVARRAY_HOST_DEVICE constexpr inline
347  { return ParentClass::operator[]( i ); }
348 
352 
362  template< typename CALLBACKS >
363  LVARRAY_HOST_DEVICE inline
364  bool insertIntoSetImpl( INDEX_TYPE const i, T const & value, CALLBACKS && cbacks ) const
365  {
367 
368  INDEX_TYPE const setSize = sizeOfSet( i );
369  T * const setValues = getSetValues( i );
370 
371  bool const success = sortedArrayManipulation::insert( setValues, setSize, value, std::move( cbacks ) );
372  this->m_sizes[i] += success;
373  return success;
374  }
375 
387  template< typename ITER, typename CALLBACKS >
388  LVARRAY_HOST_DEVICE inline
389  INDEX_TYPE_NC insertIntoSetImpl( INDEX_TYPE const i,
390  ITER const first,
391  ITER const last,
392  CALLBACKS && cbacks ) const
393  {
395 
396  INDEX_TYPE const setSize = sizeOfSet( i );
397  T * const setValues = getSetValues( i );
398 
399  INDEX_TYPE const nInserted = sortedArrayManipulation::insert( setValues,
400  setSize,
401  first,
402  last,
403  std::forward< CALLBACKS >( cbacks ) );
404  this->m_sizes[i] += nInserted;
405  return nInserted;
406  }
407 
416  template< typename CALLBACKS >
417  LVARRAY_HOST_DEVICE inline
418  bool removeFromSetImpl( INDEX_TYPE const i, T const & value, CALLBACKS && cbacks ) const
419  {
421 
422  INDEX_TYPE const setSize = sizeOfSet( i );
423  T * const setValues = getSetValues( i );
424 
425  bool const success = sortedArrayManipulation::remove( setValues, setSize, value, std::move( cbacks ) );
426  this->m_sizes[i] -= success;
427  return success;
428  }
429 
441  template< typename ITER, typename CALLBACKS >
442  LVARRAY_HOST_DEVICE inline
443  INDEX_TYPE_NC removeFromSetImpl( INDEX_TYPE const i,
444  ITER const first,
445  ITER const last,
446  CALLBACKS && cbacks ) const
447  {
449 
450  INDEX_TYPE const setSize = sizeOfSet( i );
451  T * const setValues = getSetValues( i );
452 
453  INDEX_TYPE const nRemoved = sortedArrayManipulation::remove( setValues,
454  setSize,
455  first,
456  last,
457  std::forward< CALLBACKS >( cbacks ) );
458  this->m_sizes[i] -= nRemoved;
459  return nRemoved;
460  }
461 
463 
464 private:
465 
471  {
472 public:
473 
479  LVARRAY_HOST_DEVICE inline
480  CallBacks( ArrayOfSetsView const & aos, INDEX_TYPE const i ):
481  m_aos( aos ),
482  m_indexOfSet( i )
483  {}
484 
493  LVARRAY_HOST_DEVICE inline
494  T * incrementSize( T * const curPtr, INDEX_TYPE const nToAdd ) const
495  {
496  LVARRAY_UNUSED_VARIABLE( curPtr );
497 #ifdef LVARRAY_BOUNDS_CHECK
499  "ArrayOfSetsView cannot do reallocation." );
500 #else
501  LVARRAY_DEBUG_VAR( nToAdd );
502 #endif
503  return m_aos.getSetValues( m_indexOfSet );
504  }
505 
506 private:
509 
511  INDEX_TYPE const m_indexOfSet;
512  };
513 };
514 
515 } // namespace LvArray
#define LVARRAY_UNUSED_VARIABLE(X)
Mark X as an unused variable, used to silence compiler warnings.
Definition: Macros.hpp:79
LVARRAY_HOST_DEVICE constexpr T const & operator()(INDEX_TYPE const i, INDEX_TYPE const j) const
Definition: ArrayOfSetsView.hpp:242
LVARRAY_HOST_DEVICE CONSTEXPR_WITHOUT_BOUNDS_CHECK ArraySlice< T, 1, 0, INDEX_TYPE_NC > operator[](INDEX_TYPE const i) const
Definition: ArrayOfArraysView.hpp:409
#define ARRAYOFARRAYS_CHECK_BOUNDS(i)
Check that i is a valid array index.
Definition: ArrayOfArraysView.hpp:106
INDEX_TYPE size_type
The integer type used for indexing, here for stl compatability.
Definition: ArrayOfArraysView.hpp:194
LVARRAY_HOST_DEVICE T * incrementSize(T *const curPtr, INDEX_TYPE const nToAdd) const
Callback signaling that the size of the set has increased.
Definition: ArrayOfSetsView.hpp:494
#define LVARRAY_ERROR_IF(EXP, MSG)
Abort execution if EXP is true.
Definition: Macros.hpp:155
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
LVARRAY_HOST_DEVICE INDEX_TYPE_NC removeFromSet(INDEX_TYPE const i, ITER const first, ITER const last) const
Removes multiple values from the given set.
Definition: ArrayOfSetsView.hpp:301
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
LVARRAY_HOST_DEVICE CONSTEXPR_WITH_NDEBUG INDEX_TYPE_NC capacity() const
Definition: ArrayOfArraysView.hpp:373
BUFFER_TYPE< T > m_values
Definition: ArrayOfArraysView.hpp:972
void move(MemorySpace const space, bool touch=true) const
Move this ArrayOfArrays to the given memory space.
Definition: ArrayOfArraysView.hpp:584
DISABLE_HD_WARNING LVARRAY_HOST_DEVICE bool remove(T *const LVARRAY_RESTRICT ptr, std::ptrdiff_t const size, T const &value, CALLBACKS &&callBacks)
Remove the given value from the array if it exists.
Definition: sortedArrayManipulation.hpp:473
LVARRAY_HOST_DEVICE INDEX_TYPE_NC insertIntoSetImpl(INDEX_TYPE const i, ITER const first, ITER const last, CALLBACKS &&cbacks) const
Inserts multiple values into the given set.
Definition: ArrayOfSetsView.hpp:389
Contains templates useful for type manipulation.
LVARRAY_HOST_DEVICE constexpr INDEX_TYPE_NC capacityOfSet(INDEX_TYPE const i) const
Definition: ArrayOfSetsView.hpp:181
BUFFER_TYPE< SIZE_TYPE > m_sizes
Holds the size of each array.
Definition: ArrayOfArraysView.hpp:968
LVARRAY_HOST_DEVICE bool insertIntoSet(INDEX_TYPE const i, T const &value) const
Insert a value into the given set.
Definition: ArrayOfSetsView.hpp:261
LVARRAY_HOST_DEVICE constexpr T const * getValues() const
Definition: ArrayOfArraysView.hpp:362
LVARRAY_HOST_DEVICE INDEX_TYPE_NC insertIntoSet(INDEX_TYPE const i, ITER const first, ITER const last) const
Inserts multiple values into the given set.
Definition: ArrayOfSetsView.hpp:277
T value_type
An alias for the type contained in the inner arrays, here for stl compatability.
Definition: ArrayOfArraysView.hpp:191
LVARRAY_HOST_DEVICE constexpr ArraySlice< T const, 1, 0, INDEX_TYPE_NC > operator[](INDEX_TYPE const i) const
Definition: ArrayOfSetsView.hpp:233
This class provides the callbacks for the sortedArrayManipulation routines.
Definition: ArrayOfSetsView.hpp:470
LVARRAY_HOST_DEVICE constexpr ArrayOfArraysView< T const, INDEX_TYPE const, true, BUFFER_TYPE > toArrayOfArraysView() const
Definition: ArrayOfSetsView.hpp:149
LVARRAY_HOST_DEVICE bool removeFromSet(INDEX_TYPE const i, T const &value) const
Remove a value from the given set.
Definition: ArrayOfSetsView.hpp:287
Contains the implementation of LvArray::ArraySlice.
LVARRAY_HOST_DEVICE constexpr ArrayOfSetsView(INDEX_TYPE const numArrays, BUFFER_TYPE< INDEX_TYPE > const &offsets, BUFFER_TYPE< SIZE_TYPE > const &sizes, BUFFER_TYPE< T > const &values)
Construct a new ArrayOfArraysView from the given buffers.
Definition: ArrayOfSetsView.hpp:90
LVARRAY_HOST_DEVICE constexpr ArrayOfSetsView< T, INDEX_TYPE const, BUFFER_TYPE > toView() const
Definition: ArrayOfSetsView.hpp:123
LVARRAY_HOST_DEVICE CONSTEXPR_WITHOUT_BOUNDS_CHECK T & operator()(INDEX_TYPE const i, INDEX_TYPE const j) const
Definition: ArrayOfArraysView.hpp:421
ArrayOfSetsView const & m_aos
The ArrayOfSetsView the call back is associated with.
Definition: ArrayOfSetsView.hpp:508
#define LVARRAY_ERROR_IF_GT(lhs, rhs)
Raise a hard error if one value compares greater than the other.
Definition: Macros.hpp:392
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 const m_indexOfSet
The index of the set the call back is associated with.
Definition: ArrayOfSetsView.hpp:511
LVARRAY_HOST_DEVICE constexpr INDEX_TYPE_NC valueCapacity() const
Definition: ArrayOfArraysView.hpp:394
LVARRAY_HOST_DEVICE CONSTEXPR_WITHOUT_BOUNDS_CHECK INDEX_TYPE_NC sizeOfArray(INDEX_TYPE const i) const
Definition: ArrayOfArraysView.hpp:332
void move(MemorySpace const space, bool const touch=true) const
Move this ArrayOfSets to the given memory space.
Definition: ArrayOfSetsView.hpp:320
DISABLE_HD_WARNING LVARRAY_HOST_DEVICE bool isSortedUnique(ITER first, ITER const last, Compare &&comp=Compare())
Definition: sortedArrayManipulation.hpp:374
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
Contains functions for manipulating a contiguous array of values.
#define LVARRAY_DEBUG_VAR(X)
Mark X as an debug variable, used to silence compiler warnings.
Definition: Macros.hpp:85
LVARRAY_HOST_DEVICE bool removeFromSetImpl(INDEX_TYPE const i, T const &value, CALLBACKS &&cbacks) const
Helper function to remove a value from the given set.
Definition: ArrayOfSetsView.hpp:418
The top level namespace.
Definition: Array.hpp:24
#define LVARRAY_ERROR_IF_GT_MSG(lhs, rhs, msg)
Raise a hard error if one value compares greater than the other.
Definition: Macros.hpp:376
LVARRAY_HOST_DEVICE CallBacks(ArrayOfSetsView const &aos, INDEX_TYPE const i)
Constructor.
Definition: ArrayOfSetsView.hpp:480
Contains the implementation of LvArray::ArrayOfArraysView.
This file contains common sorted array manipulation routines. Aside from the functions that take a ca...
ArrayOfSetsView(bool)
Protected constructor to be used by parent classes.
Definition: ArrayOfSetsView.hpp:336
LVARRAY_HOST_DEVICE constexpr ArrayOfSetsView< T const, INDEX_TYPE const, BUFFER_TYPE > toViewConst() const
Definition: ArrayOfSetsView.hpp:136
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
LVARRAY_HOST_DEVICE CONSTEXPR_WITHOUT_BOUNDS_CHECK INDEX_TYPE_NC capacityOfArray(INDEX_TYPE const i) const
Definition: ArrayOfArraysView.hpp:384
std::remove_const_t< INDEX_TYPE > INDEX_TYPE_NC
Since INDEX_TYPE should always be const we need an alias for the non const version.
Definition: ArrayOfArraysView.hpp:174
ArrayOfSetsView & operator=(ArrayOfSetsView const &)=default
Default copy assignment operator, this does a shallow copy.
LVARRAY_HOST_DEVICE constexpr INDEX_TYPE const * getOffsets() const
Definition: ArrayOfArraysView.hpp:353
std::conditional_t< CONST_SIZES, INDEX_TYPE const, INDEX_TYPE_NC > SIZE_TYPE
The type contained by the m_sizes buffer.
Definition: ArrayOfArraysView.hpp:177
LVARRAY_HOST_DEVICE constexpr INDEX_TYPE_NC size() const
Definition: ArrayOfArraysView.hpp:324
LVARRAY_HOST_DEVICE constexpr SIZE_TYPE const * getSizes() const
Definition: ArrayOfArraysView.hpp:342
ArrayOfSetsView()=default
A constructor to create an uninitialized ArrayOfSetsView.
LVARRAY_HOST_DEVICE INDEX_TYPE_NC removeFromSetImpl(INDEX_TYPE const i, ITER const first, ITER const last, CALLBACKS &&cbacks) const
Removes multiple values from the given set.
Definition: ArrayOfSetsView.hpp:443
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
DISABLE_HD_WARNING LVARRAY_HOST_DEVICE bool contains(T const *const LVARRAY_RESTRICT ptr, std::ptrdiff_t const size, T const &value, Compare &&comp=Compare())
Definition: sortedArrayManipulation.hpp:451
DISABLE_HD_WARNING LVARRAY_HOST_DEVICE bool insert(T *const LVARRAY_RESTRICT ptr, std::ptrdiff_t const size, T const &value, CALLBACKS &&callBacks=CALLBACKS())
Insert the given value into the array if it doesn&#39;t already exist.
Definition: sortedArrayManipulation.hpp:620
#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