NumericArray
/// Concrete Array class for numeric data.
template <typename TYPE>
class NumericArray : public PrimitiveArray {
public:
using TypeClass = TYPE;
using value_type = typename TypeClass::c_type;
using IteratorType = stl::ArrayIterator<NumericArray<TYPE>>;
explicit NumericArray(const std::shared_ptr<ArrayData>& data) : PrimitiveArray(data) {}
// Only enable this constructor without a type argument for types without additional
// metadata
template <typename T1 = TYPE>
NumericArray(enable_if_parameter_free<T1, int64_t> length,
const std::shared_ptr<Buffer>& data,
const std::shared_ptr<Buffer>& null_bitmap = NULLPTR,
int64_t null_count = kUnknownNullCount, int64_t offset = 0)
: PrimitiveArray(TypeTraits<T1>::type_singleton(), length, data, null_bitmap,
null_count, offset) {}
const value_type* raw_values() const {
return reinterpret_cast<const value_type*>(raw_values_) + data_->offset;
}
value_type Value(int64_t i) const { return raw_values()[i]; }
// For API compatibility with BinaryArray etc.
value_type GetView(int64_t i) const { return Value(i); }
IteratorType begin() const { return IteratorType(*this); }
IteratorType end() const { return IteratorType(*this, length()); }
protected:
using PrimitiveArray::PrimitiveArray;
};
NumericArrays Cast
TEST(TestArrayCast, Int) {
auto type = arrow::date64();
auto array1 = ArrayFromJSON(type, "[1, 2, 3, 4]");
std::static_pointer_cast<arrow::Int64Array>(array1)->Value(3);
std::cout << "array1->ToString()\n:" << array1->ToString() << std::endl;
std::cout << "(array1)->Value(2):" << std::static_pointer_cast<arrow::Int64Array>(array1)->Value(2) << std::endl;
std::cout << "(array1)->Value(2 * 8):" << (int64_t)std::static_pointer_cast<arrow::Int8Array>(array1)->Value(2 * 8) << std::endl;
auto value8 = std::static_pointer_cast<arrow::Int8Array>(array1)->Value(2 * 8);
auto value64 = std::static_pointer_cast<arrow::Int64Array>(array1)->Value(2);
std::cout << "value8 type: " << getClearName(typeid(value8).name()) << std::endl;
std::cout << "value64 type:" << getClearName(typeid(value64).name()) << std::endl;
std::cout << "(int8_t)*(&value8 + 8):" << (int8_t)*(&value8 + 8) << std::endl;
std::cout << "(int64_t)*(&value64 + 1):" << (int64_t)*(&value64 + 1) << std::endl;
auto value8ptr = std::static_pointer_cast<arrow::Int8Array>(array1)->raw_values();
auto value64ptr = std::static_pointer_cast<arrow::Int64Array>(array1)->raw_values();
std::cout << "value8ptr type: " << getClearName(typeid(value8ptr).name()) << std::endl;
std::cout << "value64ptr type:" << getClearName(typeid(value64ptr).name()) << std::endl;
int64_t newarray[4];
// 预期是 [1, 2, 3, 4] 中, 最后一个元素
memcpy(newarray, value8ptr + 2*8, 8);
std::cout << "newarray[0] :" << newarray[0] << std::endl;
// for(int i=0 ; i < 32; i++) {
// std::cout << i << " :" << (int64_t)std::static_pointer_cast<arrow::Int8Array>(array1)->Value(i) << std::endl;
// }
// for(int i=0 ; i < 32; i++) {
// std::cout << i << " :" << std::static_pointer_cast<arrow::Int64Array>(array1)->Value(i) << std::endl;
// }
}
array1->ToString()
:[
1970-01-01,
1970-01-01,
1970-01-01,
1970-01-01
]
(array1)->Value(2):3
(array1)->Value(2 * 8):3
value8 type: signed char
value64 type:long
(int8_t)*(&value8 + 8):
(int64_t)*(&value64 + 1):216172782113783808
value8ptr type: signed char const*
value64ptr type:long const*
newarray[0] :3