Skip to content

add a non-mutating probe_value method for fast paths #45

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 28 additions & 6 deletions src/unify/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,12 @@ impl<S: UnificationStoreBase> UnificationTable<S> {
pub fn len(&self) -> usize {
self.values.len()
}

/// Obtains the current value for a particular key.
/// Not for end-users; they can use `probe_value`.
fn value(&self, key: S::Key) -> &VarValue<S::Key> {
&self.values[key.index() as usize]
}
}

impl<S: UnificationStoreMut> UnificationTable<S> {
Expand Down Expand Up @@ -325,12 +331,6 @@ impl<S: UnificationStoreMut> UnificationTable<S> {
});
}

/// Obtains the current value for a particular key.
/// Not for end-users; they can use `probe_value`.
fn value(&self, key: S::Key) -> &VarValue<S::Key> {
&self.values[key.index() as usize]
}

/// Find the root node for `vid`. This uses the standard
/// union-find algorithm with path compression:
/// <http://en.wikipedia.org/wiki/Disjoint-set_data_structure>.
Expand Down Expand Up @@ -447,6 +447,28 @@ impl<S: UnificationStoreMut> UnificationTable<S> {
/// ////////////////////////////////////////////////////////////////////////
/// Public API

impl<S, K, V> UnificationTable<S>
where
S: UnificationStoreBase<Key = K, Value = V>,
K: UnifyKey<Value = V>,
V: UnifyValue,
{
/// Obtains current value for key without any pointer chasing; may return `None` if key has been union'd.
#[inline]
pub fn try_probe_value<'a, K1>(&'a self, id: K1) -> Option<&'a V>
where
K1: Into<K>,
K: 'a,
{
let id = id.into();
let v = self.value(id);
if v.parent == id {
return Some(&v.value);
}
None
}
}

impl<S, K, V> UnificationTable<S>
where
S: UnificationStoreMut<Key = K, Value = V>,
Expand Down