GEOSX
ArrayOfSetsView.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 // 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 protected:
326 
332  ParentClass( true )
333  {}
334 
340  LVARRAY_HOST_DEVICE constexpr inline
342  { return ParentClass::operator[]( i ); }
343 
347 
357  template< typename CALLBACKS >
358  LVARRAY_HOST_DEVICE inline
359  bool insertIntoSetImpl( INDEX_TYPE const i, T const & value, CALLBACKS && cbacks ) const
360  {
362 
363  INDEX_TYPE const setSize = sizeOfSet( i );
364  T * const setValues = getSetValues( i );
365 
366  bool const success = sortedArrayManipulation::insert( setValues, setSize, value, std::move( cbacks ) );
367  this->m_sizes[i] += success;
368  return success;
369  }
370 
382  template< typename ITER, typename CALLBACKS >
383  LVARRAY_HOST_DEVICE inline
384  INDEX_TYPE_NC insertIntoSetImpl( INDEX_TYPE const i,
385  ITER const first,
386  ITER const last,
387  CALLBACKS && cbacks ) const
388  {
390 
391  INDEX_TYPE const setSize = sizeOfSet( i );
392  T * const setValues = getSetValues( i );
393 
394  INDEX_TYPE const nInserted = sortedArrayManipulation::insert( setValues,
395  setSize,
396  first,
397  last,
398  std::forward< CALLBACKS >( cbacks ) );
399  this->m_sizes[i] += nInserted;
400  return nInserted;
401  }
402 
411  template< typename CALLBACKS >
412  LVARRAY_HOST_DEVICE inline
413  bool removeFromSetImpl( INDEX_TYPE const i, T const & value, CALLBACKS && cbacks ) const
414  {
416 
417  INDEX_TYPE const setSize = sizeOfSet( i );
418  T * const setValues = getSetValues( i );
419 
420  bool const success = sortedArrayManipulation::remove( setValues, setSize, value, std::move( cbacks ) );
421  this->m_sizes[i] -= success;
422  return success;
423  }
424 
436  template< typename ITER, typename CALLBACKS >
437  LVARRAY_HOST_DEVICE inline
438  INDEX_TYPE_NC removeFromSetImpl( INDEX_TYPE const i,
439  ITER const first,
440  ITER const last,
441  CALLBACKS && cbacks ) const
442  {
444 
445  INDEX_TYPE const setSize = sizeOfSet( i );
446  T * const setValues = getSetValues( i );
447 
448  INDEX_TYPE const nRemoved = sortedArrayManipulation::remove( setValues,
449  setSize,
450  first,
451  last,
452  std::forward< CALLBACKS >( cbacks ) );
453  this->m_sizes[i] -= nRemoved;
454  return nRemoved;
455  }
456 
458 
459 private:
460 
465  class CallBacks : public sortedArrayManipulation::CallBacks< T >
466  {
467 public:
468 
474  LVARRAY_HOST_DEVICE inline
475  CallBacks( ArrayOfSetsView const & aos, INDEX_TYPE const i ):
476  m_aos( aos ),
477  m_indexOfSet( i )
478  {}
479 
488  LVARRAY_HOST_DEVICE inline
489  T * incrementSize( T * const curPtr, INDEX_TYPE const nToAdd ) const
490  {
491  LVARRAY_UNUSED_VARIABLE( curPtr );
492 #ifdef LVARRAY_BOUNDS_CHECK
493  LVARRAY_ERROR_IF_GT_MSG( m_aos.sizeOfSet( m_indexOfSet ) + nToAdd, m_aos.capacityOfSet( m_indexOfSet ),
494  "ArrayOfSetsView cannot do reallocation." );
495 #else
496  LVARRAY_DEBUG_VAR( nToAdd );
497 #endif
498  return m_aos.getSetValues( m_indexOfSet );
499  }
500 
501 private:
503  ArrayOfSetsView const & m_aos;
504 
506  INDEX_TYPE const m_indexOfSet;
507  };
508 };
509 
510 } // namespace LvArray
#define LVARRAY_UNUSED_VARIABLE(X)
Mark X as an unused variable, used to silence compiler warnings.
Definition: Macros.hpp:51
INDEX_TYPE_NC removeFromSet(INDEX_TYPE const i, ITER const first, ITER const last) const
Removes multiple values from the given set.
constexpr T & operator()(INDEX_TYPE const i, INDEX_TYPE const j) const
#define ARRAYOFARRAYS_CHECK_BOUNDS(i)
Check that i is a valid array index.
bool isSortedUnique(ITER first, ITER const last, Compare &&comp=Compare())
INDEX_TYPE_NC insertIntoSet(INDEX_TYPE const i, ITER const first, ITER const last) const
Inserts multiple values into 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.
#define LVARRAY_ERROR_IF(EXP, MSG)
Abort execution if EXP is true.
Definition: Macros.hpp:101
This class serves to provide a sliced multidimensional interface to the family of LvArray classes...
Definition: ArraySlice.hpp:89
constexpr INDEX_TYPE_NC sizeOfSet(INDEX_TYPE const i) const
constexpr ArrayOfSetsView< T, INDEX_TYPE const, BUFFER_TYPE > toView() const
void move(MemorySpace const space, bool touch=true) const
Move this ArrayOfArrays to the given memory space.
Contains templates useful for type manipulation.
bool insertIntoSet(INDEX_TYPE const i, T const &value) const
Insert a value into the given set.
T value_type
An alias for the type contained in the inner arrays, here for stl compatability.
Contains the implementation of LvArray::ArraySlice.
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.
constexpr ArraySlice< T, 1, 0, INDEX_TYPE_NC > operator[](INDEX_TYPE const i) const
INDEX_TYPE_NC insertIntoSetImpl(INDEX_TYPE const i, ITER const first, ITER const last, CALLBACKS &&cbacks) const
Inserts multiple values into the given set.
constexpr ArraySlice< T, 1, 0, INDEX_TYPE_NC > getSetValues(INDEX_TYPE const i) const
constexpr ArraySlice< T const, 1, 0, INDEX_TYPE_NC > operator[](INDEX_TYPE const i) const
constexpr ArrayOfSetsView< T const, INDEX_TYPE const, BUFFER_TYPE > toViewConst() const
#define LVARRAY_ERROR_IF_GT(lhs, rhs)
Raise a hard error if one value compares greater than the other.
Definition: Macros.hpp:252
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.
bool contains(INDEX_TYPE const i, T const &value) const
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.
constexpr INDEX_TYPE_NC capacityOfSet(INDEX_TYPE const i) const
void move(MemorySpace const space, bool const touch=true) const
Move this ArrayOfSets to the given memory space.
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.
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:57
MemorySpace
An enum containing the available memory spaces.
bool contains(T const *const LVARRAY_RESTRICT ptr, std::ptrdiff_t const size, T const &value, Compare &&comp=Compare())
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:245
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.
bool removeFromSetImpl(INDEX_TYPE const i, T const &value, CALLBACKS &&cbacks) const
Helper function to remove a value from the given set.
void consistencyCheck() const
Verify that the capacity of each set is greater than or equal to the size and that each set is sorted...
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.
ArrayOfSetsView & operator=(ArrayOfSetsView const &)=default
Default copy assignment operator, this does a shallow copy.
INDEX_TYPE_NC removeFromSetImpl(INDEX_TYPE const i, ITER const first, ITER const last, CALLBACKS &&cbacks) const
Removes multiple values from the given set.
bool removeFromSet(INDEX_TYPE const i, T const &value) const
Remove a value from the given set.
std::conditional_t< CONST_SIZES, INDEX_TYPE const, INDEX_TYPE_NC > SIZE_TYPE
The type contained by the m_sizes buffer.
constexpr T const & operator()(INDEX_TYPE const i, INDEX_TYPE const j) const
constexpr ArrayOfArraysView< T const, INDEX_TYPE const, true, BUFFER_TYPE > toArrayOfArraysView() const
ArrayOfSetsView()=default
A constructor to create an uninitialized ArrayOfSetsView.
#define LVARRAY_HOST_DEVICE
Mark a function for both host and device usage.
Definition: Macros.hpp:389