diff options
| author | Mark Rousskov <mark.simulacrum@gmail.com> | 2019-12-22 17:42:04 -0500 |
|---|---|---|
| committer | Mark Rousskov <mark.simulacrum@gmail.com> | 2019-12-22 17:42:47 -0500 |
| commit | a06baa56b95674fc626b3c3fd680d6a65357fe60 (patch) | |
| tree | cd9d867c2ca3cff5c1d6b3bd73377c44649fb075 /src/librustc_data_structures/transitive_relation.rs | |
| parent | 8eb7c58dbb7b32701af113bc58722d0d1fefb1eb (diff) | |
| download | rust-a06baa56b95674fc626b3c3fd680d6a65357fe60.tar.gz rust-a06baa56b95674fc626b3c3fd680d6a65357fe60.zip | |
Format the world
Diffstat (limited to 'src/librustc_data_structures/transitive_relation.rs')
| -rw-r--r-- | src/librustc_data_structures/transitive_relation.rs | 101 |
1 files changed, 48 insertions, 53 deletions
diff --git a/src/librustc_data_structures/transitive_relation.rs b/src/librustc_data_structures/transitive_relation.rs index bbf6999b983..16f2e740104 100644 --- a/src/librustc_data_structures/transitive_relation.rs +++ b/src/librustc_data_structures/transitive_relation.rs @@ -1,8 +1,8 @@ -use rustc_index::bit_set::BitMatrix; use crate::fx::FxHashMap; use crate::stable_hasher::{HashStable, StableHasher}; use crate::sync::Lock; -use rustc_serialize::{Encodable, Encoder, Decodable, Decoder}; +use rustc_index::bit_set::BitMatrix; +use rustc_serialize::{Decodable, Decoder, Encodable, Encoder}; use std::fmt::Debug; use std::hash::Hash; use std::mem; @@ -60,7 +60,7 @@ impl<T: Clone + Debug + Eq + Hash> TransitiveRelation<T> { self.edges.is_empty() } - pub fn elements(&self) -> impl Iterator<Item=&T> { + pub fn elements(&self) -> impl Iterator<Item = &T> { self.elements.iter() } @@ -69,30 +69,25 @@ impl<T: Clone + Debug + Eq + Hash> TransitiveRelation<T> { } fn add_index(&mut self, a: T) -> Index { - let &mut TransitiveRelation { - ref mut elements, - ref mut closure, - ref mut map, - .. - } = self; - - *map.entry(a.clone()) - .or_insert_with(|| { - elements.push(a); - - // if we changed the dimensions, clear the cache - *closure.get_mut() = None; - - Index(elements.len() - 1) - }) + let &mut TransitiveRelation { ref mut elements, ref mut closure, ref mut map, .. } = self; + + *map.entry(a.clone()).or_insert_with(|| { + elements.push(a); + + // if we changed the dimensions, clear the cache + *closure.get_mut() = None; + + Index(elements.len() - 1) + }) } /// Applies the (partial) function to each edge and returns a new /// relation. If `f` returns `None` for any end-point, returns /// `None`. pub fn maybe_map<F, U>(&self, mut f: F) -> Option<TransitiveRelation<U>> - where F: FnMut(&T) -> Option<U>, - U: Clone + Debug + Eq + Hash + Clone, + where + F: FnMut(&T) -> Option<U>, + U: Clone + Debug + Eq + Hash + Clone, { let mut result = TransitiveRelation::default(); for edge in &self.edges { @@ -105,10 +100,7 @@ impl<T: Clone + Debug + Eq + Hash> TransitiveRelation<T> { pub fn add(&mut self, a: T, b: T) { let a = self.add_index(a); let b = self.add_index(b); - let edge = Edge { - source: a, - target: b, - }; + let edge = Edge { source: a, target: b }; if !self.edges.contains(&edge) { self.edges.push(edge); @@ -133,9 +125,9 @@ impl<T: Clone + Debug + Eq + Hash> TransitiveRelation<T> { /// strategy -- it'd be a touch tricky anyhow. pub fn reachable_from(&self, a: &T) -> Vec<&T> { match self.index(a) { - Some(a) => self.with_closure(|closure| { - closure.iter(a.0).map(|i| &self.elements[i]).collect() - }), + Some(a) => { + self.with_closure(|closure| closure.iter(a.0).map(|i| &self.elements[i]).collect()) + } None => vec![], } } @@ -285,10 +277,11 @@ impl<T: Clone + Debug + Eq + Hash> TransitiveRelation<T> { candidates }); - lub_indices.into_iter() - .rev() // (4) - .map(|i| &self.elements[i]) - .collect() + lub_indices + .into_iter() + .rev() // (4) + .map(|i| &self.elements[i]) + .collect() } /// Given an element A, returns the maximal set {B} of elements B @@ -313,7 +306,7 @@ impl<T: Clone + Debug + Eq + Hash> TransitiveRelation<T> { pub fn parents(&self, a: &T) -> Vec<&T> { let a = match self.index(a) { Some(a) => a, - None => return vec![] + None => return vec![], }; // Steal the algorithm for `minimal_upper_bounds` above, but @@ -332,10 +325,11 @@ impl<T: Clone + Debug + Eq + Hash> TransitiveRelation<T> { ancestors }); - ancestors.into_iter() - .rev() // (4) - .map(|i| &self.elements[i]) - .collect() + ancestors + .into_iter() + .rev() // (4) + .map(|i| &self.elements[i]) + .collect() } /// A "best" parent in some sense. See `parents` and @@ -345,7 +339,8 @@ impl<T: Clone + Debug + Eq + Hash> TransitiveRelation<T> { } fn with_closure<OP, R>(&self, op: OP) -> R - where OP: FnOnce(&BitMatrix<usize, usize>) -> R + where + OP: FnOnce(&BitMatrix<usize, usize>) -> R, { let mut closure_cell = self.closure.borrow_mut(); let mut closure = closure_cell.take(); @@ -358,8 +353,7 @@ impl<T: Clone + Debug + Eq + Hash> TransitiveRelation<T> { } fn compute_closure(&self) -> BitMatrix<usize, usize> { - let mut matrix = BitMatrix::new(self.elements.len(), - self.elements.len()); + let mut matrix = BitMatrix::new(self.elements.len(), self.elements.len()); let mut changed = true; while changed { changed = false; @@ -376,7 +370,7 @@ impl<T: Clone + Debug + Eq + Hash> TransitiveRelation<T> { /// Lists all the base edges in the graph: the initial _non-transitive_ set of element /// relations, which will be later used as the basis for the transitive closure computation. - pub fn base_edges(&self) -> impl Iterator<Item=(&T, &T)> { + pub fn base_edges(&self) -> impl Iterator<Item = (&T, &T)> { self.edges .iter() .map(move |edge| (&self.elements[edge.source.0], &self.elements[edge.target.0])) @@ -420,7 +414,8 @@ fn pare_down(candidates: &mut Vec<usize>, closure: &BitMatrix<usize, usize>) { } impl<T> Encodable for TransitiveRelation<T> - where T: Clone + Encodable + Debug + Eq + Hash + Clone +where + T: Clone + Encodable + Debug + Eq + Hash + Clone, { fn encode<E: Encoder>(&self, s: &mut E) -> Result<(), E::Error> { s.emit_struct("TransitiveRelation", 2, |s| { @@ -432,23 +427,26 @@ impl<T> Encodable for TransitiveRelation<T> } impl<T> Decodable for TransitiveRelation<T> - where T: Clone + Decodable + Debug + Eq + Hash + Clone +where + T: Clone + Decodable + Debug + Eq + Hash + Clone, { fn decode<D: Decoder>(d: &mut D) -> Result<Self, D::Error> { d.read_struct("TransitiveRelation", 2, |d| { let elements: Vec<T> = d.read_struct_field("elements", 0, |d| Decodable::decode(d))?; let edges = d.read_struct_field("edges", 1, |d| Decodable::decode(d))?; - let map = elements.iter() - .enumerate() - .map(|(index, elem)| (elem.clone(), Index(index))) - .collect(); + let map = elements + .iter() + .enumerate() + .map(|(index, elem)| (elem.clone(), Index(index))) + .collect(); Ok(TransitiveRelation { elements, edges, map, closure: Lock::new(None) }) }) } } impl<CTX, T> HashStable<CTX> for TransitiveRelation<T> - where T: HashStable<CTX> + Eq + Debug + Clone + Hash +where + T: HashStable<CTX> + Eq + Debug + Clone + Hash, { fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) { // We are assuming here that the relation graph has been built in a @@ -459,7 +457,7 @@ impl<CTX, T> HashStable<CTX> for TransitiveRelation<T> // "map" is just a copy of elements vec map: _, // "closure" is just a copy of the data above - closure: _ + closure: _, } = *self; elements.hash_stable(hcx, hasher); @@ -469,10 +467,7 @@ impl<CTX, T> HashStable<CTX> for TransitiveRelation<T> impl<CTX> HashStable<CTX> for Edge { fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) { - let Edge { - ref source, - ref target, - } = *self; + let Edge { ref source, ref target } = *self; source.hash_stable(hcx, hasher); target.hash_stable(hcx, hasher); |
