-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement iterators for ObjectChunked
- Loading branch information
Showing
15 changed files
with
164 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
use crate::chunked_array::object::{ObjectArray, PolarsObject}; | ||
use arrow::array::Array; | ||
|
||
/// An iterator that returns Some(T) or None, that can be used on any ObjectArray | ||
// Note: This implementation is based on std's [Vec]s' [IntoIter]. | ||
#[derive(Debug)] | ||
pub struct ObjectIter<'a, T: PolarsObject> { | ||
array: &'a ObjectArray<T>, | ||
current: usize, | ||
current_end: usize, | ||
} | ||
|
||
impl<'a, T: PolarsObject> ObjectIter<'a, T> { | ||
/// create a new iterator | ||
pub fn new(array: &'a ObjectArray<T>) -> Self { | ||
ObjectIter::<T> { | ||
array, | ||
current: 0, | ||
current_end: array.len(), | ||
} | ||
} | ||
} | ||
|
||
impl<'a, T: PolarsObject> std::iter::Iterator for ObjectIter<'a, T> { | ||
type Item = Option<&'a T>; | ||
|
||
#[inline] | ||
fn next(&mut self) -> Option<Self::Item> { | ||
if self.current == self.current_end { | ||
None | ||
} else if self.array.is_null(self.current) { | ||
self.current += 1; | ||
Some(None) | ||
} else { | ||
let old = self.current; | ||
self.current += 1; | ||
// Safety: | ||
// we just checked bounds in `self.current_end == self.current` | ||
// this is safe on the premise that this struct is initialized with | ||
// current = array.len() | ||
// and that current_end is ever only decremented | ||
unsafe { Some(Some(self.array.value_unchecked(old))) } | ||
} | ||
} | ||
|
||
fn size_hint(&self) -> (usize, Option<usize>) { | ||
( | ||
self.array.len() - self.current, | ||
Some(self.array.len() - self.current), | ||
) | ||
} | ||
} | ||
|
||
impl<'a, T: PolarsObject> std::iter::DoubleEndedIterator for ObjectIter<'a, T> { | ||
fn next_back(&mut self) -> Option<Self::Item> { | ||
if self.current_end == self.current { | ||
None | ||
} else { | ||
self.current_end -= 1; | ||
Some(if self.array.is_null(self.current_end) { | ||
None | ||
} else { | ||
// Safety: | ||
// we just checked bounds in `self.current_end == self.current` | ||
// this is safe on the premise that this struct is initialized with | ||
// current = array.len() | ||
// and that current_end is ever only decremented | ||
unsafe { Some(self.array.value_unchecked(self.current_end)) } | ||
}) | ||
} | ||
} | ||
} | ||
|
||
/// all arrays have known size. | ||
impl<'a, T: PolarsObject> std::iter::ExactSizeIterator for ObjectIter<'a, T> {} | ||
|
||
impl<'a, T: PolarsObject> IntoIterator for &'a ObjectArray<T> { | ||
type Item = Option<&'a T>; | ||
type IntoIter = ObjectIter<'a, T>; | ||
|
||
fn into_iter(self) -> Self::IntoIter { | ||
ObjectIter::<'a, T>::new(self) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.