Skip to content

Commit

Permalink
move TupleSet to variadics/variadic_set.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
jhellerstein committed Sep 26, 2024
1 parent b4cb439 commit 43496b9
Show file tree
Hide file tree
Showing 9 changed files with 172 additions and 457 deletions.
13 changes: 1 addition & 12 deletions hydroflow/tests/surface_lattice_bimorphism.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use lattices::map_union::{KeyedBimorphism, MapUnionHashMap, MapUnionSingletonMap
use lattices::set_union::{CartesianProductBimorphism, SetUnionHashSet, SetUnionSingletonSet};
use lattices::GhtType;
use multiplatform_test::multiplatform_test;
use variadics::hash_set::VariadicHashSet;
use variadics::variadic_sets::VariadicHashSet;
use variadics::{var_expr, CloneVariadic};

#[multiplatform_test]
Expand Down Expand Up @@ -156,17 +156,6 @@ fn test_ght_join_bimorphism() {
>>::DeepJoinLatticeBimorphism;
type MyBim = GhtBimorphism<MyNodeBim>;

// let mut hf = hydroflow_syntax! {
// lhs = source_iter_delta([
// var_expr!(123, 2, 5, "hello"),
// var_expr!(50, 1, 1, "hi"),
// var_expr!(5, 1, 7, "hi"),
// var_expr!(5, 1, 7, "bye"),
// ])
// -> map(|row| MyGhtATrie::new_from([row]))
// -> state::<'tick, MyGhtATrie>();
// };

let mut hf = hydroflow_syntax! {
lhs = source_iter_delta([
var_expr!(123, 2, 5, "hello"),
Expand Down
2 changes: 1 addition & 1 deletion hydroflow/tests/surface_lattice_generalized_hash_trie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use hydroflow::lattices::ght_lattice::{DeepJoinLatticeBimorphism, GhtBimorphism}
use hydroflow::lattices::GhtType;
use hydroflow::util::collect_ready;
use hydroflow::variadics::{var_expr, var_type};
use variadics::hash_set::VariadicHashSet; // Import the Insert trait
use variadics::variadic_sets::VariadicHashSet; // Import the Insert trait

#[test]
fn test_basic() {
Expand Down
93 changes: 17 additions & 76 deletions lattices/src/ght.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::hash::Hash;
use std::marker::PhantomData;

use sealed::sealed;
use variadics::hash_set::VariadicHashSet;
use variadics::variadic_sets::VariadicSet;
use variadics::{
var_args, var_type, PartialEqVariadic, RefVariadic, Split, SplitBySuffix, VariadicExt,
};
Expand All @@ -24,7 +24,7 @@ pub trait GeneralizedHashTrieNode: Default {
/// This type is the same in all nodes of the trie.
type ValType: VariadicExt + Eq + Hash + Clone;
/// The type that holds the data in the leaves
type Storage: TupleSet<Schema = Self::Schema> + Default + IntoIterator<Item = Self::Schema>;
type Storage: VariadicSet<Schema = Self::Schema> + Default + IntoIterator<Item = Self::Schema>;

/// SuffixSchema variadic: the suffix of the schema *from this node of the trie
/// downward*. The first entry in this variadic is of type Head.
Expand Down Expand Up @@ -206,7 +206,7 @@ where
impl<Schema, ValType, Storage> FromIterator<Schema> for GhtLeaf<Schema, ValType, Storage>
where
Schema: Eq + Hash,
Storage: TupleSet<Schema = Schema> + Default + FromIterator<Schema>,
Storage: VariadicSet<Schema = Schema> + Default + FromIterator<Schema>,
{
fn from_iter<Iter: IntoIterator<Item = Schema>>(iter: Iter) -> Self {
let elements = iter.into_iter().collect();
Expand All @@ -218,72 +218,13 @@ where
}
}

/// Trait for a set of Tuples
pub trait TupleSet {
/// The Schema (aka Variadic type) associated with tuples in this set
type Schema: PartialEqVariadic;

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

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

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

/// 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>;

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

impl<Schema> TupleSet for VariadicHashSet<Schema>
where
Schema: 'static + Eq + Hash + PartialEqVariadic,
for<'a> <Schema as VariadicExt>::AsRefVar<'a>: Hash,
{
type Schema = Schema;

fn insert(&mut self, element: Self::Schema) -> bool {
self.insert(element)
}

fn iter(&self) -> impl Iterator<Item = <Self::Schema as VariadicExt>::AsRefVar<'_>> {
self.iter()
}

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

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

fn drain(&mut self) -> impl Iterator<Item = Self::Schema> {
self.drain()
}

fn contains(&self, value: <Self::Schema as VariadicExt>::AsRefVar<'_>) -> bool {
self.get(value).is_some()
}
}

/// leaf node of a HashTrie
#[derive(Debug, PartialEq, Eq, Clone)]
// #[repr(transparent)]
pub struct GhtLeaf<Schema, ValType, Storage>
where
Schema: Eq + Hash,
Storage: TupleSet<Schema = Schema>,
Storage: VariadicSet<Schema = Schema>,
{
pub(crate) elements: Storage,
pub(crate) forced: bool,
Expand All @@ -292,7 +233,7 @@ where
impl<Schema, ValType, Storage> Default for GhtLeaf<Schema, ValType, Storage>
where
Schema: Eq + Hash,
Storage: TupleSet<Schema = Schema> + Default,
Storage: VariadicSet<Schema = Schema> + Default,
{
fn default() -> Self {
let elements = Default::default();
Expand All @@ -319,7 +260,7 @@ where
var_type!(ValHead, ...ValRest): Clone + Eq + Hash + PartialEqVariadic,
<Schema as SplitBySuffix<var_type!(ValHead, ...ValRest)>>::Prefix: Eq + Hash + Clone,
// for<'a> Schema::AsRefVar<'a>: PartialEq,
Storage: TupleSet<Schema = Schema> + Default + IntoIterator<Item = Schema>,
Storage: VariadicSet<Schema = Schema> + Default + IntoIterator<Item = Schema>,
{
type Schema = Schema;
type SuffixSchema = var_type!(ValHead, ...ValRest);
Expand Down Expand Up @@ -389,7 +330,7 @@ where
+ Clone
// + SplitBySuffix<var_type!(ValHead, ...ValRest)>
+ PartialEqVariadic,
Storage: TupleSet<Schema = Schema> + Default + IntoIterator<Item = Schema>,
Storage: VariadicSet<Schema = Schema> + Default + IntoIterator<Item = Schema>,
// ValHead: Clone + Eq + Hash,
// var_type!(ValHead, ...ValRest): Clone + Eq + Hash + PartialEqVariadic,
// <Schema as SplitBySuffix<var_type!(ValHead, ...ValRest)>>::Prefix: Eq + Hash + Clone,
Expand Down Expand Up @@ -510,7 +451,7 @@ where
ValType: Eq + Hash + Clone + PartialEqVariadic,
<Schema as SplitBySuffix<ValType>>::Prefix: Eq + Hash + Clone,
GhtLeaf<Schema, ValType, Storage>: GeneralizedHashTrieNode<Schema = Schema>,
Storage: TupleSet<Schema = Schema>,
Storage: VariadicSet<Schema = Schema>,
{
/// Type returned by [`Self::get`].
type Get = GhtLeaf<Schema, ValType, Storage>;
Expand Down Expand Up @@ -621,7 +562,7 @@ where
Head,
<GhtLeaf<Schema, ValType, Storage> as GeneralizedHashTrieNode>::SuffixSchema,
)>,
Storage: 'static + TupleSet<Schema = Schema> + Default,
Storage: 'static + VariadicSet<Schema = Schema> + Default,
{
fn take_containing_leaf(
&mut self,
Expand Down Expand Up @@ -666,7 +607,7 @@ where
Schema: 'static + Hash + Clone + Eq + PartialEqVariadic + SplitBySuffix<ValType>,
ValType: 'static + Hash + Clone + Eq + VariadicExt + PartialEqVariadic,
GhtLeaf<Schema, ValType, Storage>: GeneralizedHashTrieNode,
Storage: 'static + TupleSet<Schema = Schema>,
Storage: 'static + VariadicSet<Schema = Schema>,
{
fn take_containing_leaf(
&mut self,
Expand Down Expand Up @@ -752,7 +693,7 @@ where
// for<'a> SuffixSchema::AsRefVar<'a>: Split<KeyPrefixRef>,
ValType: Split<KeyPrefixRef::UnRefVar>,
KeyPrefixRef::UnRefVar: PartialEqVariadic,
Storage: 'static + TupleSet<Schema = Schema>,
Storage: 'static + VariadicSet<Schema = Schema>,
{
type Item = Schema;
fn prefix_iter<'a>(
Expand Down Expand Up @@ -787,17 +728,17 @@ macro_rules! GhtRowTypeWithSchema {

// Empty key (Leaf)
(() => $( $z:ty ),* => $schema:ty ) => (
$crate::ght::GhtLeaf::<$schema, $crate::variadics::var_type!($( $z ),* ), $crate::variadics::hash_set::VariadicHashSet<$schema> >
$crate::ght::GhtLeaf::<$schema, $crate::variadics::var_type!($( $z ),* ), $crate::variadics::variadic_sets::VariadicHashSet<$schema> >
);

// Singleton key & Empty val (Inner over Leaf)
($a:ty => () => $schema:ty ) => (
$crate::ght::GhtInner::<$a, $crate::ght::GhtLeaf::<$schema, (), $crate::variadics::hash_set::VariadicHashSet<$schema> >>
$crate::ght::GhtInner::<$a, $crate::ght::GhtLeaf::<$schema, (), $crate::variadics::variadic_sets::VariadicHashSet<$schema> >>
);

// Singleton key (Inner over Leaf)
($a:ty => $( $z:ty ),* => $schema:ty ) => (
$crate::ght::GhtInner::<$a, $crate::ght::GhtLeaf::<$schema, $crate::variadics::var_type!($( $z ),*), $crate::variadics::hash_set::VariadicHashSet<$schema> >>
$crate::ght::GhtInner::<$a, $crate::ght::GhtLeaf::<$schema, $crate::variadics::var_type!($( $z ),*), $crate::variadics::variadic_sets::VariadicHashSet<$schema> >>
);

// Recursive case with empty val
Expand All @@ -823,17 +764,17 @@ macro_rules! GhtColumnTypeWithSchema {

// Empty key (Leaf)
(() => $( $z:ty ),* => $schema:ty ) => (
$crate::ght::GhtLeaf::<$schema, $crate::variadics::var_type!($( $z ),* ), $crate::ght_lazy::VariadicColumnarSet<$schema> >
$crate::ght::GhtLeaf::<$schema, $crate::variadics::var_type!($( $z ),* ), $crate::variadics::variadic_sets::VariadicColumnarSet<$schema> >
);

// Singleton key & Empty val (Inner over Leaf)
($a:ty => () => $schema:ty ) => (
$crate::ght::GhtInner::<$a, $crate::ght::GhtLeaf::<$schema, (), $crate::ght_lazy::VariadicColumnarSet<$schema> >>
$crate::ght::GhtInner::<$a, $crate::ght::GhtLeaf::<$schema, (), $crate::variadics::variadic_sets::VariadicColumnarSet<$schema> >>
);

// Singleton key (Inner over Leaf)
($a:ty => $( $z:ty ),* => $schema:ty ) => (
$crate::ght::GhtInner::<$a, $crate::ght::GhtLeaf::<$schema, $crate::variadics::var_type!($( $z ),*), $crate::ght_lazy::VariadicColumnarSet<$schema> >>
$crate::ght::GhtInner::<$a, $crate::ght::GhtLeaf::<$schema, $crate::variadics::var_type!($( $z ),*), $crate::variadics::variadic_sets::VariadicColumnarSet<$schema> >>
);

// Recursive case with empty val
Expand Down
21 changes: 11 additions & 10 deletions lattices/src/ght_lattice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ use std::cmp::Ordering;
use std::collections::HashMap;
use std::hash::Hash;

use variadics::variadic_sets::VariadicSet;
use variadics::{var_expr, var_type, CloneVariadic, PartialEqVariadic, SplitBySuffix, VariadicExt};

use crate::ght::{GeneralizedHashTrieNode, GhtGet, GhtInner, GhtLeaf, TupleSet};
use crate::ght::{GeneralizedHashTrieNode, GhtGet, GhtInner, GhtLeaf};
use crate::{IsBot, IsTop, LatticeBimorphism, LatticeOrd, Merge};

//////////////////////////
Expand Down Expand Up @@ -51,7 +52,7 @@ impl<Schema, ValType, Storage> Merge<GhtLeaf<Schema, ValType, Storage>>
for GhtLeaf<Schema, ValType, Storage>
where
Schema: Eq + Hash,
Storage: TupleSet<Schema = Schema> + Extend<Schema> + IntoIterator<Item = Schema>,
Storage: VariadicSet<Schema = Schema> + Extend<Schema> + IntoIterator<Item = Schema>,
{
fn merge(&mut self, other: GhtLeaf<Schema, ValType, Storage>) -> bool {
let old_len = self.elements.len();
Expand Down Expand Up @@ -170,7 +171,7 @@ impl<Schema, SuffixSchema, Storage> PartialOrd<GhtLeaf<Schema, SuffixSchema, Sto
where
Schema: Eq + Hash + PartialEqVariadic,
SuffixSchema: Eq + Hash,
Storage: TupleSet<Schema = Schema> + PartialEq,
Storage: VariadicSet<Schema = Schema> + PartialEq,
{
fn partial_cmp(&self, other: &GhtLeaf<Schema, SuffixSchema, Storage>) -> Option<Ordering> {
match self.elements.len().cmp(&other.elements.len()) {
Expand Down Expand Up @@ -228,7 +229,7 @@ impl<Schema, SuffixSchema, Storage> LatticeOrd<GhtLeaf<Schema, SuffixSchema, Sto
where
Schema: Eq + Hash + PartialEqVariadic,
SuffixSchema: Eq + Hash,
Storage: TupleSet<Schema = Schema> + PartialEq,
Storage: VariadicSet<Schema = Schema> + PartialEq,
{
}

Expand Down Expand Up @@ -257,7 +258,7 @@ impl<Schema, SuffixSchema, Storage> IsBot for GhtLeaf<Schema, SuffixSchema, Stor
where
Schema: Eq + Hash,
SuffixSchema: Eq + Hash,
Storage: TupleSet<Schema = Schema>,
Storage: VariadicSet<Schema = Schema>,
{
fn is_bot(&self) -> bool {
self.elements.is_empty()
Expand Down Expand Up @@ -289,7 +290,7 @@ impl<Schema, SuffixSchema, Storage> IsTop for GhtLeaf<Schema, SuffixSchema, Stor
where
Schema: Eq + Hash,
SuffixSchema: Eq + Hash,
Storage: TupleSet<Schema = Schema>,
Storage: VariadicSet<Schema = Schema>,
{
fn is_top(&self) -> bool {
false
Expand Down Expand Up @@ -484,7 +485,7 @@ where
NodeA: 'static + GeneralizedHashTrieNode,
NodeB: 'static + GeneralizedHashTrieNode,
(NodeA, NodeB): DeepJoinLatticeBimorphism<Storage>,
Storage: TupleSet<Schema = var_type!(...NodeA::Schema, ...NodeB::ValType)>,
Storage: VariadicSet<Schema = var_type!(...NodeA::Schema, ...NodeB::ValType)>,
{
type DeepJoinLatticeBimorphism = GhtNodeKeyedBimorphism<
<(NodeA, NodeB) as DeepJoinLatticeBimorphism<Storage>>::DeepJoinLatticeBimorphism,
Expand All @@ -501,9 +502,9 @@ where
ValTypeA: 'static + VariadicExt + Eq + Hash, // + AsRefVariadicPartialEq
SchemaB: 'static + VariadicExt + Eq + Hash + SplitBySuffix<ValTypeB>, /* + AsRefVariadicPartialEq */
ValTypeB: 'static + VariadicExt + Eq + Hash, // + AsRefVariadicPartialEq
StorageA: TupleSet<Schema = SchemaA>,
StorageB: TupleSet<Schema = SchemaB>,
StorageOut: TupleSet<Schema = var_type!(...SchemaA, ...ValTypeB)>,
StorageA: VariadicSet<Schema = SchemaA>,
StorageB: VariadicSet<Schema = SchemaB>,
StorageOut: VariadicSet<Schema = var_type!(...SchemaA, ...ValTypeB)>,
for<'x> SchemaA::AsRefVar<'x>: CloneVariadic,
for<'x> SchemaB::AsRefVar<'x>: CloneVariadic,
var_type!(...SchemaA, ...ValTypeB): Eq + Hash,
Expand Down
Loading

0 comments on commit 43496b9

Please sign in to comment.