rluvaton commented on code in PR #8785:
URL: https://github.com/apache/arrow-rs/pull/8785#discussion_r2492233804


##########
arrow-array/src/iterator.rs:
##########
@@ -102,6 +102,35 @@ impl<T: ArrayAccessor> Iterator for ArrayIter<T> {
             Some(self.current_end - self.current),
         )
     }
+
+    fn nth(&mut self, n: usize) -> Option<Self::Item> {
+        // Check if we can advance to the desired offset
+        match self.current.checked_add(n) {
+            // Yes, and still within bounds
+            Some(new_current) if new_current < self.current_end => {
+                self.current = new_current;
+            }
+
+            // Either overflow or would exceed current_end
+            _ => {
+                self.current = self.current_end;
+                return None;
+            }
+        }
+
+        self.next()
+    }
+
+    fn last(mut self) -> Option<Self::Item> {
+        self.next_back()
+    }

Review Comment:
   The default implementation is doing it in `O(n)` and is not (currently) 
taking advantage of it being `DoubleEndedIterator` while we are doing it in 
constant time.
   
   this is the default impl:
   ```rust
   #[inline]
   #[stable(feature = "rust1", since = "1.0.0")]
   fn last(self) -> Option<Self::Item>
   where
       Self: Sized,
   {
       #[inline]
       fn some<T>(_: Option<T>, x: T) -> Option<T> {
           Some(x)
       }
   
       self.fold(None, some)
   }
   ```
   
   from [Rust source 
code](https://github.com/rust-lang/rust/blob/f15a7f38580ddbdc1a23909dd05cf6cc6d9f3919/library/core/src/iter/traits/iterator.rs#L233-L260)



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