add #[rustc_pass_by_value] to more types
This commit is contained in:
@@ -49,7 +49,7 @@ struct Edge {
|
||||
target: Index,
|
||||
}
|
||||
|
||||
impl<T: Eq + Hash> TransitiveRelation<T> {
|
||||
impl<T: Eq + Hash + Copy> TransitiveRelation<T> {
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.edges.is_empty()
|
||||
}
|
||||
@@ -58,8 +58,8 @@ impl<T: Eq + Hash> TransitiveRelation<T> {
|
||||
self.elements.iter()
|
||||
}
|
||||
|
||||
fn index(&self, a: &T) -> Option<Index> {
|
||||
self.elements.get_index_of(a).map(Index)
|
||||
fn index(&self, a: T) -> Option<Index> {
|
||||
self.elements.get_index_of(&a).map(Index)
|
||||
}
|
||||
|
||||
fn add_index(&mut self, a: T) -> Index {
|
||||
@@ -76,12 +76,12 @@ impl<T: Eq + Hash> TransitiveRelation<T> {
|
||||
/// `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,
|
||||
F: FnMut(T) -> Option<U>,
|
||||
U: Clone + Debug + Eq + Hash + Copy,
|
||||
{
|
||||
let mut result = TransitiveRelation::default();
|
||||
for edge in &self.edges {
|
||||
result.add(f(&self.elements[edge.source.0])?, f(&self.elements[edge.target.0])?);
|
||||
result.add(f(self.elements[edge.source.0])?, f(self.elements[edge.target.0])?);
|
||||
}
|
||||
Some(result)
|
||||
}
|
||||
@@ -100,7 +100,7 @@ impl<T: Eq + Hash> TransitiveRelation<T> {
|
||||
}
|
||||
|
||||
/// Checks whether `a < target` (transitively)
|
||||
pub fn contains(&self, a: &T, b: &T) -> bool {
|
||||
pub fn contains(&self, a: T, b: T) -> bool {
|
||||
match (self.index(a), self.index(b)) {
|
||||
(Some(a), Some(b)) => self.with_closure(|closure| closure.contains(a.0, b.0)),
|
||||
(None, _) | (_, None) => false,
|
||||
@@ -113,10 +113,10 @@ impl<T: Eq + Hash> TransitiveRelation<T> {
|
||||
/// Really this probably ought to be `impl Iterator<Item = &T>`, but
|
||||
/// I'm too lazy to make that work, and -- given the caching
|
||||
/// strategy -- it'd be a touch tricky anyhow.
|
||||
pub fn reachable_from(&self, a: &T) -> Vec<&T> {
|
||||
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())
|
||||
self.with_closure(|closure| closure.iter(a.0).map(|i| self.elements[i]).collect())
|
||||
}
|
||||
None => vec![],
|
||||
}
|
||||
@@ -157,7 +157,7 @@ impl<T: Eq + Hash> TransitiveRelation<T> {
|
||||
/// a -> a1
|
||||
/// b -> b1
|
||||
/// ```
|
||||
pub fn postdom_upper_bound(&self, a: &T, b: &T) -> Option<&T> {
|
||||
pub fn postdom_upper_bound(&self, a: T, b: T) -> Option<T> {
|
||||
let mubs = self.minimal_upper_bounds(a, b);
|
||||
self.mutual_immediate_postdominator(mubs)
|
||||
}
|
||||
@@ -165,7 +165,7 @@ impl<T: Eq + Hash> TransitiveRelation<T> {
|
||||
/// Viewing the relation as a graph, computes the "mutual
|
||||
/// immediate postdominator" of a set of points (if one
|
||||
/// exists). See `postdom_upper_bound` for details.
|
||||
pub fn mutual_immediate_postdominator<'a>(&'a self, mut mubs: Vec<&'a T>) -> Option<&'a T> {
|
||||
pub fn mutual_immediate_postdominator<'a>(&'a self, mut mubs: Vec<T>) -> Option<T> {
|
||||
loop {
|
||||
match mubs.len() {
|
||||
0 => return None,
|
||||
@@ -189,7 +189,7 @@ impl<T: Eq + Hash> TransitiveRelation<T> {
|
||||
/// internal indices).
|
||||
///
|
||||
/// Note that this set can, in principle, have any size.
|
||||
pub fn minimal_upper_bounds(&self, a: &T, b: &T) -> Vec<&T> {
|
||||
pub fn minimal_upper_bounds(&self, a: T, b: T) -> Vec<T> {
|
||||
let (Some(mut a), Some(mut b)) = (self.index(a), self.index(b)) else {
|
||||
return vec![];
|
||||
};
|
||||
@@ -267,7 +267,7 @@ impl<T: Eq + Hash> TransitiveRelation<T> {
|
||||
lub_indices
|
||||
.into_iter()
|
||||
.rev() // (4)
|
||||
.map(|i| &self.elements[i])
|
||||
.map(|i| self.elements[i])
|
||||
.collect()
|
||||
}
|
||||
|
||||
@@ -290,7 +290,7 @@ impl<T: Eq + Hash> TransitiveRelation<T> {
|
||||
///
|
||||
/// then `parents(a)` returns `[b, c]`. The `postdom_parent` function
|
||||
/// would further reduce this to just `f`.
|
||||
pub fn parents(&self, a: &T) -> Vec<&T> {
|
||||
pub fn parents(&self, a: T) -> Vec<T> {
|
||||
let Some(a) = self.index(a) else {
|
||||
return vec![];
|
||||
};
|
||||
@@ -314,7 +314,7 @@ impl<T: Eq + Hash> TransitiveRelation<T> {
|
||||
ancestors
|
||||
.into_iter()
|
||||
.rev() // (4)
|
||||
.map(|i| &self.elements[i])
|
||||
.map(|i| self.elements[i])
|
||||
.collect()
|
||||
}
|
||||
|
||||
@@ -350,10 +350,10 @@ impl<T: 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]))
|
||||
.map(move |edge| (self.elements[edge.source.0], self.elements[edge.target.0]))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
use super::*;
|
||||
|
||||
impl<T: Eq + Hash> TransitiveRelation<T> {
|
||||
impl<T: Eq + Hash + Copy> TransitiveRelation<T> {
|
||||
/// A "best" parent in some sense. See `parents` and
|
||||
/// `postdom_upper_bound` for more details.
|
||||
fn postdom_parent(&self, a: &T) -> Option<&T> {
|
||||
fn postdom_parent(&self, a: T) -> Option<T> {
|
||||
self.mutual_immediate_postdominator(self.parents(a))
|
||||
}
|
||||
}
|
||||
@@ -13,10 +13,10 @@ fn test_one_step() {
|
||||
let mut relation = TransitiveRelation::default();
|
||||
relation.add("a", "b");
|
||||
relation.add("a", "c");
|
||||
assert!(relation.contains(&"a", &"c"));
|
||||
assert!(relation.contains(&"a", &"b"));
|
||||
assert!(!relation.contains(&"b", &"a"));
|
||||
assert!(!relation.contains(&"a", &"d"));
|
||||
assert!(relation.contains("a", "c"));
|
||||
assert!(relation.contains("a", "b"));
|
||||
assert!(!relation.contains("b", "a"));
|
||||
assert!(!relation.contains("a", "d"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -32,17 +32,17 @@ fn test_many_steps() {
|
||||
|
||||
relation.add("e", "g");
|
||||
|
||||
assert!(relation.contains(&"a", &"b"));
|
||||
assert!(relation.contains(&"a", &"c"));
|
||||
assert!(relation.contains(&"a", &"d"));
|
||||
assert!(relation.contains(&"a", &"e"));
|
||||
assert!(relation.contains(&"a", &"f"));
|
||||
assert!(relation.contains(&"a", &"g"));
|
||||
assert!(relation.contains("a", "b"));
|
||||
assert!(relation.contains("a", "c"));
|
||||
assert!(relation.contains("a", "d"));
|
||||
assert!(relation.contains("a", "e"));
|
||||
assert!(relation.contains("a", "f"));
|
||||
assert!(relation.contains("a", "g"));
|
||||
|
||||
assert!(relation.contains(&"b", &"g"));
|
||||
assert!(relation.contains("b", "g"));
|
||||
|
||||
assert!(!relation.contains(&"a", &"x"));
|
||||
assert!(!relation.contains(&"b", &"f"));
|
||||
assert!(!relation.contains("a", "x"));
|
||||
assert!(!relation.contains("b", "f"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -54,9 +54,9 @@ fn mubs_triangle() {
|
||||
let mut relation = TransitiveRelation::default();
|
||||
relation.add("a", "tcx");
|
||||
relation.add("b", "tcx");
|
||||
assert_eq!(relation.minimal_upper_bounds(&"a", &"b"), vec![&"tcx"]);
|
||||
assert_eq!(relation.parents(&"a"), vec![&"tcx"]);
|
||||
assert_eq!(relation.parents(&"b"), vec![&"tcx"]);
|
||||
assert_eq!(relation.minimal_upper_bounds("a", "b"), vec!["tcx"]);
|
||||
assert_eq!(relation.parents("a"), vec!["tcx"]);
|
||||
assert_eq!(relation.parents("b"), vec!["tcx"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -81,10 +81,10 @@ fn mubs_best_choice1() {
|
||||
relation.add("3", "1");
|
||||
relation.add("3", "2");
|
||||
|
||||
assert_eq!(relation.minimal_upper_bounds(&"0", &"3"), vec![&"2"]);
|
||||
assert_eq!(relation.parents(&"0"), vec![&"2"]);
|
||||
assert_eq!(relation.parents(&"2"), vec![&"1"]);
|
||||
assert!(relation.parents(&"1").is_empty());
|
||||
assert_eq!(relation.minimal_upper_bounds("0", "3"), vec!["2"]);
|
||||
assert_eq!(relation.parents("0"), vec!["2"]);
|
||||
assert_eq!(relation.parents("2"), vec!["1"]);
|
||||
assert!(relation.parents("1").is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -108,10 +108,10 @@ fn mubs_best_choice2() {
|
||||
relation.add("3", "1");
|
||||
relation.add("3", "2");
|
||||
|
||||
assert_eq!(relation.minimal_upper_bounds(&"0", &"3"), vec![&"1"]);
|
||||
assert_eq!(relation.parents(&"0"), vec![&"1"]);
|
||||
assert_eq!(relation.parents(&"1"), vec![&"2"]);
|
||||
assert!(relation.parents(&"2").is_empty());
|
||||
assert_eq!(relation.minimal_upper_bounds("0", "3"), vec!["1"]);
|
||||
assert_eq!(relation.parents("0"), vec!["1"]);
|
||||
assert_eq!(relation.parents("1"), vec!["2"]);
|
||||
assert!(relation.parents("2").is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -125,9 +125,9 @@ fn mubs_no_best_choice() {
|
||||
relation.add("3", "1");
|
||||
relation.add("3", "2");
|
||||
|
||||
assert_eq!(relation.minimal_upper_bounds(&"0", &"3"), vec![&"1", &"2"]);
|
||||
assert_eq!(relation.parents(&"0"), vec![&"1", &"2"]);
|
||||
assert_eq!(relation.parents(&"3"), vec![&"1", &"2"]);
|
||||
assert_eq!(relation.minimal_upper_bounds("0", "3"), vec!["1", "2"]);
|
||||
assert_eq!(relation.parents("0"), vec!["1", "2"]);
|
||||
assert_eq!(relation.parents("3"), vec!["1", "2"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -145,8 +145,8 @@ fn mubs_best_choice_scc() {
|
||||
relation.add("3", "1");
|
||||
relation.add("3", "2");
|
||||
|
||||
assert_eq!(relation.minimal_upper_bounds(&"0", &"3"), vec![&"1"]);
|
||||
assert_eq!(relation.parents(&"0"), vec![&"1"]);
|
||||
assert_eq!(relation.minimal_upper_bounds("0", "3"), vec!["1"]);
|
||||
assert_eq!(relation.parents("0"), vec!["1"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -165,10 +165,10 @@ fn pdub_crisscross() {
|
||||
relation.add("a1", "x");
|
||||
relation.add("b1", "x");
|
||||
|
||||
assert_eq!(relation.minimal_upper_bounds(&"a", &"b"), vec![&"a1", &"b1"]);
|
||||
assert_eq!(relation.postdom_upper_bound(&"a", &"b"), Some(&"x"));
|
||||
assert_eq!(relation.postdom_parent(&"a"), Some(&"x"));
|
||||
assert_eq!(relation.postdom_parent(&"b"), Some(&"x"));
|
||||
assert_eq!(relation.minimal_upper_bounds("a", "b"), vec!["a1", "b1"]);
|
||||
assert_eq!(relation.postdom_upper_bound("a", "b"), Some("x"));
|
||||
assert_eq!(relation.postdom_parent("a"), Some("x"));
|
||||
assert_eq!(relation.postdom_parent("b"), Some("x"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -195,12 +195,12 @@ fn pdub_crisscross_more() {
|
||||
relation.add("a3", "x");
|
||||
relation.add("b2", "x");
|
||||
|
||||
assert_eq!(relation.minimal_upper_bounds(&"a", &"b"), vec![&"a1", &"b1"]);
|
||||
assert_eq!(relation.minimal_upper_bounds(&"a1", &"b1"), vec![&"a2", &"b2"]);
|
||||
assert_eq!(relation.postdom_upper_bound(&"a", &"b"), Some(&"x"));
|
||||
assert_eq!(relation.minimal_upper_bounds("a", "b"), vec!["a1", "b1"]);
|
||||
assert_eq!(relation.minimal_upper_bounds("a1", "b1"), vec!["a2", "b2"]);
|
||||
assert_eq!(relation.postdom_upper_bound("a", "b"), Some("x"));
|
||||
|
||||
assert_eq!(relation.postdom_parent(&"a"), Some(&"x"));
|
||||
assert_eq!(relation.postdom_parent(&"b"), Some(&"x"));
|
||||
assert_eq!(relation.postdom_parent("a"), Some("x"));
|
||||
assert_eq!(relation.postdom_parent("b"), Some("x"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -216,13 +216,13 @@ fn pdub_lub() {
|
||||
relation.add("a1", "x");
|
||||
relation.add("b1", "x");
|
||||
|
||||
assert_eq!(relation.minimal_upper_bounds(&"a", &"b"), vec![&"x"]);
|
||||
assert_eq!(relation.postdom_upper_bound(&"a", &"b"), Some(&"x"));
|
||||
assert_eq!(relation.minimal_upper_bounds("a", "b"), vec!["x"]);
|
||||
assert_eq!(relation.postdom_upper_bound("a", "b"), Some("x"));
|
||||
|
||||
assert_eq!(relation.postdom_parent(&"a"), Some(&"a1"));
|
||||
assert_eq!(relation.postdom_parent(&"b"), Some(&"b1"));
|
||||
assert_eq!(relation.postdom_parent(&"a1"), Some(&"x"));
|
||||
assert_eq!(relation.postdom_parent(&"b1"), Some(&"x"));
|
||||
assert_eq!(relation.postdom_parent("a"), Some("a1"));
|
||||
assert_eq!(relation.postdom_parent("b"), Some("b1"));
|
||||
assert_eq!(relation.postdom_parent("a1"), Some("x"));
|
||||
assert_eq!(relation.postdom_parent("b1"), Some("x"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -238,7 +238,7 @@ fn mubs_intermediate_node_on_one_side_only() {
|
||||
relation.add("c", "d");
|
||||
relation.add("b", "d");
|
||||
|
||||
assert_eq!(relation.minimal_upper_bounds(&"a", &"b"), vec![&"d"]);
|
||||
assert_eq!(relation.minimal_upper_bounds("a", "b"), vec!["d"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -259,7 +259,7 @@ fn mubs_scc_1() {
|
||||
relation.add("a", "d");
|
||||
relation.add("b", "d");
|
||||
|
||||
assert_eq!(relation.minimal_upper_bounds(&"a", &"b"), vec![&"c"]);
|
||||
assert_eq!(relation.minimal_upper_bounds("a", "b"), vec!["c"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -279,7 +279,7 @@ fn mubs_scc_2() {
|
||||
relation.add("b", "d");
|
||||
relation.add("b", "c");
|
||||
|
||||
assert_eq!(relation.minimal_upper_bounds(&"a", &"b"), vec![&"c"]);
|
||||
assert_eq!(relation.minimal_upper_bounds("a", "b"), vec!["c"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -300,7 +300,7 @@ fn mubs_scc_3() {
|
||||
relation.add("b", "d");
|
||||
relation.add("b", "e");
|
||||
|
||||
assert_eq!(relation.minimal_upper_bounds(&"a", &"b"), vec![&"c"]);
|
||||
assert_eq!(relation.minimal_upper_bounds("a", "b"), vec!["c"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -322,7 +322,7 @@ fn mubs_scc_4() {
|
||||
relation.add("a", "d");
|
||||
relation.add("b", "e");
|
||||
|
||||
assert_eq!(relation.minimal_upper_bounds(&"a", &"b"), vec![&"c"]);
|
||||
assert_eq!(relation.minimal_upper_bounds("a", "b"), vec!["c"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -357,6 +357,6 @@ fn parent() {
|
||||
relation.add(a, b);
|
||||
}
|
||||
|
||||
let p = relation.postdom_parent(&3);
|
||||
assert_eq!(p, Some(&0));
|
||||
let p = relation.postdom_parent(3);
|
||||
assert_eq!(p, Some(0));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user