Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 52 additions & 1 deletion arrow-array/src/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl<T: ArrayAccessor> ArrayIter<T> {
/// create a new iterator
pub fn new(array: T) -> Self {
let len = array.len();
let logical_nulls = array.logical_nulls();
let logical_nulls = array.logical_nulls().filter(|x| x.null_count() > 0);
ArrayIter {
array,
logical_nulls,
Expand Down Expand Up @@ -102,6 +102,38 @@ impl<T: ArrayAccessor> Iterator for ArrayIter<T> {
Some(self.current_end - self.current),
)
}

#[inline]
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()
}

#[inline]
fn last(mut self) -> Option<Self::Item> {
self.next_back()
}

#[inline]
fn count(self) -> usize
where
Self: Sized,
{
self.len()
}
}

impl<T: ArrayAccessor> DoubleEndedIterator for ArrayIter<T> {
Expand All @@ -122,6 +154,25 @@ impl<T: ArrayAccessor> DoubleEndedIterator for ArrayIter<T> {
})
}
}

#[inline]
fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
// Check if we advance to the one before the desired offset
match self.current_end.checked_sub(n) {
// Yes, and still within bounds
Some(new_offset) if self.current < new_offset => {
self.current_end = new_offset;
}

// Either underflow or would exceed current
_ => {
self.current = self.current_end;
return None;
}
}

self.next_back()
}
}

/// all arrays have known size.
Expand Down