1use std::{
6 collections::VecDeque,
7 fmt::Debug,
8 hash::{Hash, Hasher},
9};
10
11use cfg_attrs::cfg_attrs;
12#[cfg(feature = "serde")]
13use serde::{Deserialize, Serialize};
14
15use crate::{
16 iter,
17 remove_children_linked,
18 sealed::{Idx, Sealed},
19 Arena,
20 BaseNode,
21 BranchNode,
22 LinkedNode,
23 Node,
24 NodeToken,
25 Token,
26};
27
28#[cfg(feature = "deque")]
29#[doc(cfg(feature = "deque"))]
30mod deque;
31
32#[cfg(feature = "deque")]
33pub use deque::*;
34
35#[cfg_attrs]
36#[configure(
38 feature = "deque",
39 )]
59#[configure(
61 any(feature = "split", feature = "deque"),
62 #[configure(
64 feature = "deque",
65 )]
70 #[configure(
71 feature = "split",
72 #[configure(
74 not(feature = "deque"),
75 )]
78 #[configure(
79 feature = "deque",
80 )]
84 )]
87)]
88#[derive(Debug)]
93#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
94pub struct UnifiedNode<Data: Debug> {
95 token: Token<Self>,
96
97 parent: Option<Token<Self>>,
98
99 prev: Option<Token<Self>>,
100 next: Option<Token<Self>>,
101
102 first_child: Option<Token<Self>>,
103 last_child: Option<Token<Self>>,
104
105 data: Data,
106}
107
108#[derive(Debug, PartialEq, Eq, Hash, Clone)]
114#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
115pub struct UnifiedNodeRepresentation<Data: Debug> {
116 pub children: VecDeque<UnifiedNodeRepresentation<Data>>,
121 pub data: Data,
126}
127
128impl<Data: Debug> PartialEq for UnifiedNode<Data> {
129 #[inline(always)]
130 fn eq(&self, other: &Self) -> bool {
131 self.token == other.token
132 }
133}
134
135impl<Data: Debug> Eq for UnifiedNode<Data> {}
136
137impl<Data: Debug> Hash for UnifiedNode<Data> {
138 #[inline(always)]
139 fn hash<H: Hasher>(&self, state: &mut H) {
140 self.token.hash(state);
141 }
142}
143
144impl<Data: Debug> Sealed for UnifiedNode<Data> {}
145
146impl<Data: Debug> Node for UnifiedNode<Data> {
147 type Base = Self;
148 type Token = Token<Self>;
149
150 type Data = Data;
151 type DataRef<'data> = &'data Data
152 where
153 Self: 'data;
154 type DataRefMut<'data> = &'data mut Data
155 where
156 Self: 'data;
157
158 fn new(arena: &mut Arena<Self::Base>, data: Self::Data) -> Self::Token {
159 Token::new(arena.0.insert_with(|idx| Self {
160 token: Token::new(idx),
161
162 parent: None,
163
164 prev: None,
165 next: None,
166
167 first_child: None,
168 last_child: None,
169
170 data,
171 }))
172 }
173
174 #[inline(always)]
175 fn token(&self) -> Self::Token {
176 self.token
177 }
178
179 #[inline(always)]
180 fn parent(&self) -> Option<Token<Self>> {
181 self.parent
182 }
183
184 #[inline(always)]
185 fn data(&self) -> &Self::Data {
186 &self.data
187 }
188
189 #[inline(always)]
190 fn data_mut(&mut self) -> &mut Self::Data {
191 &mut self.data
192 }
193}
194
195impl<Data: Debug> BaseNode for UnifiedNode<Data> {
196 type Representation = UnifiedNodeRepresentation<Data>;
197
198 type Branch = Self;
199 type Leaf = Self;
200
201 fn into_representation(self, arena: &mut Arena<Self>) -> Self::Representation {
202 UnifiedNodeRepresentation {
203 children: remove_children_linked(&self, arena),
204 data: self.data,
205 }
206 }
207}
208
209impl<Data: Debug> LinkedNode for UnifiedNode<Data> {
210 #[inline(always)]
211 fn prev(&self) -> Option<Token<Self>> {
212 self.prev
213 }
214
215 #[inline(always)]
216 fn next(&self) -> Option<Token<Self>> {
217 self.next
218 }
219}
220
221impl<Data: Debug> BranchNode for UnifiedNode<Data> {
222 type ChildrenIter<'branch> = iter::ChildrenLinked<'branch, Self> where Self: 'branch;
223
224 #[inline(always)]
225 fn first(&self) -> Option<Token<Self>> {
226 self.first_child
227 }
228
229 #[inline(always)]
230 fn last(&self) -> Option<Token<Self>> {
231 self.last_child
232 }
233
234 #[inline(always)]
235 fn children<'branch>(&'branch self, arena: &'branch Arena<Self>) -> Self::ChildrenIter<'branch> {
236 iter::ChildrenLinked::new(arena, self.first_child, self.last_child)
237 }
238
239 #[inline(always)]
240 fn is_empty(&self) -> bool {
241 debug_assert!(!(self.first_child.is_none() ^ self.last_child.is_none()));
244
245 self.first_child.is_none()
246 }
247
248 fn detach_front(token: Self::Token, arena: &mut Arena<Self>) -> Option<Token<Self>> {
249 let this = &mut arena.0[token.idx()];
250
251 match (this.first_child, this.last_child) {
252 (Some(first), Some(last)) if first == last => {
254 (this.first_child, this.last_child) = (None, None);
255
256 let node = &mut arena.0[first.idx()];
257
258 node.parent = None;
260
261 node.prev = None;
262 node.next = None;
263
264 Some(first)
265 },
266
267 (Some(first), Some(_)) => {
269 let next = first.next(arena).expect("There are multiple children");
270
271 arena.0[token.idx()].first_child = Some(next);
273 arena.0[next.idx()].prev = None;
274
275 let node = &mut arena.0[first.idx()];
276
277 node.parent = None;
279
280 node.prev = None;
281 node.next = None;
282
283 Some(first)
284 },
285
286 (..) => {
288 debug_assert!(this.first_child.is_none() && this.last_child.is_none());
290
291 None
292 },
293 }
294 }
295
296 fn detach_back(token: Self::Token, arena: &mut Arena<Self>) -> Option<Token<Self>> {
297 let this = &mut arena.0[token.idx()];
298
299 match (this.first_child, this.last_child) {
300 (Some(first), Some(last)) if first == last => {
302 (this.first_child, this.last_child) = (None, None);
303
304 let node = &mut arena.0[last.idx()];
305
306 node.parent = None;
308
309 node.prev = None;
310 node.next = None;
311
312 Some(last)
313 },
314
315 (Some(_), Some(last)) => {
317 let prev = last.prev(arena).expect("There are multiple children");
318
319 arena.0[token.idx()].last_child = Some(prev);
321 arena.0[prev.idx()].next = None;
322
323 let node = &mut arena.0[last.idx()];
324
325 node.parent = None;
327
328 node.prev = None;
329 node.next = None;
330
331 Some(last)
332 },
333
334 (..) => {
336 debug_assert!(this.first_child.is_none() && this.last_child.is_none());
338
339 None
340 },
341 }
342 }
343
344 fn pop_front(token: Self::Token, arena: &mut Arena<Self>) -> Option<UnifiedNodeRepresentation<Data>> {
345 let this = &mut arena.0[token.idx()];
346
347 match (this.first_child, this.last_child) {
348 (Some(first), Some(last)) if first == last => {
350 (this.first_child, this.last_child) = (None, None);
351
352 Some(
353 arena
354 .0
355 .remove(first.idx())
356 .expect("tried to remove child but there was no such node in the `arena`")
357 .into_representation(arena),
358 )
359 },
360
361 (Some(first), Some(_)) => {
363 let next = first.next(arena).expect("There are multiple children.");
364
365 arena.0[token.idx()].first_child = Some(next);
367 arena.0[next.idx()].prev = None;
368
369 Some(
370 arena
371 .0
372 .remove(first.idx())
373 .expect("tried to remove child but there was no such node in the `arena`")
374 .into_representation(arena),
375 )
376 },
377
378 (..) => {
380 debug_assert!(this.first_child.is_none() && this.last_child.is_none());
382
383 None
384 },
385 }
386 }
387
388 fn pop_back(token: Self::Token, arena: &mut Arena<Self>) -> Option<UnifiedNodeRepresentation<Data>> {
389 let this = &mut arena.0[token.idx()];
390
391 match (this.first_child, this.last_child) {
392 (Some(first), Some(last)) if first == last => {
394 (this.first_child, this.last_child) = (None, None);
395
396 Some(
397 arena
398 .0
399 .remove(last.idx())
400 .expect("tried to remove child but there was no such node in the `arena`")
401 .into_representation(arena),
402 )
403 },
404
405 (Some(_), Some(last)) => {
407 let prev = last.prev(arena).expect("There are multiple children.");
408
409 arena.0[token.idx()].last_child = Some(prev);
411 arena.0[prev.idx()].next = None;
412
413 Some(
414 arena
415 .0
416 .remove(last.idx())
417 .expect("tried to remove child but there was no such node in the `arena`")
418 .into_representation(arena),
419 )
420 },
421
422 (..) => {
424 debug_assert!(this.first_child.is_none() && this.last_child.is_none());
426
427 None
428 },
429 }
430 }
431
432 fn push_front(token: Self::Token, arena: &mut Arena<Self>, new: Token<Self>) {
433 assert_ne!(
435 arena.0[token.idx()].root(arena),
436 new,
437 "tried to push this branch's own root as a child"
438 );
439 assert!(
441 arena.0[new.idx()].parent.is_none(),
442 "tried to push a child that already has a parent"
443 );
444
445 arena.0[new.idx()].parent = Some(token);
447
448 let this = &mut arena.0[token.idx()];
449
450 match (this.first_child, this.last_child) {
452 (Some(first), Some(_)) => {
454 this.first_child = Some(new);
456 arena.0[first.idx()].prev = Some(new);
457 },
458
459 (..) => {
461 debug_assert!(this.first_child.is_none() && this.last_child.is_none());
463
464 (this.first_child, this.last_child) = (Some(new), Some(new));
466 },
467 }
468 }
469
470 fn push_back(token: Self::Token, arena: &mut Arena<Self>, new: Token<Self>) {
471 assert_ne!(
473 arena.0[token.idx()].root(arena),
474 new,
475 "tried to push this branch's own root as a child"
476 );
477 assert!(
479 arena.0[new.idx()].parent.is_none(),
480 "tried to push a child that already has a parent"
481 );
482
483 arena.0[new.idx()].parent = Some(token);
485
486 let this = &mut arena.0[token.idx()];
487
488 match (this.first_child, this.last_child) {
490 (Some(_), Some(last)) => {
492 this.last_child = Some(new);
494 arena.0[last.idx()].next = Some(new);
495 },
496
497 (..) => {
499 debug_assert!(this.first_child.is_none() && this.last_child.is_none());
501
502 (this.first_child, this.last_child) = (Some(new), Some(new));
504 },
505 }
506 }
507}