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  bool is_strided() const { return stride_ == 1; }
58 
59  T& operator[](size_type index) { return *(data_ + index * stride_); }
60  T& operator[](size_type index) const { return *(data_ + index * stride_); }
61 
62  private:
63  PyArrayObject* arr_;
64  T* data_;
65  int64_t stride_;
66 };
67 
68 static inline std::string GetNumPyTypeName(int npy_type) {
69 #define TYPE_CASE(TYPE, NAME) \
70  case NPY_##TYPE: \
71  return NAME;
72 
73  switch (npy_type) {
74  TYPE_CASE(BOOL, "bool")
75  TYPE_CASE(INT8, "int8")
76  TYPE_CASE(INT16, "int16")
77  TYPE_CASE(INT32, "int32")
78  TYPE_CASE(INT64, "int64")
79 #if (NPY_INT64 != NPY_LONGLONG)
80  TYPE_CASE(LONGLONG, "longlong")
81 #endif
82  TYPE_CASE(UINT8, "uint8")
83  TYPE_CASE(UINT16, "uint16")
84  TYPE_CASE(UINT32, "uint32")
85  TYPE_CASE(UINT64, "uint64")
86 #if (NPY_UINT64 != NPY_ULONGLONG)
87  TYPE_CASE(ULONGLONG, "ulonglong")
88 #endif
89  TYPE_CASE(FLOAT16, "float16")
90  TYPE_CASE(FLOAT32, "float32")
91  TYPE_CASE(FLOAT64, "float64")
92  TYPE_CASE(DATETIME, "datetime64")
93  TYPE_CASE(OBJECT, "object")
94  TYPE_CASE(VOID, "void")
95  default:
96  break;
97  }
98 
99 #undef TYPE_CASE
100  return "unrecognized type in GetNumPyTypeName";
101 }
102 
103 } // namespace py
104 } // namespace arrow
105 
106 #endif // ARROW_PYTHON_NUMPY_INTERNAL_H
T * data() const
Definition: numpy-internal.h:55
~Ndarray1DIndexer()
Definition: numpy-internal.h:51
void Init(PyArrayObject *arr)
Definition: numpy-internal.h:43
int64_t size() const
Definition: numpy-internal.h:53
T & operator[](size_type index)
Definition: numpy-internal.h:59
Ndarray1DIndexer()
Definition: numpy-internal.h:39
bool is_strided() const
Definition: numpy-internal.h:57
#define TYPE_CASE(TYPE, NAME)
Top-level namespace for Apache Arrow C++ API.
Definition: allocator.h:29
#define DCHECK_EQ(val1, val2)
Definition: logging.h:79
Ndarray1DIndexer(PyArrayObject *arr)
Definition: numpy-internal.h:41
T & operator[](size_type index) const
Definition: numpy-internal.h:60
int64_t size_type
Definition: numpy-internal.h:37
Indexing convenience for interacting with strided 1-dim ndarray objects.
Definition: numpy-internal.h:35