forked from apache/arrow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumpy-internal.h
More file actions
188 lines (157 loc) · 4.99 KB
/
Copy pathnumpy-internal.h
File metadata and controls
188 lines (157 loc) · 4.99 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
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
// Internal utilities for dealing with NumPy
#ifndef ARROW_PYTHON_NUMPY_INTERNAL_H
#define ARROW_PYTHON_NUMPY_INTERNAL_H
#include "arrow/python/numpy_interop.h"
#include "arrow/status.h"
#include "arrow/python/platform.h"
#include <cstdint>
#include <sstream>
#include <string>
namespace arrow {
namespace py {
/// Indexing convenience for interacting with strided 1-dim ndarray objects
template <typename T>
class Ndarray1DIndexer {
public:
typedef int64_t size_type;
Ndarray1DIndexer() : arr_(NULLPTR), data_(NULLPTR) {}
explicit Ndarray1DIndexer(PyArrayObject* arr) : Ndarray1DIndexer() {
arr_ = arr;
DCHECK_EQ(1, PyArray_NDIM(arr)) << "Only works with 1-dimensional arrays";
Py_INCREF(arr);
data_ = reinterpret_cast<uint8_t*>(PyArray_DATA(arr));
stride_ = PyArray_STRIDES(arr)[0];
}
~Ndarray1DIndexer() { Py_XDECREF(arr_); }
int64_t size() const { return PyArray_SIZE(arr_); }
T* data() const { return data_; }
bool is_strided() const { return stride_ != sizeof(T); }
T& operator[](size_type index) {
return *reinterpret_cast<T*>(data_ + index * stride_);
}
const T& operator[](size_type index) const {
return *reinterpret_cast<const T*>(data_ + index * stride_);
}
private:
PyArrayObject* arr_;
uint8_t* data_;
int64_t stride_;
};
// Handling of Numpy Types by their static numbers
// (the NPY_TYPES enum and related defines)
static inline std::string GetNumPyTypeName(int npy_type) {
#define TYPE_CASE(TYPE, NAME) \
case NPY_##TYPE: \
return NAME;
switch (npy_type) {
TYPE_CASE(BOOL, "bool")
TYPE_CASE(INT8, "int8")
TYPE_CASE(INT16, "int16")
TYPE_CASE(INT32, "int32")
TYPE_CASE(INT64, "int64")
#if !NPY_INT32_IS_INT
TYPE_CASE(INT, "intc")
#endif
#if !NPY_INT64_IS_LONG_LONG
TYPE_CASE(LONGLONG, "longlong")
#endif
TYPE_CASE(UINT8, "uint8")
TYPE_CASE(UINT16, "uint16")
TYPE_CASE(UINT32, "uint32")
TYPE_CASE(UINT64, "uint64")
#if !NPY_INT32_IS_INT
TYPE_CASE(UINT, "uintc")
#endif
#if !NPY_INT64_IS_LONG_LONG
TYPE_CASE(ULONGLONG, "ulonglong")
#endif
TYPE_CASE(FLOAT16, "float16")
TYPE_CASE(FLOAT32, "float32")
TYPE_CASE(FLOAT64, "float64")
TYPE_CASE(DATETIME, "datetime64")
TYPE_CASE(OBJECT, "object")
TYPE_CASE(VOID, "void")
default:
break;
}
#undef TYPE_CASE
std::stringstream ss;
ss << "unrecognized type (" << npy_type << ") in GetNumPyTypeName";
return ss.str();
}
#define TYPE_VISIT_INLINE(TYPE) \
case NPY_##TYPE: \
return visitor->template Visit<NPY_##TYPE>(arr);
template <typename VISITOR>
inline Status VisitNumpyArrayInline(PyArrayObject* arr, VISITOR* visitor) {
switch (PyArray_TYPE(arr)) {
TYPE_VISIT_INLINE(BOOL);
TYPE_VISIT_INLINE(INT8);
TYPE_VISIT_INLINE(UINT8);
TYPE_VISIT_INLINE(INT16);
TYPE_VISIT_INLINE(UINT16);
TYPE_VISIT_INLINE(INT32);
TYPE_VISIT_INLINE(UINT32);
TYPE_VISIT_INLINE(INT64);
TYPE_VISIT_INLINE(UINT64);
#if !NPY_INT32_IS_INT
TYPE_VISIT_INLINE(INT);
TYPE_VISIT_INLINE(UINT);
#endif
#if !NPY_INT64_IS_LONG_LONG
TYPE_VISIT_INLINE(LONGLONG);
TYPE_VISIT_INLINE(ULONGLONG);
#endif
TYPE_VISIT_INLINE(FLOAT16);
TYPE_VISIT_INLINE(FLOAT32);
TYPE_VISIT_INLINE(FLOAT64);
TYPE_VISIT_INLINE(DATETIME);
TYPE_VISIT_INLINE(OBJECT);
}
return Status::NotImplemented("NumPy type not implemented: ",
GetNumPyTypeName(PyArray_TYPE(arr)));
}
#undef TYPE_VISIT_INLINE
namespace internal {
inline bool PyFloatScalar_Check(PyObject* obj) {
return PyFloat_Check(obj) || PyArray_IsScalar(obj, Floating);
}
inline bool PyIntScalar_Check(PyObject* obj) {
#if PY_MAJOR_VERSION < 3
if (PyInt_Check(obj)) {
return true;
}
#endif
return PyLong_Check(obj) || PyArray_IsScalar(obj, Integer);
}
inline bool PyBoolScalar_Check(PyObject* obj) {
return PyBool_Check(obj) || PyArray_IsScalar(obj, Bool);
}
static inline PyArray_Descr* GetSafeNumPyDtype(int type) {
if (type == NPY_DATETIME) {
// It is not safe to mutate the result of DescrFromType
return PyArray_DescrNewFromType(type);
} else {
return PyArray_DescrFromType(type);
}
}
} // namespace internal
} // namespace py
} // namespace arrow
#endif // ARROW_PYTHON_NUMPY_INTERNAL_H