1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
use std::fmt::Debug;
use std::iter::ExactSizeIterator;
use crate::{
unionfind::{Key, UnionFind, Value},
ENode, Id, Language,
};
pub trait Metadata<L>: Sized + Debug {
type Error: Debug;
fn merge(&self, other: &Self) -> Self;
fn make(enode: ENode<L, &Self>) -> Self;
fn modify(_eclass: &mut EClass<L, Self>) {}
}
impl<L: Language> Metadata<L> for () {
type Error = std::convert::Infallible;
fn merge(&self, _other: &()) {}
fn make(_enode: ENode<L, &()>) {}
}
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct EClass<L, M> {
pub id: Id,
pub nodes: Vec<ENode<L>>,
pub metadata: M,
#[cfg(feature = "parent-pointers")]
pub(crate) parents: indexmap::IndexSet<usize>,
}
impl<L, M> EClass<L, M> {
pub fn is_empty(&self) -> bool {
self.nodes.is_empty()
}
pub fn len(&self) -> usize {
self.nodes.len()
}
pub fn iter(&self) -> impl ExactSizeIterator<Item = &ENode<L>> {
self.nodes.iter()
}
}
impl<L: Language, M: Metadata<L>> Value for EClass<L, M> {
type Error = std::convert::Infallible;
fn merge<K: Key>(
_unionfind: &mut UnionFind<K, Self>,
to: Self,
from: Self,
) -> Result<Self, Self::Error> {
let mut less = to.nodes;
let mut more = from.nodes;
if more.len() < less.len() {
std::mem::swap(&mut less, &mut more);
}
more.extend(less);
let mut eclass = EClass {
id: to.id,
nodes: more,
metadata: to.metadata.merge(&from.metadata),
#[cfg(feature = "parent-pointers")]
parents: {
let mut parents = to.parents;
parents.extend(from.parents);
parents
},
};
M::modify(&mut eclass);
Ok(eclass)
}
}