1use crate::containers::AppendOnlyVec;
9use crate::containers::DenseId;
10use crate::containers::IndexLike;
11use crate::containers::ShrinkPolicy;
12use crate::containers::VecToken;
13
14pub trait NodeIds {
16 type Index: crate::containers::IndexLike + crate::containers::Tagged;
18 type L0: DenseId<Index = Self::Index>;
19 type L1: DenseId<Index = Self::Index>;
20 type L2: DenseId<Index = Self::Index>;
21 type L3: DenseId<Index = Self::Index>;
22 type LSPair: DenseId<Index = Self::Index>;
23 type LN: DenseId<Index = Self::Index>;
24 type LSeq: DenseId<Index = Self::Index>;
25 type LMSet: DenseId<Index = Self::Index>;
26 type LSet: DenseId<Index = Self::Index>;
27 type LLit: DenseId<Index = Self::Index>;
28}
29
30pub enum NodeRef<I: NodeIds> {
32 Plain0(I::L0),
33 Plain1(I::L1),
34 Plain2(I::L2),
35 Plain3(I::L3),
36 SPair(I::LSPair),
37 PlainN(I::LN),
38 Seq(I::LSeq),
39 MSet(I::LMSet),
40 Set(I::LSet),
41 Lit(I::LLit),
42}
43
44impl<I: NodeIds> Clone for NodeRef<I> {
45 fn clone(&self) -> Self {
46 *self
47 }
48}
49
50impl<I: NodeIds> Copy for NodeRef<I> {}
51
52impl<I: NodeIds> PartialEq for NodeRef<I> {
53 fn eq(&self, other: &Self) -> bool {
54 match (self, other) {
55 (Self::Plain0(a), Self::Plain0(b)) => a == b,
56 (Self::Plain1(a), Self::Plain1(b)) => a == b,
57 (Self::Plain2(a), Self::Plain2(b)) => a == b,
58 (Self::Plain3(a), Self::Plain3(b)) => a == b,
59 (Self::SPair(a), Self::SPair(b)) => a == b,
60 (Self::PlainN(a), Self::PlainN(b)) => a == b,
61 (Self::Seq(a), Self::Seq(b)) => a == b,
62 (Self::MSet(a), Self::MSet(b)) => a == b,
63 (Self::Set(a), Self::Set(b)) => a == b,
64 (Self::Lit(a), Self::Lit(b)) => a == b,
65 _ => false,
66 }
67 }
68}
69impl<I: NodeIds> Eq for NodeRef<I> {}
70
71impl<I: NodeIds> core::fmt::Debug for NodeRef<I> {
72 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
73 match self {
74 Self::Plain0(id) => write!(f, "Plain0({:?})", id),
75 Self::Plain1(id) => write!(f, "Plain1({:?})", id),
76 Self::Plain2(id) => write!(f, "Plain2({:?})", id),
77 Self::Plain3(id) => write!(f, "Plain3({:?})", id),
78 Self::SPair(id) => write!(f, "SPair({:?})", id),
79 Self::PlainN(id) => write!(f, "PlainN({:?})", id),
80 Self::Seq(id) => write!(f, "Seq({:?})", id),
81 Self::MSet(id) => write!(f, "MSet({:?})", id),
82 Self::Set(id) => write!(f, "Set({:?})", id),
83 Self::Lit(id) => write!(f, "Lit({:?})", id),
84 }
85 }
86}
87
88pub struct TypedRouting<G: DenseId<Index = I::Index>, I: NodeIds, const TRACK: bool = true> {
95 entries: AppendOnlyVec<NodeRef<I>, I::Index, TRACK>,
99 reserved: bool,
100 _phantom: core::marker::PhantomData<G>,
101}
102
103impl<G: DenseId<Index = I::Index>, I: NodeIds, const TRACK: bool> Default
104 for TypedRouting<G, I, TRACK>
105{
106 fn default() -> Self {
107 Self::new()
108 }
109}
110
111impl<G: DenseId<Index = I::Index>, I: NodeIds, const TRACK: bool> TypedRouting<G, I, TRACK> {
112 pub fn new() -> Self {
113 Self {
114 entries: AppendOnlyVec::new(),
115 reserved: false,
116 _phantom: core::marker::PhantomData,
117 }
118 }
119
120 pub fn reserve(&mut self) -> G {
121 assert!(!self.reserved, "already have a reserved id");
122 self.reserved = true;
123 G::try_new(self.entries.len().as_usize()).expect("routing table exceeds the id space")
127 }
128
129 pub fn finalize(&mut self, fresh_id: G, entry: NodeRef<I>) {
130 assert!(self.reserved, "no reserved id to finalize");
131 assert_eq!(fresh_id.to_index(), self.entries.len());
132 self.entries
133 .try_push(entry)
134 .expect("routing entries exhausted the id index word (reserve checked the id bound)");
135 self.reserved = false;
136 }
137
138 pub fn unreserve(&mut self) {
139 assert!(self.reserved, "no reserved id to cancel");
140 self.reserved = false;
141 }
142
143 pub fn get(&self, id: G) -> NodeRef<I> {
144 *self.entries.get(id.to_index())
145 }
146
147 pub fn len(&self) -> usize {
148 self.entries.len().as_usize()
149 }
150
151 pub fn is_empty(&self) -> bool {
152 self.entries.is_empty()
153 }
154
155 pub fn mark(&mut self, shrink: ShrinkPolicy) -> RoutingToken {
156 RoutingToken {
157 entries: self
158 .entries
159 .try_mark(shrink)
160 .expect("routing mark: depth is bounded by the saturation driver"),
161 }
162 }
163
164 pub fn restore(&mut self, token: RoutingToken) {
165 self.entries
166 .try_restore(token.entries)
167 .expect("routing restore: token minted by this container's own mark");
168 self.reserved = false;
169 }
170}
171
172#[derive(Clone, Copy, Debug)]
173pub struct RoutingToken {
174 entries: VecToken,
175}
176
177#[cfg(test)]
178mod tests {
179 use super::*;
180 use crate::id::ENodeId;
181 use crate::nodes::*;
182
183 struct TestIds;
184 impl NodeIds for TestIds {
185 type Index = u32;
186 type L0 = Plain0Id;
187 type L1 = Plain1Id;
188 type L2 = Plain2Id;
189 type L3 = Plain3Id;
190 type LSPair = SPairNodeId;
191 type LN = PlainNId;
192 type LSeq = SeqNodeId;
193 type LMSet = MSetNodeId;
194 type LSet = SetNodeId;
195 type LLit = LitNodeId;
196 }
197
198 type RT = TypedRouting<ENodeId, TestIds, true>;
199
200 #[test]
201 fn reserve_finalize() {
202 let mut rt = RT::new();
203 let id0 = rt.reserve();
204 rt.finalize(id0, NodeRef::Plain0(Plain0Id::new(0)));
205 let id1 = rt.reserve();
206 rt.finalize(id1, NodeRef::MSet(MSetNodeId::new(0)));
207 assert_eq!(rt.get(id0), NodeRef::Plain0(Plain0Id::new(0)));
208 assert_eq!(rt.get(id1), NodeRef::MSet(MSetNodeId::new(0)));
209 assert_eq!(rt.len(), 2);
210 }
211
212 #[test]
213 fn unreserve() {
214 let mut rt = RT::new();
215 rt.reserve();
216 rt.unreserve();
217 assert_eq!(rt.len(), 0);
218 let id = rt.reserve();
219 rt.finalize(id, NodeRef::Lit(LitNodeId::new(0)));
220 assert_eq!(rt.len(), 1);
221 }
222
223 #[test]
224 fn mark_restore_rolls_back() {
225 let mut rt = RT::new();
226 let id0 = rt.reserve();
227 rt.finalize(id0, NodeRef::Plain1(Plain1Id::new(0)));
228 let token = rt.mark(ShrinkPolicy::Never);
229 let id1 = rt.reserve();
230 rt.finalize(id1, NodeRef::SPair(SPairNodeId::new(0)));
231 assert_eq!(rt.len(), 2);
232 rt.restore(token);
233 assert_eq!(rt.len(), 1);
234 assert_eq!(rt.get(id0), NodeRef::Plain1(Plain1Id::new(0)));
235 }
236
237 #[test]
238 #[should_panic(expected = "already have")]
239 fn double_reserve_panics() {
240 let mut rt = RT::new();
241 rt.reserve();
242 rt.reserve();
243 }
244}