-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathPyArrayOfArrays.hpp
More file actions
300 lines (260 loc) · 11.1 KB
/
PyArrayOfArrays.hpp
File metadata and controls
300 lines (260 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Copyright (c) 2019, Lawrence Livermore National Security, LLC.
*
* Produced at the Lawrence Livermore National Laboratory
*
* LLNL-CODE-746361
*
* All rights reserved. See COPYRIGHT for details.
*
* This file is part of the GEOSX Simulation Framework.
*
* GEOSX is a free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License (as published by the
* Free Software Foundation) version 2.1 dated February 1999.
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
/**
* @file
*/
#pragma once
// Source includes
#include "pythonForwardDeclarations.hpp"
#include "numpyHelpers.hpp"
#include "../ArrayOfArrays.hpp"
#include "../Macros.hpp"
#include "../limits.hpp"
#include "../output.hpp"
// System includes
#include <string>
#include <memory>
#include <typeindex>
namespace LvArray
{
namespace python
{
namespace internal
{
/**
* @class PyArrayOfArraysWrapperBase
* @brief Provides a virtual Python wrapper around an ArrayOfArrays.
*/
class PyArrayOfArraysWrapperBase
{
public:
/**
* @brief Default destructor.
*/
virtual ~PyArrayOfArraysWrapperBase() = default;
/**
* @brief Return the access level for the ArrayOfArrays.
* @return The access level for the ArrayOfArrays.
*/
virtual int getAccessLevel() const
{ return m_accessLevel; }
/**
* @brief Set the access level for the ArrayOfArrays.
* @param accessLevel The access level.
*/
virtual void setAccessLevel( int const accessLevel ) = 0;
/**
* @brief Return a string representing the underlying ArrayOfArrays type.
* @return a string representing the underlying ArrayOfArrays type.
*/
virtual std::string repr() const = 0;
/**
* @brief Return the size of the array (number of sub-arrays).
* @return The size of the array (number of sub-arrays).
* @note This wraps ArrayOfArrays::size.
*/
virtual long long size() const = 0;
/**
* @brief Return the size of the array at index @c arrayIndex.
* @param arrayIndex The array to query.
* @return The size of the array at index @c arrayIndex.
* @note This wraps ArrayOfArrays::sizeOfArray.
*/
virtual long long sizeOfArray( long long arrayIndex ) const = 0;
/**
* @brief Return a 1D numpy array representing the array at index @c arrayIndex.
* @param arrayIndex The array to access.
* @return An immutable 1D numpy array representing the array at index @c arrayIndex.
* @note This wraps ArrayOfArrays::operator[].
*/
virtual PyObject * operator[]( long long arrayIndex ) = 0;
/**
* @brief Return the type of the values stored in the sub-arrays.
* @return The type of the values stored in the sub-arrays.
*/
virtual std::type_index valueType() const = 0;
/**
* @brief Erase the array at index @c arrayIndex.
* @param arrayIndex The array to erase.
* @note This wraps ArrayOfArrays::eraseArray.
*/
virtual void eraseArray( long long arrayIndex ) = 0;
/**
* @brief Erase a value from an array.
* @param arrayIndex The index of the array to erase from.
* @param valueIndex The index of the value to erase from the array.
* @note This wraps ArrayOfArrays::eraseFromArray.
*/
virtual void eraseFromArray( long long arrayIndex, long long valueIndex ) = 0;
/**
* @brief Insert a new array.
* @param arrayIndex The index at which to insert the new array.
* @param values The values of the array to insert, must be of type @c valueType() and length @c numVals.
* @param numVals The number of values in the array to insert.
* @note This wraps ArrayOfArrays::insertArray.
*/
virtual void insertArray( long long arrayIndex, void const * values, long long numVals ) = 0;
/**
* @brief Insert new values into an existing array.
* @param arrayIndex The index of the array to insert into.
* @param valueIndex The index at which to insert into the array.
* @param values The values to insert, must be of type @c valueType() and length @c numVals.
* @param numVals The number of values to insert.
* @note This wraps ArrayOfArrays::insertIntoArray.
*/
virtual void insertIntoArray( long long arrayIndex,
long long valueIndex,
void const * values,
long long numVals ) = 0;
protected:
/**
* @brief Construct an empty PyArrayOfArraysWrapperBase.
*/
PyArrayOfArraysWrapperBase():
m_accessLevel( static_cast< int >( LvArray::python::PyModify::READ_ONLY ) )
{}
/// access level for the ArrayOfArrays.
int m_accessLevel;
};
/**
* @class PyArrayOfArraysWrapper
* @brief Provides a concrete implementation of PyArrayOfArraysWrapperBase.
* @tparam T The type of the entries in the ArrayOfArrays.
* @tparam INDEX_TYPE The index type of the ArrayOfArrays.
* @tparam BUFFER_TYPE The buffer type of the ArrayOfArrays.
* @note This holds a reference to the wrapped ArrayOfArrays, you must ensure that the reference remains valid
* for the lifetime of this object.
*/
template< typename T,
typename INDEX_TYPE,
template< typename > class BUFFER_TYPE >
class PyArrayOfArraysWrapper final : public PyArrayOfArraysWrapperBase
{
public:
/**
* @brief Construct a new Python wrapper around @c arrayOfArrays.
* @param arrayOfArrays The ArrayOfArrays to wrap.
*/
PyArrayOfArraysWrapper( ArrayOfArrays< T, INDEX_TYPE, BUFFER_TYPE > & arrayOfArrays ):
PyArrayOfArraysWrapperBase( ),
m_arrayOfArrays( arrayOfArrays )
{}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual ~PyArrayOfArraysWrapper() = default;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual void setAccessLevel( int accessLevel ) final override
{
if( accessLevel >= static_cast< int >( LvArray::python::PyModify::RESIZEABLE ) )
{
// touch
}
m_accessLevel = accessLevel;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual std::string repr() const final override
{ return system::demangleType< ArrayOfArrays< T, INDEX_TYPE, BUFFER_TYPE > >(); }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual long long size() const final override
{ return integerConversion< long long >( m_arrayOfArrays.size() ); }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual long long sizeOfArray( long long arrayIndex ) const final override
{
INDEX_TYPE convertedIndex = integerConversion< INDEX_TYPE >( arrayIndex );
return integerConversion< long long >( m_arrayOfArrays.sizeOfArray( convertedIndex ) );
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual PyObject * operator[]( long long arrayIndex ) final override
{
INDEX_TYPE convertedIndex = integerConversion< INDEX_TYPE >( arrayIndex );
ArraySlice< T, 1, 0, INDEX_TYPE > slice = m_arrayOfArrays[ convertedIndex ];
T * data = slice;
constexpr INDEX_TYPE strides = 1;
INDEX_TYPE size = slice.size();
return createNumPyArray( data, getAccessLevel() >= static_cast< int >( LvArray::python::PyModify::MODIFIABLE ), 1, &size, &strides );
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual std::type_index valueType() const final override
{ return std::type_index( typeid( T ) ); }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual void eraseArray( long long arrayIndex ) final override
{
INDEX_TYPE convertedIndex = integerConversion< INDEX_TYPE >( arrayIndex );
m_arrayOfArrays.eraseArray( convertedIndex );
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual void eraseFromArray( long long arrayIndex, long long valueIndex ) final override
{
INDEX_TYPE convertedIndex = integerConversion< INDEX_TYPE >( arrayIndex );
INDEX_TYPE convertedBegin = integerConversion< INDEX_TYPE >( valueIndex );
m_arrayOfArrays.eraseFromArray( convertedIndex, convertedBegin );
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual void insertArray( long long arrayIndex, void const * values, long long numVals ) final override
{
INDEX_TYPE convertedIndex = integerConversion< INDEX_TYPE >( arrayIndex );
T const * begin = static_cast< T const * >( values );
T const * end = begin + numVals;
m_arrayOfArrays.insertArray( convertedIndex, begin, end );
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual void insertIntoArray( long long arrayIndex,
long long valueIndex,
void const * values,
long long numVals ) final override
{
INDEX_TYPE convertedArray = integerConversion< INDEX_TYPE >( arrayIndex );
INDEX_TYPE convertedIndex = integerConversion< INDEX_TYPE >( valueIndex );
T const * begin = static_cast< T const * >( values );
T const * end = begin + numVals;
m_arrayOfArrays.insertIntoArray( convertedArray, convertedIndex, begin, end );
}
private:
/// The wrapped ArrayOfArrays.
ArrayOfArrays< T, INDEX_TYPE, BUFFER_TYPE > & m_arrayOfArrays;
};
/**
* @brief Create a Python object corresponding to @c arrayOfArrays.
* @param arrayOfArrays The ArrayOfArrays to export to Python.
* @return A Python object corresponding to @c arrayOfArrays.
*/
PyObject * create( std::unique_ptr< internal::PyArrayOfArraysWrapperBase > && arrayOfArrays );
} // namespace internal
/**
* @brief Create a Python object corresponding to @c arrayOfArrays.
* @tparam T The type of the entries in the ArrayOfArrays.
* @tparam INDEX_TYPE The index type of the ArrayOfArrays.
* @tparam BUFFER_TYPE The buffer type of the ArrayOfArrays.
* @param arrayOfArrays The ArrayOfArrays to export to Python.
* @return A Python object corresponding to @c arrayOfArrays.
* @note The returned Python object holds a reference to @c arrayOfArrays, you must ensure
* the reference remains valid throughout the lifetime of the object.
*/
template< typename T, typename INDEX_TYPE, template< typename > class BUFFER_TYPE >
std::enable_if_t< internal::canExportToNumpy< T >, PyObject * >
create( ArrayOfArrays< T, INDEX_TYPE, BUFFER_TYPE > & arrayOfArrays )
{
auto tmp = std::make_unique< internal::PyArrayOfArraysWrapper< T, INDEX_TYPE, BUFFER_TYPE > >( arrayOfArrays );
return internal::create( std::move( tmp ) );
}
/**
* @brief Return the Python type object for the ArrayOfArrays.
* @return The Python type object for the ArrayOfArrays.
*/
PyTypeObject * getPyArrayOfArraysType();
} // namespace python
} // namespace LvArray