|
| 1 | +use std::{fmt::Debug, hash::Hash}; |
| 2 | + |
| 3 | +use ena::unify::{InPlaceUnificationTable, UnifyKey}; |
| 4 | +use rustc_hash::FxHashMap; |
| 5 | + |
| 6 | +/// Represents a definition that contains a direct reference to itself. |
| 7 | +/// |
| 8 | +/// Recursive definitions are not valid and must be reported to the user. |
| 9 | +/// It is preferable to group definitions together such that recursions |
| 10 | +/// are reported in-whole rather than separately. `RecursiveDef` can be |
| 11 | +/// used with `RecursiveDefHelper` to perform this grouping operation. |
| 12 | +/// |
| 13 | +/// The fields `from` and `to` are the relevant identifiers and `site` can |
| 14 | +/// be used to carry diagnostic information. |
| 15 | +#[derive(Eq, PartialEq, Clone, Debug, Hash)] |
| 16 | +pub struct RecursiveDef<T, U> |
| 17 | +where |
| 18 | + T: PartialEq + Copy, |
| 19 | +{ |
| 20 | + pub from: T, |
| 21 | + pub to: T, |
| 22 | + pub site: U, |
| 23 | +} |
| 24 | + |
| 25 | +impl<T, U> RecursiveDef<T, U> |
| 26 | +where |
| 27 | + T: PartialEq + Copy, |
| 28 | +{ |
| 29 | + pub fn new(from: T, to: T, site: U) -> Self { |
| 30 | + Self { from, to, site } |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +#[derive(PartialEq, Debug, Clone, Copy)] |
| 35 | +struct RecursiveDefKey(u32); |
| 36 | + |
| 37 | +impl UnifyKey for RecursiveDefKey { |
| 38 | + type Value = (); |
| 39 | + |
| 40 | + fn index(&self) -> u32 { |
| 41 | + self.0 |
| 42 | + } |
| 43 | + |
| 44 | + fn from_index(idx: u32) -> Self { |
| 45 | + Self(idx) |
| 46 | + } |
| 47 | + |
| 48 | + fn tag() -> &'static str { |
| 49 | + "RecursiveDefKey" |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +pub struct RecursiveDefHelper<T, U> |
| 54 | +where |
| 55 | + T: Eq + Clone + Debug + Copy, |
| 56 | +{ |
| 57 | + defs: Vec<RecursiveDef<T, U>>, |
| 58 | + table: InPlaceUnificationTable<RecursiveDefKey>, |
| 59 | + keys: FxHashMap<T, RecursiveDefKey>, |
| 60 | +} |
| 61 | + |
| 62 | +impl<T, U> RecursiveDefHelper<T, U> |
| 63 | +where |
| 64 | + T: Eq + Clone + Debug + Copy + Hash, |
| 65 | +{ |
| 66 | + pub fn new(defs: Vec<RecursiveDef<T, U>>) -> Self { |
| 67 | + let mut table = InPlaceUnificationTable::new(); |
| 68 | + let keys: FxHashMap<_, _> = defs |
| 69 | + .iter() |
| 70 | + .map(|def| (def.from, table.new_key(()))) |
| 71 | + .collect(); |
| 72 | + |
| 73 | + for def in defs.iter() { |
| 74 | + table.union(keys[&def.from], keys[&def.to]) |
| 75 | + } |
| 76 | + |
| 77 | + Self { defs, table, keys } |
| 78 | + } |
| 79 | + |
| 80 | + /// Removes a disjoint set of recursive definitions from the helper |
| 81 | + /// and returns it, if one exists. |
| 82 | + pub fn remove_disjoint_set(&mut self) -> Option<Vec<RecursiveDef<T, U>>> { |
| 83 | + let mut disjoint_set = vec![]; |
| 84 | + let mut remaining_set = vec![]; |
| 85 | + let mut union_key: Option<&RecursiveDefKey> = None; |
| 86 | + |
| 87 | + while let Some(def) = self.defs.pop() { |
| 88 | + let cur_key = &self.keys[&def.from]; |
| 89 | + |
| 90 | + if union_key.is_none() || self.table.unioned(*union_key.unwrap(), *cur_key) { |
| 91 | + disjoint_set.push(def) |
| 92 | + } else { |
| 93 | + remaining_set.push(def) |
| 94 | + } |
| 95 | + |
| 96 | + if union_key.is_none() { |
| 97 | + union_key = Some(cur_key) |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + self.defs = remaining_set; |
| 102 | + |
| 103 | + if union_key.is_some() { |
| 104 | + Some(disjoint_set) |
| 105 | + } else { |
| 106 | + None |
| 107 | + } |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +#[test] |
| 112 | +fn one_recursion() { |
| 113 | + let defs = vec![RecursiveDef::new(0, 1, ()), RecursiveDef::new(1, 0, ())]; |
| 114 | + let mut helper = RecursiveDefHelper::new(defs); |
| 115 | + assert!(helper.remove_disjoint_set().is_some()); |
| 116 | + assert!(helper.remove_disjoint_set().is_none()); |
| 117 | +} |
| 118 | + |
| 119 | +#[test] |
| 120 | +fn two_recursions() { |
| 121 | + let defs = vec![ |
| 122 | + RecursiveDef::new(0, 1, ()), |
| 123 | + RecursiveDef::new(1, 0, ()), |
| 124 | + RecursiveDef::new(2, 3, ()), |
| 125 | + RecursiveDef::new(3, 4, ()), |
| 126 | + RecursiveDef::new(4, 2, ()), |
| 127 | + ]; |
| 128 | + let mut helper = RecursiveDefHelper::new(defs); |
| 129 | + assert!(helper.remove_disjoint_set().is_some()); |
| 130 | + assert!(helper.remove_disjoint_set().is_some()); |
| 131 | + assert!(helper.remove_disjoint_set().is_none()); |
| 132 | +} |
0 commit comments