kiszk commented on a change in pull request #7507:
URL: https://github.com/apache/arrow/pull/7507#discussion_r465270135



##########
File path: cpp/src/arrow/array/util.cc
##########
@@ -74,6 +74,186 @@ class ArrayDataWrapper {
   std::shared_ptr<Array>* out_;
 };
 
+class ArrayDataEndianSwapper {
+ public:
+  ArrayDataEndianSwapper(std::shared_ptr<ArrayData>& data, int64_t length)
+      : data_(data), length_(length) {}
+
+  Status SwapType(const DataType& type) { return VisitTypeInline(type, this); }
+
+  Status SwapChildren(std::vector<std::shared_ptr<Field>> child_fields) {
+    int i = 0;
+    for (const auto& child_field : child_fields) {
+      auto orig_data = data_;
+      auto orig_length = length_;
+      data_ = data_->child_data[i++];
+      length_ = data_->length;
+      RETURN_NOT_OK(SwapType(*child_field.get()->type()));
+      length_ = orig_length;
+      data_ = orig_data;
+    }
+    return Status::OK();
+  }
+
+  template <typename T>
+  Status SwapOffset(const T&, int index) {
+    if (data_->buffers[index] == nullptr) {
+      return Status::OK();
+    }
+    using value_type = typename T::c_type;
+    auto buffer = const_cast<value_type*>(
+        reinterpret_cast<const value_type*>(data_->buffers[index]->data()));
+    // offset has one more element rather than data->length
+    int64_t length = length_ + 1;
+    for (int64_t i = 0; i < length; i++) {
+#if ARROW_LITTLE_ENDIAN
+      buffer[i] = BitUtil::FromBigEndian(buffer[i]);
+#else
+      buffer[i] = BitUtil::FromLittleEndian(buffer[i]);
+#endif
+    }
+    return Status::OK();
+  }
+
+  Status SwapSmallOffset(int index = 1) {
+    Int32Type i32;
+    RETURN_NOT_OK(SwapOffset(i32, index));
+    return Status::OK();
+  }
+
+  Status SwapLargeOffset() {
+    Int64Type i64;
+    RETURN_NOT_OK(SwapOffset(i64, 1));
+    return Status::OK();
+  }

Review comment:
       Thanks for the good suggestion. Done.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to