Skip to content

Commit

Permalink
Added extra methods
Browse files Browse the repository at this point in the history
  • Loading branch information
dewert99 committed Apr 1, 2024
1 parent 70ba130 commit 8ee283b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/raw/egraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,37 @@ impl<L: Language> EGraphResidual<L> {
self.lookup_internal(enode).map(|id| self.find(id))
}

/// Lookup the uncanonical id of the given uncanonical enode.
///
/// Returns `Some(id)` such that `self.id_to_node(id) == enode` if possible
///
/// # Example
/// ```
/// # use egg::{SymbolLang, raw::*};
/// let mut egraph: RawEGraph<SymbolLang, ()> = Default::default();
/// let a = egraph.add_uncanonical(SymbolLang::leaf("a"));
/// let b = egraph.add_uncanonical(SymbolLang::leaf("b"));
///
/// let mut node_f_ab = SymbolLang::new("f", vec![a, b]);
/// let id = egraph.add_uncanonical(node_f_ab.clone());
/// assert_eq!(egraph.lookup_uncanonical(&node_f_ab), Some(id));
///
/// // if the query node isn't canonical, and its passed in by &mut instead of owned,
/// // its children will be canonicalized
/// egraph.union(a, b);
/// egraph.rebuild();
/// assert_eq!(egraph.lookup_uncanonical(&node_f_ab), Some(id));
/// assert_eq!(egraph.lookup_uncanonical(&SymbolLang::new("f", vec![a, a])), None);
/// ```
pub fn lookup_uncanonical(&self, enode: &L) -> Option<Id> {
let id = *self.memo.get(enode).0?;
if self.id_to_node(id) == enode {
Some(id)
} else {
None
}
}

#[inline]
fn lookup_internal<B>(&self, mut enode: B) -> Option<Id>
where
Expand Down
5 changes: 5 additions & 0 deletions src/raw/semi_persistent1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ impl PushInfo {
pub fn number_of_uncanonical_nodes(&self) -> usize {
self.node_count
}

/// Returns the number of unions from the state where `self` was created
pub fn number_of_unions(&self) -> usize {
self.union_count
}
}

#[derive(Clone, Debug)]
Expand Down
5 changes: 5 additions & 0 deletions src/raw/semi_persistent2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ impl PushInfo {
pub fn number_of_uncanonical_nodes(&self) -> usize {
self.node_count
}

/// Returns the number of unions from the state where `self` was created
pub fn number_of_unions(&self) -> usize {
self.union_count
}
}

/// Value for [`UndoLogT`] that enables [`push2`](RawEGraph::push2) and [`raw_pop2`](RawEGraph::raw_pop2)
Expand Down

0 comments on commit 8ee283b

Please sign in to comment.