Skip to content

Commit

Permalink
Remove sequential
Browse files Browse the repository at this point in the history
Signed-off-by: Moritz Hoffmann <[email protected]>
  • Loading branch information
antiguru committed Jun 24, 2024
1 parent 63f3861 commit 4d3af85
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 93 deletions.
57 changes: 27 additions & 30 deletions src/impls/deduplicate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

use crate::impls::offsets::{OffsetContainer, OffsetOptimized, OffsetStride};
use crate::impls::offsets::{OffsetContainer, OffsetOptimized};
use crate::impls::tuple::TupleABRegion;
use crate::{Push, Region, ReserveItems};

Expand Down Expand Up @@ -122,13 +122,12 @@ where
///
/// The following example shows that two inserts into a copy region have a collapsible index:
/// ```
/// use flatcontainer::impls::deduplicate::{CollapseSequence, ConsecutiveOffsetPairs, Sequential};
/// use flatcontainer::impls::deduplicate::{CollapseSequence, ConsecutiveOffsetPairs};
/// use flatcontainer::{Push, OwnedRegion, Region, StringRegion};
/// let mut r = <ConsecutiveOffsetPairs<OwnedRegion<u8>>>::default();
///
/// let index = r.push(&b"abc");
/// assert_eq!(index, Sequential(0));
/// assert_eq!(b"abc", r.index(Sequential(0)));
/// assert_eq!(b"abc", r.index(index));
/// ```
#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
Expand Down Expand Up @@ -184,7 +183,7 @@ where
where
Self: 'a;

type Index = Sequential;
type Index = usize;

#[inline]
fn merge_regions<'a>(regions: impl Iterator<Item = &'a Self> + Clone) -> Self
Expand All @@ -203,7 +202,7 @@ where
#[inline]
fn index(&self, index: Self::Index) -> Self::ReadItem<'_> {
self.inner
.index((self.offsets.index(index.0), self.offsets.index(index.0 + 1)))
.index((self.offsets.index(index), self.offsets.index(index + 1)))
}

#[inline]
Expand Down Expand Up @@ -249,7 +248,7 @@ where
debug_assert_eq!(index.0, self.last_index);
self.last_index = index.1;
self.offsets.push(index.1);
Sequential(self.offsets.len() - 2)
self.offsets.len() - 2
}
}

Expand All @@ -266,51 +265,49 @@ where
}
}

/// TODO
#[derive(Copy, Clone, Debug, Ord, PartialOrd, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Sequential(pub usize);

/// TODO
#[derive(Default)]
pub struct CombineSequential<R, S>(R, S);

impl<A, B, T> Push<T> for CombineSequential<TupleABRegion<A, B>, (OffsetStride, OffsetStride)>
impl<A, B, T, O1, O2> Push<T> for CombineSequential<TupleABRegion<A, B>, (O1, O2)>
where
A: Region<Index = Sequential>,
B: Region<Index = Sequential>,
TupleABRegion<A, B>: Region<Index = (Sequential, Sequential)> + Push<T>,
Self: Region<Index = Sequential>,
A: Region<Index = usize>,
B: Region<Index = usize>,
O1: OffsetContainer<usize>,
O2: OffsetContainer<usize>,
TupleABRegion<A, B>: Region<Index = (usize, usize)> + Push<T>,
Self: Region<Index = usize>,
{
fn push(&mut self, item: T) -> Self::Index {
let (a, b) = self.0.push(item);
let pushed = self.1.0.push(a.0);
assert!(pushed, "0 ({:?}).push({a:?})", self.1.0);
let pushed = self.1.1.push(b.0);
assert!(pushed, "1 ({:?}).push({b:?})", self.1.1);
assert_eq!(self.1.0.len(), self.1.1.len());
Sequential(self.1.0.len() - 1)
self.1 .0.push(a);
self.1 .1.push(b);
assert_eq!(self.1 .0.len(), self.1 .1.len());
self.1 .0.len() - 1
}
}

impl<A, B> Region for CombineSequential<TupleABRegion<A, B>, (OffsetStride, OffsetStride)>
impl<A, B, O1, O2> Region for CombineSequential<TupleABRegion<A, B>, (O1, O2)>
where
A: Region<Index = Sequential>,
B: Region<Index = Sequential>,
A: Region<Index = usize>,
B: Region<Index = usize>,
O1: OffsetContainer<usize>,
O2: OffsetContainer<usize>,
{
type Owned = <TupleABRegion<A, B> as Region>::Owned;
type ReadItem<'a> = <TupleABRegion<A, B> as Region>::ReadItem<'a>
where
Self: 'a;
type Index = Sequential;
type Index = usize;

fn merge_regions<'a>(regions: impl Iterator<Item = &'a Self> + Clone) -> Self
where
Self: 'a,
{
Self(<TupleABRegion<A, B> as Region>::merge_regions(
regions.map(|r| &r.0),
), Default::default())
Self(
<TupleABRegion<A, B> as Region>::merge_regions(regions.map(|r| &r.0)),
Default::default(),
)
}

fn index(&self, index: Self::Index) -> Self::ReadItem<'_> {
Expand Down
50 changes: 1 addition & 49 deletions src/impls/offsets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

use crate::impls::deduplicate::Sequential;
use crate::impls::storage::Storage;

/// A container to store offsets.
Expand Down Expand Up @@ -226,53 +225,6 @@ where
spilled: OffsetList<S, L>,
}

impl Storage<Sequential> for OffsetStride {
fn with_capacity(_capacity: usize) -> Self {
Self::default()
}

fn clear(&mut self) {
self.clear();
}

fn len(&self) -> usize {
self.len()
}

fn reserve(&mut self, _additional: usize) {
// Nop
}

fn heap_size<F: FnMut(usize, usize)>(&self, _callback: F) {
// Nop
}

fn is_empty(&self) -> bool {
self.is_empty()
}
}

impl OffsetContainer<Sequential> for OffsetStride {
fn index(&self, index: usize) -> Sequential {
Sequential(self.index(index))
}

fn push(&mut self, item: Sequential) {
let pushed = self.push(item.0);
debug_assert!(pushed, "Failed to push {item:?} into {self:?}");
}

fn extend<I: IntoIterator<Item = Sequential>>(&mut self, iter: I)
where
I::IntoIter: ExactSizeIterator,
{
for item in iter {
let pushed = self.push(item.0);
debug_assert!(pushed);
}
}
}

impl<S, L> Storage<usize> for OffsetOptimized<S, L>
where
S: OffsetContainer<u32>,
Expand Down Expand Up @@ -375,7 +327,7 @@ mod tests {

let mut r = SliceRegion::<
ConsecutiveOffsetPairs<StringRegion, OffsetOptimized>,
OffsetStride,
OffsetOptimized,
>::default();
let idx = copy(&mut r, ["abc"]);
assert_eq!("abc", r.index(idx).get(0))
Expand Down
2 changes: 1 addition & 1 deletion src/impls/slice_copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::marker::PhantomData;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

use crate::impls::storage::{Storage, PushStorage};
use crate::impls::storage::{PushStorage, Storage};
use crate::{CopyIter, Push, Region, ReserveItems};

/// A container for owned types.
Expand Down
27 changes: 14 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,8 +413,8 @@ pub struct CopyIter<I>(pub I);

#[cfg(test)]
mod tests {
use crate::impls::deduplicate::{CollapseSequence, ConsecutiveOffsetPairs, Sequential};
use crate::impls::offsets::OffsetStride;
use crate::impls::deduplicate::{CollapseSequence, ConsecutiveOffsetPairs};
use crate::impls::offsets::OffsetOptimized;
use crate::impls::tuple::{TupleABRegion, TupleARegion};

use super::*;
Expand Down Expand Up @@ -662,43 +662,44 @@ mod tests {
ConsecutiveOffsetPairs<SliceRegion<MirrorRegion<u8>>>,
ConsecutiveOffsetPairs<OwnedRegion<u8>>,
>>::default();
let _index: (Sequential, Sequential) = r.push(&item);
let _index: (usize, usize) = r.push(&item);

let mut r = <CombineSequential<
TupleABRegion<
ConsecutiveOffsetPairs<SliceRegion<MirrorRegion<u8>>>,
ConsecutiveOffsetPairs<OwnedRegion<u8>>,
>,
(OffsetStride, OffsetStride),
(OffsetOptimized, OffsetOptimized),
>>::default();
let _index: Sequential = r.push(&item);
let _index: usize = r.push(&item);

let mut fs = FlatStack::<
CombineSequential<
TupleABRegion<
ConsecutiveOffsetPairs<SliceRegion<MirrorRegion<u8>>>,
// CollapseSequence<ConsecutiveOffsetPairs<OwnedRegion<u8>>>,
ConsecutiveOffsetPairs<OwnedRegion<u8>>,
CollapseSequence<ConsecutiveOffsetPairs<OwnedRegion<u8>>>,
>,
(OffsetStride, OffsetStride),
(OffsetOptimized, OffsetOptimized),
>,
OffsetStride,
OffsetOptimized,
>::default();

for _ in 0..1000 {
fs.copy(&item);
for item in std::iter::repeat(&item)
.take(1000)
.chain(std::iter::once(&item2))
{
fs.copy(item);
let mut size = 0;
let mut capacity = 0;
let mut count = 0;
fs.heap_size(|siz, cap| {
size += siz;
capacity += cap;
count += 1;
count += (cap > 0) as usize
});

println!("size {size}, capacity {capacity}, allocations {count}");
}
fs.copy(&item2);
assert_eq!(&item.1, fs.get(0).1);
}

Expand Down

0 comments on commit 4d3af85

Please sign in to comment.