Skip to content

Commit

Permalink
clean up variadics
Browse files Browse the repository at this point in the history
  • Loading branch information
jhellerstein committed Sep 30, 2024
1 parent 41bc04a commit 7540c69
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 90 deletions.
60 changes: 7 additions & 53 deletions variadics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -650,33 +650,6 @@ where
}
}

// #[sealed]
// pub trait Suffix<Suf>: Variadic {
// fn suffix(self) -> Suf;
// }
// #[sealed]
// impl<Item, Rest, PrefixRest> Suffix<PrefixRest> for (Item, Rest)
// where
// PrefixRest: Variadic,
// Rest: Split<PrefixRest>,
// {
// type Suffix = <Rest as Split<PrefixRest>>::Suffix;
// fn split(self) -> ((Item, PrefixRest), Self::Suffix) {
// let (item, rest) = self;
// let (prefix_rest, suffix) = rest.split();
// ((item, prefix_rest), suffix)
// }
// }
// #[sealed]
// impl<Rest> Suffix<Rest> for Rest
// where
// Rest: Variadic,
// {
// fn suffix(self) -> Rest {
// self
// }
// }

/// Helper trait for splitting a variadic into two parts. `Prefix` is the first part, everything
/// after is the `Suffix` or second part.
///
Expand Down Expand Up @@ -793,26 +766,7 @@ where
}
}

// pub trait Split<Prefix>: VariadicExt
// where
// Prefix: VariadicExt,
// {
// /// The second part when splitting this variadic by `Prefix`.
// type Suffix: VariadicExt;
// /// Splits this variadic into two parts, first the `Prefix`, and second the `Suffix`.
// fn split(self) -> (Prefix, Self::Suffix);

// impl<Item, Rest, PrefixRest> Split<(Item, PrefixRest)> for (Item, Rest)
// where
// PrefixRest: VariadicExt,
// Rest: Split<PrefixRest>,
// {
// /// The second part when splitting this variadic by `Prefix`.
// type Suffix = <Rest as Split<PrefixRest>>::Suffix;
// /// Splits this variadic into two parts, first the `Prefix`, and second the `Suffix`.
// fn split(self) -> ((Item, PrefixRest), Self::Suffix) {

/// trait for Variadic of vecs, as formed by `VariadicExt::as_vec()`
/// trait for Variadic of vecs, as formed by `VariadicExt::into_vec()`
#[sealed]
pub trait VecVariadic: VariadicExt {
/// Individual variadic items without the Vec wrapper
Expand All @@ -836,7 +790,7 @@ pub trait VecVariadic: VariadicExt {
type Drain<'a>: Iterator<Item = Self::UnVec>
where
Self: 'a;
/// Turns into a Drain of items `UnVec` -- i.e. rows (not columns!).
/// Turns into a Drain of items `UnVec` -- i.e. iterate through rows (not columns!).
fn drain<R>(&mut self, range: R) -> Self::Drain<'_>
where
R: RangeBounds<usize> + Clone;
Expand All @@ -863,7 +817,6 @@ where
rest_vecs.push(rest_cols);
}

/// get the unvec'ed Variadic at position `index`
fn get(&mut self, index: usize) -> Option<<Self::UnVec as VariadicExt>::AsRefVar<'_>> {
let (this_vec, rest_vecs) = self;
if let Some(rest) = VecVariadic::get(rest_vecs, index) {
Expand All @@ -874,11 +827,11 @@ where
}

type IntoZip = std::iter::Zip<std::vec::IntoIter<Item>, Rest::IntoZip>;
/// Turns into an iterator of items `UnVec` -- i.e. iterate through rows (not columns!).
fn into_zip(self) -> Self::IntoZip {
let (this, rest) = self;
std::iter::zip(this, rest.into_zip())
}

type Drain<'a> = std::iter::Zip<std::vec::Drain<'a, Item>, Rest::Drain<'a>> where Self: 'a;
fn drain<R>(&mut self, range: R) -> Self::Drain<'_>
where
Expand All @@ -896,17 +849,18 @@ impl VecVariadic for var_type!() {
fn zip_vecs(&self) -> impl Iterator<Item = <Self::UnVec as VariadicExt>::AsRefVar<'_>> {
std::iter::repeat(var_expr!())
}

fn push(&mut self, _item: Self::UnVec) {}
/// get the unvec'ed Variadic at position `index`

fn get(&mut self, _index: usize) -> Option<<Self::UnVec as VariadicExt>::AsRefVar<'_>> {
Some(())
}

type IntoZip = std::iter::Repeat<var_type!()>;
// // type IntoIter = Iterator<Item = var_type!()>;
fn into_zip(self) -> Self::IntoZip {
std::iter::repeat(var_expr!())
}

type Drain<'a> = std::iter::Repeat<var_type!()> where Self: 'a;
fn drain<R>(&mut self, _range: R) -> Self::Drain<'_>
where
Expand Down Expand Up @@ -1010,7 +964,7 @@ mod test {
}

#[test]
fn test_as_vec() {
fn test_into_vec() {
use crate::VecVariadic;

type Item = var_type!(i32, String);
Expand Down
57 changes: 20 additions & 37 deletions variadics/src/variadic_collections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub trait VariadicSet {
/// The Schema (aka Variadic type) associated with tuples in this set
type Schema: PartialEqVariadic;

/// Insert an element into the set
/// Insert an element into the set, return true if successful
fn insert(&mut self, element: Self::Schema) -> bool;

/// Iterate over the elements of the set
Expand All @@ -23,7 +23,7 @@ pub trait VariadicSet {
/// Return true if empty
fn is_empty(&self) -> bool;

/// iterate and drain items from the set
/// iterate and drain items from the set without deallocating the container
fn drain(&mut self) -> impl Iterator<Item = Self::Schema>;

/// Check for containment
Expand Down Expand Up @@ -86,7 +86,6 @@ where
{
type Schema = T;

/// insert a tuple
fn insert(&mut self, element: T) -> bool {
// let hash = Self::get_hash(&self.hasher, element.as_ref_var());
let hash = self.hasher.hash_one(element.as_ref_var());
Expand All @@ -104,18 +103,14 @@ where
}
}

/// return the number of tuples in the set
fn len(&self) -> usize {
self.table.len()
}

/// return the number of tuples in the set
fn is_empty(&self) -> bool {
self.table.len() == 0
}

/// drain the set: iterate and remove the tuples without deallocating
// fn drain(&mut self) -> hashbrown::hash_table::Drain<'_, T> {
fn drain(&mut self) -> impl Iterator<Item = Self::Schema> {
self.table.drain()
}
Expand All @@ -124,7 +119,6 @@ where
self.get(value).is_some()
}

/// iterate through the set
fn iter(&self) -> impl Iterator<Item = T::AsRefVar<'_>> {
self.table.iter().map(|item| item.as_ref_var())
}
Expand Down Expand Up @@ -167,7 +161,6 @@ where
S: BuildHasher,
for<'a> K::AsRefVar<'a>: Hash,
{
// #[cfg_attr(feature = "inline-more", inline)]
fn extend<T: IntoIterator<Item = K>>(&mut self, iter: T) {
// Keys may be already present or show multiple times in the iterator.
// Reserve the entire hint lower bound if the map is empty.
Expand All @@ -180,6 +173,7 @@ where
} else {
(iter.size_hint().0 + 1) / 2
};
// TODO figure out reserve!
// let hasher = self.hasher.build_hasher();
// self.table.reserve(reserve, hasher);
iter.for_each(move |k| {
Expand All @@ -198,7 +192,6 @@ where
if self.len() != other.len() {
return false;
}

self.iter().all(|key| other.get(key).is_some())
}
}
Expand All @@ -220,30 +213,30 @@ where
/// Trait for a multiset of Tuples
#[sealed]
pub trait VariadicMultiset {
/// The Schema (aka Variadic type) associated with tuples in this set
/// The Schema (aka Variadic type) associated with tuples in this multiset
type Schema: PartialEqVariadic;

/// Insert an element into the set
fn insert(&mut self, element: Self::Schema);
/// Insert an element into the multiset; return true on success (should be always!)
fn insert(&mut self, element: Self::Schema) -> bool;

/// Iterate over the elements of the set
/// Iterate over the elements of the multiset
fn iter(&self) -> impl Iterator<Item = <Self::Schema as VariadicExt>::AsRefVar<'_>>;

/// Return number of elements in the set
/// Return number of elements in the multiset (including duplicates)
fn len(&self) -> usize;

/// Return true if empty
fn is_empty(&self) -> bool;

/// iterate and drain items from the set
/// iterate and drain items from the multiset without deallocating container
fn drain(&mut self) -> impl Iterator<Item = Self::Schema>;

/// Check for containment
fn contains(&self, value: <Self::Schema as VariadicExt>::AsRefVar<'_>) -> bool;
}

/// HashMap keyed on Variadics of owned values but allows
/// for lookups with RefVariadics as well
/// HashMap keyed on Variadics of (owned value, count) pairs, allows
/// for lookups with RefVariadics.
#[derive(Clone)]
pub struct VariadicCountedHashSet<K, S = RandomState>
where
Expand Down Expand Up @@ -311,8 +304,7 @@ where
{
type Schema = K;

/// insert a tuple
fn insert(&mut self, element: K) {
fn insert(&mut self, element: K) -> bool {
let hash = self.hasher.hash_one(element.as_ref_var());
self.table
.entry(
Expand All @@ -323,19 +315,17 @@ where
.and_modify(|(_, count)| *count += 1)
.or_insert((element, 1));
self.len += 1;
true
}

/// return the number of tuples in the multiset
fn len(&self) -> usize {
self.len
}

/// return the number of tuples in the multiset
fn is_empty(&self) -> bool {
self.len() == 0
}

/// drain the multiset: iterate and remove the tuples without deallocating
fn drain(&mut self) -> impl Iterator<Item = Self::Schema> {
// TODO: this shouldn't clone the last copy of each k!
// particularly bad when there's typically only 1 copy per item
Expand All @@ -349,7 +339,6 @@ where
self.get(value).is_some()
}

/// iterate through the multiset
fn iter(&self) -> impl Iterator<Item = K::AsRefVar<'_>> {
self.table
.iter()
Expand Down Expand Up @@ -377,7 +366,6 @@ where
pub struct DuplicateCounted<Iter, Item> {
iter: Iter,
state: Option<(Item, usize)>,
// count: usize,
}
impl<Iter, Item> Iterator for DuplicateCounted<Iter, Item>
where
Expand Down Expand Up @@ -436,7 +424,6 @@ where
S: BuildHasher,
for<'a> K::AsRefVar<'a>: Hash,
{
// #[cfg_attr(feature = "inline-more", inline)]
fn extend<T: IntoIterator<Item = K>>(&mut self, iter: T) {
// Keys may be already present or show multiple times in the iterator.
// Reserve the entire hint lower bound if the map is empty.
Expand Down Expand Up @@ -495,7 +482,7 @@ where
}

/// Column storage for Variadic tuples of type Schema
/// An alternative to VariadicHashSet
/// An alternative to VariadicHashMultiset
pub struct VariadicColumnMultiset<Schema>
where
Schema: VariadicExt,
Expand All @@ -508,7 +495,7 @@ impl<T> VariadicColumnMultiset<T>
where
T: VariadicExt,
{
/// initialize an empty columnar set
/// initialize an empty columnar multiset
pub fn new() -> Self {
Self {
columns: <T::IntoVec as Default>::default(),
Expand All @@ -533,32 +520,28 @@ where
{
type Schema = Schema;

/// Insert an element into the set
fn insert(&mut self, element: Schema) {
fn insert(&mut self, element: Schema) -> bool {
if self.last_offset == 0 {
self.columns = element.into_singleton_vec()
} else {
self.columns.push(element);
}
self.last_offset += 1;
true
}

/// Iterate over the elements of the set
fn iter(&self) -> impl Iterator<Item = <Schema as VariadicExt>::AsRefVar<'_>> {
self.columns.zip_vecs()
}

/// Return number of elements in the set
fn len(&self) -> usize {
self.last_offset
}

/// Return true if empty
fn is_empty(&self) -> bool {
self.len() == 0
}

/// iterate and drain items from the set
fn drain(&mut self) -> impl Iterator<Item = Self::Schema> {
self.last_offset = 0;
self.columns.drain(0..)
Expand Down Expand Up @@ -660,9 +643,9 @@ mod test {
assert!(test_data.iter().all(|t| multi_set.contains(t.as_ref_var())));
assert!(test_data.iter().all(|t| columnar.contains(t.as_ref_var())));

hash_set.drain();
multi_set.drain();
columnar.drain();
let _hs = hash_set.drain().collect::<Vec<_>>();
let _ms = multi_set.drain().collect::<Vec<_>>();
let _c = columnar.drain().collect::<Vec<_>>();
assert_eq!(hash_set.len(), 0);
assert_eq!(multi_set.len(), 0);
assert_eq!(columnar.len(), 0);
Expand Down

0 comments on commit 7540c69

Please sign in to comment.