// option. This file may not be copied, modified, or distributed
// except according to those terms.
-use bitvec::BitMatrix;
+use bit_set::BitMatrix;
use fx::FxHashMap;
use sync::Lock;
use rustc_serialize::{Encodable, Encoder, Decodable, Decoder};
target: Index,
}
-impl<T: Clone + Debug + Eq + Hash> TransitiveRelation<T> {
- pub fn new() -> TransitiveRelation<T> {
+impl<T: Clone + Debug + Eq + Hash> Default for TransitiveRelation<T> {
+ fn default() -> TransitiveRelation<T> {
TransitiveRelation {
elements: vec![],
- map: FxHashMap(),
+ map: FxHashMap::default(),
edges: vec![],
closure: Lock::new(None),
}
}
+}
+impl<T: Clone + Debug + Eq + Hash> TransitiveRelation<T> {
pub fn is_empty(&self) -> bool {
self.edges.is_empty()
}
where F: FnMut(&T) -> Option<U>,
U: Clone + Debug + Eq + Hash + Clone,
{
- let mut result = TransitiveRelation::new();
+ let mut result = TransitiveRelation::default();
for edge in &self.edges {
result.add(f(&self.elements[edge.source.0])?, f(&self.elements[edge.target.0])?);
}
//
// This same algorithm is used in `parents` below.
- let mut candidates = closure.intersection(a.0, b.0); // (1)
+ let mut candidates = closure.intersect_rows(a.0, b.0); // (1)
pare_down(&mut candidates, closure); // (2)
candidates.reverse(); // (3a)
pare_down(&mut candidates, closure); // (3b)
// with a slight tweak. In the case where `a R a`, we remove
// that from the set of candidates.
let ancestors = self.with_closure(|closure| {
- let mut ancestors = closure.intersection(a.0, a.0);
+ let mut ancestors = closure.intersect_rows(a.0, a.0);
// Remove anything that can reach `a`. If this is a
// reflexive relation, this will include `a` itself.
changed = false;
for edge in &self.edges {
// add an edge from S -> T
- changed |= matrix.add(edge.source.0, edge.target.0);
+ changed |= matrix.insert(edge.source.0, edge.target.0);
// add all outgoing edges from T into S
- changed |= matrix.merge(edge.target.0, edge.source.0);
+ changed |= matrix.union_rows(edge.target.0, edge.source.0);
}
}
matrix
#[test]
fn test_one_step() {
- let mut relation = TransitiveRelation::new();
+ let mut relation = TransitiveRelation::default();
relation.add("a", "b");
relation.add("a", "c");
assert!(relation.contains(&"a", &"c"));
#[test]
fn test_many_steps() {
- let mut relation = TransitiveRelation::new();
+ let mut relation = TransitiveRelation::default();
relation.add("a", "b");
relation.add("a", "c");
relation.add("a", "f");
// ^
// |
// b
- let mut relation = TransitiveRelation::new();
+ let mut relation = TransitiveRelation::default();
relation.add("a", "tcx");
relation.add("b", "tcx");
assert_eq!(relation.minimal_upper_bounds(&"a", &"b"), vec![&"tcx"]);
// need the second pare down call to get the right result (after
// intersection, we have [1, 2], but 2 -> 1).
- let mut relation = TransitiveRelation::new();
+ let mut relation = TransitiveRelation::default();
relation.add("0", "1");
relation.add("0", "2");
// Like the precedecing test, but in this case intersection is [2,
// 1], and hence we rely on the first pare down call.
- let mut relation = TransitiveRelation::new();
+ let mut relation = TransitiveRelation::default();
relation.add("0", "1");
relation.add("0", "2");
fn mubs_no_best_choice() {
// in this case, the intersection yields [1, 2], and the "pare
// down" calls find nothing to remove.
- let mut relation = TransitiveRelation::new();
+ let mut relation = TransitiveRelation::default();
relation.add("0", "1");
relation.add("0", "2");
// in this case, 1 and 2 form a cycle; we pick arbitrarily (but
// consistently).
- let mut relation = TransitiveRelation::new();
+ let mut relation = TransitiveRelation::default();
relation.add("0", "1");
relation.add("0", "2");
// /\ |
// b -> b1 ---+
- let mut relation = TransitiveRelation::new();
+ let mut relation = TransitiveRelation::default();
relation.add("a", "a1");
relation.add("a", "b1");
relation.add("b", "a1");
// /\ /\ |
// b -> b1 -> b2 ---------+
- let mut relation = TransitiveRelation::new();
+ let mut relation = TransitiveRelation::default();
relation.add("a", "a1");
relation.add("a", "b1");
relation.add("b", "a1");
// |
// b -> b1 ---+
- let mut relation = TransitiveRelation::new();
+ let mut relation = TransitiveRelation::default();
relation.add("a", "a1");
relation.add("b", "b1");
relation.add("a1", "x");
// b
// "digraph { a -> c -> d; b -> d; }",
- let mut relation = TransitiveRelation::new();
+ let mut relation = TransitiveRelation::default();
relation.add("a", "c");
relation.add("c", "d");
relation.add("b", "d");
// b
// "digraph { a -> c -> d; d -> c; a -> d; b -> d; }",
- let mut relation = TransitiveRelation::new();
+ let mut relation = TransitiveRelation::default();
relation.add("a", "c");
relation.add("c", "d");
relation.add("d", "c");
// +--- b
// "digraph { a -> c -> d; d -> c; b -> d; b -> c; }",
- let mut relation = TransitiveRelation::new();
+ let mut relation = TransitiveRelation::default();
relation.add("a", "c");
relation.add("c", "d");
relation.add("d", "c");
// b ---+
// "digraph { a -> c -> d -> e -> c; b -> d; b -> e; }",
- let mut relation = TransitiveRelation::new();
+ let mut relation = TransitiveRelation::default();
relation.add("a", "c");
relation.add("c", "d");
relation.add("d", "e");
// b ---+
// "digraph { a -> c -> d -> e -> c; a -> d; b -> e; }"
- let mut relation = TransitiveRelation::new();
+ let mut relation = TransitiveRelation::default();
relation.add("a", "c");
relation.add("c", "d");
relation.add("d", "e");
(1, /*->*/ 3),
];
- let mut relation = TransitiveRelation::new();
+ let mut relation = TransitiveRelation::default();
for (a, b) in pairs {
relation.add(a, b);
}