Skip to content

Commit

Permalink
Use vectors as regions (#46)
Browse files Browse the repository at this point in the history
Signed-off-by: Moritz Hoffmann <[email protected]>
  • Loading branch information
antiguru authored Jun 23, 2024
1 parent 4f4d747 commit 84f6123
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/impls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ pub mod slice;
pub mod slice_copy;
pub mod string;
pub mod tuple;
mod vec;
95 changes: 95 additions & 0 deletions src/impls/vec.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
//! Definitions to use `Vec<T>` as a region.
use crate::{Push, Region, ReserveItems};

impl<T: Clone> Region for Vec<T> {
type Owned = T;
type ReadItem<'a> = &'a T where Self: 'a;
type Index = usize;

fn merge_regions<'a>(regions: impl Iterator<Item = &'a Self> + Clone) -> Self
where
Self: 'a,
{
Self::with_capacity(regions.map(Vec::len).sum())
}

fn index(&self, index: Self::Index) -> Self::ReadItem<'_> {
&self[index]
}

fn reserve_regions<'a, I>(&mut self, regions: I)
where
Self: 'a,
I: Iterator<Item = &'a Self> + Clone,
{
self.reserve(regions.map(Vec::len).sum());
}

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

fn heap_size<F: FnMut(usize, usize)>(&self, mut callback: F) {
let size_of_t = std::mem::size_of::<T>();
callback(self.len() * size_of_t, self.capacity() * size_of_t);
}

fn reborrow<'b, 'a: 'b>(item: Self::ReadItem<'a>) -> Self::ReadItem<'b>
where
Self: 'a,
{
item
}
}

impl<T: Clone> Push<T> for Vec<T> {
fn push(&mut self, item: T) -> Self::Index {
self.push(item);
self.len() - 1
}
}

impl<T: Clone> Push<&T> for Vec<T> {
fn push(&mut self, item: &T) -> Self::Index {
self.push(item.clone());
self.len() - 1
}
}

impl<T: Clone> Push<&&T> for Vec<T> {
fn push(&mut self, item: &&T) -> Self::Index {
self.push((*item).clone());
self.len() - 1
}
}

impl<T: Clone, D> ReserveItems<D> for Vec<T> {
fn reserve_items<I>(&mut self, items: I)
where
I: Iterator<Item = D> + Clone,
{
self.reserve(items.count());
}
}

#[cfg(test)]
mod tests {
#[test]
fn vec() {
use crate::{Push, Region, ReserveItems};

let mut region = Vec::<u32>::new();
let index = <_ as Push<_>>::push(&mut region, 42);
assert_eq!(region.index(index), &42);

let mut region = Vec::<u32>::new();
region.push(42);
region.push(43);
region.push(44);
region.reserve_items([1, 2, 3].iter());
assert_eq!(region.index(0), &42);
assert_eq!(region.index(1), &43);
assert_eq!(region.index(2), &44);
}
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ impl<R: Region> FlatStack<R> {
pub fn heap_size<F: FnMut(usize, usize)>(&self, mut callback: F) {
use crate::impls::offsets::OffsetContainer;
self.region.heap_size(&mut callback);
self.indices.heap_size(callback);
OffsetContainer::heap_size(&self.indices, callback);
}
}

Expand Down
1 change: 0 additions & 1 deletion tests/recursive.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! Demonstration of how to encode recursive data structures.
use flatcontainer::impls::deduplicate::ConsecutiveOffsetPairs;
use flatcontainer::impls::offsets::OffsetContainer;
use flatcontainer::{IntoOwned, Push, Region, StringRegion};

#[derive(Clone)]
Expand Down

0 comments on commit 84f6123

Please sign in to comment.