Apache Arrow (C++)
A columnar in-memory analytics layer designed to accelerate big data.
numpy-internal.h
Go to the documentation of this file.
1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements. See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership. The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License. You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing,
12 // software distributed under the License is distributed on an
13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, either express or implied. See the License for the
15 // specific language governing permissions and limitations
16 // under the License.
17 
18 // Internal utilities for dealing with NumPy
19 
20 #ifndef ARROW_PYTHON_NUMPY_INTERNAL_H
21 #define ARROW_PYTHON_NUMPY_INTERNAL_H
22 
24 
25 #include "arrow/python/platform.h"
26 
27 #include <cstdint>
28 #include <string>
29 
30 namespace arrow {
31 namespace py {
32 
34 template <typename T>
36  public:
37  typedef int64_t size_type;
38 
39  Ndarray1DIndexer() : arr_(nullptr), data_(nullptr) {}
40 
41  explicit Ndarray1DIndexer(PyArrayObject* arr) : Ndarray1DIndexer() { Init(arr); }
42 
43  void Init(PyArrayObject* arr) {
44  arr_ = arr;
45  DCHECK_EQ(1, PyArray_NDIM(arr)) << "Only works with 1-dimensional arrays";
46  Py_INCREF(arr);
47  data_ = reinterpret_cast<T*>(PyArray_DATA(arr));
48  stride_ = PyArray_STRIDES(arr)[0] / sizeof(T);
49  }
50 
51  ~Ndarray1DIndexer() { Py_XDECREF(arr_); }
52 
53  int64_t size() const { return PyArray_SIZE(arr_); }
54 
55  T* data() const { return data_; }
56 
57  T* begin() const { return data(); }
58  T* end() const { return begin() + size() * stride_; }
59 
60  bool is_strided() const { return stride_ != 1; }
61 
62  T& operator[](size_type index) { return data_[index * stride_]; }
63  T& operator[](size_type index) const { return data_[index * stride_]; }
64 
65  private:
66  PyArrayObject* arr_;
67  T* data_;
68  int64_t stride_;
69 };
70 
71 // Handling of Numpy Types by their static numbers
72 // (the NPY_TYPES enum and related defines)
73 
74 static inline std::string GetNumPyTypeName(int npy_type) {
75 #define TYPE_CASE(TYPE, NAME) \
76  case NPY_##TYPE: \
77  return NAME;
78 
79  switch (npy_type) {
80  TYPE_CASE(BOOL, "bool")
81  TYPE_CASE(INT8, "int8")
82  TYPE_CASE(INT16, "int16")
83  TYPE_CASE(INT32, "int32")
84  TYPE_CASE(INT64, "int64")
85 #if !NPY_INT32_IS_INT
86  TYPE_CASE(INT, "intc")
87 #endif
88 #if !NPY_INT64_IS_LONG_LONG
89  TYPE_CASE(LONGLONG, "longlong")
90 #endif
91  TYPE_CASE(UINT8, "uint8")
92  TYPE_CASE(UINT16, "uint16")
93  TYPE_CASE(UINT32, "uint32")
94  TYPE_CASE(UINT64, "uint64")
95 #if !NPY_INT32_IS_INT
96  TYPE_CASE(UINT, "uintc")
97 #endif
98 #if !NPY_INT64_IS_LONG_LONG
99  TYPE_CASE(ULONGLONG, "ulonglong")
100 #endif
101  TYPE_CASE(FLOAT16, "float16")
102  TYPE_CASE(FLOAT32, "float32")
103  TYPE_CASE(FLOAT64, "float64")
104  TYPE_CASE(DATETIME, "datetime64")
105  TYPE_CASE(OBJECT, "object")
106  TYPE_CASE(VOID, "void")
107  default:
108  break;
109  }
110 
111 #undef TYPE_CASE
112  std::stringstream ss;
113  ss << "unrecognized type (" << npy_type << ") in GetNumPyTypeName";
114  return ss.str();
115 }
116 
117 #define TYPE_VISIT_INLINE(TYPE) \
118  case NPY_##TYPE: \
119  return visitor->template Visit<NPY_##TYPE>(arr);
120 
121 template <typename VISITOR>
122 inline Status VisitNumpyArrayInline(PyArrayObject* arr, VISITOR* visitor) {
123  switch (PyArray_TYPE(arr)) {
124  TYPE_VISIT_INLINE(BOOL);
125  TYPE_VISIT_INLINE(INT8);
126  TYPE_VISIT_INLINE(UINT8);
127  TYPE_VISIT_INLINE(INT16);
128  TYPE_VISIT_INLINE(UINT16);
129  TYPE_VISIT_INLINE(INT32);
130  TYPE_VISIT_INLINE(UINT32);
131  TYPE_VISIT_INLINE(INT64);
132  TYPE_VISIT_INLINE(UINT64);
133 #if !NPY_INT32_IS_INT
134  TYPE_VISIT_INLINE(INT);
135  TYPE_VISIT_INLINE(UINT);
136 #endif
137 #if !NPY_INT64_IS_LONG_LONG
138  TYPE_VISIT_INLINE(LONGLONG);
139  TYPE_VISIT_INLINE(ULONGLONG);
140 #endif
141  TYPE_VISIT_INLINE(FLOAT16);
142  TYPE_VISIT_INLINE(FLOAT32);
143  TYPE_VISIT_INLINE(FLOAT64);
144  TYPE_VISIT_INLINE(DATETIME);
145  TYPE_VISIT_INLINE(OBJECT);
146  }
147  std::stringstream ss;
148  ss << "NumPy type not implemented: " << GetNumPyTypeName(PyArray_TYPE(arr));
149  return Status::NotImplemented(ss.str());
150 }
151 
152 #undef TYPE_VISIT_INLINE
153 
154 } // namespace py
155 } // namespace arrow
156 
157 #endif // ARROW_PYTHON_NUMPY_INTERNAL_H
T * data() const
Definition: numpy-internal.h:55
~Ndarray1DIndexer()
Definition: numpy-internal.h:51
std::shared_ptr< DataType > int16()
std::shared_ptr< DataType > uint16()
std::shared_ptr< DataType > int32()
std::shared_ptr< DataType > uint32()
void Init(PyArrayObject *arr)
Definition: numpy-internal.h:43
Definition: status.h:93
std::shared_ptr< DataType > uint64()
#define TYPE_VISIT_INLINE(TYPE)
Definition: numpy-internal.h:117
std::shared_ptr< DataType > int8()
int64_t size() const
Definition: numpy-internal.h:53
std::shared_ptr< DataType > float32()
T & operator[](size_type index)
Definition: numpy-internal.h:62
Ndarray1DIndexer()
Definition: numpy-internal.h:39
Status VisitNumpyArrayInline(PyArrayObject *arr, VISITOR *visitor)
Definition: numpy-internal.h:122
std::shared_ptr< DataType > int64()
bool is_strided() const
Definition: numpy-internal.h:60
#define TYPE_CASE(TYPE, NAME)
Top-level namespace for Apache Arrow C++ API.
Definition: adapter.h:32
#define DCHECK_EQ(val1, val2)
Definition: logging.h:95
Ndarray1DIndexer(PyArrayObject *arr)
Definition: numpy-internal.h:41
T * end() const
Definition: numpy-internal.h:58
std::shared_ptr< DataType > float64()
T * begin() const
Definition: numpy-internal.h:57
T & operator[](size_type index) const
Definition: numpy-internal.h:63
static Status NotImplemented(const std::string &msg)
Definition: status.h:125
int64_t size_type
Definition: numpy-internal.h:37
Indexing convenience for interacting with strided 1-dim ndarray objects.
Definition: numpy-internal.h:35