Skip to content

Commit ee37a93

Browse files
committed
add some comments and a len method
1 parent 953c918 commit ee37a93

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

src/unify/mod.rs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ impl<K: UnifyKey> UnificationTable<K> {
252252
self.values.commit(snapshot.snapshot);
253253
}
254254

255+
/// Creates a fresh key with the given value.
255256
pub fn new_key(&mut self, value: K::Value) -> K {
256257
let len = self.values.len();
257258
let key: K = UnifyKey::from_index(len as u32);
@@ -260,6 +261,7 @@ impl<K: UnifyKey> UnificationTable<K> {
260261
key
261262
}
262263

264+
/// Returns an iterator over all keys unioned with `key`.
263265
pub fn unioned_keys(&mut self, key: K) -> UnionedKeys<K> {
264266
let root_key = self.get_root_key(key);
265267
UnionedKeys {
@@ -268,6 +270,13 @@ impl<K: UnifyKey> UnificationTable<K> {
268270
}
269271
}
270272

273+
/// Returns the number of keys created so far.
274+
pub fn len(&self) -> usize {
275+
self.values.len()
276+
}
277+
278+
/// Obtains the current value for a particular key.
279+
/// Not for end-users; they can use `probe_value`.
271280
fn value(&self, key: K) -> &VarValue<K> {
272281
&self.values[key.index() as usize]
273282
}
@@ -344,13 +353,16 @@ impl<K: UnifyKey> UnificationTable<K> {
344353
}
345354
}
346355

356+
/// Internal method to redirect `old_root_key` (which is currently
357+
/// a root) to a child of `new_root_key` (which will remain a
358+
/// root). The rank and value of `new_root_key` will be updated to
359+
/// `new_rank` and `new_value` respectively.
347360
fn redirect_root(&mut self,
348361
new_rank: u32,
349362
old_root_key: K,
350363
new_root_key: K,
351364
new_value: K::Value) {
352-
let sibling = self.value(new_root_key).child(new_root_key)
353-
.unwrap_or(old_root_key);
365+
let sibling = self.value(new_root_key).child(new_root_key).unwrap_or(old_root_key);
354366
self.update_value(old_root_key, |old_root_value| {
355367
old_root_value.redirect(new_root_key, sibling);
356368
});
@@ -367,9 +379,9 @@ impl<K: UnifyKey> sv::SnapshotVecDelegate for Delegate<K> {
367379
fn reverse(_: &mut Vec<VarValue<K>>, _: ()) {}
368380
}
369381

370-
/// ////////////////////////////////////////////////////////////////////////
371-
/// Iterator over keys that have been unioned together
372-
382+
/// Iterator over keys that have been unioned together.
383+
///
384+
/// Returned by the `unioned_keys` method.
373385
pub struct UnionedKeys<'a, K>
374386
where K: UnifyKey + 'a,
375387
K::Value: 'a
@@ -458,6 +470,9 @@ impl<'tcx, K, V> UnificationTable<K>
458470
self.get_root_key(id)
459471
}
460472

473+
/// Unions together two variables, merging their values. If
474+
/// merging the values fails, the error is propagated and this
475+
/// method has no effect.
461476
pub fn unify_var_var(&mut self, a_id: K, b_id: K) -> Result<(), V::Error> {
462477
let root_a = self.get_root_key(a_id);
463478
let root_b = self.get_root_key(b_id);
@@ -466,7 +481,7 @@ impl<'tcx, K, V> UnificationTable<K>
466481
return Ok(());
467482
}
468483

469-
let combined = try!(V::unify_values(&self.value(root_a).value, &self.value(root_b).value));
484+
let combined = V::unify_values(&self.value(root_a).value, &self.value(root_b).value)?;
470485

471486
Ok(self.unify_roots(root_a, root_b, combined))
472487
}
@@ -475,11 +490,13 @@ impl<'tcx, K, V> UnificationTable<K>
475490
/// with the previous value.
476491
pub fn unify_var_value(&mut self, a_id: K, b: V) -> Result<(), V::Error> {
477492
let root_a = self.get_root_key(a_id);
478-
let value = try!(V::unify_values(&self.value(root_a).value, &b));
493+
let value = V::unify_values(&self.value(root_a).value, &b)?;
479494
self.update_value(root_a, |node| node.value = value);
480495
Ok(())
481496
}
482497

498+
/// Returns the current value for the given key. If the key has
499+
/// been union'd, this will give the value from the current root.
483500
pub fn probe_value(&mut self, id: K) -> V {
484501
let id = self.get_root_key(id);
485502
self.value(id).value.clone()

0 commit comments

Comments
 (0)