GEOSX
SortedArrayView.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 "ArraySlice.hpp"
17 #include "bufferManipulation.hpp"
19 
20 #ifdef LVARRAY_BOUNDS_CHECK
21 
27 #define SORTEDARRAY_CHECK_BOUNDS( index ) \
28  LVARRAY_ERROR_IF( index < 0 || index >= size(), \
29  "Array Bounds Check Failed: index=" << index << " size()=" << size())
30 
31 #else // LVARRAY_BOUNDS_CHECK
32 
38 #define SORTEDARRAY_CHECK_BOUNDS( index )
39 
40 #endif // LVARRAY_BOUNDS_CHECK
41 
42 namespace LvArray
43 {
44 
55 template< typename T,
56  typename INDEX_TYPE,
57  template< typename > class BUFFER_TYPE >
59 {
60 public:
61 
63  using ValueType = T;
64 
66  using IndexType = INDEX_TYPE;
67 
69  using value_type = T;
70 
72  using size_type = INDEX_TYPE;
73 
77 
84  SortedArrayView( SortedArrayView const & src ) = default;
85 
90  LVARRAY_HOST_DEVICE constexpr inline
92  m_values( std::move( src.m_values ) ),
93  m_size( src.m_size )
94  {
95  src.m_size = 0;
96  }
97 
103  LVARRAY_HOST_DEVICE constexpr inline
104  SortedArrayView( INDEX_TYPE const size, BUFFER_TYPE< T > const & buffer ):
105  m_values( buffer ),
106  m_size( size )
107  {}
108 
114  SortedArrayView & operator=( SortedArrayView const & src ) = default;
115 
121  LVARRAY_HOST_DEVICE constexpr inline
123  {
124  m_values = std::move( src.m_values );
125  m_size = src.m_size;
126  src.m_size = 0;
127  return *this;
128  }
129 
131 
135 
140  LVARRAY_HOST_DEVICE constexpr inline
142  toView() const
144 
148  LVARRAY_HOST_DEVICE constexpr inline
150  toViewConst() const
151  { return toView(); }
152 
156  LVARRAY_HOST_DEVICE constexpr inline
158  { return ArraySlice< T const, 1, 0, INDEX_TYPE >( data(), &m_size, nullptr ); }
159 
167  LVARRAY_HOST_DEVICE constexpr inline
169 
171 
175 
180  LVARRAY_HOST_DEVICE constexpr inline
181  bool empty() const
182  { return size() == 0; }
183 
187  LVARRAY_HOST_DEVICE constexpr inline
188  INDEX_TYPE size() const
189  { return m_size; }
190 
195  LVARRAY_HOST_DEVICE inline
196  bool contains( T const & value ) const
197  { return sortedArrayManipulation::contains( data(), size(), value ); }
198 
204  LVARRAY_HOST_DEVICE inline
205  bool count( T const & value ) const
206  { return contains( value ); }
207 
209 
213 
220  T const & operator[]( INDEX_TYPE const i ) const
221  {
223  return data()[ i ];
224  }
225 
231  T const & operator()( INDEX_TYPE const i ) const
232  {
234  return data()[ i ];
235  }
236 
240  LVARRAY_HOST_DEVICE constexpr inline
241  T const * data() const
242  { return m_values.data(); }
243 
247  LVARRAY_HOST_DEVICE constexpr inline
248  T const * begin() const
249  { return data(); }
250 
254  LVARRAY_HOST_DEVICE constexpr inline
255  T const * end() const
256  { return data() + size(); }
257 
259 
263 
272  inline
273  void move( MemorySpace const space, bool touch=true ) const
274  {
275  #if defined(LVARRAY_USE_CUDA)
276  if( space == MemorySpace::GPU ) touch = false;
277  #endif
278  m_values.move( space, touch );
279  }
280 
282 
283 protected:
284 
291  m_values( true )
292  {}
293 
295  BUFFER_TYPE< T > m_values;
296 
298  INDEX_TYPE m_size = 0;
299 };
300 
304 template< class >
305 constexpr bool isSortedArrayView = false;
306 
313 template< class T,
314  class INDEX_TYPE,
315  template< typename > class BUFFER_TYPE >
316 constexpr bool isSortedArrayView< SortedArrayView< T, INDEX_TYPE, BUFFER_TYPE > > = true;
317 
318 } // namespace LvArray
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.
bool contains(T const &value) const
constexpr ArraySlice< T const, 1, 0, INDEX_TYPE > toSlice() const &
BUFFER_TYPE< T > m_values
Holds the array of values.
constexpr SortedArrayView(SortedArrayView &&src)
Default move constructor, performs a shallow copy.
constexpr SortedArrayView< T const, INDEX_TYPE, BUFFER_TYPE > toViewConst() const
This class serves to provide a sliced multidimensional interface to the family of LvArray classes...
Definition: ArraySlice.hpp:89
constexpr T const & operator()(INDEX_TYPE const i) const
INDEX_TYPE m_size
The number of values.
constexpr T const * begin() const
#define CONSTEXPR_WITHOUT_BOUNDS_CHECK
Expands to constexpr when array bound checking is disabled.
Definition: Macros.hpp:449
bool count(T const &value) const
Contains the implementation of LvArray::ArraySlice.
INDEX_TYPE size_type
The integer type used for indexing, here for stl compatability.
constexpr SortedArrayView< T const, INDEX_TYPE, BUFFER_TYPE > toView() const
SortedArrayView()
Default constructor.
Contains functions for manipulating buffers.
constexpr T const * end() const
constexpr INDEX_TYPE size() const
constexpr bool isSortedArrayView
True if the template type is a SortedArrayView.
localIndex const value_type
The type of the values contained in the SortedArrayView, here for stl compatability.
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
constexpr T const & operator[](INDEX_TYPE const i) const
This file contains common sorted array manipulation routines. Aside from the functions that take a ca...
INDEX_TYPE IndexType
The integer type used for indexing.
constexpr SortedArrayView(INDEX_TYPE const size, BUFFER_TYPE< T > const &buffer)
Construct a new SortedArrayView from the given buffer.
localIndex const ValueType
The type of the values contained in the SortedArrayView.
constexpr bool empty() const
constexpr SortedArrayView & operator=(SortedArrayView &&src)
Default move assignment operator, this does a shallow copy.
constexpr T const * data() const
This class provides a view into a SortedArray.
#define LVARRAY_HOST_DEVICE
Mark a function for both host and device usage.
Definition: Macros.hpp:389
#define SORTEDARRAY_CHECK_BOUNDS(index)
Check that index falls within the size of the first dimension.