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 numeric data.
39 template <typename TYPE>
40 class NumericArray : public PrimitiveArray {
41  public:
42   using TypeClass = TYPE;
43   using value_type = typename TypeClass::c_type;
44   using IteratorType = stl::ArrayIterator<NumericArray<TYPE>>;
45 
NumericArray(const std::shared_ptr<ArrayData> & data)46   explicit NumericArray(const std::shared_ptr<ArrayData>& data) : PrimitiveArray(data) {}
47 
48   // Only enable this constructor without a type argument for types without additional
49   // metadata
50   template <typename T1 = TYPE>
51   NumericArray(enable_if_parameter_free<T1, int64_t> length,
52                const std::shared_ptr<Buffer>& data,
53                const std::shared_ptr<Buffer>& null_bitmap = NULLPTR,
54                int64_t null_count = kUnknownNullCount, int64_t offset = 0)
PrimitiveArray(TypeTraits<T1>::type_singleton (),length,data,null_bitmap,null_count,offset)55       : PrimitiveArray(TypeTraits<T1>::type_singleton(), length, data, null_bitmap,
56                        null_count, offset) {}
57 
raw_values()58   const value_type* raw_values() const {
59     return reinterpret_cast<const value_type*>(raw_values_) + data_->offset;
60   }
61 
Value(int64_t i)62   value_type Value(int64_t i) const { return raw_values()[i]; }
63 
64   // For API compatibility with BinaryArray etc.
GetView(int64_t i)65   value_type GetView(int64_t i) const { return Value(i); }
66 
begin()67   IteratorType begin() { return IteratorType(*this); }
68 
end()69   IteratorType end() { return IteratorType(*this, length()); }
70 
71  protected:
72   using PrimitiveArray::PrimitiveArray;
73 };
74 
75 /// Concrete Array class for boolean data
76 class ARROW_EXPORT BooleanArray : public PrimitiveArray {
77  public:
78   using TypeClass = BooleanType;
79   using IteratorType = stl::ArrayIterator<BooleanArray>;
80 
81   explicit BooleanArray(const std::shared_ptr<ArrayData>& data);
82 
83   BooleanArray(int64_t length, const std::shared_ptr<Buffer>& data,
84                const std::shared_ptr<Buffer>& null_bitmap = NULLPTR,
85                int64_t null_count = kUnknownNullCount, int64_t offset = 0);
86 
Value(int64_t i)87   bool Value(int64_t i) const {
88     return BitUtil::GetBit(reinterpret_cast<const uint8_t*>(raw_values_),
89                            i + data_->offset);
90   }
91 
GetView(int64_t i)92   bool GetView(int64_t i) const { return Value(i); }
93 
94   /// \brief Return the number of false (0) values among the valid
95   /// values. Result is not cached.
96   int64_t false_count() const;
97 
98   /// \brief Return the number of true (1) values among the valid
99   /// values. Result is not cached.
100   int64_t true_count() const;
101 
begin()102   IteratorType begin() { return IteratorType(*this); }
103 
end()104   IteratorType end() { return IteratorType(*this, length()); }
105 
106  protected:
107   using PrimitiveArray::PrimitiveArray;
108 };
109 
110 /// DayTimeArray
111 /// ---------------------
112 /// \brief Array of Day and Millisecond values.
113 class ARROW_EXPORT DayTimeIntervalArray : public PrimitiveArray {
114  public:
115   using TypeClass = DayTimeIntervalType;
116 
117   explicit DayTimeIntervalArray(const std::shared_ptr<ArrayData>& data);
118 
119   DayTimeIntervalArray(const std::shared_ptr<DataType>& type, int64_t length,
120                        const std::shared_ptr<Buffer>& data,
121                        const std::shared_ptr<Buffer>& null_bitmap = NULLPTR,
122                        int64_t null_count = kUnknownNullCount, int64_t offset = 0);
123 
124   TypeClass::DayMilliseconds GetValue(int64_t i) const;
Value(int64_t i)125   TypeClass::DayMilliseconds Value(int64_t i) const { return GetValue(i); }
126 
127   // For compatibility with Take kernel.
GetView(int64_t i)128   TypeClass::DayMilliseconds GetView(int64_t i) const { return GetValue(i); }
129 
byte_width()130   int32_t byte_width() const { return sizeof(TypeClass::DayMilliseconds); }
131 
raw_values()132   const uint8_t* raw_values() const { return raw_values_ + data_->offset * byte_width(); }
133 };
134 
135 }  // namespace arrow
136