@@ -252,6 +252,7 @@ impl<K: UnifyKey> UnificationTable<K> {
252
252
self . values . commit ( snapshot. snapshot ) ;
253
253
}
254
254
255
+ /// Creates a fresh key with the given value.
255
256
pub fn new_key ( & mut self , value : K :: Value ) -> K {
256
257
let len = self . values . len ( ) ;
257
258
let key: K = UnifyKey :: from_index ( len as u32 ) ;
@@ -260,6 +261,7 @@ impl<K: UnifyKey> UnificationTable<K> {
260
261
key
261
262
}
262
263
264
+ /// Returns an iterator over all keys unioned with `key`.
263
265
pub fn unioned_keys ( & mut self , key : K ) -> UnionedKeys < K > {
264
266
let root_key = self . get_root_key ( key) ;
265
267
UnionedKeys {
@@ -268,6 +270,13 @@ impl<K: UnifyKey> UnificationTable<K> {
268
270
}
269
271
}
270
272
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`.
271
280
fn value ( & self , key : K ) -> & VarValue < K > {
272
281
& self . values [ key. index ( ) as usize ]
273
282
}
@@ -344,13 +353,16 @@ impl<K: UnifyKey> UnificationTable<K> {
344
353
}
345
354
}
346
355
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.
347
360
fn redirect_root ( & mut self ,
348
361
new_rank : u32 ,
349
362
old_root_key : K ,
350
363
new_root_key : K ,
351
364
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) ;
354
366
self . update_value ( old_root_key, |old_root_value| {
355
367
old_root_value. redirect ( new_root_key, sibling) ;
356
368
} ) ;
@@ -367,9 +379,9 @@ impl<K: UnifyKey> sv::SnapshotVecDelegate for Delegate<K> {
367
379
fn reverse ( _: & mut Vec < VarValue < K > > , _: ( ) ) { }
368
380
}
369
381
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.
373
385
pub struct UnionedKeys < ' a , K >
374
386
where K : UnifyKey + ' a ,
375
387
K :: Value : ' a
@@ -458,6 +470,9 @@ impl<'tcx, K, V> UnificationTable<K>
458
470
self . get_root_key ( id)
459
471
}
460
472
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.
461
476
pub fn unify_var_var ( & mut self , a_id : K , b_id : K ) -> Result < ( ) , V :: Error > {
462
477
let root_a = self . get_root_key ( a_id) ;
463
478
let root_b = self . get_root_key ( b_id) ;
@@ -466,7 +481,7 @@ impl<'tcx, K, V> UnificationTable<K>
466
481
return Ok ( ( ) ) ;
467
482
}
468
483
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 ) ? ;
470
485
471
486
Ok ( self . unify_roots ( root_a, root_b, combined) )
472
487
}
@@ -475,11 +490,13 @@ impl<'tcx, K, V> UnificationTable<K>
475
490
/// with the previous value.
476
491
pub fn unify_var_value ( & mut self , a_id : K , b : V ) -> Result < ( ) , V :: Error > {
477
492
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) ? ;
479
494
self . update_value ( root_a, |node| node. value = value) ;
480
495
Ok ( ( ) )
481
496
}
482
497
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.
483
500
pub fn probe_value ( & mut self , id : K ) -> V {
484
501
let id = self . get_root_key ( id) ;
485
502
self . value ( id) . value . clone ( )
0 commit comments