Skip to content

Commit

Permalink
Adding several implementations that were missing.
Browse files Browse the repository at this point in the history
Signed-off-by: Moritz Hoffmann <[email protected]>
  • Loading branch information
antiguru committed May 22, 2024
1 parent 2487d33 commit ce8ca6e
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/impls/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ fn consolidate_slice<T: Ord>(slice: &mut [(T, usize)]) -> usize {
}

/// A region that encodes its data in a codec `C`.
#[derive(Default, Debug)]
#[derive(Default, Debug, Clone)]
pub struct CodecRegion<C: Codec, R = OwnedRegion<u8>> {
inner: R,
codec: C,
Expand Down
2 changes: 1 addition & 1 deletion src/impls/columns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ use crate::{CopyOnto, OwnedRegion, Region};
/// assert!(row.iter().copied().eq(r.index(index).iter()));
/// }
/// ```
#[derive(Debug)]
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(
feature = "serde",
Expand Down
6 changes: 3 additions & 3 deletions src/impls/offsets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub trait OffsetContainer<T>: Default + Extend<T> {
/// A container for offsets that can represent strides of offsets.
///
/// Does not implement `OffsetContainer` because it cannot accept arbitrary pushes.
#[derive(Debug, Default)]
#[derive(Eq, PartialEq, Debug, Default, Clone, Copy)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum OffsetStride {
/// No push has occurred.
Expand Down Expand Up @@ -136,7 +136,7 @@ impl OffsetStride {
}

/// A list of unsigned integers that uses `u32` elements as long as they are small enough, and switches to `u64` once they are not.
#[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Debug, Default)]
#[derive(Eq, PartialEq, Clone, Debug, Default)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct OffsetList {
/// Offsets that fit within a `u32`.
Expand Down Expand Up @@ -216,7 +216,7 @@ impl OffsetList {

/// An offset container implementation that first tries to recognize strides, and then spilles into
/// a regular offset list.
#[derive(Default, Debug)]
#[derive(Eq, PartialEq, Default, Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct OffsetOptimized {
strided: OffsetStride,
Expand Down
25 changes: 24 additions & 1 deletion src/impls/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,18 @@ impl<'a, C: Region, O: OffsetContainer<C::Index>> IntoIterator for ReadSlice<'a,
}

/// An iterator over the items read from a slice region.
#[derive(Debug, Clone)]
#[derive(Debug)]
pub struct ReadSliceIter<'a, C: Region, O: OffsetContainer<C::Index>>(
&'a SliceRegion<C, O>,
Range<usize>,
);

impl<'a, C: Region, O: OffsetContainer<C::Index>> Clone for ReadSliceIter<'a, C, O> {
fn clone(&self) -> Self {
Self(self.0, self.1.clone())
}
}

impl<'a, C: Region, O: OffsetContainer<C::Index>> Iterator for ReadSliceIter<'a, C, O> {
type Item = C::ReadItem<'a>;

Expand Down Expand Up @@ -361,6 +367,23 @@ where
}
}

impl<R, O> ReserveItems<SliceRegion<R, O>> for ReadSlice<'_, R, O>
where
for<'a> R::ReadItem<'a>: ReserveItems<R>,
R: Region,
O: OffsetContainer<R::Index>,
{
fn reserve_items<I>(target: &mut SliceRegion<R, O>, items: I)
where
I: Iterator<Item = Self> + Clone,
{
target
.slices
.reserve(items.clone().map(|read_slice| read_slice.len()).sum());
ReserveItems::reserve_items(&mut target.inner, items.flatten());
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
35 changes: 34 additions & 1 deletion src/impls/tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,22 @@ macro_rules! tuple_flatcontainer {
tuple_flatcontainer!(reserve_items target items $($name)* @ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31);
}
}

#[allow(non_camel_case_types)]
#[allow(non_snake_case)]
impl<$($name, [<$name _C>]: Region ),*>
ReserveItems<[<Tuple $($name)* Region>]<$([<$name _C>]),*>>
for ($($name,)*)
where
$($name: ReserveItems<[<$name _C>]>),*
{
fn reserve_items<It>(target: &mut [<Tuple $($name)* Region>]<$([<$name _C>]),*>, items: It)
where
It: Iterator<Item = Self> + Clone
{
tuple_flatcontainer!(reserve_items_owned target items $($name)* @ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31);
}
}
}
);
(reserve_items $target:ident $items:ident $name0:ident $($name:ident)* @ $num0:tt $($num:tt)*) => {
Expand All @@ -120,6 +136,15 @@ macro_rules! tuple_flatcontainer {
}
};
(reserve_items $target:ident $items:ident @ $($num:tt)*) => {};
(reserve_items_owned $target:ident $items:ident $name0:ident $($name:ident)* @ $num0:tt $($num:tt)*) => {
paste! {
ReserveItems::reserve_items(&mut $target.[<container $name0>], $items.clone().map(|i| {
i.$num0
}));
tuple_flatcontainer!(reserve_items_owned $target $items $($name)* @ $($num)*);
}
};
(reserve_items_owned $target:ident $items:ident @ $($num:tt)*) => {};
}

tuple_flatcontainer!(A);
Expand Down Expand Up @@ -162,7 +187,7 @@ cfg_if::cfg_if! {
#[cfg(test)]
mod tests {
use crate::impls::tuple::TupleABCRegion;
use crate::{CopyOnto, MirrorRegion, Region, StringRegion};
use crate::{CopyOnto, FlatStack, MirrorRegion, Region, StringRegion};

#[test]
fn test_tuple() {
Expand Down Expand Up @@ -221,4 +246,12 @@ mod tests {
assert!(cap > 0);
assert!(cnt > 0);
}
#[test]
fn test_reserve_items() {
let mut c = FlatStack::default_impl::<(usize, String, Vec<String>)>();
c.copy((1, format!("Hello"), &["abc"]));

let mut c2 = FlatStack::default_impl::<(usize, String, Vec<String>)>();
c2.reserve_items(c.iter());
}
}
21 changes: 14 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,15 @@ impl<'a, R: Region> Iterator for Iter<'a, R> {

impl<R: Region> ExactSizeIterator for Iter<'_, R> {}

impl<R: Region> Clone for Iter<'_, R> {
fn clone(&self) -> Self {
Self {
inner: self.inner.clone(),
region: self.region,
}
}
}

impl<R: Region, T: CopyOnto<R>> FromIterator<T> for FlatStack<R> {
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
let iter = iter.into_iter();
Expand All @@ -287,14 +296,12 @@ impl<R: Region, T: CopyOnto<R>> FromIterator<T> for FlatStack<R> {
}
}

impl<R: Region> Clone for FlatStack<R>
where
for<'a> R::ReadItem<'a>: CopyOnto<R>,
{
impl<R: Region + Clone> Clone for FlatStack<R> {
fn clone(&self) -> Self {
let mut clone = Self::merge_capacity(std::iter::once(self));
clone.extend(self.iter());
clone
Self {
region: self.region.clone(),
indices: self.indices.clone(),
}
}
}

Expand Down

0 comments on commit ce8ca6e

Please sign in to comment.