LvArray
PySortedArray.hpp
Go to the documentation of this file.
1 /*
2  *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3  * Copyright (c) 2019, Lawrence Livermore National Security, LLC.
4  *
5  * Produced at the Lawrence Livermore National Laboratory
6  *
7  * LLNL-CODE-746361
8  *
9  * All rights reserved. See COPYRIGHT for details.
10  *
11  * This file is part of the GEOSX Simulation Framework.
12  *
13  * GEOSX is a free software; you can redistribute it and/or modify it under
14  * the terms of the GNU Lesser General Public License (as published by the
15  * Free Software Foundation) version 2.1 dated February 1999.
16  *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
17  */
18 
23 #pragma once
24 
25 // Source includes
27 #include "numpyHelpers.hpp"
28 #include "../SortedArray.hpp"
29 #include "../Macros.hpp"
30 #include "../limits.hpp"
31 #include "../output.hpp"
32 
33 // System includes
34 #include <string>
35 #include <memory>
36 #include <typeindex>
37 
38 namespace LvArray
39 {
40 namespace python
41 {
42 namespace internal
43 {
44 
50 {
51 public:
52 
56  virtual ~PySortedArrayWrapperBase() = default;
57 
62  virtual int getAccessLevel() const
63  { return m_accessLevel; }
64 
70  virtual void setAccessLevel( int const accessLevel, int const memorySpace ) = 0;
71 
76  virtual std::string repr() const = 0;
77 
82  virtual std::type_index valueType() const = 0;
83 
91  virtual long long insert( void const * const values, long long const nVals ) = 0;
92 
100  virtual long long remove( void const * const values, long long const nVals ) = 0;
101 
106  virtual PyObject * toNumPy() = 0;
107 
108 protected:
109 
114  m_accessLevel( static_cast< int >( LvArray::python::PyModify::READ_ONLY ) )
115  {}
116 
119 };
120 
130 template< typename T,
131  typename INDEX_TYPE,
132  template< typename > class BUFFER_TYPE >
134 {
135 public:
136 
143  m_sortedArray( sortedArray )
144  {}
145 
147  virtual ~PySortedArrayWrapper() = default;
148 
150  virtual void setAccessLevel( int const accessLevel, int const memorySpace ) final override
151  {
152  LVARRAY_UNUSED_VARIABLE( memorySpace );
153  if( accessLevel >= static_cast< int >( LvArray::python::PyModify::RESIZEABLE ) )
154  {
155  // touch
156  }
157  m_accessLevel = accessLevel;
158  }
159 
161  virtual std::string repr() const final override
162  { return system::demangleType< SortedArray< T, INDEX_TYPE, BUFFER_TYPE > >(); }
163 
165  virtual std::type_index valueType() const final override
166  { return std::type_index( typeid( T ) ); }
167 
169  virtual long long insert( void const * const values, long long const nVals ) final override
170  {
171  T const * const castedValues = reinterpret_cast< T const * >( values );
172  return integerConversion< long long >( m_sortedArray.insert( castedValues, castedValues + nVals ) );
173  }
174 
176  virtual long long remove( void const * const values, long long const nVals ) final override
177  {
178  T const * const castedValues = reinterpret_cast< T const * >( values );
179  return integerConversion< long long >( m_sortedArray.remove( castedValues, castedValues + nVals ) );
180  }
181 
183  virtual PyObject * toNumPy() final override
184  {
185  INDEX_TYPE const dims = m_sortedArray.size();
186  INDEX_TYPE const strides = 1;
187  return createNumPyArray( m_sortedArray.data(), false, 1, &dims, &strides );
188  }
189 
190 private:
193 };
194 
200 PyObject * create( std::unique_ptr< internal::PySortedArrayWrapperBase > && sortedArray );
201 
202 } // namespace internal
203 
214 template< typename T, typename INDEX_TYPE, template< typename > class BUFFER_TYPE >
215 std::enable_if_t< internal::canExportToNumpy< T >, PyObject * >
217 {
218  auto tmp = std::make_unique< internal::PySortedArrayWrapper< T, INDEX_TYPE, BUFFER_TYPE > >( sortedArray );
219  return internal::create( std::move( tmp ) );
220 }
221 
226 PyTypeObject * getPySortedArrayType();
227 
228 } // namespace python
229 } // namespace LvArray
#define LVARRAY_UNUSED_VARIABLE(X)
Mark X as an unused variable, used to silence compiler warnings.
Definition: Macros.hpp:79
std::enable_if_t< internal::canExportToNumpy< T >, PyObject *> create(T &value)
Create a NumPy 1D array of length 1 containing the scalar value.
Definition: numpyHelpers.hpp:147
PyModify
An enumeration of the various access policies for Python objects.
Definition: pythonHelpers.hpp:136
virtual void setAccessLevel(int const accessLevel, int const memorySpace) final override
Set the access level for the SortedArray.
Definition: PySortedArray.hpp:150
PySortedArrayWrapper(SortedArray< T, INDEX_TYPE, BUFFER_TYPE > &sortedArray)
Construct a new Python wrapper around sortedArray.
Definition: PySortedArray.hpp:141
virtual void setAccessLevel(int const accessLevel, int const memorySpace)=0
Set the access level for the SortedArray.
virtual PyObject * toNumPy()=0
Return an immutable NumPy array representing the set.
virtual std::type_index valueType() const final override
Return the type of the values stored in the sub-arrays.
Definition: PySortedArray.hpp:165
This class provides an interface similar to an std::set.
Definition: SortedArray.hpp:35
PyTypeObject * getPySortedArrayType()
Return the Python type for the SortedArray.
virtual PyObject * toNumPy() final override
Return an immutable NumPy array representing the set.
Definition: PySortedArray.hpp:183
virtual ~PySortedArrayWrapperBase()=default
Default destructor.
std::enable_if_t< internal::canExportToNumpy< T >, PyObject *> create(SortedArray< T, INDEX_TYPE, BUFFER_TYPE > &sortedArray)
Create a Python object corresponding to sortedArray.
Definition: PySortedArray.hpp:216
SortedArray< T, INDEX_TYPE, BUFFER_TYPE > & m_sortedArray
The wrapped SortedArray.
Definition: PySortedArray.hpp:192
Forward declarations of Python Objects.
virtual std::type_index valueType() const =0
Return the type of the values stored in the sub-arrays.
Provides a concrete implementation of PySortedArrayWrapperBase.
Definition: PySortedArray.hpp:133
virtual long long insert(void const *const values, long long const nVals)=0
Insert values into the set.
The top level namespace.
Definition: Array.hpp:24
Contains methods to help with conversion to python objects.
Provides a virtual Python wrapper around a SortedArray.
Definition: PySortedArray.hpp:49
PySortedArrayWrapperBase()
Construct an empty PySortedArrayWrapperBase.
Definition: PySortedArray.hpp:113
virtual int getAccessLevel() const
Return the access level for the SortedArray.
Definition: PySortedArray.hpp:62
int m_accessLevel
The access level for the SortedArray.
Definition: PySortedArray.hpp:118
virtual std::string repr() const final override
Return a string representing the underlying SortedArray type.
Definition: PySortedArray.hpp:161
virtual long long insert(void const *const values, long long const nVals) final override
Insert values into the set.
Definition: PySortedArray.hpp:169
std::enable_if_t< internal::canExportToNumpy< T >, PyObject *> createNumPyArray(T *const data, bool const modify, int const ndim, INDEX_TYPE const *const dimsPtr, INDEX_TYPE const *const stridesPtr)
Return a NumPy ndarray view of data.
Definition: numpyHelpers.hpp:107
virtual std::string repr() const =0
Return a string representing the underlying SortedArray type.