pitrou commented on code in PR #37792:
URL: https://github.com/apache/arrow/pull/37792#discussion_r1338137548


##########
cpp/src/arrow/util/binary_view_util.h:
##########
@@ -0,0 +1,163 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#pragma once
+
+#include <string_view>
+#include <utility>
+
+#include "arrow/type.h"
+#include "arrow/util/assume_aligned.h"
+#include "arrow/util/span.h"
+
+namespace arrow::util {
+
+inline BinaryViewType::c_type ToInlineBinaryView(const void* data, int32_t 
size) {
+  // Small string: inlined. Bytes beyond size are zeroed
+  BinaryViewType::c_type out;
+  out.inlined = {size, {}};
+  memcpy(&out.inlined.data, data, size);
+  return out;
+}
+
+inline BinaryViewType::c_type ToInlineBinaryView(std::string_view v) {
+  return ToInlineBinaryView(v.data(), static_cast<int32_t>(v.size()));
+}
+
+inline BinaryViewType::c_type ToRawPointerBinaryView(const void* data, int32_t 
size) {
+  if (size <= BinaryViewType::kInlineSize) {
+    return ToInlineBinaryView(data, size);
+  }
+
+  // Large string: store pointer.
+  BinaryViewType::c_type out;
+  out.raw = {size, {}, static_cast<const uint8_t*>(data)};
+  memcpy(&out.raw.prefix, data, sizeof(out.raw.prefix));
+  return out;
+}
+
+inline BinaryViewType::c_type ToRawPointerBinaryView(std::string_view v) {
+  return ToRawPointerBinaryView(v.data(), static_cast<int32_t>(v.size()));
+}
+
+inline BinaryViewType::c_type ToIndexOffsetBinaryView(const void* data, 
int32_t size,
+                                                      int32_t buffer_index,
+                                                      int32_t offset) {
+  if (size <= BinaryViewType::kInlineSize) {
+    return ToInlineBinaryView(data, size);
+  }
+
+  // Large string: store index/offset.
+  BinaryViewType::c_type out;
+  out.io = {size, {}, buffer_index, offset};
+  memcpy(&out.raw.prefix, data, sizeof(out.raw.prefix));
+  return out;
+}
+
+inline BinaryViewType::c_type ToIndexOffsetBinaryView(std::string_view v,
+                                                      int32_t buffer_index,
+                                                      int32_t offset) {
+  return ToIndexOffsetBinaryView(v.data(), static_cast<int32_t>(v.size()), 
buffer_index,
+                                 offset);
+}
+
+inline std::string_view FromRawPointerBinaryView(const BinaryViewType::c_type& 
v) {
+  auto* data = v.is_inline() ? v.inlined.data.data() : v.raw.data;
+  return {reinterpret_cast<const char*>(data), static_cast<size_t>(v.size())};
+}
+inline std::string_view FromRawPointerBinaryView(BinaryViewType::c_type&&) = 
delete;
+
+template <typename BufferPtr>
+std::string_view FromIndexOffsetBinaryView(const BinaryViewType::c_type& v,
+                                           const BufferPtr* data_buffers) {
+  auto* data = v.is_inline() ? v.inlined.data.data()
+                             : data_buffers[v.io.buffer_index]->data() + 
v.io.offset;
+  return {reinterpret_cast<const char*>(data), static_cast<size_t>(v.size())};
+}
+template <typename BufferPtr>
+std::string_view FromIndexOffsetBinaryView(BinaryViewType::c_type&&,
+                                           const BufferPtr*) = delete;
+
+inline int MemcmpRawPointerBinaryViewSkipPrefix(int32_t size, 
BinaryViewType::c_type l,
+                                                BinaryViewType::c_type r) {
+  return memcmp(l.raw.data + BinaryViewType::kPrefixSize,  //
+                r.raw.data + BinaryViewType::kPrefixSize,  //
+                size - BinaryViewType::kPrefixSize);
+}
+
+template <typename BufferPtr>
+inline int MemcmpIndexOffsetBinaryViewSkipPrefix(int32_t size, 
BinaryViewType::c_type l,
+                                                 BinaryViewType::c_type r,
+                                                 const BufferPtr* l_buffers,
+                                                 const BufferPtr* r_buffers) {
+  return memcmp(
+      l_buffers[l.io.buffer_index]->data() + l.io.offset + 
BinaryViewType::kPrefixSize,
+      r_buffers[r.io.buffer_index]->data() + r.io.offset + 
BinaryViewType::kPrefixSize,
+      size - BinaryViewType::kPrefixSize);
+}
+
+template <typename... BufferPtr>
+bool EqualBinaryViewImpl(BinaryViewType::c_type l, BinaryViewType::c_type r,
+                         const BufferPtr*... buffers) {
+  int64_t l_size_and_prefix, r_size_and_prefix;
+  memcpy(&l_size_and_prefix, &l, sizeof(l_size_and_prefix));
+  memcpy(&r_size_and_prefix, &r, sizeof(r_size_and_prefix));
+
+  if (l_size_and_prefix != r_size_and_prefix) return false;
+
+  if (l.is_inline()) {
+    // The inline part is zeroed at construction, so we can compare
+    // a word at a time if data extends past 'prefix_'.
+    int64_t l_inlined, r_inlined;
+    memcpy(&l_inlined,
+           AssumeAlignedAs<int64_t>(l.inline_data() + 
BinaryViewType::kPrefixSize),

Review Comment:
   `SafeLoadAs`'s purpose is already to avoid UB on unaligned loads, so I'm not 
sure why an additional function would be needed.



-- 
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.

To unsubscribe, e-mail: [email protected]

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

Reply via email to