This repository has been archived by the owner on Feb 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 224
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added MutableUtf8ValuesArray (#1260)
- Loading branch information
1 parent
828d976
commit 9574d7f
Showing
10 changed files
with
804 additions
and
242 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
use crate::trusted_len::TrustedLen; | ||
|
||
mod private { | ||
pub trait Sealed {} | ||
|
||
impl<'a, T: super::ArrayAccessor<'a>> Sealed for T {} | ||
} | ||
|
||
/// | ||
/// # Safety | ||
/// Implementers of this trait guarantee that | ||
/// `value_unchecked` is safe when called up to `len` | ||
/// Implementations must guarantee that | ||
pub unsafe trait ArrayAccessor<'a>: private::Sealed { | ||
type Item: 'a; | ||
unsafe fn value_unchecked(&'a self, index: usize) -> Self::Item; | ||
fn len(&self) -> usize; | ||
} | ||
|
||
/// Iterator of values of an `ArrayAccessor`. | ||
#[derive(Debug, Clone)] | ||
pub struct ArrayValuesIter<'a, A: ArrayAccessor<'a>> { | ||
array: &'a A, | ||
index: usize, | ||
end: usize, | ||
} | ||
|
||
impl<'a, A: ArrayAccessor<'a>> ArrayValuesIter<'a, A> { | ||
/// Creates a new [`ArrayValuesIter`] | ||
#[inline] | ||
pub fn new(array: &'a A) -> Self { | ||
Self { | ||
array, | ||
index: 0, | ||
end: array.len(), | ||
} | ||
} | ||
} | ||
|
||
impl<'a, A: ArrayAccessor<'a>> Iterator for ArrayValuesIter<'a, A> { | ||
type Item = A::Item; | ||
|
||
#[inline] | ||
fn next(&mut self) -> Option<Self::Item> { | ||
if self.index == self.end { | ||
return None; | ||
} | ||
let old = self.index; | ||
self.index += 1; | ||
Some(unsafe { self.array.value_unchecked(old) }) | ||
} | ||
|
||
#[inline] | ||
fn size_hint(&self) -> (usize, Option<usize>) { | ||
(self.end - self.index, Some(self.end - self.index)) | ||
} | ||
|
||
#[inline] | ||
fn nth(&mut self, n: usize) -> Option<Self::Item> { | ||
let new_index = self.index + n; | ||
if new_index > self.end { | ||
self.index = self.end; | ||
None | ||
} else { | ||
self.index = new_index; | ||
self.next() | ||
} | ||
} | ||
} | ||
|
||
impl<'a, A: ArrayAccessor<'a>> DoubleEndedIterator for ArrayValuesIter<'a, A> { | ||
#[inline] | ||
fn next_back(&mut self) -> Option<Self::Item> { | ||
if self.index == self.end { | ||
None | ||
} else { | ||
self.end -= 1; | ||
Some(unsafe { self.array.value_unchecked(self.end) }) | ||
} | ||
} | ||
} | ||
|
||
unsafe impl<'a, A: ArrayAccessor<'a>> TrustedLen for ArrayValuesIter<'a, A> {} |
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 |
---|---|---|
@@ -1,77 +1,79 @@ | ||
use crate::array::{ArrayAccessor, ArrayValuesIter, Offset}; | ||
use crate::bitmap::utils::ZipValidity; | ||
use crate::{array::Offset, trusted_len::TrustedLen}; | ||
|
||
use super::Utf8Array; | ||
use super::{MutableUtf8Array, MutableUtf8ValuesArray, Utf8Array}; | ||
|
||
/// Iterator of values of an `Utf8Array`. | ||
#[derive(Debug, Clone)] | ||
pub struct Utf8ValuesIter<'a, O: Offset> { | ||
array: &'a Utf8Array<O>, | ||
index: usize, | ||
end: usize, | ||
unsafe impl<'a, O: Offset> ArrayAccessor<'a> for Utf8Array<O> { | ||
type Item = &'a str; | ||
|
||
#[inline] | ||
unsafe fn value_unchecked(&'a self, index: usize) -> Self::Item { | ||
self.value_unchecked(index) | ||
} | ||
|
||
#[inline] | ||
fn len(&self) -> usize { | ||
self.len() | ||
} | ||
} | ||
|
||
impl<'a, O: Offset> Utf8ValuesIter<'a, O> { | ||
/// Creates a new [`Utf8ValuesIter`] | ||
pub fn new(array: &'a Utf8Array<O>) -> Self { | ||
Self { | ||
array, | ||
index: 0, | ||
end: array.len(), | ||
} | ||
/// Iterator of values of an [`Utf8Array`]. | ||
pub type Utf8ValuesIter<'a, O> = ArrayValuesIter<'a, Utf8Array<O>>; | ||
|
||
impl<'a, O: Offset> IntoIterator for &'a Utf8Array<O> { | ||
type Item = Option<&'a str>; | ||
type IntoIter = ZipValidity<'a, &'a str, Utf8ValuesIter<'a, O>>; | ||
|
||
fn into_iter(self) -> Self::IntoIter { | ||
self.iter() | ||
} | ||
} | ||
|
||
impl<'a, O: Offset> Iterator for Utf8ValuesIter<'a, O> { | ||
unsafe impl<'a, O: Offset> ArrayAccessor<'a> for MutableUtf8Array<O> { | ||
type Item = &'a str; | ||
|
||
#[inline] | ||
fn next(&mut self) -> Option<Self::Item> { | ||
if self.index == self.end { | ||
return None; | ||
} | ||
let old = self.index; | ||
self.index += 1; | ||
Some(unsafe { self.array.value_unchecked(old) }) | ||
unsafe fn value_unchecked(&'a self, index: usize) -> Self::Item { | ||
self.value_unchecked(index) | ||
} | ||
|
||
#[inline] | ||
fn size_hint(&self) -> (usize, Option<usize>) { | ||
(self.end - self.index, Some(self.end - self.index)) | ||
fn len(&self) -> usize { | ||
self.len() | ||
} | ||
} | ||
|
||
#[inline] | ||
fn nth(&mut self, n: usize) -> Option<Self::Item> { | ||
let new_index = self.index + n; | ||
if new_index > self.end { | ||
self.index = self.end; | ||
None | ||
} else { | ||
self.index = new_index; | ||
self.next() | ||
} | ||
/// Iterator of values of an [`MutableUtf8ValuesArray`]. | ||
pub type MutableUtf8ValuesIter<'a, O> = ArrayValuesIter<'a, MutableUtf8ValuesArray<O>>; | ||
|
||
impl<'a, O: Offset> IntoIterator for &'a MutableUtf8Array<O> { | ||
type Item = Option<&'a str>; | ||
type IntoIter = ZipValidity<'a, &'a str, MutableUtf8ValuesIter<'a, O>>; | ||
|
||
fn into_iter(self) -> Self::IntoIter { | ||
self.iter() | ||
} | ||
} | ||
|
||
impl<'a, O: Offset> DoubleEndedIterator for Utf8ValuesIter<'a, O> { | ||
unsafe impl<'a, O: Offset> ArrayAccessor<'a> for MutableUtf8ValuesArray<O> { | ||
type Item = &'a str; | ||
|
||
#[inline] | ||
fn next_back(&mut self) -> Option<Self::Item> { | ||
if self.index == self.end { | ||
None | ||
} else { | ||
self.end -= 1; | ||
Some(unsafe { self.array.value_unchecked(self.end) }) | ||
} | ||
unsafe fn value_unchecked(&'a self, index: usize) -> Self::Item { | ||
self.value_unchecked(index) | ||
} | ||
|
||
#[inline] | ||
fn len(&self) -> usize { | ||
self.len() | ||
} | ||
} | ||
|
||
impl<'a, O: Offset> IntoIterator for &'a Utf8Array<O> { | ||
type Item = Option<&'a str>; | ||
type IntoIter = ZipValidity<'a, &'a str, Utf8ValuesIter<'a, O>>; | ||
impl<'a, O: Offset> IntoIterator for &'a MutableUtf8ValuesArray<O> { | ||
type Item = &'a str; | ||
type IntoIter = ArrayValuesIter<'a, MutableUtf8ValuesArray<O>>; | ||
|
||
fn into_iter(self) -> Self::IntoIter { | ||
self.iter() | ||
} | ||
} | ||
|
||
unsafe impl<O: Offset> TrustedLen for Utf8ValuesIter<'_, O> {} |
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.