]> git.proxmox.com Git - ceph.git/blob - ceph/src/arrow/cpp/src/arrow/array/array_primitive.h
import quincy 17.2.0
[ceph.git] / ceph / src / arrow / cpp / src / arrow / array / array_primitive.h
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 // Array accessor types for primitive/C-type-based arrays, such as numbers,
19 // boolean, and temporal types.
20
21 #pragma once
22
23 #include <cstdint>
24 #include <memory>
25
26 #include "arrow/array/array_base.h"
27 #include "arrow/array/data.h"
28 #include "arrow/stl_iterator.h"
29 #include "arrow/type.h"
30 #include "arrow/type_fwd.h" // IWYU pragma: export
31 #include "arrow/type_traits.h"
32 #include "arrow/util/bit_util.h"
33 #include "arrow/util/macros.h"
34 #include "arrow/util/visibility.h"
35
36 namespace arrow {
37
38 /// Concrete Array class for boolean data
39 class ARROW_EXPORT BooleanArray : public PrimitiveArray {
40 public:
41 using TypeClass = BooleanType;
42 using IteratorType = stl::ArrayIterator<BooleanArray>;
43
44 explicit BooleanArray(const std::shared_ptr<ArrayData>& data);
45
46 BooleanArray(int64_t length, const std::shared_ptr<Buffer>& data,
47 const std::shared_ptr<Buffer>& null_bitmap = NULLPTR,
48 int64_t null_count = kUnknownNullCount, int64_t offset = 0);
49
50 bool Value(int64_t i) const {
51 return BitUtil::GetBit(reinterpret_cast<const uint8_t*>(raw_values_),
52 i + data_->offset);
53 }
54
55 bool GetView(int64_t i) const { return Value(i); }
56
57 /// \brief Return the number of false (0) values among the valid
58 /// values. Result is not cached.
59 int64_t false_count() const;
60
61 /// \brief Return the number of true (1) values among the valid
62 /// values. Result is not cached.
63 int64_t true_count() const;
64
65 IteratorType begin() const { return IteratorType(*this); }
66
67 IteratorType end() const { return IteratorType(*this, length()); }
68
69 protected:
70 using PrimitiveArray::PrimitiveArray;
71 };
72
73 /// \addtogroup numeric-arrays
74 ///
75 /// @{
76
77 /// \brief Concrete Array class for numeric data with a corresponding C type
78 ///
79 /// This class is templated on the corresponding DataType subclass for the
80 /// given data, for example NumericArray<Int8Type> or NumericArray<Date32Type>.
81 ///
82 /// Note that convenience aliases are available for all accepted types
83 /// (for example Int8Array for NumericArray<Int8Type>).
84 template <typename TYPE>
85 class NumericArray : public PrimitiveArray {
86 public:
87 using TypeClass = TYPE;
88 using value_type = typename TypeClass::c_type;
89 using IteratorType = stl::ArrayIterator<NumericArray<TYPE>>;
90
91 explicit NumericArray(const std::shared_ptr<ArrayData>& data) : PrimitiveArray(data) {}
92
93 // Only enable this constructor without a type argument for types without additional
94 // metadata
95 template <typename T1 = TYPE>
96 NumericArray(enable_if_parameter_free<T1, int64_t> length,
97 const std::shared_ptr<Buffer>& data,
98 const std::shared_ptr<Buffer>& null_bitmap = NULLPTR,
99 int64_t null_count = kUnknownNullCount, int64_t offset = 0)
100 : PrimitiveArray(TypeTraits<T1>::type_singleton(), length, data, null_bitmap,
101 null_count, offset) {}
102
103 const value_type* raw_values() const {
104 return reinterpret_cast<const value_type*>(raw_values_) + data_->offset;
105 }
106
107 value_type Value(int64_t i) const { return raw_values()[i]; }
108
109 // For API compatibility with BinaryArray etc.
110 value_type GetView(int64_t i) const { return Value(i); }
111
112 IteratorType begin() const { return IteratorType(*this); }
113
114 IteratorType end() const { return IteratorType(*this, length()); }
115
116 protected:
117 using PrimitiveArray::PrimitiveArray;
118 };
119
120 /// DayTimeArray
121 /// ---------------------
122 /// \brief Array of Day and Millisecond values.
123 class ARROW_EXPORT DayTimeIntervalArray : public PrimitiveArray {
124 public:
125 using TypeClass = DayTimeIntervalType;
126
127 explicit DayTimeIntervalArray(const std::shared_ptr<ArrayData>& data);
128
129 DayTimeIntervalArray(const std::shared_ptr<DataType>& type, int64_t length,
130 const std::shared_ptr<Buffer>& data,
131 const std::shared_ptr<Buffer>& null_bitmap = NULLPTR,
132 int64_t null_count = kUnknownNullCount, int64_t offset = 0);
133
134 DayTimeIntervalArray(int64_t length, const std::shared_ptr<Buffer>& data,
135 const std::shared_ptr<Buffer>& null_bitmap = NULLPTR,
136 int64_t null_count = kUnknownNullCount, int64_t offset = 0);
137
138 TypeClass::DayMilliseconds GetValue(int64_t i) const;
139 TypeClass::DayMilliseconds Value(int64_t i) const { return GetValue(i); }
140
141 // For compatibility with Take kernel.
142 TypeClass::DayMilliseconds GetView(int64_t i) const { return GetValue(i); }
143
144 int32_t byte_width() const { return sizeof(TypeClass::DayMilliseconds); }
145
146 const uint8_t* raw_values() const { return raw_values_ + data_->offset * byte_width(); }
147 };
148
149 /// \brief Array of Month, Day and nanosecond values.
150 class ARROW_EXPORT MonthDayNanoIntervalArray : public PrimitiveArray {
151 public:
152 using TypeClass = MonthDayNanoIntervalType;
153
154 explicit MonthDayNanoIntervalArray(const std::shared_ptr<ArrayData>& data);
155
156 MonthDayNanoIntervalArray(const std::shared_ptr<DataType>& type, int64_t length,
157 const std::shared_ptr<Buffer>& data,
158 const std::shared_ptr<Buffer>& null_bitmap = NULLPTR,
159 int64_t null_count = kUnknownNullCount, int64_t offset = 0);
160
161 MonthDayNanoIntervalArray(int64_t length, const std::shared_ptr<Buffer>& data,
162 const std::shared_ptr<Buffer>& null_bitmap = NULLPTR,
163 int64_t null_count = kUnknownNullCount, int64_t offset = 0);
164
165 TypeClass::MonthDayNanos GetValue(int64_t i) const;
166 TypeClass::MonthDayNanos Value(int64_t i) const { return GetValue(i); }
167
168 // For compatibility with Take kernel.
169 TypeClass::MonthDayNanos GetView(int64_t i) const { return GetValue(i); }
170
171 int32_t byte_width() const { return sizeof(TypeClass::MonthDayNanos); }
172
173 const uint8_t* raw_values() const { return raw_values_ + data_->offset * byte_width(); }
174 };
175
176 /// @}
177
178 } // namespace arrow