-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathPyArrayOfSets.hpp
More file actions
284 lines (245 loc) · 10.1 KB
/
PyArrayOfSets.hpp
File metadata and controls
284 lines (245 loc) · 10.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
/*
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* 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 "../ArrayOfSets.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 PyArrayOfSetsWrapperBase
* @brief Provides a virtual Python wrapper around an ArrayOfSets.
*/
class PyArrayOfSetsWrapperBase
{
public:
/**
* brief Default destructor.
*/
virtual ~PyArrayOfSetsWrapperBase() = default;
/**
* @brief Return the access level for the array.
* @return The access level for the array.
*/
virtual int getAccessLevel() const
{ return m_accessLevel; }
/**
* @brief Set the access level for the array.
* @param accessLevel The access level.
*/
virtual void setAccessLevel( int const accessLevel ) = 0;
/**
* @brief Return a string representing the underlying ArrayOfSetsType.
* @return a string representing the underlying ArrayOfSetsType.
*/
virtual std::string repr() const = 0;
/**
* @brief Return the size of the array (number of sets).
* @return The size of the array (number of sets).
* @note This wraps ArrayOfSets::size.
*/
virtual long long size() const = 0;
/**
* @brief Return an immutable 1D NumPy array representing the set at index @c setIndex.
* @param setIndex The set to access.
* @return An immutable 1D NumPy array representing the set at index @c setIndex.
* @note This wraps ArrayOfSets::operator[].
*/
virtual PyObject * operator[]( long long const setIndex ) = 0;
/**
* @brief Return the type of the values stored in the sets.
* @return The type of the values stored in the sets.
*/
virtual std::type_index valueType() const = 0;
/**
* @brief Erase the set at index @c setIndex.
* @param setIndex The set to erase.
* @note This wraps ArrayOfSets::eraseSet.
*/
virtual void eraseSet( long long const setIndex ) = 0;
/**
* @brief Insert a new set at index @c setIndex.
* @param setIndex The index to insert the new set at.
* @param capacity The capacity of the new set.
* @note This wraps ArrayOfSets::insertSet.
*/
virtual void insertSet( long long const setIndex, long long const capacity ) = 0;
/**
* @brief Remove values from the a set.
* @param setIndex The set to remove from.
* @param values The values to remove. Must be of type @c valueType, of length @c numVals, and sorted unique.
* @param numVals The number of values to remove.
* @return The number of values removed.
* @note This wraps ArrayOfSets::removeFromSet.
*/
virtual long long removeFromSet( long long const setIndex, void const * const values, long long const numVals ) = 0;
/**
* @brief Insert values into a set.
* @param setIndex The set to insert into.
* @param values The values to insert. Must be of type @c valueType, of length @c numVals, and sorted unique.
* @param numVals The number of values to insert.
* @return The number of values inserted.
* @note This wraps ArrayOfSets::insertIntoSet.
*/
virtual long long insertIntoSet( long long const setIndex, void const * const values, long long const numVals ) = 0;
protected:
/**
* @brief Construct an empty PyArrayOfSetsWrapperBase.
*/
PyArrayOfSetsWrapperBase():
m_accessLevel( static_cast< int >( LvArray::python::PyModify::READ_ONLY ) )
{}
/// access level for the ArrayOfSets.
int m_accessLevel;
};
/**
* @class PyArrayOfSetsWrapper
* @brief Provides a concrete implementation of PyArrayOfSetsWrapperBase.
* @tparam T The type of the entries in the ArrayOfSets.
* @tparam INDEX_TYPE The index type of the ArrayOfSets.
* @tparam BUFFER_TYPE The buffer type of the ArrayOfSets.
* @note This holds a reference to the wrapped ArrayOfSets, 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 PyArrayOfSetsWrapper final : public PyArrayOfSetsWrapperBase
{
public:
/**
* @brief Construct a new Python wrapper around @c arrayOfSets.
* @param arrayOfSets The ArrayOfSets to wrap.
*/
PyArrayOfSetsWrapper( ArrayOfSets< T, INDEX_TYPE, BUFFER_TYPE > & arrayOfSets ):
PyArrayOfSetsWrapperBase( ),
m_arrayOfSets( arrayOfSets )
{}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual ~PyArrayOfSetsWrapper() = default;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual void setAccessLevel( int const 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< ArrayOfSets< T, INDEX_TYPE, BUFFER_TYPE > >(); }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual long long size() const final override
{ return integerConversion< long long >( m_arrayOfSets.size() ); }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual PyObject * operator[]( long long const setIndex ) final override
{
INDEX_TYPE convertedIndex = integerConversion< INDEX_TYPE >( setIndex );
ArraySlice< T const, 1, 0, INDEX_TYPE > slice = m_arrayOfSets[ convertedIndex ];
T const * data = slice;
constexpr INDEX_TYPE strides = 1;
INDEX_TYPE size = slice.size();
return createNumPyArray( data, false, 1, &size, &strides );
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual std::type_index valueType() const final override
{ return std::type_index( typeid( T ) ); }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual void eraseSet( long long const setIndex ) final override
{
INDEX_TYPE convertedIndex = integerConversion< INDEX_TYPE >( setIndex );
m_arrayOfSets.eraseSet( convertedIndex );
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual void insertSet( long long const setIndex, long long const capacity ) final override
{
INDEX_TYPE convertedIndex = integerConversion< INDEX_TYPE >( setIndex );
INDEX_TYPE convertedCapacity = integerConversion< INDEX_TYPE >( capacity );
m_arrayOfSets.insertSet( convertedIndex, convertedCapacity );
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual long long removeFromSet( long long const setIndex,
void const * const values,
long long const numVals ) final override
{
INDEX_TYPE convertedIndex = integerConversion< INDEX_TYPE >( setIndex );
T const * begin = static_cast< T const * >( values );
T const * end = begin + numVals;
return integerConversion< long long >( m_arrayOfSets.removeFromSet( convertedIndex, begin, end ) );
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual long long insertIntoSet( long long const setIndex,
void const * const values,
long long const numVals ) final override
{
INDEX_TYPE convertedSetIndex = integerConversion< INDEX_TYPE >( setIndex );
T const * begin = static_cast< T const * >( values );
T const * end = begin + numVals;
return integerConversion< long long >( m_arrayOfSets.insertIntoSet( convertedSetIndex, begin, end ) );
}
private:
/// The wrapped ArrayOfSets.
ArrayOfSets< T, INDEX_TYPE, BUFFER_TYPE > & m_arrayOfSets;
};
/**
* @brief Create a Python object corresponding to @c arrayOfSets.
* @param arrayOfSets The ArrayOfSets to export to Python.
* @return A Python object corresponding to @c arrayOfSets.
*/
PyObject * create( std::unique_ptr< internal::PyArrayOfSetsWrapperBase > && arrayOfSets );
} // namespace internal
/**
* @brief Create a Python object corresponding to @c arrayOfSets.
* @tparam T The type of the entries in the ArrayOfSets.
* @tparam INDEX_TYPE The index type of the ArrayOfSets.
* @tparam BUFFER_TYPE The buffer type of the ArrayOfSets.
* @param arrayOfSets The ArrayOfSets to export to Python.
* @return A Python object corresponding to @c arrayOfSets.
* @note The returned Python object holds a reference to @c arrayOfSets, 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( ArrayOfSets< T, INDEX_TYPE, BUFFER_TYPE > & arrayOfSets )
{
auto tmp = std::make_unique< internal::PyArrayOfSetsWrapper< T, INDEX_TYPE, BUFFER_TYPE > >( arrayOfSets );
return internal::create( std::move( tmp ) );
}
/**
* @brief Return the Python type for the ArrayOfSets.
* @return The Python type object for the ArrayOfSets.
*/
PyTypeObject * getPyArrayOfSetsType();
} // namespace python
} // namespace LvArray