LvArray
PyArrayOfSets.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 "../ArrayOfSets.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 ~PyArrayOfSetsWrapperBase() = default;
57 
62  virtual int getAccessLevel() const
63  { return m_accessLevel; }
64 
69  virtual void setAccessLevel( int const accessLevel ) = 0;
70 
75  virtual std::string repr() const = 0;
76 
82  virtual long long size() const = 0;
83 
90  virtual PyObject * operator[]( long long const setIndex ) = 0;
91 
96  virtual std::type_index valueType() const = 0;
97 
103  virtual void eraseSet( long long const setIndex ) = 0;
104 
111  virtual void insertSet( long long const setIndex, long long const capacity ) = 0;
112 
121  virtual long long removeFromSet( long long const setIndex, void const * const values, long long const numVals ) = 0;
122 
131  virtual long long insertIntoSet( long long const setIndex, void const * const values, long long const numVals ) = 0;
132 
133 protected:
134 
139  m_accessLevel( static_cast< int >( LvArray::python::PyModify::READ_ONLY ) )
140  {}
141 
144 };
145 
155 template< typename T,
156  typename INDEX_TYPE,
157  template< typename > class BUFFER_TYPE >
159 {
160 public:
161 
168  m_arrayOfSets( arrayOfSets )
169  {}
170 
172  virtual ~PyArrayOfSetsWrapper() = default;
173 
175  virtual void setAccessLevel( int const accessLevel ) final override
176  {
177  if( accessLevel >= static_cast< int >( LvArray::python::PyModify::RESIZEABLE ) )
178  {
179  // touch
180  }
181  m_accessLevel = accessLevel;
182  }
183 
185  virtual std::string repr() const final override
186  { return system::demangleType< ArrayOfSets< T, INDEX_TYPE, BUFFER_TYPE > >(); }
187 
189  virtual long long size() const final override
190  { return integerConversion< long long >( m_arrayOfSets.size() ); }
191 
193  virtual PyObject * operator[]( long long const setIndex ) final override
194  {
195  INDEX_TYPE convertedIndex = integerConversion< INDEX_TYPE >( setIndex );
196  ArraySlice< T const, 1, 0, INDEX_TYPE > slice = m_arrayOfSets[ convertedIndex ];
197  T const * data = slice;
198  constexpr INDEX_TYPE strides = 1;
199  INDEX_TYPE size = slice.size();
200  return createNumPyArray( data, false, 1, &size, &strides );
201  }
202 
204  virtual std::type_index valueType() const final override
205  { return std::type_index( typeid( T ) ); }
206 
208  virtual void eraseSet( long long const setIndex ) final override
209  {
210  INDEX_TYPE convertedIndex = integerConversion< INDEX_TYPE >( setIndex );
211  m_arrayOfSets.eraseSet( convertedIndex );
212  }
213 
215  virtual void insertSet( long long const setIndex, long long const capacity ) final override
216  {
217  INDEX_TYPE convertedIndex = integerConversion< INDEX_TYPE >( setIndex );
218  INDEX_TYPE convertedCapacity = integerConversion< INDEX_TYPE >( capacity );
219  m_arrayOfSets.insertSet( convertedIndex, convertedCapacity );
220  }
221 
223  virtual long long removeFromSet( long long const setIndex,
224  void const * const values,
225  long long const numVals ) final override
226  {
227  INDEX_TYPE convertedIndex = integerConversion< INDEX_TYPE >( setIndex );
228  T const * begin = static_cast< T const * >( values );
229  T const * end = begin + numVals;
230  return integerConversion< long long >( m_arrayOfSets.removeFromSet( convertedIndex, begin, end ) );
231  }
232 
234  virtual long long insertIntoSet( long long const setIndex,
235  void const * const values,
236  long long const numVals ) final override
237  {
238  INDEX_TYPE convertedSetIndex = integerConversion< INDEX_TYPE >( setIndex );
239  T const * begin = static_cast< T const * >( values );
240  T const * end = begin + numVals;
241  return integerConversion< long long >( m_arrayOfSets.insertIntoSet( convertedSetIndex, begin, end ) );
242  }
243 
244 private:
247 };
248 
254 PyObject * create( std::unique_ptr< internal::PyArrayOfSetsWrapperBase > && arrayOfSets );
255 
256 } // namespace internal
257 
258 
269 template< typename T, typename INDEX_TYPE, template< typename > class BUFFER_TYPE >
270 std::enable_if_t< internal::canExportToNumpy< T >, PyObject * >
272 {
273  auto tmp = std::make_unique< internal::PyArrayOfSetsWrapper< T, INDEX_TYPE, BUFFER_TYPE > >( arrayOfSets );
274  return internal::create( std::move( tmp ) );
275 }
276 
281 PyTypeObject * getPyArrayOfSetsType();
282 
283 } // namespace python
284 } // namespace LvArray
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) final override
Set the access level for the array.
Definition: PyArrayOfSets.hpp:175
virtual void eraseSet(long long const setIndex) final override
Erase the set at index setIndex.
Definition: PyArrayOfSets.hpp:208
virtual std::string repr() const =0
Return a string representing the underlying ArrayOfSetsType.
virtual int getAccessLevel() const
Return the access level for the array.
Definition: PyArrayOfSets.hpp:62
PyArrayOfSetsWrapper(ArrayOfSets< T, INDEX_TYPE, BUFFER_TYPE > &arrayOfSets)
Construct a new Python wrapper around arrayOfSets.
Definition: PyArrayOfSets.hpp:166
int m_accessLevel
access level for the ArrayOfSets.
Definition: PyArrayOfSets.hpp:143
virtual void eraseSet(long long const setIndex)=0
Erase the set at index setIndex.
This class serves to provide a sliced multidimensional interface to the family of LvArray classes...
Definition: ArraySlice.hpp:89
Provides a virtual Python wrapper around an ArrayOfSets.
Definition: PyArrayOfSets.hpp:49
virtual long long size() const =0
Return the size of the array (number of sets).
virtual long long insertIntoSet(long long const setIndex, void const *const values, long long const numVals)=0
Insert values into a set.
std::enable_if_t< internal::canExportToNumpy< T >, PyObject *> create(ArrayOfSets< T, INDEX_TYPE, BUFFER_TYPE > &arrayOfSets)
Create a Python object corresponding to arrayOfSets.
Definition: PyArrayOfSets.hpp:271
virtual std::string repr() const final override
Return a string representing the underlying ArrayOfSetsType.
Definition: PyArrayOfSets.hpp:185
virtual std::type_index valueType() const =0
Return the type of the values stored in the sets.
PyTypeObject * getPyArrayOfSetsType()
Return the Python type for the ArrayOfSets.
Forward declarations of Python Objects.
virtual PyObject * operator[](long long const setIndex)=0
Return an immutable 1D NumPy array representing the set at index setIndex.
virtual long long removeFromSet(long long const setIndex, void const *const values, long long const numVals) final override
Remove values from the a set.
Definition: PyArrayOfSets.hpp:223
virtual long long insertIntoSet(long long const setIndex, void const *const values, long long const numVals) final override
Insert values into a set.
Definition: PyArrayOfSets.hpp:234
PyArrayOfSetsWrapperBase()
Construct an empty PyArrayOfSetsWrapperBase.
Definition: PyArrayOfSets.hpp:138
The top level namespace.
Definition: Array.hpp:24
virtual void insertSet(long long const setIndex, long long const capacity) final override
Insert a new set at index setIndex.
Definition: PyArrayOfSets.hpp:215
This class implements an array of sets like object with contiguous storage.
Definition: ArrayOfArrays.hpp:22
Contains methods to help with conversion to python objects.
Provides a concrete implementation of PyArrayOfSetsWrapperBase.
Definition: PyArrayOfSets.hpp:158
virtual std::type_index valueType() const final override
Return the type of the values stored in the sets.
Definition: PyArrayOfSets.hpp:204
virtual long long removeFromSet(long long const setIndex, void const *const values, long long const numVals)=0
Remove values from the a set.
ArrayOfSets< T, INDEX_TYPE, BUFFER_TYPE > & m_arrayOfSets
The wrapped ArrayOfSets.
Definition: PyArrayOfSets.hpp:246
virtual PyObject * operator[](long long const setIndex) final override
Return an immutable 1D NumPy array representing the set at index setIndex.
Definition: PyArrayOfSets.hpp:193
virtual void insertSet(long long const setIndex, long long const capacity)=0
Insert a new set at index setIndex.
virtual void setAccessLevel(int const accessLevel)=0
Set the access level for the array.
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 long long size() const final override
Return the size of the array (number of sets).
Definition: PyArrayOfSets.hpp:189