Skip to content

Commit

Permalink
Ght macros, GhtForest and force seem to be working now
Browse files Browse the repository at this point in the history
  • Loading branch information
jhellerstein committed Aug 26, 2024
1 parent 01b1165 commit 9e1278e
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 197 deletions.
115 changes: 76 additions & 39 deletions lattices/src/ght.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,15 @@ pub trait GeneralizedHashTrieNode: Default {
/// E.g. if we have GhtInner<GhtInner<GhtLeaf...>> the height is 2
fn height(&self) -> Option<usize>;

/// debugging function to check that height is consistent along at least the leftmost path
fn check_height(&self) -> bool;

/// report whether node is a leaf node; else an inner node
fn is_leaf(&self) -> bool;

/// Inserts items into the hash trie.
fn insert(&mut self, row: Self::Schema) -> bool;
/// Inserts items into the hash trie. Returns the height
/// of this node (leaf = 0)
fn insert(&mut self, row: Self::Schema) -> Option<usize>;

/// Returns `true` if the (entire) row is found in the trie, `false` otherwise.
/// See `get()` below to look just for "head" keys in this node
Expand Down Expand Up @@ -105,6 +109,7 @@ where
Node: GeneralizedHashTrieNode,
{
pub(crate) children: HashMap<Head, Node>,
pub(crate) height: Option<usize>,
// pub(crate) _leaf: std::marker::PhantomData<Leaf>,
}
impl<Head, Node: GeneralizedHashTrieNode> Default for GhtInner<Head, Node>
Expand All @@ -116,6 +121,7 @@ where
let children = Default::default();
Self {
children,
height: None,
// _leaf: Default::default(),
}
}
Expand All @@ -136,29 +142,44 @@ where

fn new_from(input: impl IntoIterator<Item = Self::Schema>) -> Self {
let mut retval: Self = Default::default();
let mut height = None;
for row in input {
// let (_prefix, suffix) = row.clone().split();
retval.insert(row);
height = retval.insert(row);
}
retval.height = height;
retval
}

fn height(&self) -> Option<usize> {
if let Some((_k, v)) = self.children.iter().next() {
v.height().map(|h| h + 1)
} else {
None
}
self.height
}

fn check_height(&self) -> bool {
self.height.unwrap()
== self
.children
.iter()
.next()
.and_then(|n| n.1.height())
.map(|h| h + 1)
.unwrap()
}

fn is_leaf(&self) -> bool {
false
}

fn insert(&mut self, row: Self::Schema) -> bool {
fn insert(&mut self, row: Self::Schema) -> Option<usize> {
// TODO(mingwei): clones entire row...
let (_prefix, var_args!(head, ..._rest)) = row.clone().split_by_suffix();
self.children.entry(head).or_default().insert(row)
self.height = self
.children
.entry(head)
.or_default()
.insert(row)
.map(|h| h + 1);
self.height
}

fn contains<'a>(&'a self, row: <Self::Schema as VariadicExt>::AsRefVar<'a>) -> bool {
Expand Down Expand Up @@ -297,12 +318,17 @@ where
Some(0)
}

fn check_height(&self) -> bool {
true
}

fn is_leaf(&self) -> bool {
true
}

fn insert(&mut self, row: Self::Schema) -> bool {
self.elements.insert(row)
fn insert(&mut self, row: Self::Schema) -> Option<usize> {
self.elements.insert(row);
self.height()
}

fn contains<'a>(&'a self, row: <Self::Schema as VariadicExt>::AsRefVar<'a>) -> bool {
Expand Down Expand Up @@ -495,10 +521,13 @@ where
) -> bool {
// TODO(mingwei): clones head...
let (_prefix, var_args!(head, ..._rest)) = Self::Schema::split_by_suffix_ref(row);
self.children
let retval = self
.children
.entry(head.clone())
.or_default()
.merge_leaf(row, leaf)
.merge_leaf(row, leaf);
self.height = self.get(head).and_then(|n| n.height).map(|h| h + 1);
retval
}
}

Expand Down Expand Up @@ -540,8 +569,10 @@ where
>>::split_by_suffix_ref(row);
if let Some(old_val) = self.children.insert(head.clone(), leaf) {
self.children.insert(head.clone(), old_val);
panic!();
false

Check warning on line 573 in lattices/src/ght.rs

View workflow job for this annotation

GitHub Actions / Docs (rustdoc)

unreachable expression

Check warning on line 573 in lattices/src/ght.rs

View workflow job for this annotation

GitHub Actions / Test Suite (WebAssembly) (latest-nightly)

unreachable expression

Check warning on line 573 in lattices/src/ght.rs

View workflow job for this annotation

GitHub Actions / Test Suite (WebAssembly) (pinned-nightly)

unreachable expression
} else {
self.height = Some(1);
true
}
}
Expand Down Expand Up @@ -723,33 +754,34 @@ where
/// Helper that does the heavy lifting for GhtNodeType!
#[macro_export]
macro_rules! GhtNodeTypeWithSchema {
// Empty key, singleton val base case.
(() => $z:ty => $( $schema:ty ),+ ) => (
$crate::ght::GhtLeaf::<$( $schema ),*, $crate::variadics::var_type!( $z ) >
);
// Empty key, compound val base case.
(() => $y:ty, $( $z:ty ),* => $( $schema:ty ),+ ) => (
$crate::ght::GhtLeaf::<$( $schema ),*, ( $y, $crate::variadics::var_type!($( $z ),* )) >
// Empty key & Val (Leaf)
(() => () => $( $schema:ty ),+ ) => (
$crate::ght::GhtLeaf::<$( $schema ),*, () >
);
// Singleton key, empty val base case.
($a:ty => () => ( $schema:ty ),+ ) => (
$crate::ght::GhtInner::<$a, $crate::ght::GhtLeaf::<$( $schema ),*, $crate::variadics::var_type!( $z ) >>

// Empty key (Leaf)
(() => $( $z:ty ),* => $schema:ty ) => (
$crate::ght::GhtLeaf::<$schema, $crate::variadics::var_type!($( $z ),* ) >
);
// Singleton key, singleton val base case.
($a:ty => $z:ty => $( $schema:ty ),+ ) => (
$crate::ght::GhtInner::<$a, $crate::ght::GhtLeaf::<$( $schema ),*, $crate::variadics::var_type!( $z ) >>

// Singleton key & Empty val (Inner over Leaf)
($a:ty => () => $schema:ty ) => (
$crate::ght::GhtInner::<$a, $crate::ght::GhtLeaf::<$schema, () >>
);
// Singleton key, compound val base case.
($a:ty => $y:ty, $( $z:ty ),* => $( $schema:ty ),+ ) => (
$crate::ght::GhtInner::<$a, $crate::ght::GhtLeaf::<$( $schema ),*, $crate::variadics::var_type!($y, $( $z ),*) >>

// Singleton key (Inner over Leaf)
($a:ty => $( $z:ty ),* => $schema:ty ) => (
$crate::ght::GhtInner::<$a, $crate::ght::GhtLeaf::<$schema, $crate::variadics::var_type!($( $z ),*) >>
);
// Compound key, singleton val base case.
($a:ty, $( $b:ty ),* => $z:ty => $( $schema:ty ),+ ) => (
$crate::ght::GhtInner::<$a, $crate::GhtNodeTypeWithSchema!($( $b ),* => $z => $( $schema ),*)>

// Recursive case with empty val
($a:ty, $( $b:ty ),* => () => $schema:ty ) => (
$crate::ght::GhtInner::<$a, $crate::GhtNodeTypeWithSchema!($( $b ),* => () => $schema)>
);

// Recursive case.
($a:ty, $( $b:ty ),* => $( $z:ty ),* => $( $schema:ty ),+ ) => (
$crate::ght::GhtInner::<$a, $crate::GhtNodeTypeWithSchema!($( $b ),* => $( $z ),* => $( $schema ),*)>
($a:ty, $( $b:ty ),* => $( $z:ty ),* => $schema:ty ) => (
$crate::ght::GhtInner::<$a, $crate::GhtNodeTypeWithSchema!($( $b ),* => $( $z ),* => $schema)>
);
}

Expand All @@ -763,13 +795,18 @@ macro_rules! GhtNodeTypeWithSchema {
/// a la var_expr!(T1, T2, T3)
#[macro_export]
macro_rules! GhtType {
// Empty key
(() => $( $z:ty ),* ) => (
$crate::GhtNodeTypeWithSchema!(() => $( $z ),* => $crate::variadics::var_type!($( $z ),* ))
);
($a:ty => $( $z:ty ),*) => (
$crate::GhtNodeTypeWithSchema!($a => $( $z ),* => $crate::variadics::var_type!($a, $( $z ),+ ))

// Recursive case empty val
($( $b:ty ),* => () ) => (
$crate::GhtNodeTypeWithSchema!($( $b ),* => () => $crate::variadics::var_type!($( $b ),*))
);
($a:ty, $( $b:ty ),* => $( $z:ty ),*) => (
$crate::GhtNodeTypeWithSchema!($a, $( $b ),* => $( $z ),* => $crate::variadics::var_type!($a, $( $b ),*, $( $z ),*))

// Recursive case
($( $b:ty ),* => $( $z:ty ),*) => (
$crate::GhtNodeTypeWithSchema!($( $b ),* => $( $z ),* => $crate::variadics::var_type!($( $b ),*, $( $z ),*))
);
}
5 changes: 4 additions & 1 deletion lattices/src/ght_lattice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,10 @@ where
children.insert(head.clone(), val);
}
}
GhtInner { children }
GhtInner {
children,
height: ght_a.height(),
}
}
}

Expand Down
11 changes: 5 additions & 6 deletions lattices/src/ght_lazy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ where
/// one for each height from 1 to length of the schema
macro_rules! GhtForestType {
($a:tt, $( $b:tt ),* => ()) => {
GhtType!($a, $( $b ),* => ())
var_type!(GhtType!($a, $( $b ),* => ()))
};
($a:tt => $c:tt, $( $d:tt ),* ) => {
(GhtType!($a => $c, $( $d ),*), GhtForestType!($a, $c => $( $d ),*))
Expand Down Expand Up @@ -148,11 +148,9 @@ impl<TrieFirst, TrieSecond, TrieRest, SearchKey /* , Head, Rest */> GhtForest<Se
where
TrieFirst: GeneralizedHashTrieNode + GhtTakeLeaf,
TrieSecond: GeneralizedHashTrieNode<Schema = TrieFirst::Schema> + GhtTakeLeaf,
SearchKey: Split<TrieFirst::Schema>,
var_type!(TrieSecond, ...TrieRest): VariadicExt + GhtForest<SearchKey>,
SearchKey: VariadicExt + Clone,
// GhtForestStruct<var_type!(TrieSecond, ...TrieRest)>: GhtForest<SearchKey>,
SearchKey: VariadicExt + Split<TrieFirst::Schema> + Clone,
var_type!(TrieSecond, ...TrieRest): GhtForest<SearchKey>,
// GhtForestStruct<var_type!(TrieSecond, ...TrieRest)>: GhtForest<SearchKey>,
TrieFirst::Schema: PartialEqVariadic + SplitBySuffix<TrieFirst::ValType> + Eq + Hash + Clone,
TrieSecond::Schema: PartialEqVariadic + SplitBySuffix<TrieSecond::ValType> + Eq + Hash + Clone,
Self: ForestFindLeaf<TrieFirst::Schema>,
Expand Down Expand Up @@ -180,6 +178,7 @@ where
_suffix_schema: PhantomData,
};
rest_first.merge_leaf(row.as_ref_var(), leaf);
assert!(rest_first.check_height());
// drop through and recurse: we may have to force again in the neighbor
}
// recurse
Expand Down Expand Up @@ -227,7 +226,7 @@ impl<TrieFirst, TrieRest> ForestFindLeaf<<TrieFirst as GeneralizedHashTrieNode>:
where
<TrieFirst as GeneralizedHashTrieNode>::Schema: PartialEqVariadic,
TrieFirst: GeneralizedHashTrieNode,
TrieRest: VariadicExt + ForestFindLeaf<<TrieFirst as GeneralizedHashTrieNode>::Schema>,
TrieRest: ForestFindLeaf<<TrieFirst as GeneralizedHashTrieNode>::Schema>,
{
fn find_containing_leaf(
&self,
Expand Down
Loading

0 comments on commit 9e1278e

Please sign in to comment.