LvArray
SortedArray.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 "SortedArrayView.hpp"
16 
17 namespace LvArray
18 {
19 
32 template< typename T,
33  typename INDEX_TYPE,
34  template< typename > class BUFFER_TYPE >
35 class SortedArray : protected SortedArrayView< T, INDEX_TYPE, BUFFER_TYPE >
36 {
37 public:
38 
41 
42  // Alias public typedefs of SortedArrayView.
43  using typename ParentClass::ValueType;
44  using typename ParentClass::IndexType;
45  using typename ParentClass::value_type;
46  using typename ParentClass::size_type;
47 
50 
56 
57  // Alias public methods of SortedArrayView.
58 
62 
67  inline
69  ParentClass()
70  { setName( "" ); }
71 
76  inline
77  SortedArray( SortedArray const & src ):
78  ParentClass()
79  { *this = src; }
80 
85  inline
86  SortedArray( SortedArray && src ) = default;
87 
91  inline
93  { bufferManipulation::free( this->m_values, size() ); }
94 
100  inline
102  {
103  bufferManipulation::copyInto( this->m_values, size(), src.m_values, src.size() );
104  this->m_size = src.size();
105  return *this;
106  }
107 
113  inline
115  {
117  ParentClass::operator=( std::move( src ) );
118  return *this;
119  }
120 
122 
126 
134  LVARRAY_HOST_DEVICE inline
136  { return ParentClass::toView(); }
137 
145  LVARRAY_HOST_DEVICE inline
147 
154  LVARRAY_HOST_DEVICE inline
156  { return ParentClass::toViewConst(); }
157 
165  LVARRAY_HOST_DEVICE inline
167 
168  using ParentClass::toSlice;
169 
171 
175 
177  using ParentClass::empty;
178 
185  LVARRAY_HOST_DEVICE constexpr inline
186  INDEX_TYPE size() const
187  { return ParentClass::size(); }
188 
189  using ParentClass::contains;
190  using ParentClass::count;
191 
193 
197 
199  using ParentClass::operator[];
200  using ParentClass::operator();
201 
208  LVARRAY_HOST_DEVICE constexpr inline
209  T const * data() const
210  { return ParentClass::data(); }
211 
212  using ParentClass::begin;
213  using ParentClass::end;
214 
216 
220 
227  inline
228  bool insert( T const & value )
229  {
230  bool const success = sortedArrayManipulation::insert( this->m_values.data(),
231  size(),
232  value,
233  CallBacks( this->m_values, size() ) );
234  this->m_size += success;
235  return success;
236  }
237 
246  template< typename ITER >
247  INDEX_TYPE insert( ITER const first, ITER const last )
248  {
249  INDEX_TYPE const nInserted = sortedArrayManipulation::insert( this->m_values.data(),
250  size(),
251  first,
252  last,
253  CallBacks( this->m_values, size() ) );
254  this->m_size += nInserted;
255  return nInserted;
256  }
257 
263  inline
264  bool remove( T const & value )
265  {
266  bool const success = sortedArrayManipulation::remove( this->m_values.data(),
267  size(),
268  value,
269  CallBacks( this->m_values, size() ) );
270  this->m_size -= success;
271  return success;
272  }
273 
282  template< typename ITER >
283  INDEX_TYPE remove( ITER const first, ITER const last )
284  {
285  INDEX_TYPE const nRemoved = sortedArrayManipulation::remove( this->m_values.data(),
286  size(),
287  first,
288  last,
289  CallBacks( this->m_values, size() ) );
290  this->m_size -= nRemoved;
291  return nRemoved;
292  }
293 
295 
299 
304  inline
305  void clear()
306  {
308  this->m_size = 0;
309  }
310 
315  inline
316  void reserve( INDEX_TYPE const nVals )
317  { bufferManipulation::reserve( this->m_values, size(), MemorySpace::host, nVals ); }
318 
320 
324 
332  inline
333  void move( MemorySpace const space, bool const touch=true ) const
334  { ParentClass::move( space, touch ); }
335 
337 
342  void setName( std::string const & name )
343  { this->m_values.template setName< decltype( *this ) >( name ); }
344 
345 private:
346 
352  {
353 public:
354 
360  inline
361  CallBacks( BUFFER_TYPE< T > & buffer, INDEX_TYPE const size ):
362  m_buffer( buffer ),
363  m_size( size )
364  {}
365 
373  inline
374  T * incrementSize( T * const curPtr,
375  INDEX_TYPE const nToAdd ) const
376  {
377  LVARRAY_UNUSED_VARIABLE( curPtr );
379  return m_buffer.data();
380  }
381 
382 private:
384  BUFFER_TYPE< T > & m_buffer;
385 
387  INDEX_TYPE const m_size;
388  };
389 };
390 
394 template< class >
395 constexpr bool isSortedArray = false;
396 
403 template< class T,
404  class INDEX_TYPE,
405  template< typename > class BUFFER_TYPE >
406 constexpr bool isSortedArray< SortedArray< T, INDEX_TYPE, BUFFER_TYPE > > = true;
407 
408 
409 } // 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 void resize(BUFFER &buf, std::ptrdiff_t const size, std::ptrdiff_t const newSize, ARGS &&... args)
Resize the buffer to the given size.
Definition: bufferManipulation.hpp:272
LVARRAY_HOST_DEVICE constexpr SortedArrayView< T const, INDEX_TYPE, BUFFER_TYPE > toViewConst() const
Definition: SortedArrayView.hpp:150
SortedArrayView & operator=(SortedArrayView const &src)=default
Default copy assignment operator, this does a shallow copy.
void move(MemorySpace const space, bool touch=true) const
Moves the SortedArrayView to the given execution space.
Definition: SortedArrayView.hpp:273
BUFFER_TYPE< T > m_values
Holds the array of values.
Definition: SortedArrayView.hpp:298
SortedArray & operator=(SortedArray const &src)
Copy assignment operator, performs a deep copy.
Definition: SortedArray.hpp:101
INDEX_TYPE m_size
The number of values.
Definition: SortedArrayView.hpp:301
void move(MemorySpace const space, bool const touch=true) const
Moves the SortedArrayView to the given execution space.
Definition: SortedArray.hpp:333
void dynamicReserve(BUFFER &buf, std::ptrdiff_t const size, std::ptrdiff_t const newCapacity)
If the buffer&#39;s capacity is greater than newCapacity this is a no-op. Otherwise the buffer&#39;s capacity...
Definition: bufferManipulation.hpp:251
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
This class provides an interface similar to an std::set.
Definition: SortedArray.hpp:35
~SortedArray()
Destructor, frees the values array.
Definition: SortedArray.hpp:92
SortedArray()
Default constructor.
Definition: SortedArray.hpp:68
constexpr bool isSortedArray
True if the template type is a SortedArray.
Definition: SortedArray.hpp:395
LVARRAY_HOST_DEVICE constexpr SortedArrayView< T const, INDEX_TYPE, BUFFER_TYPE > toView() const
Definition: SortedArrayView.hpp:142
bool insert(T const &value)
Insert the given value into the array if it doesn&#39;t already exist.
Definition: SortedArray.hpp:228
LVARRAY_HOST_DEVICE constexpr INDEX_TYPE size() const
Definition: SortedArray.hpp:186
INDEX_TYPE size_type
The integer type used for indexing, here for stl compatability.
Definition: SortedArrayView.hpp:72
LVARRAY_HOST_DEVICE bool contains(T const &value) const
Definition: SortedArrayView.hpp:196
SortedArray(SortedArray const &src)
The copy constructor, performs a deep copy.
Definition: SortedArray.hpp:77
This class provides a no-op callbacks interface for the ArrayManipulation sorted routines.
Definition: sortedArrayManipulation.hpp:72
CallBacks(BUFFER_TYPE< T > &buffer, INDEX_TYPE const size)
Constructor.
Definition: SortedArray.hpp:361
LVARRAY_HOST_DEVICE bool count(T const &value) const
Definition: SortedArrayView.hpp:205
DISABLE_HD_WARNING LVARRAY_HOST_DEVICE void free(BUFFER &buf, std::ptrdiff_t const size)
Destroy the values in the buffer and free it&#39;s memory.
Definition: bufferManipulation.hpp:188
T value_type
The type of the values contained in the SortedArrayView, here for stl compatability.
Definition: SortedArrayView.hpp:69
LVARRAY_HOST_DEVICE constexpr T const * end() const
Definition: SortedArrayView.hpp:255
camp::resources::Platform MemorySpace
an alias for camp::resources::Platform.
Definition: bufferManipulation.hpp:31
INDEX_TYPE insert(ITER const first, ITER const last)
Insert the values in [ first, last ) into the array if they don&#39;t already exist.
Definition: SortedArray.hpp:247
LVARRAY_HOST_DEVICE constexpr T const * data() const
Definition: SortedArrayView.hpp:241
This class provides the callbacks for the sortedArrayManipulation sorted routines.
Definition: SortedArray.hpp:351
The top level namespace.
Definition: Array.hpp:24
LVARRAY_HOST_DEVICE void reserve(BUFFER &buf, std::ptrdiff_t const size, MemorySpace const space, std::ptrdiff_t const newCapacity)
Reserve space in the buffer for at least the given capacity.
Definition: bufferManipulation.hpp:230
SortedArray & operator=(SortedArray &&src)
Default move assignment operator, performs a shallow copy.
Definition: SortedArray.hpp:114
void setName(std::string const &name)
Set the name to be displayed whenever the underlying Buffer&#39;s user call back is called.
Definition: SortedArray.hpp:342
Contains the implementation of LvArray::SortedArrayView.
LVARRAY_HOST_DEVICE SortedArrayView< T const, INDEX_TYPE, BUFFER_TYPE > toViewConst() const &
Definition: SortedArray.hpp:155
T * incrementSize(T *const curPtr, INDEX_TYPE const nToAdd) const
Callback signaling that the size of the array has increased.
Definition: SortedArray.hpp:374
LVARRAY_HOST_DEVICE constexpr ArraySlice< T const, 1, 0, INDEX_TYPE > toSlice() const &
Definition: SortedArrayView.hpp:157
void clear()
Remove all the values from the array.
Definition: SortedArray.hpp:305
INDEX_TYPE IndexType
The integer type used for indexing.
Definition: SortedArrayView.hpp:66
LVARRAY_HOST_DEVICE constexpr T const * data() const
Definition: SortedArray.hpp:209
T ValueType
The type of the values contained in the SortedArrayView.
Definition: SortedArrayView.hpp:63
DISABLE_HD_WARNING LVARRAY_HOST_DEVICE void copyInto(DST_BUFFER &dst, std::ptrdiff_t const dstSize, SRC_BUFFER const &src, std::ptrdiff_t const srcSize)
Copy values from the source buffer into the destination buffer.
Definition: bufferManipulation.hpp:393
void reserve(INDEX_TYPE const nVals)
Reserve space to store the given number of values without resizing.
Definition: SortedArray.hpp:316
LVARRAY_HOST_DEVICE constexpr T const * begin() const
Definition: SortedArrayView.hpp:248
LVARRAY_HOST_DEVICE constexpr bool empty() const
Definition: SortedArrayView.hpp:181
BUFFER_TYPE< T > & m_buffer
The buffer associated with the callback.
Definition: SortedArray.hpp:384
INDEX_TYPE const m_size
The number of values in the buffer.
Definition: SortedArray.hpp:387
LVARRAY_HOST_DEVICE SortedArrayView< T const, INDEX_TYPE, BUFFER_TYPE > toView() const &
Definition: SortedArray.hpp:135
This class provides a view into a SortedArray.
Definition: SortedArrayView.hpp:58
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
LVARRAY_HOST_DEVICE constexpr INDEX_TYPE size() const
Definition: SortedArrayView.hpp:188