Array 之 PrimitiveArray

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值