1
- use crate :: fx:: FxHashMap ;
1
+ use crate :: fx:: FxIndexSet ;
2
2
use crate :: stable_hasher:: { HashStable , StableHasher } ;
3
3
use crate :: sync:: Lock ;
4
4
use rustc_index:: bit_set:: BitMatrix ;
@@ -13,10 +13,7 @@ mod tests;
13
13
#[ derive( Clone , Debug ) ]
14
14
pub struct TransitiveRelation < T : Eq + Hash > {
15
15
// List of elements. This is used to map from a T to a usize.
16
- elements : Vec < T > ,
17
-
18
- // Maps each element to an index.
19
- map : FxHashMap < T , Index > ,
16
+ elements : FxIndexSet < T > ,
20
17
21
18
// List of base edges in the graph. Require to compute transitive
22
19
// closure.
@@ -39,7 +36,6 @@ impl<T: Eq + Hash> Default for TransitiveRelation<T> {
39
36
fn default ( ) -> Self {
40
37
TransitiveRelation {
41
38
elements : Default :: default ( ) ,
42
- map : Default :: default ( ) ,
43
39
edges : Default :: default ( ) ,
44
40
closure : Default :: default ( ) ,
45
41
}
@@ -65,20 +61,16 @@ impl<T: Clone + Debug + Eq + Hash> TransitiveRelation<T> {
65
61
}
66
62
67
63
fn index ( & self , a : & T ) -> Option < Index > {
68
- self . map . get ( a) . cloned ( )
64
+ self . elements . get_index_of ( a) . map ( Index )
69
65
}
70
66
71
67
fn add_index ( & mut self , a : T ) -> Index {
72
- let & mut TransitiveRelation { ref mut elements, ref mut closure, ref mut map, .. } = self ;
73
-
74
- * map. entry ( a. clone ( ) ) . or_insert_with ( || {
75
- elements. push ( a) ;
76
-
68
+ let ( index, added) = self . elements . insert_full ( a) ;
69
+ if added {
77
70
// if we changed the dimensions, clear the cache
78
- * closure. get_mut ( ) = None ;
79
-
80
- Index ( elements. len ( ) - 1 )
81
- } )
71
+ * self . closure . get_mut ( ) = None ;
72
+ }
73
+ Index ( index)
82
74
}
83
75
84
76
/// Applies the (partial) function to each edge and returns a new
@@ -430,14 +422,11 @@ where
430
422
{
431
423
fn decode < D : Decoder > ( d : & mut D ) -> Result < Self , D :: Error > {
432
424
d. read_struct ( "TransitiveRelation" , 2 , |d| {
433
- let elements: Vec < T > = d. read_struct_field ( "elements" , 0 , |d| Decodable :: decode ( d) ) ?;
434
- let edges = d. read_struct_field ( "edges" , 1 , |d| Decodable :: decode ( d) ) ?;
435
- let map = elements
436
- . iter ( )
437
- . enumerate ( )
438
- . map ( |( index, elem) | ( elem. clone ( ) , Index ( index) ) )
439
- . collect ( ) ;
440
- Ok ( TransitiveRelation { elements, edges, map, closure : Lock :: new ( None ) } )
425
+ Ok ( TransitiveRelation {
426
+ elements : d. read_struct_field ( "elements" , 0 , |d| Decodable :: decode ( d) ) ?,
427
+ edges : d. read_struct_field ( "edges" , 1 , |d| Decodable :: decode ( d) ) ?,
428
+ closure : Lock :: new ( None ) ,
429
+ } )
441
430
} )
442
431
}
443
432
}
@@ -452,8 +441,6 @@ where
452
441
let TransitiveRelation {
453
442
ref elements,
454
443
ref edges,
455
- // "map" is just a copy of elements vec
456
- map : _,
457
444
// "closure" is just a copy of the data above
458
445
closure : _,
459
446
} = * self ;
0 commit comments