1 use crate::fx::FxIndexSet;
2 use crate::stable_hasher::{HashStable, StableHasher};
4 use rustc_index::bit_set::BitMatrix;
5 use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
13 #[derive(Clone, Debug)]
14 pub struct TransitiveRelation<T: Eq + Hash> {
15 // List of elements. This is used to map from a T to a usize.
16 elements: FxIndexSet<T>,
18 // List of base edges in the graph. Require to compute transitive
22 // This is a cached transitive closure derived from the edges.
23 // Currently, we build it lazilly and just throw out any existing
24 // copy whenever a new edge is added. (The Lock is to permit
25 // the lazy computation.) This is kind of silly, except for the
26 // fact its size is tied to `self.elements.len()`, so I wanted to
27 // wait before building it up to avoid reallocating as new edges
28 // are added with new elements. Perhaps better would be to ask the
29 // user for a batch of edges to minimize this effect, but I
30 // already wrote the code this way. :P -nmatsakis
31 closure: Lock<Option<BitMatrix<usize, usize>>>,
34 // HACK(eddyb) manual impl avoids `Default` bound on `T`.
35 impl<T: Eq + Hash> Default for TransitiveRelation<T> {
36 fn default() -> Self {
38 elements: Default::default(),
39 edges: Default::default(),
40 closure: Default::default(),
45 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, RustcEncodable, RustcDecodable, Debug)]
48 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Debug)]
54 impl<T: Clone + Debug + Eq + Hash> TransitiveRelation<T> {
55 pub fn is_empty(&self) -> bool {
59 pub fn elements(&self) -> impl Iterator<Item = &T> {
63 fn index(&self, a: &T) -> Option<Index> {
64 self.elements.get_index_of(a).map(Index)
67 fn add_index(&mut self, a: T) -> Index {
68 let (index, added) = self.elements.insert_full(a);
70 // if we changed the dimensions, clear the cache
71 *self.closure.get_mut() = None;
76 /// Applies the (partial) function to each edge and returns a new
77 /// relation. If `f` returns `None` for any end-point, returns
79 pub fn maybe_map<F, U>(&self, mut f: F) -> Option<TransitiveRelation<U>>
81 F: FnMut(&T) -> Option<U>,
82 U: Clone + Debug + Eq + Hash + Clone,
84 let mut result = TransitiveRelation::default();
85 for edge in &self.edges {
86 result.add(f(&self.elements[edge.source.0])?, f(&self.elements[edge.target.0])?);
91 /// Indicate that `a < b` (where `<` is this relation)
92 pub fn add(&mut self, a: T, b: T) {
93 let a = self.add_index(a);
94 let b = self.add_index(b);
95 let edge = Edge { source: a, target: b };
96 if !self.edges.contains(&edge) {
97 self.edges.push(edge);
99 // added an edge, clear the cache
100 *self.closure.get_mut() = None;
104 /// Checks whether `a < target` (transitively)
105 pub fn contains(&self, a: &T, b: &T) -> bool {
106 match (self.index(a), self.index(b)) {
107 (Some(a), Some(b)) => self.with_closure(|closure| closure.contains(a.0, b.0)),
108 (None, _) | (_, None) => false,
112 /// Thinking of `x R y` as an edge `x -> y` in a graph, this
113 /// returns all things reachable from `a`.
115 /// Really this probably ought to be `impl Iterator<Item = &T>`, but
116 /// I'm too lazy to make that work, and -- given the caching
117 /// strategy -- it'd be a touch tricky anyhow.
118 pub fn reachable_from(&self, a: &T) -> Vec<&T> {
119 match self.index(a) {
121 self.with_closure(|closure| closure.iter(a.0).map(|i| &self.elements[i]).collect())
127 /// Picks what I am referring to as the "postdominating"
128 /// upper-bound for `a` and `b`. This is usually the least upper
129 /// bound, but in cases where there is no single least upper
130 /// bound, it is the "mutual immediate postdominator", if you
131 /// imagine a graph where `a < b` means `a -> b`.
133 /// This function is needed because region inference currently
134 /// requires that we produce a single "UB", and there is no best
135 /// choice for the LUB. Rather than pick arbitrarily, I pick a
136 /// less good, but predictable choice. This should help ensure
137 /// that region inference yields predictable results (though it
138 /// itself is not fully sufficient).
140 /// Examples are probably clearer than any prose I could write
141 /// (there are corresponding tests below, btw). In each case,
142 /// the query is `postdom_upper_bound(a, b)`:
145 /// // Returns Some(x), which is also LUB.
151 /// // Returns `Some(x)`, which is not LUB (there is none)
152 /// // diagonal edges run left-to-right.
158 /// // Returns `None`.
162 pub fn postdom_upper_bound(&self, a: &T, b: &T) -> Option<&T> {
163 let mubs = self.minimal_upper_bounds(a, b);
164 self.mutual_immediate_postdominator(mubs)
167 /// Viewing the relation as a graph, computes the "mutual
168 /// immediate postdominator" of a set of points (if one
169 /// exists). See `postdom_upper_bound` for details.
170 pub fn mutual_immediate_postdominator<'a>(&'a self, mut mubs: Vec<&'a T>) -> Option<&'a T> {
174 1 => return Some(mubs[0]),
176 let m = mubs.pop().unwrap();
177 let n = mubs.pop().unwrap();
178 mubs.extend(self.minimal_upper_bounds(n, m));
184 /// Returns the set of bounds `X` such that:
186 /// - `a < X` and `b < X`
187 /// - there is no `Y != X` such that `a < Y` and `Y < X`
188 /// - except for the case where `X < a` (i.e., a strongly connected
189 /// component in the graph). In that case, the smallest
190 /// representative of the SCC is returned (as determined by the
191 /// internal indices).
193 /// Note that this set can, in principle, have any size.
194 pub fn minimal_upper_bounds(&self, a: &T, b: &T) -> Vec<&T> {
195 let (mut a, mut b) = match (self.index(a), self.index(b)) {
196 (Some(a), Some(b)) => (a, b),
197 (None, _) | (_, None) => {
202 // in some cases, there are some arbitrary choices to be made;
203 // it doesn't really matter what we pick, as long as we pick
204 // the same thing consistently when queried, so ensure that
205 // (a, b) are in a consistent relative order
207 mem::swap(&mut a, &mut b);
210 let lub_indices = self.with_closure(|closure| {
211 // Easy case is when either a < b or b < a:
212 if closure.contains(a.0, b.0) {
215 if closure.contains(b.0, a.0) {
219 // Otherwise, the tricky part is that there may be some c
220 // where a < c and b < c. In fact, there may be many such
221 // values. So here is what we do:
223 // 1. Find the vector `[X | a < X && b < X]` of all values
224 // `X` where `a < X` and `b < X`. In terms of the
225 // graph, this means all values reachable from both `a`
226 // and `b`. Note that this vector is also a set, but we
227 // use the term vector because the order matters
228 // to the steps below.
229 // - This vector contains upper bounds, but they are
230 // not minimal upper bounds. So you may have e.g.
231 // `[x, y, tcx, z]` where `x < tcx` and `y < tcx` and
232 // `z < x` and `z < y`:
234 // z --+---> x ----+----> tcx
239 // In this case, we really want to return just `[z]`.
240 // The following steps below achieve this by gradually
241 // reducing the list.
242 // 2. Pare down the vector using `pare_down`. This will
243 // remove elements from the vector that can be reached
244 // by an earlier element.
245 // - In the example above, this would convert `[x, y,
246 // tcx, z]` to `[x, y, z]`. Note that `x` and `y` are
247 // still in the vector; this is because while `z < x`
248 // (and `z < y`) holds, `z` comes after them in the
250 // 3. Reverse the vector and repeat the pare down process.
251 // - In the example above, we would reverse to
252 // `[z, y, x]` and then pare down to `[z]`.
253 // 4. Reverse once more just so that we yield a vector in
254 // increasing order of index. Not necessary, but why not.
256 // I believe this algorithm yields a minimal set. The
257 // argument is that, after step 2, we know that no element
258 // can reach its successors (in the vector, not the graph).
259 // After step 3, we know that no element can reach any of
260 // its predecesssors (because of step 2) nor successors
261 // (because we just called `pare_down`)
263 // This same algorithm is used in `parents` below.
265 let mut candidates = closure.intersect_rows(a.0, b.0); // (1)
266 pare_down(&mut candidates, closure); // (2)
267 candidates.reverse(); // (3a)
268 pare_down(&mut candidates, closure); // (3b)
275 .map(|i| &self.elements[i])
279 /// Given an element A, returns the maximal set {B} of elements B
284 /// - for each i, j: `B[i]` R `B[j]` does not hold
286 /// The intuition is that this moves "one step up" through a lattice
287 /// (where the relation is encoding the `<=` relation for the lattice).
288 /// So e.g., if the relation is `->` and we have
296 /// then `parents(a)` returns `[b, c]`. The `postdom_parent` function
297 /// would further reduce this to just `f`.
298 pub fn parents(&self, a: &T) -> Vec<&T> {
299 let a = match self.index(a) {
301 None => return vec![],
304 // Steal the algorithm for `minimal_upper_bounds` above, but
305 // with a slight tweak. In the case where `a R a`, we remove
306 // that from the set of candidates.
307 let ancestors = self.with_closure(|closure| {
308 let mut ancestors = closure.intersect_rows(a.0, a.0);
310 // Remove anything that can reach `a`. If this is a
311 // reflexive relation, this will include `a` itself.
312 ancestors.retain(|&e| !closure.contains(e, a.0));
314 pare_down(&mut ancestors, closure); // (2)
315 ancestors.reverse(); // (3a)
316 pare_down(&mut ancestors, closure); // (3b)
323 .map(|i| &self.elements[i])
327 /// A "best" parent in some sense. See `parents` and
328 /// `postdom_upper_bound` for more details.
329 pub fn postdom_parent(&self, a: &T) -> Option<&T> {
330 self.mutual_immediate_postdominator(self.parents(a))
333 fn with_closure<OP, R>(&self, op: OP) -> R
335 OP: FnOnce(&BitMatrix<usize, usize>) -> R,
337 let mut closure_cell = self.closure.borrow_mut();
338 let mut closure = closure_cell.take();
339 if closure.is_none() {
340 closure = Some(self.compute_closure());
342 let result = op(closure.as_ref().unwrap());
343 *closure_cell = closure;
347 fn compute_closure(&self) -> BitMatrix<usize, usize> {
348 let mut matrix = BitMatrix::new(self.elements.len(), self.elements.len());
349 let mut changed = true;
352 for edge in &self.edges {
353 // add an edge from S -> T
354 changed |= matrix.insert(edge.source.0, edge.target.0);
356 // add all outgoing edges from T into S
357 changed |= matrix.union_rows(edge.target.0, edge.source.0);
363 /// Lists all the base edges in the graph: the initial _non-transitive_ set of element
364 /// relations, which will be later used as the basis for the transitive closure computation.
365 pub fn base_edges(&self) -> impl Iterator<Item = (&T, &T)> {
368 .map(move |edge| (&self.elements[edge.source.0], &self.elements[edge.target.0]))
372 /// Pare down is used as a step in the LUB computation. It edits the
373 /// candidates array in place by removing any element j for which
374 /// there exists an earlier element i<j such that i -> j. That is,
375 /// after you run `pare_down`, you know that for all elements that
376 /// remain in candidates, they cannot reach any of the elements that
379 /// Examples follow. Assume that a -> b -> c and x -> y -> z.
381 /// - Input: `[a, b, x]`. Output: `[a, x]`.
382 /// - Input: `[b, a, x]`. Output: `[b, a, x]`.
383 /// - Input: `[a, x, b, y]`. Output: `[a, x]`.
384 fn pare_down(candidates: &mut Vec<usize>, closure: &BitMatrix<usize, usize>) {
386 while let Some(&candidate_i) = candidates.get(i) {
391 while let Some(&candidate_j) = candidates.get(j) {
392 if closure.contains(candidate_i, candidate_j) {
393 // If `i` can reach `j`, then we can remove `j`. So just
394 // mark it as dead and move on; subsequent indices will be
395 // shifted into its place.
398 candidates[j - dead] = candidate_j;
402 candidates.truncate(j - dead);
406 impl<T> Encodable for TransitiveRelation<T>
408 T: Clone + Encodable + Debug + Eq + Hash + Clone,
410 fn encode<E: Encoder>(&self, s: &mut E) -> Result<(), E::Error> {
411 s.emit_struct("TransitiveRelation", 2, |s| {
412 s.emit_struct_field("elements", 0, |s| self.elements.encode(s))?;
413 s.emit_struct_field("edges", 1, |s| self.edges.encode(s))?;
419 impl<T> Decodable for TransitiveRelation<T>
421 T: Clone + Decodable + Debug + Eq + Hash + Clone,
423 fn decode<D: Decoder>(d: &mut D) -> Result<Self, D::Error> {
424 d.read_struct("TransitiveRelation", 2, |d| {
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),
434 impl<CTX, T> HashStable<CTX> for TransitiveRelation<T>
436 T: HashStable<CTX> + Eq + Debug + Clone + Hash,
438 fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {
439 // We are assuming here that the relation graph has been built in a
440 // deterministic way and we can just hash it the way it is.
441 let TransitiveRelation {
444 // "closure" is just a copy of the data above
448 elements.hash_stable(hcx, hasher);
449 edges.hash_stable(hcx, hasher);
453 impl<CTX> HashStable<CTX> for Edge {
454 fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {
455 let Edge { ref source, ref target } = *self;
457 source.hash_stable(hcx, hasher);
458 target.hash_stable(hcx, hasher);
462 impl<CTX> HashStable<CTX> for Index {
463 fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {
464 let Index(idx) = *self;
465 idx.hash_stable(hcx, hasher);