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
9 // http://www.apache.org/licenses/LICENSE-2.0
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
18 // Array accessor types for primitive/C-type-based arrays, such as numbers,
19 // boolean, and temporal types.
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"
38 /// Concrete Array class for boolean data
39 class ARROW_EXPORT BooleanArray
: public PrimitiveArray
{
41 using TypeClass
= BooleanType
;
42 using IteratorType
= stl::ArrayIterator
<BooleanArray
>;
44 explicit BooleanArray(const std::shared_ptr
<ArrayData
>& data
);
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);
50 bool Value(int64_t i
) const {
51 return BitUtil::GetBit(reinterpret_cast<const uint8_t*>(raw_values_
),
55 bool GetView(int64_t i
) const { return Value(i
); }
57 /// \brief Return the number of false (0) values among the valid
58 /// values. Result is not cached.
59 int64_t false_count() const;
61 /// \brief Return the number of true (1) values among the valid
62 /// values. Result is not cached.
63 int64_t true_count() const;
65 IteratorType
begin() const { return IteratorType(*this); }
67 IteratorType
end() const { return IteratorType(*this, length()); }
70 using PrimitiveArray::PrimitiveArray
;
73 /// \addtogroup numeric-arrays
77 /// \brief Concrete Array class for numeric data with a corresponding C type
79 /// This class is templated on the corresponding DataType subclass for the
80 /// given data, for example NumericArray<Int8Type> or NumericArray<Date32Type>.
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
{
87 using TypeClass
= TYPE
;
88 using value_type
= typename
TypeClass::c_type
;
89 using IteratorType
= stl::ArrayIterator
<NumericArray
<TYPE
>>;
91 explicit NumericArray(const std::shared_ptr
<ArrayData
>& data
) : PrimitiveArray(data
) {}
93 // Only enable this constructor without a type argument for types without additional
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
) {}
103 const value_type
* raw_values() const {
104 return reinterpret_cast<const value_type
*>(raw_values_
) + data_
->offset
;
107 value_type
Value(int64_t i
) const { return raw_values()[i
]; }
109 // For API compatibility with BinaryArray etc.
110 value_type
GetView(int64_t i
) const { return Value(i
); }
112 IteratorType
begin() const { return IteratorType(*this); }
114 IteratorType
end() const { return IteratorType(*this, length()); }
117 using PrimitiveArray::PrimitiveArray
;
121 /// ---------------------
122 /// \brief Array of Day and Millisecond values.
123 class ARROW_EXPORT DayTimeIntervalArray
: public PrimitiveArray
{
125 using TypeClass
= DayTimeIntervalType
;
127 explicit DayTimeIntervalArray(const std::shared_ptr
<ArrayData
>& data
);
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);
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);
138 TypeClass::DayMilliseconds
GetValue(int64_t i
) const;
139 TypeClass::DayMilliseconds
Value(int64_t i
) const { return GetValue(i
); }
141 // For compatibility with Take kernel.
142 TypeClass::DayMilliseconds
GetView(int64_t i
) const { return GetValue(i
); }
144 int32_t byte_width() const { return sizeof(TypeClass::DayMilliseconds
); }
146 const uint8_t* raw_values() const { return raw_values_
+ data_
->offset
* byte_width(); }
149 /// \brief Array of Month, Day and nanosecond values.
150 class ARROW_EXPORT MonthDayNanoIntervalArray
: public PrimitiveArray
{
152 using TypeClass
= MonthDayNanoIntervalType
;
154 explicit MonthDayNanoIntervalArray(const std::shared_ptr
<ArrayData
>& data
);
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);
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);
165 TypeClass::MonthDayNanos
GetValue(int64_t i
) const;
166 TypeClass::MonthDayNanos
Value(int64_t i
) const { return GetValue(i
); }
168 // For compatibility with Take kernel.
169 TypeClass::MonthDayNanos
GetView(int64_t i
) const { return GetValue(i
); }
171 int32_t byte_width() const { return sizeof(TypeClass::MonthDayNanos
); }
173 const uint8_t* raw_values() const { return raw_values_
+ data_
->offset
* byte_width(); }