Skip to content

Commit 25bf0df

Browse files
authored
Merge pull request #45 from the8472/try-get-val
add a non-mutating probe_value method for fast paths
2 parents bde9953 + b02546e commit 25bf0df

File tree

1 file changed

+28
-6
lines changed

1 file changed

+28
-6
lines changed

src/unify/mod.rs

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,12 @@ impl<S: UnificationStoreBase> UnificationTable<S> {
295295
pub fn len(&self) -> usize {
296296
self.values.len()
297297
}
298+
299+
/// Obtains the current value for a particular key.
300+
/// Not for end-users; they can use `probe_value`.
301+
fn value(&self, key: S::Key) -> &VarValue<S::Key> {
302+
&self.values[key.index() as usize]
303+
}
298304
}
299305

300306
impl<S: UnificationStoreMut> UnificationTable<S> {
@@ -325,12 +331,6 @@ impl<S: UnificationStoreMut> UnificationTable<S> {
325331
});
326332
}
327333

328-
/// Obtains the current value for a particular key.
329-
/// Not for end-users; they can use `probe_value`.
330-
fn value(&self, key: S::Key) -> &VarValue<S::Key> {
331-
&self.values[key.index() as usize]
332-
}
333-
334334
/// Find the root node for `vid`. This uses the standard
335335
/// union-find algorithm with path compression:
336336
/// <http://en.wikipedia.org/wiki/Disjoint-set_data_structure>.
@@ -447,6 +447,28 @@ impl<S: UnificationStoreMut> UnificationTable<S> {
447447
/// ////////////////////////////////////////////////////////////////////////
448448
/// Public API
449449
450+
impl<S, K, V> UnificationTable<S>
451+
where
452+
S: UnificationStoreBase<Key = K, Value = V>,
453+
K: UnifyKey<Value = V>,
454+
V: UnifyValue,
455+
{
456+
/// Obtains current value for key without any pointer chasing; may return `None` if key has been union'd.
457+
#[inline]
458+
pub fn try_probe_value<'a, K1>(&'a self, id: K1) -> Option<&'a V>
459+
where
460+
K1: Into<K>,
461+
K: 'a,
462+
{
463+
let id = id.into();
464+
let v = self.value(id);
465+
if v.parent == id {
466+
return Some(&v.value);
467+
}
468+
None
469+
}
470+
}
471+
450472
impl<S, K, V> UnificationTable<S>
451473
where
452474
S: UnificationStoreMut<Key = K, Value = V>,

0 commit comments

Comments
 (0)