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
use std::{
cmp::Ordering,
hash::{Hash, Hasher},
marker::PhantomData,
};
pub(crate) struct Inliner<T>
where
T: Eq,
{
inner: Vec<T>,
}
impl<T> Inliner<T>
where
T: Eq,
{
pub fn inline(&mut self, t: T) -> NodeId<T> {
for (index, item) in self.inner.iter().enumerate() {
if *item == t {
return NodeId(index, PhantomData);
}
}
self.inner.push(t);
NodeId(self.inner.len() - 1, PhantomData)
}
}
impl<T> Default for Inliner<T>
where
T: Eq,
{
fn default() -> Self {
Self { inner: Default::default() }
}
}
pub(crate) struct NodeId<T>(usize, PhantomData<T>);
impl<T> Clone for NodeId<T> {
fn clone(&self) -> Self {
NodeId(self.0, self.1)
}
}
impl<T> Copy for NodeId<T> {}
impl<T> PartialEq for NodeId<T> {
fn eq(&self, other: &Self) -> bool {
self.0 == other.0
}
}
impl<T> Eq for NodeId<T> {}
impl<T> PartialOrd for NodeId<T> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
self.0.partial_cmp(&other.0)
}
}
impl<T> Ord for NodeId<T> {
fn cmp(&self, other: &Self) -> Ordering {
self.0.cmp(&other.0)
}
}
impl<T> Hash for NodeId<T> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.0.hash(state)
}
}