GEOSX
SortedArray.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 "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
114  SortedArray & operator=( SortedArray && src ) = default;
115 
117 
121 
129  LVARRAY_HOST_DEVICE inline
131  { return ParentClass::toView(); }
132 
140  LVARRAY_HOST_DEVICE inline
142 
149  LVARRAY_HOST_DEVICE inline
151  { return ParentClass::toViewConst(); }
152 
160  LVARRAY_HOST_DEVICE inline
162 
163  using ParentClass::toSlice;
164 
166 
170 
172  using ParentClass::empty;
173 
180  LVARRAY_HOST_DEVICE constexpr inline
181  INDEX_TYPE size() const
182  { return ParentClass::size(); }
183 
184  using ParentClass::contains;
185  using ParentClass::count;
186 
188 
192 
194  using ParentClass::operator[];
195  using ParentClass::operator();
196 
203  LVARRAY_HOST_DEVICE constexpr inline
204  T const * data() const
205  { return ParentClass::data(); }
206 
207  using ParentClass::begin;
208  using ParentClass::end;
209 
211 
215 
222  inline
223  bool insert( T const & value )
224  {
225  bool const success = sortedArrayManipulation::insert( this->m_values.data(),
226  size(),
227  value,
228  CallBacks( this->m_values, size() ) );
229  this->m_size += success;
230  return success;
231  }
232 
241  template< typename ITER >
242  INDEX_TYPE insert( ITER const first, ITER const last )
243  {
244  INDEX_TYPE const nInserted = sortedArrayManipulation::insert( this->m_values.data(),
245  size(),
246  first,
247  last,
248  CallBacks( this->m_values, size() ) );
249  this->m_size += nInserted;
250  return nInserted;
251  }
252 
258  inline
259  bool remove( T const & value )
260  {
261  bool const success = sortedArrayManipulation::remove( this->m_values.data(),
262  size(),
263  value,
264  CallBacks( this->m_values, size() ) );
265  this->m_size -= success;
266  return success;
267  }
268 
277  template< typename ITER >
278  INDEX_TYPE remove( ITER const first, ITER const last )
279  {
280  INDEX_TYPE const nRemoved = sortedArrayManipulation::remove( this->m_values.data(),
281  size(),
282  first,
283  last,
284  CallBacks( this->m_values, size() ) );
285  this->m_size -= nRemoved;
286  return nRemoved;
287  }
288 
290 
294 
299  inline
300  void clear()
301  {
303  this->m_size = 0;
304  }
305 
310  inline
311  void reserve( INDEX_TYPE const nVals )
312  { bufferManipulation::reserve( this->m_values, size(), nVals ); }
313 
315 
319 
327  inline
328  void move( MemorySpace const space, bool const touch=true ) const
329  { ParentClass::move( space, touch ); }
330 
332 
337  void setName( std::string const & name )
338  { this->m_values.template setName< decltype( *this ) >( name ); }
339 
340 private:
341 
346  class CallBacks : public sortedArrayManipulation::CallBacks< T >
347  {
348 public:
349 
355  inline
356  CallBacks( BUFFER_TYPE< T > & buffer, INDEX_TYPE const size ):
357  m_buffer( buffer ),
358  m_size( size )
359  {}
360 
368  inline
369  T * incrementSize( T * const curPtr,
370  INDEX_TYPE const nToAdd ) const
371  {
372  LVARRAY_UNUSED_VARIABLE( curPtr );
373  bufferManipulation::dynamicReserve( m_buffer, m_size, m_size + nToAdd );
374  return m_buffer.data();
375  }
376 
377 private:
379  BUFFER_TYPE< T > & m_buffer;
380 
382  INDEX_TYPE const m_size;
383  };
384 };
385 
389 template< class >
390 constexpr bool isSortedArray = false;
391 
398 template< class T,
399  class INDEX_TYPE,
400  template< typename > class BUFFER_TYPE >
401 constexpr bool isSortedArray< SortedArray< T, INDEX_TYPE, BUFFER_TYPE > > = true;
402 
403 
404 } // namespace LvArray
#define LVARRAY_UNUSED_VARIABLE(X)
Mark X as an unused variable, used to silence compiler warnings.
Definition: Macros.hpp:51
void move(MemorySpace const space, bool touch=true) const
Moves the SortedArrayView to the given execution space.
bool contains(T const &value) const
void reserve(BUFFER &buf, std::ptrdiff_t const size, std::ptrdiff_t const newCapacity)
Reserve space in the buffer for at least the given capacity.
constexpr ArraySlice< T const, 1, 0, INDEX_TYPE > toSlice() const &
BUFFER_TYPE< T > m_values
Holds the array of values.
constexpr SortedArrayView< T const, INDEX_TYPE, BUFFER_TYPE > toViewConst() const
SortedArray & operator=(SortedArray const &src)
Copy assignment operator, performs a deep copy.
INDEX_TYPE m_size
The number of values.
void move(MemorySpace const space, bool const touch=true) const
Moves the SortedArrayView to the given execution space.
SortedArrayView< T const, INDEX_TYPE, BUFFER_TYPE > toView() const &
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...
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
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.
constexpr T const * begin() const
constexpr bool isSortedArray
True if the template type is a SortedArray.
bool insert(T const &value)
Insert the given value into the array if it doesn&#39;t already exist.
bool count(T const &value) const
INDEX_TYPE size_type
The integer type used for indexing, here for stl compatability.
SortedArrayView< T const, INDEX_TYPE, BUFFER_TYPE > toViewConst() const &
constexpr SortedArrayView< T const, INDEX_TYPE, BUFFER_TYPE > toView() const
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.
void free(BUFFER &buf, std::ptrdiff_t const size)
Destroy the values in the buffer and free it&#39;s memory.
constexpr T const * data() const
constexpr T const * end() 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 size() const
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.
T value_type
The type of the values contained in the SortedArrayView, here for stl compatability.
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.
constexpr INDEX_TYPE size() const
MemorySpace
An enum containing the available memory spaces.
The top level namespace.
Definition: Array.hpp:24
void setName(std::string const &name)
Set the name to be displayed whenever the underlying Buffer&#39;s user call back is called.
Contains the implementation of LvArray::SortedArrayView.
void clear()
Remove all the values from the array.
INDEX_TYPE IndexType
The integer type used for indexing.
T ValueType
The type of the values contained in the SortedArrayView.
constexpr bool empty() const
constexpr T const * data() const
std::string string
String type.
Definition: DataTypes.hpp:131
void reserve(INDEX_TYPE const nVals)
Reserve space to store the given number of values without resizing.
This class provides a view into a SortedArray.
void resize(BUFFER &buf, std::ptrdiff_t const size, std::ptrdiff_t const newSize, ARGS &&... args)
Resize the buffer to the given size.
#define LVARRAY_HOST_DEVICE
Mark a function for both host and device usage.
Definition: Macros.hpp:389