You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2020/12/09 15:33:07 UTC

[GitHub] [arrow] bkietz commented on a change in pull request #8806: ARROW-10776: [C++] Allow STL iteration over concrete primitive arrays

bkietz commented on a change in pull request #8806:
URL: https://github.com/apache/arrow/pull/8806#discussion_r539405203



##########
File path: cpp/src/arrow/stl_iterator_test.cc
##########
@@ -0,0 +1,263 @@
+// 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.
+
+#include <algorithm>
+#include <cstdint>
+
+#include <gtest/gtest.h>
+
+#include "arrow/stl.h"
+#include "arrow/stl_iterator.h"
+#include "arrow/testing/gtest_util.h"
+#include "arrow/type.h"
+#include "arrow/util/checked_cast.h"
+
+namespace arrow {
+
+using internal::checked_cast;
+using internal::checked_pointer_cast;
+using util::nullopt;
+using util::optional;
+
+namespace stl {
+
+template <typename T>
+void AssertOptionalEquals(optional<T> value, T expected) {
+  ASSERT_TRUE(value.has_value());
+  ASSERT_EQ(*value, expected);
+}
+
+template <typename T>
+void AssertOptionalEmpty(optional<T> value) {
+  ASSERT_FALSE(value.has_value());
+}
+
+TEST(ArrayIterator, Basics) {
+  auto array =
+      checked_pointer_cast<Int32Array>(ArrayFromJSON(int32(), "[4, 5, null, 6]"));
+
+  ArrayIterator<Int32Array> it(*array);
+  optional<int32_t> v = *it;
+  AssertOptionalEquals(v, 4);
+  AssertOptionalEquals(it[0], 4);
+  ++it;
+  AssertOptionalEquals(it[0], 5);
+  AssertOptionalEquals(*it, 5);
+  AssertOptionalEmpty(it[1]);

Review comment:
       Since this will be used in testing we'll need to reiterate AssertOptionalEquals and AssertOptionalEmpty everywhere, which is a pain. Instead, please use:
   ```suggestion
     EXPECT_EQ(*it, 5);
     EXPECT_EQ(it[1], nullopt);
   ```
   
   And add `void PrintTo(const optional<T>&, std::ostream*)`. This will make equality checks easier to write and will also allow gtest to format containers of optional usefully. I think we'd even be able to write:
   
   ```
   EXPECT_THAT(*array, ElementsAre(1, 2, nullopt, 4));
   ``` 

##########
File path: cpp/src/arrow/stl_iterator.h
##########
@@ -0,0 +1,143 @@
+// 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 <cstddef>
+#include <iterator>
+#include <utility>
+
+#include "arrow/type_fwd.h"
+#include "arrow/util/macros.h"
+#include "arrow/util/optional.h"
+
+namespace arrow {
+namespace stl {
+
+namespace detail {
+
+template <typename ArrayType>
+struct DefaultValueAccessor {
+  using ValueType = decltype(std::declval<ArrayType>().GetView(0));
+
+  ValueType operator()(const ArrayType& array, int64_t index) {

Review comment:
       ```suggestion
     using ViewType = decltype(std::declval<ArrayType>().GetView(0));
   
     ViewType operator()(const ArrayType& array, int64_t index) {
   ```

##########
File path: cpp/src/arrow/stl_iterator.h
##########
@@ -0,0 +1,143 @@
+// 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 <cstddef>
+#include <iterator>
+#include <utility>
+
+#include "arrow/type_fwd.h"
+#include "arrow/util/macros.h"
+#include "arrow/util/optional.h"
+
+namespace arrow {
+namespace stl {
+
+namespace detail {
+
+template <typename ArrayType>
+struct DefaultValueAccessor {
+  using ValueType = decltype(std::declval<ArrayType>().GetView(0));
+
+  ValueType operator()(const ArrayType& array, int64_t index) {
+    return array.GetView(index);
+  }
+};
+
+}  // namespace detail
+
+template <typename ArrayType,
+          typename ValueAccessor = detail::DefaultValueAccessor<ArrayType>>
+class ArrayIterator {
+ public:
+  using value_type =
+      arrow::util::optional<decltype(ValueAccessor()(std::declval<ArrayType>(), 0))>;

Review comment:
       ```suggestion
         arrow::util::optional<typename ValueAccessor::ViewType>;
   ```




----------------------------------------------------------------
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:
users@infra.apache.org