Copilot commented on code in PR #905:
URL: https://github.com/apache/iceberg-cpp/pull/905#discussion_r3926584119


##########
src/iceberg/util/iterator.h:
##########
@@ -0,0 +1,124 @@
+/*
+ * 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
+
+/// \file iceberg/util/iterator.h
+/// \brief Pull-based iterator interface for fallible, lazily produced values.
+
+#include <deque>
+#include <optional>
+#include <type_traits>
+#include <utility>
+#include <vector>
+
+#include "iceberg/result.h"
+
+namespace iceberg {
+
+/// \brief A pull-based iterator whose reads may fail.
+///
+/// Iterator implementations own any resources needed to produce values. 
Destroying an
+/// iterator releases those resources, including when iteration stops before 
reaching the
+/// end. Iterators are not thread-safe unless an implementation explicitly 
says otherwise.
+/// Once Next() returns an error or std::nullopt, the iterator is terminal. 
Subsequent
+/// calls return the same terminal result without invoking the implementation 
again.
+///
+/// \tparam T Value returned by the iterator.
+template <typename T>
+class Iterator {
+ public:
+  virtual ~Iterator() = default;
+
+  Iterator() = default;
+  Iterator(const Iterator&) = delete;
+  Iterator& operator=(const Iterator&) = delete;
+  Iterator(Iterator&&) noexcept = default;
+  Iterator& operator=(Iterator&&) noexcept = default;
+
+  /// \brief Return the next value, or std::nullopt when the iterator is 
exhausted.
+  ///
+  /// After this method returns an error or std::nullopt, subsequent calls 
return the same
+  /// terminal result without invoking NextImpl().
+  virtual Result<std::optional<T>> Next() final {
+    if (error_.has_value()) {
+      return std::unexpected(*error_);
+    }
+    if (finished_) {
+      return std::nullopt;
+    }
+
+    auto result = NextImpl();
+    if (!result.has_value()) {
+      error_ = result.error();
+    } else if (!result.value().has_value()) {
+      finished_ = true;
+    }
+    return result;
+  }

Review Comment:
   `Next()` is declared `virtual ... final`, but it’s not intended to be a 
customization point (that’s `NextImpl()`). Making `Next()` non-virtual (and 
removing `final`) avoids implying it can/should participate in overriding, and 
keeps the vtable surface smaller/clearer for a public API.



##########
src/iceberg/util/iterator.h:
##########
@@ -0,0 +1,124 @@
+/*
+ * 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
+
+/// \file iceberg/util/iterator.h
+/// \brief Pull-based iterator interface for fallible, lazily produced values.
+
+#include <deque>
+#include <optional>
+#include <type_traits>
+#include <utility>
+#include <vector>
+
+#include "iceberg/result.h"
+
+namespace iceberg {
+
+/// \brief A pull-based iterator whose reads may fail.
+///
+/// Iterator implementations own any resources needed to produce values. 
Destroying an
+/// iterator releases those resources, including when iteration stops before 
reaching the
+/// end. Iterators are not thread-safe unless an implementation explicitly 
says otherwise.
+/// Once Next() returns an error or std::nullopt, the iterator is terminal. 
Subsequent
+/// calls return the same terminal result without invoking the implementation 
again.
+///
+/// \tparam T Value returned by the iterator.
+template <typename T>
+class Iterator {
+ public:
+  virtual ~Iterator() = default;
+
+  Iterator() = default;
+  Iterator(const Iterator&) = delete;
+  Iterator& operator=(const Iterator&) = delete;
+  Iterator(Iterator&&) noexcept = default;
+  Iterator& operator=(Iterator&&) noexcept = default;
+
+  /// \brief Return the next value, or std::nullopt when the iterator is 
exhausted.
+  ///
+  /// After this method returns an error or std::nullopt, subsequent calls 
return the same
+  /// terminal result without invoking NextImpl().
+  virtual Result<std::optional<T>> Next() final {
+    if (error_.has_value()) {
+      return std::unexpected(*error_);
+    }
+    if (finished_) {
+      return std::nullopt;
+    }
+
+    auto result = NextImpl();
+    if (!result.has_value()) {
+      error_ = result.error();
+    } else if (!result.value().has_value()) {
+      finished_ = true;
+    }
+    return result;
+  }
+
+  /// \brief Consume the remaining values into a vector.
+  Result<std::vector<T>> ToVector() {
+    if constexpr (!std::is_move_constructible_v<T>) {
+      static_assert(std::is_copy_constructible_v<T>,
+                    "Iterator::ToVector requires T to be move- or 
copy-constructible");
+
+      // Stage copy-only values in a deque to avoid copying previously 
collected
+      // values during growth, then allocate the final vector storage once.
+      std::deque<T> values;
+      while (true) {
+        auto result = Next();
+        if (!result.has_value()) {
+          return std::unexpected(std::move(result.error()));
+        }
+        auto& value = result.value();
+        if (!value.has_value()) {
+          return std::vector<T>(values.cbegin(), values.cend());
+        }
+        values.push_back(value.value());
+      }
+    } else {
+      std::vector<T> values;
+      while (true) {
+        auto result = Next();
+        if (!result.has_value()) {
+          return std::unexpected(std::move(result.error()));
+        }
+        auto& value = result.value();
+        if (!value.has_value()) {
+          return values;
+        }
+        values.push_back(std::move_if_noexcept(value.value()));
+      }
+    }
+  }

Review Comment:
   The `while (true)` loop, `Next()` call, and terminal/error handling logic is 
duplicated across both `ToVector()` branches. Consider factoring the shared 
`Next()`/error/exhaustion handling into a small local helper (or a shared loop 
that delegates only the container insertion) to reduce drift risk if 
`ToVector()` behavior changes in the future.



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to