forked from irods/irods_rule_engine_plugin_python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray_indexing_suite.hpp
172 lines (140 loc) · 4.44 KB
/
array_indexing_suite.hpp
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
// From Praetorian's answer here: http://stackoverflow.com/a/27560620
#include <algorithm>
#include <cstddef>
#include <iterator>
#include <type_traits>
#include <patchlevel.h>
#include <boost/version.hpp>
#pragma GCC diagnostic push
#if PY_VERSION_HEX < 0x030400A2
#pragma GCC diagnostic ignored "-Wregister"
#endif
#if PY_VERSION_HEX >= 0x03090000 && BOOST_VERSION < 107500
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif
#include <boost/python.hpp>
#include <boost/python/suite/indexing/indexing_suite.hpp>
#pragma GCC diagnostic pop
// Forward declaration
template<
typename Array,
bool NoProxy,
typename DerivedPolicies>
class array_indexing_suite;
namespace detail {
template<typename Array, bool NoProxy>
struct final_array_derived_policies
: array_indexing_suite<Array, NoProxy, final_array_derived_policies<Array, NoProxy>>
{};
} /* namespace detail */
template<
typename Array,
bool NoProxy = std::is_arithmetic<typename Array::value_type>::value,
typename DerivedPolicies = detail::final_array_derived_policies<Array, NoProxy>
>
class array_indexing_suite
: public boost::python::indexing_suite<Array,
DerivedPolicies,
NoProxy>
{
public:
typedef typename Array::value_type data_type;
typedef typename Array::value_type key_type;
typedef typename Array::size_type index_type;
typedef typename Array::size_type size_type;
typedef typename Array::difference_type difference_type;
static data_type& get_item( Array& arr, index_type i )
{
return arr[i];
}
static void set_item( Array& arr, index_type i, data_type const& v )
{
arr[i] = v;
}
static void delete_item( Array& /*arr*/, index_type /*i*/ )
{
::PyErr_SetString( ::PyExc_TypeError, "Cannot delete array item" );
boost::python::throw_error_already_set();
}
static size_type size( Array& arr )
{
return arr.size();
}
static bool contains( Array& arr, key_type const& key )
{
return std::find( arr.cbegin(), arr.cend(), key ) != arr.cend();
}
static index_type get_min_index( Array& )
{
return 0;
}
static index_type get_max_index( Array& arr )
{
return arr.size();
}
static bool compare_index( Array&, index_type a, index_type b )
{
return a < b;
}
static index_type convert_index( Array& arr, PyObject *i_ )
{
boost::python::extract<long> i(i_);
if( i.check() ) {
long index = i();
if( index < 0 ) {
index += static_cast<decltype(index)>(DerivedPolicies::size( arr ));
}
if( ( index >= long(arr.size()) ) || ( index < 0 ) ) {
::PyErr_SetString( ::PyExc_IndexError, "Index out of range" );
boost::python::throw_error_already_set();
}
return index;
}
::PyErr_SetString( ::PyExc_TypeError, "Invalid index type" );
boost::python::throw_error_already_set();
return index_type();
}
static boost::python::object get_slice( Array& arr, index_type from, index_type to )
{
if( from > to ) {
return boost::python::object( Array() );
}
return boost::python::object( Array( arr.begin() + from, arr.begin() + to ) );
}
static void set_slice( Array& arr, index_type from, index_type to, data_type const& v )
{
if( from > to ) {
return;
} else if( to > arr.size() ) {
::PyErr_SetString( ::PyExc_IndexError, "Index out of range" );
boost::python::throw_error_already_set();
} else {
std::fill( arr.begin() + from, arr.begin() + to, v );
}
}
template<typename Iter>
static void set_slice( Array& arr, index_type from, index_type to, Iter first, Iter last )
{
auto num_items = std::distance( first, last );
if( ( from + num_items ) > arr.size() ) {
::PyErr_SetString( ::PyExc_IndexError, "Index out of range" );
boost::python::throw_error_already_set();
return;
}
if( from > to ) {
std::copy( first, last, arr.begin() + from );
} else {
if( static_cast<decltype(num_items)>( to - from ) != num_items ) {
::PyErr_SetString( ::PyExc_TypeError, "Array length is immutable" );
boost::python::throw_error_already_set();
return;
}
std::copy( first, last, arr.begin() + from );
}
}
static void delete_slice( Array& /*arr*/, index_type /*from*/, index_type /*to*/ )
{
::PyErr_SetString( ::PyExc_TypeError, "Cannot delete array item(s)" );
boost::python::throw_error_already_set();
}
};