Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-08-28 08:26:54

0001 // Licensed to the Apache Software Foundation (ASF) under one
0002 // or more contributor license agreements.  See the NOTICE file
0003 // distributed with this work for additional information
0004 // regarding copyright ownership.  The ASF licenses this file
0005 // to you under the Apache License, Version 2.0 (the
0006 // "License"); you may not use this file except in compliance
0007 // with the License.  You may obtain a copy of the License at
0008 //
0009 //   http://www.apache.org/licenses/LICENSE-2.0
0010 //
0011 // Unless required by applicable law or agreed to in writing,
0012 // software distributed under the License is distributed on an
0013 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
0014 // KIND, either express or implied.  See the License for the
0015 // specific language governing permissions and limitations
0016 // under the License.
0017 
0018 // Array accessor types for primitive/C-type-based arrays, such as numbers,
0019 // boolean, and temporal types.
0020 
0021 #pragma once
0022 
0023 #include <cstdint>
0024 #include <memory>
0025 
0026 #include "arrow/array/array_base.h"
0027 #include "arrow/array/data.h"
0028 #include "arrow/stl_iterator.h"
0029 #include "arrow/type.h"
0030 #include "arrow/type_fwd.h"  // IWYU pragma: export
0031 #include "arrow/type_traits.h"
0032 #include "arrow/util/bit_util.h"
0033 #include "arrow/util/macros.h"
0034 #include "arrow/util/visibility.h"
0035 
0036 namespace arrow {
0037 
0038 /// Concrete Array class for boolean data
0039 class ARROW_EXPORT BooleanArray : public PrimitiveArray {
0040  public:
0041   using TypeClass = BooleanType;
0042   using IteratorType = stl::ArrayIterator<BooleanArray>;
0043 
0044   explicit BooleanArray(const std::shared_ptr<ArrayData>& data);
0045 
0046   BooleanArray(int64_t length, const std::shared_ptr<Buffer>& data,
0047                const std::shared_ptr<Buffer>& null_bitmap = NULLPTR,
0048                int64_t null_count = kUnknownNullCount, int64_t offset = 0);
0049 
0050   bool Value(int64_t i) const {
0051     return bit_util::GetBit(reinterpret_cast<const uint8_t*>(raw_values_),
0052                             i + data_->offset);
0053   }
0054 
0055   bool GetView(int64_t i) const { return Value(i); }
0056 
0057   std::optional<bool> operator[](int64_t i) const { return *IteratorType(*this, i); }
0058 
0059   /// \brief Return the number of false (0) values among the valid
0060   /// values. Result is not cached.
0061   int64_t false_count() const;
0062 
0063   /// \brief Return the number of true (1) values among the valid
0064   /// values. Result is not cached.
0065   int64_t true_count() const;
0066 
0067   IteratorType begin() const { return IteratorType(*this); }
0068 
0069   IteratorType end() const { return IteratorType(*this, length()); }
0070 
0071  protected:
0072   using PrimitiveArray::PrimitiveArray;
0073 };
0074 
0075 /// \addtogroup numeric-arrays
0076 ///
0077 /// @{
0078 
0079 /// \brief Concrete Array class for numeric data with a corresponding C type
0080 ///
0081 /// This class is templated on the corresponding DataType subclass for the
0082 /// given data, for example NumericArray<Int8Type> or NumericArray<Date32Type>.
0083 ///
0084 /// Note that convenience aliases are available for all accepted types
0085 /// (for example Int8Array for NumericArray<Int8Type>).
0086 template <typename TYPE>
0087 class NumericArray : public PrimitiveArray {
0088  public:
0089   using TypeClass = TYPE;
0090   using value_type = typename TypeClass::c_type;
0091   using IteratorType = stl::ArrayIterator<NumericArray<TYPE>>;
0092 
0093   explicit NumericArray(const std::shared_ptr<ArrayData>& data) {
0094     NumericArray::SetData(data);
0095   }
0096 
0097   // Only enable this constructor without a type argument for types without additional
0098   // metadata
0099   template <typename T1 = TYPE>
0100   NumericArray(enable_if_parameter_free<T1, int64_t> length,
0101                const std::shared_ptr<Buffer>& data,
0102                const std::shared_ptr<Buffer>& null_bitmap = NULLPTR,
0103                int64_t null_count = kUnknownNullCount, int64_t offset = 0) {
0104     NumericArray::SetData(ArrayData::Make(TypeTraits<T1>::type_singleton(), length,
0105                                           {null_bitmap, data}, null_count, offset));
0106   }
0107 
0108   NumericArray(std::shared_ptr<DataType> type, int64_t length,
0109                const std::shared_ptr<Buffer>& data,
0110                const std::shared_ptr<Buffer>& null_bitmap = NULLPTR,
0111                int64_t null_count = kUnknownNullCount, int64_t offset = 0) {
0112     NumericArray::SetData(ArrayData::Make(std::move(type), length, {null_bitmap, data},
0113                                           null_count, offset));
0114   }
0115 
0116   const value_type* raw_values() const { return values_; }
0117 
0118   value_type Value(int64_t i) const { return values_[i]; }
0119 
0120   // For API compatibility with BinaryArray etc.
0121   value_type GetView(int64_t i) const { return values_[i]; }
0122 
0123   std::optional<value_type> operator[](int64_t i) const {
0124     return *IteratorType(*this, i);
0125   }
0126 
0127   IteratorType begin() const { return IteratorType(*this); }
0128 
0129   IteratorType end() const { return IteratorType(*this, length()); }
0130 
0131  protected:
0132   NumericArray() : values_(NULLPTR) {}
0133 
0134   void SetData(const std::shared_ptr<ArrayData>& data) {
0135     this->PrimitiveArray::SetData(data);
0136     values_ = raw_values_
0137                   ? (reinterpret_cast<const value_type*>(raw_values_) + data_->offset)
0138                   : NULLPTR;
0139   }
0140 
0141   const value_type* values_;
0142 };
0143 
0144 /// DayTimeArray
0145 /// ---------------------
0146 /// \brief Array of Day and Millisecond values.
0147 class ARROW_EXPORT DayTimeIntervalArray : public PrimitiveArray {
0148  public:
0149   using TypeClass = DayTimeIntervalType;
0150   using IteratorType = stl::ArrayIterator<DayTimeIntervalArray>;
0151 
0152   explicit DayTimeIntervalArray(const std::shared_ptr<ArrayData>& data);
0153 
0154   DayTimeIntervalArray(const std::shared_ptr<DataType>& type, int64_t length,
0155                        const std::shared_ptr<Buffer>& data,
0156                        const std::shared_ptr<Buffer>& null_bitmap = NULLPTR,
0157                        int64_t null_count = kUnknownNullCount, int64_t offset = 0);
0158 
0159   DayTimeIntervalArray(int64_t length, const std::shared_ptr<Buffer>& data,
0160                        const std::shared_ptr<Buffer>& null_bitmap = NULLPTR,
0161                        int64_t null_count = kUnknownNullCount, int64_t offset = 0);
0162 
0163   TypeClass::DayMilliseconds GetValue(int64_t i) const;
0164   TypeClass::DayMilliseconds Value(int64_t i) const { return GetValue(i); }
0165 
0166   // For compatibility with Take kernel.
0167   TypeClass::DayMilliseconds GetView(int64_t i) const { return GetValue(i); }
0168 
0169   IteratorType begin() const { return IteratorType(*this); }
0170 
0171   IteratorType end() const { return IteratorType(*this, length()); }
0172 
0173   std::optional<TypeClass::DayMilliseconds> operator[](int64_t i) const {
0174     return *IteratorType(*this, i);
0175   }
0176 
0177   int32_t byte_width() const { return sizeof(TypeClass::DayMilliseconds); }
0178 
0179   const uint8_t* raw_values() const { return raw_values_ + data_->offset * byte_width(); }
0180 };
0181 
0182 /// \brief Array of Month, Day and nanosecond values.
0183 class ARROW_EXPORT MonthDayNanoIntervalArray : public PrimitiveArray {
0184  public:
0185   using TypeClass = MonthDayNanoIntervalType;
0186   using IteratorType = stl::ArrayIterator<MonthDayNanoIntervalArray>;
0187 
0188   explicit MonthDayNanoIntervalArray(const std::shared_ptr<ArrayData>& data);
0189 
0190   MonthDayNanoIntervalArray(const std::shared_ptr<DataType>& type, int64_t length,
0191                             const std::shared_ptr<Buffer>& data,
0192                             const std::shared_ptr<Buffer>& null_bitmap = NULLPTR,
0193                             int64_t null_count = kUnknownNullCount, int64_t offset = 0);
0194 
0195   MonthDayNanoIntervalArray(int64_t length, const std::shared_ptr<Buffer>& data,
0196                             const std::shared_ptr<Buffer>& null_bitmap = NULLPTR,
0197                             int64_t null_count = kUnknownNullCount, int64_t offset = 0);
0198 
0199   TypeClass::MonthDayNanos GetValue(int64_t i) const;
0200   TypeClass::MonthDayNanos Value(int64_t i) const { return GetValue(i); }
0201 
0202   // For compatibility with Take kernel.
0203   TypeClass::MonthDayNanos GetView(int64_t i) const { return GetValue(i); }
0204 
0205   IteratorType begin() const { return IteratorType(*this); }
0206 
0207   IteratorType end() const { return IteratorType(*this, length()); }
0208 
0209   std::optional<TypeClass::MonthDayNanos> operator[](int64_t i) const {
0210     return *IteratorType(*this, i);
0211   }
0212 
0213   int32_t byte_width() const { return sizeof(TypeClass::MonthDayNanos); }
0214 
0215   const uint8_t* raw_values() const { return raw_values_ + data_->offset * byte_width(); }
0216 };
0217 
0218 /// @}
0219 
0220 }  // namespace arrow