Skip to content

Commit

Permalink
Use handle equality
Browse files Browse the repository at this point in the history
  • Loading branch information
hendrikvanantwerpen committed Mar 20, 2023
1 parent 5f9f4ae commit 1caa5ee
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 37 deletions.
20 changes: 20 additions & 0 deletions stack-graphs/src/arena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,10 @@ pub trait Arena<T> {

/// Returns the number of instances stored in this arena.
fn len(&self) -> usize;

/// Returns whether the values associated with the arena are equal, if this can be determined from the
/// handles alone. Otherwise, no value is returned.
fn try_equals(&self, lhs: Handle<T>, rhs: Handle<T>) -> Option<bool>;
}

/// Manages the life cycle of instances of type `T`. You can allocate new instances of `T` from
Expand Down Expand Up @@ -261,6 +265,11 @@ impl<T> Arena<T> for VecArena<T> {
fn len(&self) -> usize {
self.items.len()
}

#[inline]
fn try_equals(&self, _lhs: Handle<T>, _rhs: Handle<T>) -> Option<bool> {
None
}
}

/// An interning arena allocation. Equal handles means equal elements.
Expand Down Expand Up @@ -320,6 +329,11 @@ where
fn len(&self) -> usize {
self.arena.len()
}

#[inline]
fn try_equals(&self, lhs: Handle<T>, rhs: Handle<T>) -> Option<bool> {
Some(lhs == rhs)
}
}

//-------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -672,6 +686,9 @@ where
T: Eq,
{
pub fn equals(self, arena: &impl ListArena<T>, other: List<T>) -> bool {
if let Some(equals) = arena.try_equals(self.cells, other.cells) {
return equals;
}
self.equals_with(arena, other, |a, b| *a == *b)
}
}
Expand Down Expand Up @@ -987,6 +1004,9 @@ where
T: Eq,
{
pub fn equals(self, arena: &impl ReversibleListArena<T>, other: ReversibleList<T>) -> bool {
if let Some(equals) = arena.try_equals(self.cells, other.cells) {
return equals;
}
self.equals_with(arena, other, |a, b| *a == *b)
}
}
Expand Down
52 changes: 15 additions & 37 deletions stack-graphs/src/partial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ impl DisplayWithPartialPaths for PartialScopedSymbol {
/// A pattern that might match against a symbol stack. Consists of a (possibly empty) list of
/// partial scoped symbols, along with an optional symbol stack variable.
#[repr(C)]
#[derive(Clone, Copy)]
#[derive(Clone, Copy, Eq, Hash, PartialEq)]
pub struct PartialSymbolStack {
symbols: ReversibleList<PartialScopedSymbol>,
length: u32,
Expand Down Expand Up @@ -791,24 +791,13 @@ impl PartialSymbolStack {
unreachable!();
}

pub fn equals(mut self, partials: &mut PartialPaths, mut other: PartialSymbolStack) -> bool {
while let Some(self_symbol) = self.pop_front(partials) {
if let Some(other_symbol) = other.pop_front(partials) {
if !self_symbol.equals(partials, &other_symbol) {
return false;
}
} else {
return false;
}
}
if !other.symbols.is_empty() {
return false;
}
equals_option(
self.variable.into_option(),
other.variable.into_option(),
|a, b| a == b,
)
pub fn equals(self, _partials: &mut PartialPaths, other: PartialSymbolStack) -> bool {
self.symbols == other.symbols
&& equals_option(
self.variable.into_option(),
other.variable.into_option(),
|a, b| a == b,
)
}

pub fn cmp(
Expand Down Expand Up @@ -1204,11 +1193,8 @@ impl PartialScopeStack {
self.variable.into_option()
}

pub fn equals(self, partials: &mut PartialPaths, other: PartialScopeStack) -> bool {
self.scopes
.equals_with(&mut partials.partial_scope_stacks, other.scopes, |a, b| {
*a == *b
})
pub fn equals(self, _partials: &mut PartialPaths, other: PartialScopeStack) -> bool {
self.scopes == other.scopes
&& equals_option(
self.variable.into_option(),
other.variable.into_option(),
Expand Down Expand Up @@ -1852,21 +1838,13 @@ impl PartialPath {
self.edges.shadows(partials, other.edges)
}

pub fn equals(&self, partials: &mut PartialPaths, other: &PartialPath) -> bool {
pub fn equals(&self, _partials: &mut PartialPaths, other: &PartialPath) -> bool {
self.start_node == other.start_node
&& self.end_node == other.end_node
&& self
.symbol_stack_precondition
.equals(partials, other.symbol_stack_precondition)
&& self
.symbol_stack_postcondition
.equals(partials, other.symbol_stack_postcondition)
&& self
.scope_stack_precondition
.equals(partials, other.scope_stack_precondition)
&& self
.scope_stack_postcondition
.equals(partials, other.scope_stack_postcondition)
&& self.symbol_stack_precondition == other.symbol_stack_precondition
&& self.symbol_stack_postcondition == other.symbol_stack_postcondition
&& self.scope_stack_precondition == other.scope_stack_precondition
&& self.scope_stack_postcondition == other.scope_stack_postcondition
}

pub fn cmp(
Expand Down

0 comments on commit 1caa5ee

Please sign in to comment.