Skip to content

Commit

Permalink
better doubling
Browse files Browse the repository at this point in the history
Signed-off-by: Moritz Hoffmann <[email protected]>
  • Loading branch information
antiguru committed May 30, 2024
1 parent 11dc039 commit 1f92fbc
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
20 changes: 5 additions & 15 deletions src/impls/slice_copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,18 +110,14 @@ where
{
#[inline]
fn push(&mut self, item: [T; N]) -> <OwnedRegion<T> as Region>::Index {
let start = self.slices.len();
self.slices.extend(item);
(start, self.slices.len())
(self.slices.len(), self.slices.extend(item))
}
}

impl<T: Clone, S: Storage<T>, const N: usize> Push<&[T; N]> for OwnedRegion<T, S> {
#[inline]
fn push(&mut self, item: &[T; N]) -> <OwnedRegion<T> as Region>::Index {
let start = self.slices.len();
self.slices.extend_from_slice(item);
(start, self.slices.len())
self.push(item.as_slice())
}
}

Expand All @@ -145,9 +141,7 @@ impl<'b, T: Clone, S: Storage<T>, const N: usize> ReserveItems<&'b [T; N]> for O
impl<T: Clone, S: Storage<T>> Push<&[T]> for OwnedRegion<T, S> {
#[inline]
fn push(&mut self, item: &[T]) -> <OwnedRegion<T, S> as Region>::Index {
let start = self.slices.len();
self.slices.extend_from_slice(item);
(start, self.slices.len())
(self.slices.len(), self.slices.extend_from_slice(item))
}
}

Expand Down Expand Up @@ -182,9 +176,7 @@ where
{
#[inline]
fn push(&mut self, mut item: Vec<T>) -> <OwnedRegion<T, S> as Region>::Index {
let start = self.slices.len();
self.slices.append(&mut item);
(start, self.slices.len())
(self.slices.len(), self.slices.append(&mut item))
}
}

Expand Down Expand Up @@ -212,9 +204,7 @@ where
impl<T: Clone, S: Storage<T>, I: IntoIterator<Item = T>> Push<CopyIter<I>> for OwnedRegion<T, S> {
#[inline]
fn push(&mut self, item: CopyIter<I>) -> <OwnedRegion<T, S> as Region>::Index {
let start = self.slices.len();
self.slices.extend(item.0);
(start, self.slices.len())
(self.slices.len(), self.slices.extend(item.0))
}
}

Expand Down
18 changes: 14 additions & 4 deletions src/impls/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ impl<T> Default for Doubling<T> {
}
}

impl<T: Clone> Storage<T> for Doubling<T> {
impl<T> Storage<T> for Doubling<T> {
#[inline]
fn with_capacity(capacity: usize) -> Self {
Self {
Expand Down Expand Up @@ -179,7 +179,10 @@ impl<T: Clone> Storage<T> for Doubling<T> {

#[inline]
fn clear(&mut self) {
self.inner.clear();
self.inner.drain(..self.len().saturating_sub(1));
if let Some(last) = self.inner.last_mut() {
last.clear();
}
}

#[inline]
Expand All @@ -192,8 +195,15 @@ impl<T: Clone> Storage<T> for Doubling<T> {

#[inline]
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) -> usize {

Check warning on line 197 in src/impls/storage.rs

View workflow job for this annotation

GitHub Actions / cargo fmt

Diff in /home/runner/work/flatcontainer/flatcontainer/src/impls/storage.rs
let vec: Vec<T> = iter.into_iter().collect();
self.extend_from_slice(&vec)
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));
}
self.len()
}

#[inline]
Expand Down

0 comments on commit 1f92fbc

Please sign in to comment.