Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
Signed-off-by: Moritz Hoffmann <[email protected]>
  • Loading branch information
antiguru committed May 31, 2024
1 parent 6ed3422 commit 5fc0e05
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 20 deletions.
22 changes: 18 additions & 4 deletions src/impls/columns.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//! A region to contain a variable number of columns.
use std::fmt::Debug;
use std::iter::Zip;
use std::slice::Iter;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -304,13 +306,11 @@ where
}

/// An iterator over the elements of a row.
pub struct ReadColumnsIter<'a, R: Region>(
Result<ReadColumnsIterInner<'a, R>, std::slice::Iter<'a, R::Owned>>,
);
pub struct ReadColumnsIter<'a, R: Region>(Result<ReadColumnsIterInner<'a, R>, Iter<'a, R::Owned>>);

/// An iterator over the elements of a row.
pub struct ReadColumnsIterInner<'a, R: Region> {
iter: std::iter::Zip<std::slice::Iter<'a, R::Index>, std::slice::Iter<'a, R>>,
iter: Zip<Iter<'a, R::Index>, Iter<'a, R>>,
}

impl<'a, R> Iterator for ReadColumnsIter<'a, R>
Expand All @@ -325,8 +325,17 @@ where
Err(slice) => slice.next().map(IntoOwned::borrow_as),
}
}

fn size_hint(&self) -> (usize, Option<usize>) {
match &self.0 {
Ok(inner) => inner.size_hint(),
Err(slice) => slice.size_hint(),
}
}
}

impl<'a, R> ExactSizeIterator for ReadColumnsIter<'a, R> where R: Region {}

impl<'a, R> Iterator for ReadColumnsIterInner<'a, R>
where
R: Region,
Expand All @@ -336,6 +345,10 @@ where
fn next(&mut self) -> Option<Self::Item> {
self.iter.next().map(|(&i, r)| r.index(i))
}

fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
}

impl<R> Push<ReadColumns<'_, R>> for ColumnsRegion<R>
Expand Down Expand Up @@ -450,6 +463,7 @@ impl<R, T, I> Push<CopyIter<I>> for ColumnsRegion<R>
where
R: Region + Push<T>,
I: IntoIterator<Item = T>,
I::IntoIter: ExactSizeIterator,
{
#[inline]
fn push(&mut self, item: CopyIter<I>) -> <ColumnsRegion<R> as Region>::Index {
Expand Down
10 changes: 7 additions & 3 deletions src/impls/slice_copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,10 @@ where
}
}

impl<T: Clone, S: Storage<T>, I: IntoIterator<Item = T>> Push<CopyIter<I>> for OwnedRegion<T, S> {
impl<T: Clone, S: Storage<T>, I: IntoIterator<Item = T>> Push<CopyIter<I>> for OwnedRegion<T, S>
where
<I as IntoIterator>::IntoIter: ExactSizeIterator,
{
#[inline]
fn push(&mut self, item: CopyIter<I>) -> <OwnedRegion<T, S> as Region>::Index {
(self.slices.len(), self.slices.extend(item.0))
Expand Down Expand Up @@ -257,8 +260,9 @@ mod tests {
#[test]
fn test_copy_iter() {
let mut r = <OwnedRegion<u8>>::default();
r.reserve_items(std::iter::once(CopyIter(std::iter::repeat(1).take(4))));
let index = r.push(CopyIter(std::iter::repeat(1).take(4)));
let iter = [1; 4].into_iter();
r.reserve_items(std::iter::once(CopyIter(iter.clone())));
let index = r.push(CopyIter(iter));
assert_eq!([1, 1, 1, 1], r.index(index));
}
}
20 changes: 7 additions & 13 deletions src/impls/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ pub trait Storage<T>: Default {
/// TODO
fn heap_size<F: FnMut(usize, usize)>(&self, callback: F);
/// TODO
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) -> usize;
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) -> usize
where
I::IntoIter: ExactSizeIterator;
/// TODO
fn append(&mut self, data: &mut Vec<T>) -> usize;
/// TODO
Expand Down Expand Up @@ -195,18 +197,10 @@ impl<T> Storage<T> for Doubling<T> {

#[inline]
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) -> usize {
let mut iter = iter.into_iter().peekable();
while iter.peek().is_some() {
if let Some(last) = self
.inner
.last_mut()
.filter(|last| last.len() < last.capacity())
{
Extend::extend(last, (&mut iter).take(last.capacity() - last.len()));
}
let (lo, hi) = iter.size_hint();
self.reserve(hi.unwrap_or(lo));
}
let iter = iter.into_iter();
let (lo, hi) = iter.size_hint();
self.reserve(hi.unwrap_or(lo));
Extend::extend(self.inner.last_mut().unwrap(), iter);
self.len()
}

Expand Down

0 comments on commit 5fc0e05

Please sign in to comment.