mirror of
https://github.com/rust-lang/rust.git
synced 2025-02-08 04:56:58 +00:00
rustc_data_structures: use IndexSet in TransitiveRelation
This commit is contained in:
parent
1f71f0f2b5
commit
d3c70b8af5
@ -1,4 +1,4 @@
|
|||||||
use crate::fx::FxHashMap;
|
use crate::fx::FxIndexSet;
|
||||||
use crate::stable_hasher::{HashStable, StableHasher};
|
use crate::stable_hasher::{HashStable, StableHasher};
|
||||||
use crate::sync::Lock;
|
use crate::sync::Lock;
|
||||||
use rustc_index::bit_set::BitMatrix;
|
use rustc_index::bit_set::BitMatrix;
|
||||||
@ -13,10 +13,7 @@ mod tests;
|
|||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct TransitiveRelation<T: Eq + Hash> {
|
pub struct TransitiveRelation<T: Eq + Hash> {
|
||||||
// List of elements. This is used to map from a T to a usize.
|
// List of elements. This is used to map from a T to a usize.
|
||||||
elements: Vec<T>,
|
elements: FxIndexSet<T>,
|
||||||
|
|
||||||
// Maps each element to an index.
|
|
||||||
map: FxHashMap<T, Index>,
|
|
||||||
|
|
||||||
// List of base edges in the graph. Require to compute transitive
|
// List of base edges in the graph. Require to compute transitive
|
||||||
// closure.
|
// closure.
|
||||||
@ -39,7 +36,6 @@ impl<T: Eq + Hash> Default for TransitiveRelation<T> {
|
|||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
TransitiveRelation {
|
TransitiveRelation {
|
||||||
elements: Default::default(),
|
elements: Default::default(),
|
||||||
map: Default::default(),
|
|
||||||
edges: Default::default(),
|
edges: Default::default(),
|
||||||
closure: Default::default(),
|
closure: Default::default(),
|
||||||
}
|
}
|
||||||
@ -65,20 +61,16 @@ impl<T: Clone + Debug + Eq + Hash> TransitiveRelation<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn index(&self, a: &T) -> Option<Index> {
|
fn index(&self, a: &T) -> Option<Index> {
|
||||||
self.map.get(a).cloned()
|
self.elements.get_index_of(a).map(Index)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn add_index(&mut self, a: T) -> Index {
|
fn add_index(&mut self, a: T) -> Index {
|
||||||
let &mut TransitiveRelation { ref mut elements, ref mut closure, ref mut map, .. } = self;
|
let (index, added) = self.elements.insert_full(a);
|
||||||
|
if added {
|
||||||
*map.entry(a.clone()).or_insert_with(|| {
|
|
||||||
elements.push(a);
|
|
||||||
|
|
||||||
// if we changed the dimensions, clear the cache
|
// if we changed the dimensions, clear the cache
|
||||||
*closure.get_mut() = None;
|
*self.closure.get_mut() = None;
|
||||||
|
}
|
||||||
Index(elements.len() - 1)
|
Index(index)
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Applies the (partial) function to each edge and returns a new
|
/// Applies the (partial) function to each edge and returns a new
|
||||||
@ -430,14 +422,11 @@ where
|
|||||||
{
|
{
|
||||||
fn decode<D: Decoder>(d: &mut D) -> Result<Self, D::Error> {
|
fn decode<D: Decoder>(d: &mut D) -> Result<Self, D::Error> {
|
||||||
d.read_struct("TransitiveRelation", 2, |d| {
|
d.read_struct("TransitiveRelation", 2, |d| {
|
||||||
let elements: Vec<T> = d.read_struct_field("elements", 0, |d| Decodable::decode(d))?;
|
Ok(TransitiveRelation {
|
||||||
let edges = d.read_struct_field("edges", 1, |d| Decodable::decode(d))?;
|
elements: d.read_struct_field("elements", 0, |d| Decodable::decode(d))?,
|
||||||
let map = elements
|
edges: d.read_struct_field("edges", 1, |d| Decodable::decode(d))?,
|
||||||
.iter()
|
closure: Lock::new(None),
|
||||||
.enumerate()
|
})
|
||||||
.map(|(index, elem)| (elem.clone(), Index(index)))
|
|
||||||
.collect();
|
|
||||||
Ok(TransitiveRelation { elements, edges, map, closure: Lock::new(None) })
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -452,8 +441,6 @@ where
|
|||||||
let TransitiveRelation {
|
let TransitiveRelation {
|
||||||
ref elements,
|
ref elements,
|
||||||
ref edges,
|
ref edges,
|
||||||
// "map" is just a copy of elements vec
|
|
||||||
map: _,
|
|
||||||
// "closure" is just a copy of the data above
|
// "closure" is just a copy of the data above
|
||||||
closure: _,
|
closure: _,
|
||||||
} = *self;
|
} = *self;
|
||||||
|
Loading…
Reference in New Issue
Block a user