1use std::{collections::vec_deque, fmt::Debug, iter::Copied, ops::Index};
6
7use super::*;
8use crate::{remove_children_deque, BranchNodeDeque};
9
10type BranchTokenDeque<BranchData, LeafData> = Token<BranchDeque<BranchData, LeafData>>;
11type LeafTokenDeque<BranchData, LeafData> = Token<LeafDeque<BranchData, LeafData>>;
12
13#[cfg_attrs]
14#[configure(
46 feature = "unified",
47 #[configure(
49 not(feature = "deque"),
50 )]
53 #[configure(
54 feature = "deque",
55 )]
59 )]
61#[derive(Debug)]
62#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
63pub enum SplitNodeDeque<BranchData: Debug, LeafData: Debug> {
64 Branch(BranchDeque<BranchData, LeafData>),
65 Leaf(LeafDeque<BranchData, LeafData>),
66}
67
68#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
72pub enum SplitTokenDeque<BranchData: Debug, LeafData: Debug> {
73 Branch(BranchTokenDeque<BranchData, LeafData>),
78 Leaf(LeafTokenDeque<BranchData, LeafData>),
83}
84
85#[derive(Debug)]
103#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
104pub struct BranchDeque<BranchData: Debug, LeafData: Debug> {
105 token: Token<Self>,
106
107 parent: Option<BranchTokenDeque<BranchData, LeafData>>,
108 children: VecDeque<SplitTokenDeque<BranchData, LeafData>>,
109
110 data: BranchData,
111}
112
113#[derive(Debug)]
130#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
131pub struct LeafDeque<BranchData: Debug, LeafData: Debug> {
132 token: Token<Self>,
133
134 parent: Option<BranchTokenDeque<BranchData, LeafData>>,
135
136 data: LeafData,
137}
138
139impl<BranchData, LeafData> Debug for SplitTokenDeque<BranchData, LeafData>
140where
141 BranchData: Debug,
142 LeafData: Debug,
143{
144 #[inline(always)]
145 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
146 match self {
147 Self::Branch(branch) => write!(f, "BranchDequeToken({:?})", branch.idx()),
148 Self::Leaf(leaf) => write!(f, "LeafDequeToken({:?})", leaf.idx()),
149 }
150 }
151}
152
153impl<BranchData, LeafData, I: Idx> PartialEq<I> for SplitTokenDeque<BranchData, LeafData>
154where
155 BranchData: Debug,
156 LeafData: Debug,
157{
158 #[inline(always)]
159 fn eq(&self, other: &I) -> bool {
160 self.idx() == other.idx()
161 }
162}
163
164impl<BranchData, LeafData> Eq for SplitTokenDeque<BranchData, LeafData>
165where
166 BranchData: Debug,
167 LeafData: Debug,
168{
169}
170
171impl<BranchData, LeafData> Hash for SplitTokenDeque<BranchData, LeafData>
172where
173 BranchData: Debug,
174 LeafData: Debug,
175{
176 #[inline(always)]
177 fn hash<H: Hasher>(&self, state: &mut H) {
178 match self {
179 Self::Branch(branch) => branch.hash(state),
180 Self::Leaf(leaf) => leaf.hash(state),
181 }
182 }
183}
184
185impl<BranchData, LeafData> Clone for SplitTokenDeque<BranchData, LeafData>
186where
187 BranchData: Debug,
188 LeafData: Debug,
189{
190 #[inline(always)]
191 fn clone(&self) -> Self {
192 *self
193 }
194}
195
196impl<BranchData, LeafData> Copy for SplitTokenDeque<BranchData, LeafData>
197where
198 BranchData: Debug,
199 LeafData: Debug,
200{
201}
202
203impl<BranchData, LeafData> SplitTokenDeque<BranchData, LeafData>
204where
205 BranchData: Debug,
206 LeafData: Debug,
207{
208 #[inline(always)]
212 pub const fn new_branch(token: BranchTokenDeque<BranchData, LeafData>) -> Self {
213 Self::Branch(token)
214 }
215
216 #[inline(always)]
220 pub const fn new_leaf(token: LeafTokenDeque<BranchData, LeafData>) -> Self {
221 Self::Leaf(token)
222 }
223}
224
225impl<BranchData, LeafData> Idx for SplitTokenDeque<BranchData, LeafData>
226where
227 BranchData: Debug,
228 LeafData: Debug,
229{
230 #[inline(always)]
231 fn idx(&self) -> generational_arena::Index {
232 match self {
233 Self::Branch(branch) => branch.idx(),
234 Self::Leaf(leaf) => leaf.idx(),
235 }
236 }
237}
238
239impl<BranchData, LeafData> NodeToken<SplitNodeDeque<BranchData, LeafData>> for SplitTokenDeque<BranchData, LeafData>
240where
241 BranchData: Debug,
242 LeafData: Debug,
243{
244}
245
246impl<BranchData, LeafData, Other: Node> PartialEq<Other> for BranchDeque<BranchData, LeafData>
247where
248 BranchData: Debug,
249 LeafData: Debug,
250 <Self as Node>::Token: PartialEq<Other::Token>,
251{
252 fn eq(&self, other: &Other) -> bool {
253 self.token == other.token()
254 }
255}
256
257impl<BranchData, LeafData> Eq for BranchDeque<BranchData, LeafData>
258where
259 BranchData: Debug,
260 LeafData: Debug,
261{
262}
263
264impl<BranchData, LeafData> Hash for BranchDeque<BranchData, LeafData>
265where
266 BranchData: Debug,
267 LeafData: Debug,
268{
269 #[inline(always)]
270 fn hash<H: Hasher>(&self, state: &mut H) {
271 self.token.hash(state);
272 }
273}
274
275impl<BranchData, LeafData, Other: Node> PartialEq<Other> for LeafDeque<BranchData, LeafData>
276where
277 BranchData: Debug,
278 LeafData: Debug,
279 <Self as Node>::Token: PartialEq<Other::Token>,
280{
281 fn eq(&self, other: &Other) -> bool {
282 self.token == other.token()
283 }
284}
285
286impl<BranchData, LeafData> Eq for LeafDeque<BranchData, LeafData>
287where
288 BranchData: Debug,
289 LeafData: Debug,
290{
291}
292
293impl<BranchData, LeafData> Hash for LeafDeque<BranchData, LeafData>
294where
295 BranchData: Debug,
296 LeafData: Debug,
297{
298 #[inline(always)]
299 fn hash<H: Hasher>(&self, state: &mut H) {
300 self.token.hash(state);
301 }
302}
303
304impl<'node, BranchData, LeafData> TryFrom<&'node SplitNodeDeque<BranchData, LeafData>>
305 for &'node BranchDeque<BranchData, LeafData>
306where
307 BranchData: Debug,
308 LeafData: Debug,
309{
310 type Error = &'node SplitNodeDeque<BranchData, LeafData>;
311
312 #[inline(always)]
313 fn try_from(node: &'node SplitNodeDeque<BranchData, LeafData>) -> Result<Self, Self::Error> {
314 match node {
315 SplitNodeDeque::Branch(branch) => Ok(branch),
316 SplitNodeDeque::Leaf(_) => Err(node),
317 }
318 }
319}
320
321impl<'node, BranchData, LeafData> TryFrom<&'node mut SplitNodeDeque<BranchData, LeafData>>
322 for &'node mut BranchDeque<BranchData, LeafData>
323where
324 BranchData: Debug,
325 LeafData: Debug,
326{
327 type Error = &'node mut SplitNodeDeque<BranchData, LeafData>;
328
329 #[inline(always)]
330 fn try_from(node: &'node mut SplitNodeDeque<BranchData, LeafData>) -> Result<Self, Self::Error> {
331 match node {
332 SplitNodeDeque::Branch(branch) => Ok(branch),
333 SplitNodeDeque::Leaf(_) => Err(node),
334 }
335 }
336}
337
338impl<'node, BranchData, LeafData> TryFrom<&'node SplitNodeDeque<BranchData, LeafData>>
339 for &'node LeafDeque<BranchData, LeafData>
340where
341 BranchData: Debug,
342 LeafData: Debug,
343{
344 type Error = &'node SplitNodeDeque<BranchData, LeafData>;
345
346 #[inline(always)]
347 fn try_from(node: &'node SplitNodeDeque<BranchData, LeafData>) -> Result<Self, Self::Error> {
348 match node {
349 SplitNodeDeque::Branch(_) => Err(node),
350 SplitNodeDeque::Leaf(leaf) => Ok(leaf),
351 }
352 }
353}
354
355impl<'node, BranchData, LeafData> TryFrom<&'node mut SplitNodeDeque<BranchData, LeafData>>
356 for &'node mut LeafDeque<BranchData, LeafData>
357where
358 BranchData: Debug,
359 LeafData: Debug,
360{
361 type Error = &'node mut SplitNodeDeque<BranchData, LeafData>;
362
363 #[inline(always)]
364 fn try_from(node: &'node mut SplitNodeDeque<BranchData, LeafData>) -> Result<Self, Self::Error> {
365 match node {
366 SplitNodeDeque::Branch(_) => Err(node),
367 SplitNodeDeque::Leaf(leaf) => Ok(leaf),
368 }
369 }
370}
371
372impl<BranchData, LeafData> SplitNodeDeque<BranchData, LeafData>
373where
374 BranchData: Debug,
375 LeafData: Debug,
376{
377 #[inline(always)]
378 fn set_parent(&mut self, parent: Option<BranchTokenDeque<BranchData, LeafData>>) {
379 match self {
380 Self::Branch(branch) => branch.parent = parent,
381 Self::Leaf(leaf) => leaf.parent = parent,
382 }
383 }
384}
385
386impl<BranchData, LeafData> Sealed for SplitNodeDeque<BranchData, LeafData>
387where
388 BranchData: Debug,
389 LeafData: Debug,
390{
391}
392
393impl<BranchData, LeafData> Node for SplitNodeDeque<BranchData, LeafData>
394where
395 BranchData: Debug,
396 LeafData: Debug,
397{
398 type Base = Self;
399 type Token = SplitTokenDeque<BranchData, LeafData>;
400
401 type Data = SplitData<BranchData, LeafData>;
402 type DataRef<'data> = SplitData<&'data BranchData, &'data LeafData>
403 where
404 Self: 'data;
405 type DataRefMut<'data> = SplitData<&'data mut BranchData, &'data mut LeafData>
406 where
407 Self: 'data;
408
409 #[inline(always)]
410 fn new(arena: &mut Arena<Self>, data: Self::Data) -> Self::Token {
411 match data {
412 SplitData::Branch(data) => SplitTokenDeque::Branch(BranchDeque::new(arena, data)),
413 SplitData::Leaf(data) => SplitTokenDeque::Leaf(LeafDeque::new(arena, data)),
414 }
415 }
416
417 #[inline(always)]
418 fn token(&self) -> Self::Token {
419 match self {
420 Self::Branch(branch) => SplitTokenDeque::Branch(branch.token()),
421 Self::Leaf(leaf) => SplitTokenDeque::Leaf(leaf.token()),
422 }
423 }
424
425 #[inline(always)]
426 fn parent(&self) -> Option<<<Self::Base as BaseNode>::Branch as Node>::Token> {
427 match self {
428 Self::Branch(branch) => branch.parent(),
429 Self::Leaf(leaf) => leaf.parent(),
430 }
431 }
432
433 #[inline(always)]
434 fn data(&self) -> Self::DataRef<'_> {
435 match self {
436 Self::Branch(branch) => SplitData::Branch(branch.data()),
437 Self::Leaf(leaf) => SplitData::Leaf(leaf.data()),
438 }
439 }
440
441 #[inline(always)]
442 fn data_mut(&mut self) -> Self::DataRefMut<'_> {
443 match self {
444 Self::Branch(branch) => SplitData::Branch(branch.data_mut()),
445 Self::Leaf(leaf) => SplitData::Leaf(leaf.data_mut()),
446 }
447 }
448}
449
450impl<BranchData, LeafData> BaseNode for SplitNodeDeque<BranchData, LeafData>
451where
452 BranchData: Debug,
453 LeafData: Debug,
454{
455 type Representation = SplitNodeRepresentation<BranchData, LeafData>;
456
457 type Branch = BranchDeque<BranchData, LeafData>;
458 type Leaf = LeafDeque<BranchData, LeafData>;
459
460 fn into_representation(self, arena: &mut Arena<Self>) -> Self::Representation
461 where
462 Self: Sized,
463 {
464 match self {
465 Self::Branch(branch) => SplitNodeRepresentation::Branch {
466 children: remove_children_deque(&branch, arena),
467 data: branch.data,
468 },
469
470 Self::Leaf(leaf) => SplitNodeRepresentation::Leaf { data: leaf.data },
471 }
472 }
473}
474
475impl<BranchData, LeafData> Sealed for BranchDeque<BranchData, LeafData>
476where
477 BranchData: Debug,
478 LeafData: Debug,
479{
480}
481
482impl<BranchData, LeafData> Node for BranchDeque<BranchData, LeafData>
483where
484 BranchData: Debug,
485 LeafData: Debug,
486{
487 type Base = SplitNodeDeque<BranchData, LeafData>;
488 type Token = Token<Self>;
489
490 type Data = BranchData;
491 type DataRef<'data> = &'data BranchData
492 where
493 Self: 'data;
494 type DataRefMut<'data> = &'data mut BranchData
495 where
496 Self: 'data;
497
498 fn new(arena: &mut Arena<Self::Base>, data: Self::Data) -> Token<Self> {
499 Token::new(arena.0.insert_with(|idx| {
500 SplitNodeDeque::Branch(Self {
501 token: Token::new(idx),
502
503 parent: None,
504 children: VecDeque::new(),
505
506 data,
507 })
508 }))
509 }
510
511 #[inline(always)]
512 fn token(&self) -> Self::Token {
513 self.token
514 }
515
516 #[inline(always)]
517 fn parent(&self) -> Option<Token<Self>> {
518 self.parent
519 }
520
521 #[inline(always)]
522 fn data(&self) -> &BranchData {
523 &self.data
524 }
525
526 #[inline(always)]
527 fn data_mut(&mut self) -> &mut BranchData {
528 &mut self.data
529 }
530}
531
532impl<BranchData, LeafData> BranchNode for BranchDeque<BranchData, LeafData>
533where
534 BranchData: Debug,
535 LeafData: Debug,
536{
537 type ChildrenIter<'branch>
538 = Copied<vec_deque::Iter<'branch, <Self::Base as Node>::Token>>
539 where
540 Self: 'branch;
541
542 #[inline]
543 fn first(&self) -> Option<<Self::Base as Node>::Token> {
544 match self.children.len() {
545 0 => None,
546 _ => Some(self.children[0]),
547 }
548 }
549
550 #[inline]
551 fn last(&self) -> Option<<Self::Base as Node>::Token> {
552 match self.children.len() {
553 0 => None,
554 len => Some(self.children[len - 1]),
555 }
556 }
557
558 #[inline(always)]
559 fn children<'branch>(&'branch self, _arena: &'branch Arena<Self::Base>) -> Self::ChildrenIter<'branch> {
560 self.children.iter().copied()
561 }
562
563 #[inline(always)]
564 fn is_empty(&self) -> bool {
565 self.children.is_empty()
566 }
567
568 fn detach_front(token: Self::Token, arena: &mut Arena<Self::Base>) -> Option<<Self::Base as Node>::Token> {
569 let child = token_to_node!(ref mut, Self: token, arena).children.pop_front();
570
571 if let Some(child) = &child {
572 let node = &mut arena.0[child.idx()];
573
574 node.set_parent(None);
576 }
577
578 child
579 }
580
581 fn detach_back(token: Self::Token, arena: &mut Arena<Self::Base>) -> Option<<Self::Base as Node>::Token> {
582 let child = token_to_node!(ref mut, Self: token, arena).children.pop_back();
583
584 if let Some(child) = &child {
585 let node = &mut arena.0[child.idx()];
586
587 node.set_parent(None);
589 }
590
591 child
592 }
593
594 fn pop_front(
595 token: Self::Token,
596 arena: &mut Arena<Self::Base>,
597 ) -> Option<SplitNodeRepresentation<BranchData, LeafData>> {
598 let child = token_to_node!(ref mut, Self: token, arena).children.pop_front();
599
600 child.map(|child| {
601 arena
602 .0
603 .remove(child.idx())
604 .expect("tried to remove child but there was no such node in the `arena`")
605 .into_representation(arena)
606 })
607 }
608
609 fn pop_back(
610 token: Self::Token,
611 arena: &mut Arena<Self::Base>,
612 ) -> Option<SplitNodeRepresentation<BranchData, LeafData>> {
613 let child = token_to_node!(ref mut, Self: token, arena).children.pop_back();
614
615 child.map(|child| {
616 arena
617 .0
618 .remove(child.idx())
619 .expect("tried to remove child but there was no such node in the `arena`")
620 .into_representation(arena)
621 })
622 }
623
624 fn push_front(token: Self::Token, arena: &mut Arena<Self::Base>, new: <Self::Base as Node>::Token) {
625 assert_ne!(
627 arena.0[token.idx()].root(arena),
628 new,
629 "tried to push this branch's own root as a child"
630 );
631 assert!(
633 arena.0[new.idx()].parent().is_none(),
634 "tried to push a child that already has a parent"
635 );
636
637 arena.0[new.idx()].set_parent(Some(token));
639
640 token_to_node!(ref mut, Self: token, arena).children.push_front(new);
642 }
643
644 fn push_back(token: Self::Token, arena: &mut Arena<Self::Base>, new: <Self::Base as Node>::Token) {
645 assert_ne!(
647 arena.0[token.idx()].root(arena),
648 new,
649 "tried to push this branch's own root as a child"
650 );
651 assert!(
653 arena.0[new.idx()].parent().is_none(),
654 "tried to push a child that already has a parent"
655 );
656
657 arena.0[new.idx()].set_parent(Some(token));
659
660 token_to_node!(ref mut, Self: token, arena).children.push_back(new);
662 }
663}
664
665impl<BranchData, LeafData> Index<usize> for BranchDeque<BranchData, LeafData>
666where
667 BranchData: Debug,
668 LeafData: Debug,
669{
670 type Output = <<Self as Node>::Base as Node>::Token;
671
672 #[inline(always)]
673 fn index(&self, index: usize) -> &Self::Output {
674 &self.children[index]
675 }
676}
677
678impl<BranchData, LeafData> BranchNodeDeque for BranchDeque<BranchData, LeafData>
679where
680 BranchData: Debug,
681 LeafData: Debug,
682{
683 #[inline(always)]
684 fn len(&self) -> usize {
685 self.children.len()
686 }
687
688 fn detach(token: Self::Token, arena: &mut Arena<Self::Base>, index: usize) -> <Self::Base as Node>::Token {
689 let children = &mut token_to_node!(ref mut, Self: token, arena).children;
690
691 let child = children.remove(index).unwrap_or_else(|| {
692 panic!(
693 "the given `index` ({index}) was out of bounds; there were only {} children",
694 children.len()
695 )
696 });
697
698 arena.0[child.idx()].set_parent(None);
700
701 child
702 }
703
704 fn remove(
705 token: Self::Token,
706 arena: &mut Arena<Self::Base>,
707 index: usize,
708 ) -> SplitNodeRepresentation<BranchData, LeafData> {
709 let children = &mut token_to_node!(ref mut, Self: token, arena).children;
710
711 let child = children.remove(index).unwrap_or_else(|| {
712 panic!(
713 "the given `index` ({index}) was out of bounds; there were only {} children",
714 children.len()
715 )
716 });
717
718 arena
719 .0
720 .remove(child.idx())
721 .expect("tried to remove child but there was no such node in `arena`")
722 .into_representation(arena)
723 }
724
725 fn insert(token: Self::Token, arena: &mut Arena<Self::Base>, index: usize, new: <Self::Base as Node>::Token) {
726 assert_ne!(
728 arena.0[token.idx()].root(arena),
729 new,
730 "tried to insert this branch's own root as a child"
731 );
732 assert!(
734 arena.0[new.idx()].parent().is_none(),
735 "tried to insert a child that already has a parent"
736 );
737
738 arena.0[new.idx()].set_parent(Some(token));
740
741 token_to_node!(ref mut, Self: token, arena).children.insert(index, new);
743 }
744}
745
746impl<BranchData, LeafData> Sealed for LeafDeque<BranchData, LeafData>
747where
748 BranchData: Debug,
749 LeafData: Debug,
750{
751}
752
753impl<BranchData, LeafData> Node for LeafDeque<BranchData, LeafData>
754where
755 BranchData: Debug,
756 LeafData: Debug,
757{
758 type Base = SplitNodeDeque<BranchData, LeafData>;
759 type Token = Token<Self>;
760
761 type Data = LeafData;
762 type DataRef<'data> = &'data LeafData
763 where
764 Self: 'data;
765 type DataRefMut<'data> = &'data mut LeafData
766 where
767 Self: 'data;
768
769 fn new(arena: &mut Arena<Self::Base>, data: Self::Data) -> Self::Token {
770 Token::new(arena.0.insert_with(|idx| {
771 SplitNodeDeque::Leaf(Self {
772 token: Token::new(idx),
773
774 parent: None,
775
776 data,
777 })
778 }))
779 }
780
781 #[inline(always)]
782 fn token(&self) -> Self::Token {
783 self.token
784 }
785
786 #[inline(always)]
787 fn parent(&self) -> Option<Token<<Self::Base as BaseNode>::Branch>> {
788 self.parent
789 }
790
791 #[inline(always)]
792 fn data(&self) -> &LeafData {
793 &self.data
794 }
795
796 #[inline(always)]
797 fn data_mut(&mut self) -> &mut LeafData {
798 &mut self.data
799 }
800}