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
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
use crate::bits::next_one;
use arrayvec::{Array, ArrayVec};
use debug_tag::DebugTag;
use fixedbitset::FixedBitSet;
use std::cmp::Ordering;
use std::convert::TryInto;
use std::fmt;
use std::fmt::Debug;
use std::hash::Hash;
use std::iter::Fuse;
use std::iter::FusedIterator;
use std::marker::PhantomData;

/// A type that can be used as a numeric index.
pub trait Index: Copy + Default + Hash + Ord + Debug + 'static {
    fn new(value: usize) -> Self;
    fn try_new(value: usize) -> Option<Self>;
    fn index(&self) -> usize;
}

macro_rules! impl_index {
    ($type:ty) => {
        impl Index for $type {
            fn new(value: usize) -> Self {
                value as $type
            }

            fn try_new(value: usize) -> Option<Self> {
                value.try_into().ok()
            }

            fn index(&self) -> usize {
                *self as usize
            }
        }
    };
}

impl_index!(u8);
impl_index!(u16);
impl_index!(u32);
impl_index!(u64);
impl_index!(usize);

#[derive(Debug)]
struct SkewTreeNode<Idx> {
    former: Idx,
    latter: Idx,
}

/// The shared structure of a set of skew-binary random access lists. See the module docs for more
/// details.
///
/// The type parameter `Idx` determines the size of the internal index type. This roughly determines
/// the number of nodes in the forest. See `try_push` for more details.
///
/// # Complexity
///
/// All operations on this structure has logarithmic time complexity in the size of the path.
///
/// # Panics
///
/// Any function on this structure having a `SkewPath` parameter expects that the `SkewPath` has not
/// been mutated by another `SkewForest`. I.e. `SkewPath` are implicitly bound to the `SkewForest`
/// that mutated them.
///
/// Failure to follow the above constraint can result in panics or arbitrary values being returned.
/// This is true for any function on this structure, and not just where it is explicitly written.
///
/// # Example
///
/// ```
/// use skew_forest::{SkewForest, SkewPath, SkewPathNode};
///
/// let mut forest = SkewForest::default();
/// let mut path = SkewPath::<[SkewPathNode; 8]>::default();
///
/// let node_a = forest.push(&mut path);
/// let node_b = forest.push(&mut path);
///
/// assert_eq!(
///     forest.iter(&path).collect::<Vec<_>>(),
///     vec![node_a, node_b],
/// );
/// ```
#[derive(Default, Debug)]
pub struct SkewForest<Idx = usize> {
    nodes: Vec<SkewTreeNode<Idx>>,
    inner_next: usize,
    inner_free: FixedBitSet,
    leaf_len: usize,
    leaf_next: usize,
    leaf_free: FixedBitSet,
    tag: DebugTag,
}

/// An error from pushing an element onto a `SkewPath`.
#[derive(Debug)]
pub enum PushError {
    /// The index type was out of bounds
    IndexOutOfBounds,

    /// The path array was out of bounds
    PathArrayOutOfBounds,
}

impl std::fmt::Display for PushError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            PushError::IndexOutOfBounds => write!(f, "Index out of bounds"),
            PushError::PathArrayOutOfBounds => write!(f, "Path array out of bounds"),
        }
    }
}

impl std::error::Error for PushError {}

impl<Idx: Index> SkewForest<Idx> {
    /// Returns the inner and leaf sizes.
    pub(crate) fn sizes(&self) -> (usize, usize) {
        (self.nodes.len(), self.leaf_len)
    }

    /// Returns the number of nodes in this forest. The number of nodes in use *and* garbage
    /// collected are returned.
    pub fn size(&self) -> usize {
        let (inner, leaf) = self.sizes();
        inner + leaf
    }

    /// Panics in debug if the path does not come from this `SkewForest`
    fn assert_tag<A>(&self, path: &SkewPath<A>)
    where
        A: SkewPathArray<Idx = Idx>,
    {
        if path.len != 0 {
            assert_eq!(self.tag, path.tag, "Path must stem from this SkewForest")
        }
    }

    /// Tries to push a node onto a path, returning the index of the pushed value.
    ///
    /// This operation will return `Err` in two cases:
    /// - `PushError::IndexOutOfBounds` if `Idx` is out of bounds. This *may* happen when the number
    ///   of nodes exceeds the largest value of `Idx`, but it *may* only happen later.
    /// - `PushError::PathhArrayOutOfBounds` if `A` is out of bounds for the size of this path. This
    ///   *will* happen when `2.pow(A::len()) - 1 == path.len()`.
    pub fn try_push<A>(&mut self, path: &mut SkewPath<A>) -> Result<SkewIndex<Idx>, PushError>
    where
        A: SkewPathArray<Idx = Idx>,
    {
        self.assert_tag(path);

        let (ix, height) = match (path.trees.pop(), path.trees.pop()) {
            (Some(latter), Some(former)) if former.height == latter.height => {
                let node = SkewTreeNode {
                    former: former.node,
                    latter: latter.node,
                };

                let ix = if let Some(ix) = next_one(self.inner_free.as_slice(), self.inner_next) {
                    self.nodes[ix] = node;
                    self.inner_next = ix + 1;
                    ix
                } else {
                    let ix = self.nodes.len();
                    self.nodes.push(node);
                    ix
                };

                (ix, former.height + 1)
            }
            (latter, former) => {
                if let Some(former) = former {
                    path.trees.push(former);
                }

                if let Some(latter) = latter {
                    path.trees.push(latter);
                }

                let ix = if let Some(ix) = next_one(self.leaf_free.as_slice(), self.leaf_next) {
                    self.leaf_next = ix + 1;
                    ix
                } else {
                    let ix = self.leaf_len;
                    self.leaf_len += 1;
                    ix
                };

                (ix, 0)
            }
        };

        let ix = Idx::try_new(ix).ok_or(PushError::IndexOutOfBounds)?;

        path.trees
            .try_push(SkewPathNode { height, node: ix })
            .map_err(|_| PushError::PathArrayOutOfBounds)?;

        if path.len == 0 {
            path.tag = self.tag;
        }

        path.len += 1;

        Ok(SkewIndex {
            is_leaf: height == 0,
            index: ix,
        })
    }

    /// Push a new node onto this path.
    ///
    /// # Panic
    ///
    /// Panics if `Idx` becomes out of bounds or `A` becomes out of bounds. See `try_push` for more
    /// details.
    pub fn push<A>(&mut self, path: &mut SkewPath<A>) -> SkewIndex<Idx>
    where
        A: SkewPathArray<Idx = Idx>,
    {
        self.assert_tag(path);
        self.try_push(path).unwrap()
    }

    /// Clears any nodes in this forest. This forest will be considered a new forest, i.e.
    /// `SkewPath`s referring to the old `SkewForest` should not be used with this `SkewForest`
    /// again.
    pub fn clear(&mut self) {
        self.nodes.clear();
        self.inner_next = 0;
        self.inner_free.set_range(.., true);
        self.leaf_len = 0;
        self.leaf_next = 0;
        self.leaf_free.set_range(.., true);
        self.tag = DebugTag::new();
    }

    /// Removes the last node index from a `SkewPath`. Returns `None` if this `SkewPath` is empty.
    pub fn pop<A>(&self, path: &mut SkewPath<A>) -> Option<SkewIndex<Idx>>
    where
        A: SkewPathArray<Idx = Idx>,
    {
        self.assert_tag(path);
        path.pop(&self.nodes)
    }

    /// Retrieves a node index at an arbitrary offset. Returns `None` if the offset is out of
    /// bounds.
    pub fn try_get<A>(&self, path: &SkewPath<A>, mut offset: usize) -> Option<SkewIndex<Idx>>
    where
        A: SkewPathArray<Idx = Idx>,
    {
        self.assert_tag(path);
        let mut trees = path.trees.iter();
        let mut path_node;
        let mut size;

        loop {
            path_node = trees.next()?.clone();
            size = path_node.size();

            if offset < size {
                break;
            }

            offset -= size;
        }

        loop {
            let child_size = size / 2;
            if offset < child_size {
                path_node.node = self.nodes[path_node.node.index()].former;
            } else if offset < child_size * 2 {
                offset -= child_size;
                path_node.node = self.nodes[path_node.node.index()].latter;
            } else {
                break;
            }

            size = child_size;
            path_node.height -= 1
        }

        Some(SkewIndex {
            is_leaf: path_node.height == 0,
            index: path_node.node,
        })
    }

    /// Retrieves a node index at an arbitrary offset.DebugTag
    ///
    /// # Panics
    ///
    /// Panics if the offset is out of bounds.
    pub fn get<A>(&self, path: &SkewPath<A>, offset: usize) -> SkewIndex<Idx>
    where
        A: SkewPathArray<Idx = Idx>,
    {
        self.assert_tag(path);
        self.try_get(path, offset).expect("index out of bounds")
    }

    /// Truncate this path to the given length. If the length is longer than the current length,
    /// then no changes are made.
    /// 
    /// # Example
    /// 
    /// ```
    /// use skew_forest::{SkewForest, SkewPath, SkewPathNode, SkewMap};
    ///
    /// let mut forest = SkewForest::default();
    /// let mut path = SkewPath::<[SkewPathNode; 8]>::default();
    /// 
    /// let node_a = forest.push(&mut path);
    /// let node_b = forest.push(&mut path);
    /// 
    /// forest.truncate(&mut path, 1);
    /// 
    /// assert!(forest.iter(&path).eq(vec![node_a]));
    /// ```
    pub fn truncate<A>(&self, path: &mut SkewPath<A>, mut len: usize)
    where
        A: SkewPathArray<Idx = Idx>,
    {
        self.assert_tag(path);
        let mut trees = path.trees.iter();
        let mut tree_len = 0;

        let rest = loop {
            if len == 0 {
                break None;
            }

            let path_node = if let Some(path_node) = trees.next() {
                path_node.clone()
            } else {
                return;
            };

            let size = path_node.size();

            if size > len {
                break Some((path_node, size));
            }

            tree_len += 1;
            len -= size;
        };

        path.trees.truncate(tree_len);

        if let Some((mut path_node, mut size)) = rest {
            while len > 0 {
                let tree_node = &self.nodes[path_node.node.index()];
                let child_size = size / 2;

                if len < child_size {
                    path_node.node = tree_node.former;
                } else {
                    path.trees.push(SkewPathNode {
                        node: tree_node.former,
                        height: path_node.height - 1,
                    });
                    len -= child_size;

                    if len < child_size {
                        path_node.node = tree_node.latter;
                    } else {
                        path.trees.push(SkewPathNode {
                            node: tree_node.latter,
                            height: path_node.height - 1,
                        });
                        len -= child_size;
                        debug_assert!(len == 0);
                        break;
                    }
                }

                size = child_size;
                path_node.height -= 1;
            }
        }
    }

    /// Create an iterator over the nodes in a path. Prefer `iter_rev` if possible, as it is
    /// somewhat more efficient.
    pub fn iter<'a, A>(&'a self, path: &'a SkewPath<A>) -> Iter<'a, A>
    where
        A: SkewPathArray<Idx = Idx>,
    {
        self.assert_tag(path);
        Iter {
            nodes: &self.nodes,
            stack: ArrayVec::new(),
            trees: path.trees.iter(),
            len: path.len(),
            last: None,
        }
    }

    /// Create a reverse iterator over the nodes in a path.
    pub fn iter_rev<'a, A>(&'a self, path: &SkewPath<A>) -> IterRev<'a, A>
    where
        A: SkewPathArray<Idx = Idx>,
    {
        self.assert_tag(path);
        IterRev {
            nodes: &self.nodes,
            path: (*path).clone(),
        }
    }

    /// Returns a path of the ancestors of two paths.
    /// 
    /// # Example
    /// 
    /// ```
    /// use skew_forest::{SkewForest, SkewPath, SkewPathNode, SkewMap};
    ///
    /// let mut forest = SkewForest::default();
    ///
    /// // Create a tree like this:
    /// // A - B (path_a)
    /// //   \
    /// //     C (path_b)
    ///  
    /// let mut path_a = SkewPath::<[SkewPathNode; 8]>::default();
    /// let node_a = forest.push(&mut path_a);
    /// let mut path_b = path_a.clone();
    /// let node_b = forest.push(&mut path_a);
    /// let node_c = forest.push(&mut path_b);
    /// 
    /// // Find the ancestors
    /// let ancestors = forest.ancestors(&path_a, &path_b);
    /// 
    /// // Check that the ancestors list is matches the list with node_a
    /// assert!(forest.iter(&ancestors).eq(vec![node_a]));
    /// ```
    pub fn ancestors<A>(&self, a: &SkewPath<A>, b: &SkewPath<A>) -> SkewPath<A>
    where
        A: SkewPathArray<Idx = Idx>,
    {
        self.assert_tag(a);
        self.assert_tag(b);
        let mut a_stack: ArrayVec<A::Array> = a.trees.iter().rev().cloned().collect();
        let mut b_stack: ArrayVec<A::Array> = b.trees.iter().rev().cloned().collect();
        let mut result = SkewPath::default();

        result.tag = self.tag;

        loop {
            match (a_stack.pop(), b_stack.pop()) {
                (Some(a), Some(b)) if a == b => {
                    result.len += a.size();
                    result.trees.push(a);
                }
                (Some(a), Some(b)) if a.height != 0 || b.height != 0 => {
                    for (stack, node, other) in
                        ArrayVec::from([(&mut a_stack, &a, &b), (&mut b_stack, &b, &a)])
                    {
                        if node.height >= other.height {
                            stack.clear();

                            let tree_node = &self.nodes[node.node.index()];

                            stack.push(SkewPathNode {
                                node: tree_node.latter,
                                height: node.height - 1,
                            });

                            stack.push(SkewPathNode {
                                node: tree_node.former,
                                height: node.height - 1,
                            });
                        } else {
                            stack.push(node.clone());
                        }
                    }
                }
                _ => break,
            }
        }

        result
    }

    /// Starts a garbage collection sweep. This allows you to mark what paths you want to keep. Any
    /// nodes that are garbage collected will be reused.
    ///
    /// See `Gc` for more details.
    pub fn gc(&mut self) -> Gc<Idx> {
        self.inner_free.grow(self.nodes.len());
        self.inner_free.set_range(.., true);
        self.inner_next = 0;
        self.leaf_free.grow(self.leaf_len);
        self.leaf_free.set_range(.., true);
        self.leaf_next = 0;
        Gc(self)
    }
}

/// A reverse iterator
pub struct IterRev<'a, A: SkewPathArray> {
    nodes: &'a [SkewTreeNode<A::Idx>],
    path: SkewPath<A>,
}

impl<'a, A: SkewPathArray> Iterator for IterRev<'a, A> {
    type Item = SkewIndex<A::Idx>;

    fn next(&mut self) -> Option<SkewIndex<A::Idx>> {
        self.path.pop(self.nodes)
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        (self.path.len(), Some(self.path.len()))
    }
}

impl<'a, A: SkewPathArray> ExactSizeIterator for IterRev<'a, A> {}

impl<'a, A: SkewPathArray> FusedIterator for IterRev<'a, A> {}

/// A forward iterator
pub struct Iter<'a, A: SkewPathArray> {
    nodes: &'a [SkewTreeNode<A::Idx>],
    stack: ArrayVec<A::Array>,
    trees: std::slice::Iter<'a, SkewPathNode<A::Idx>>,
    last: Option<A::Idx>,
    len: usize,
}

impl<'a, A: SkewPathArray> Iterator for Iter<'a, A> {
    type Item = SkewIndex<A::Idx>;

    fn next(&mut self) -> Option<SkewIndex<A::Idx>> {
        let mut path_node = self.stack.pop().or_else(|| self.trees.next().cloned())?;

        while path_node.height != 0 {
            let ix = path_node.node.index();
            let tree_node = &self.nodes[ix];

            if self.last == Some(tree_node.latter) {
                break;
            }

            self.stack.push(SkewPathNode {
                node: path_node.node,
                height: path_node.height,
            });

            self.stack.push(SkewPathNode {
                node: tree_node.latter,
                height: path_node.height - 1,
            });

            path_node = SkewPathNode {
                node: tree_node.former,
                height: path_node.height - 1,
            };
        }

        self.last = Some(path_node.node);
        self.len -= 1;

        Some(SkewIndex {
            is_leaf: path_node.height == 0,
            index: path_node.node,
        })
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        (self.len, Some(self.len))
    }
}

impl<'a, A: SkewPathArray> ExactSizeIterator for Iter<'a, A> {}

impl<'a, A: SkewPathArray> FusedIterator for Iter<'a, A> {}

/// An in-progress mark-and-sweep session.
/// 
/// This allows a `SkewForest` to resuse unsued nodes. Active nodes must be marked using the `mark`
/// function. After marking nodes and sweeping, the nodes unmarked nodes will be reused when
/// pushing.
/// 
/// # Example
/// 
/// ```
/// use skew_forest::{SkewForest, SkewPath, SkewPathNode};
///
/// let mut forest = SkewForest::default();
/// let mut path_a = SkewPath::<[SkewPathNode; 8]>::default();
/// let mut path_b = SkewPath::<[SkewPathNode; 8]>::default();
/// 
/// forest.push(&mut path_a);
/// forest.push(&mut path_b);
/// 
/// assert_eq!(forest.size(), 2);
/// 
/// forest.gc().mark(&path_a);
/// 
/// // `path_b` has now been garbage collected since it was not marked.
/// 
/// // Size is still 2, since `size` includes garbage collected nodes.
/// assert_eq!(forest.size(), 2);
/// 
/// forest.push(&mut path_a);
/// 
/// // But actually pushing a new node reuses the old node
/// assert_eq!(forest.size(), 2);
/// ```
pub struct Gc<'a, Idx = usize>(&'a mut SkewForest<Idx>);

impl<'a, Idx> Gc<'a, Idx>
where
    Idx: Index,
{
    /// Marks a path to be kept. The nodes of the path will be kept in the `SkewForest`.
    ///
    /// An iterator to the marked nodes are returned.
    pub fn mark<'b, A>(&'b mut self, path: &SkewPath<A>) -> Mark<'b, A>
    where
        A: SkewPathArray<Idx = Idx>,
    {
        self.0.assert_tag(path);
        Mark {
            forest: &mut self.0,
            path: path.clone(),
        }
    }

    /// An iterator over all the nodes that this GC session will not be kept.
    /// 
    /// Calling this is optional. Dropping the `Gc` will also garbage collect nodes.
    pub fn sweep(self) -> Sweep<'a, Idx> {
        Sweep {
            leaf_free: self.0.leaf_free.ones().fuse(),
            inner_free: self.0.inner_free.ones(),
            marker: PhantomData,
        }
    }
}

/// An iterator over the nodes that will be marked.
pub struct Mark<'a, A>
where
    A: SkewPathArray,
{
    forest: &'a mut SkewForest<A::Idx>,
    path: SkewPath<A>,
}

impl<A> Iterator for Mark<'_, A>
where
    A: SkewPathArray,
{
    type Item = SkewIndex<A::Idx>;

    fn next(&mut self) -> Option<SkewIndex<A::Idx>> {
        let popped = self.path.pop(&self.forest.nodes)?;

        let free_set = if popped.is_leaf {
            &mut self.forest.leaf_free
        } else {
            &mut self.forest.inner_free
        };

        if free_set[popped.index.index()] {
            free_set.set(popped.index.index(), false);
            Some(popped)
        } else {
            None
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        (self.path.len(), Some(self.path.len()))
    }
}

impl<A: SkewPathArray> ExactSizeIterator for Mark<'_, A> {}

impl<A> Drop for Mark<'_, A>
where
    A: SkewPathArray,
{
    fn drop(&mut self) {
        self.for_each(std::mem::drop);
    }
}

/// An iterator over the nodes that will not be kept.
pub struct Sweep<'a, Idx> {
    leaf_free: Fuse<fixedbitset::Ones<'a>>,
    inner_free: fixedbitset::Ones<'a>,
    marker: PhantomData<Idx>,
}

impl<Idx: Index> Iterator for Sweep<'_, Idx> {
    type Item = SkewIndex<Idx>;

    fn next(&mut self) -> Option<SkewIndex<Idx>> {
        self.leaf_free
            .next()
            .map(|idx| SkewIndex {
                is_leaf: true,
                index: Idx::new(idx),
            })
            .or_else(|| {
                self.inner_free.next().map(|idx| SkewIndex {
                    is_leaf: false,
                    index: Idx::new(idx),
                })
            })
    }
}

/// An opaque node used in the a `SkewPath`.
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub struct SkewPathNode<Idx = usize> {
    height: u8,
    node: Idx,
}

impl<Idx> SkewPathNode<Idx> {
    fn size(&self) -> usize {
        (1 << (self.height + 1)) - 1
    }
}

/// An opaque array used in a `SkewPath`.
///
/// The length of this array determines the capacity of the `SkewPath`. The capacity of the path
/// will be `2.pow(array_len) - 1`.
pub trait SkewPathArray: Clone {
    type Idx: Index;
    type Array: Array<Item = SkewPathNode<Self::Idx>>;
}

impl<T, Idx> SkewPathArray for T
where
    T: Array<Item = SkewPathNode<Idx>> + Clone,
    Idx: Index,
{
    type Idx = Idx;
    type Array = Self;
}

/// The index of a node in a `SkewForest`.
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub struct SkewIndex<Idx> {
    pub(crate) is_leaf: bool,
    pub(crate) index: Idx,
}

/// A path in a tree in a `SkewForest`. This can be seen as an index into a path in a specific
/// `SkewForest`, and must therefore only be used with a single `SkewForest`.
///
/// The type parameter `A` denotes the path node array used internally by this path. The capacity of
/// this path depends on the length of the array. If the length of this array is `n` then the
/// capacity will be `2.pow(n) - 1`.
/// 
/// The memory size and the cost of cloning a `SkewPath` is proportional to the length of the array.
pub struct SkewPath<A: SkewPathArray> {
    trees: ArrayVec<A::Array>,
    len: usize,
    tag: DebugTag,
}

impl<A: SkewPathArray> SkewPath<A> {
    fn pop(&mut self, nodes: &[SkewTreeNode<A::Idx>]) -> Option<SkewIndex<A::Idx>> {
        let popped = self.trees.pop()?;

        self.len -= 1;
        let is_leaf = popped.height == 0;

        if !is_leaf {
            let node = &nodes[popped.node.index()];

            self.trees.push(SkewPathNode {
                node: node.former,
                height: popped.height - 1,
            });

            self.trees.push(SkewPathNode {
                node: node.latter,
                height: popped.height - 1,
            });
        }

        Some(SkewIndex {
            is_leaf,
            index: popped.node,
        })
    }

    /// Return the last value in this path or `None` if the path is empty.
    pub fn last(&self) -> Option<SkewIndex<A::Idx>> {
        self.trees.last().map(|node| SkewIndex {
            is_leaf: node.height == 0,
            index: node.node,
        })
    }

    /// The number of nodes of this path.
    pub fn len(&self) -> usize {
        self.len
    }

    /// Returns true if there are no nodes in this path and false otherwise.
    pub fn is_empty(&self) -> bool {
        self.trees.is_empty()
    }
}

impl<A: SkewPathArray> Clone for SkewPath<A> {
    fn clone(&self) -> SkewPath<A> {
        SkewPath {
            trees: self.trees.clone(),
            len: self.len,
            tag: self.tag,
        }
    }
}

impl<A: SkewPathArray> PartialEq for SkewPath<A> {
    fn eq(&self, other: &SkewPath<A>) -> bool {
        self.len == other.len && self.trees == other.trees
    }
}

impl<A: SkewPathArray> Eq for SkewPath<A> {}

impl<A: SkewPathArray> PartialOrd for SkewPath<A> {
    fn partial_cmp(&self, other: &SkewPath<A>) -> Option<Ordering> {
        Some(
            self.len
                .cmp(&other.len)
                .then_with(|| self.trees.cmp(&other.trees)),
        )
    }
}

impl<A: SkewPathArray> Ord for SkewPath<A> {
    fn cmp(&self, other: &SkewPath<A>) -> Ordering {
        self.len
            .cmp(&other.len)
            .then_with(|| self.trees.cmp(&other.trees))
    }
}

impl<A: SkewPathArray> Debug for SkewPath<A> {
    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        f.debug_struct(stringify!(SkewPath))
            .field("len", &self.len)
            .field("trees", &self.trees)
            .finish()
    }
}

impl<A> Default for SkewPath<A>
where
    A: SkewPathArray,
{
    fn default() -> Self {
        SkewPath {
            trees: ArrayVec::default(),
            len: 0,
            tag: DebugTag::from(0),
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use std::collections::HashSet;

    type Forest = SkewForest<usize>;
    type Path = SkewPath<[SkewPathNode<usize>; 32]>;

    #[test]
    fn iter() {
        let mut f = Forest::default();
        let mut path = Path::default();
        let mut vec = Vec::new();
        for len in 0..64 {
            assert_eq!(path.len(), len);
            vec.push(f.push(&mut path));
            assert_eq!(f.iter(&path).collect::<Vec<_>>(), vec);
        }
    }

    #[test]
    fn iter_rev() {
        let mut f = Forest::default();
        let mut path = Path::default();
        let mut vec = Vec::new();
        for len in 0..64 {
            assert_eq!(path.len(), len);
            vec.insert(0, f.push(&mut path));
            assert_eq!(f.iter_rev(&path).collect::<Vec<_>>(), vec);
        }
    }

    #[test]
    fn get() {
        let mut f = Forest::default();
        let mut path = Path::default();
        let mut vec = Vec::new();
        for len in 0..64 {
            vec.push(f.push(&mut path));

            for i in 0..=len {
                assert_eq!(f.try_get(&path, i), vec.get(i).cloned());
            }
        }
    }

    #[test]
    fn truncate() {
        let mut f = Forest::default();
        let mut path = Path::default();
        let mut vec = Vec::new();
        for len in 0..64 {
            vec.push(f.push(&mut path));

            for i in 0..=len {
                let mut l = path.clone();
                f.truncate(&mut l, i);
                assert_eq!(&f.iter(&l).collect::<Vec<_>>()[..], &vec[..i])
            }
        }
    }

    #[test]
    fn ancestors() {
        let mut f = Forest::default();
        let mut prefix = Path::default();

        for _prefix_len in 0..32 {
            f.push(&mut prefix);
            let mut a_path = prefix.clone();

            for _a_len in 0..32 {
                f.push(&mut a_path);
                let mut b_path = prefix.clone();

                for _b_len in 0..32 {
                    f.push(&mut b_path);
                    assert_eq!(prefix, f.ancestors(&a_path, &mut b_path));
                }
            }
        }
    }

    #[test]
    fn gc() {
        let mut f = Forest::default();
        let mut a = Path::default();
        let mut b = Path::default();

        let mut b_set = HashSet::new();

        for _len in 0..64 {
            f.push(&mut a);
            b_set.insert(f.push(&mut b));
        }

        let mut gc = f.gc();
        gc.mark(&a);

        let mut b_sweep_set = b_set.clone();
        assert!(gc.sweep().all(|d| b_sweep_set.remove(&d)));
        assert!(b_sweep_set.is_empty());

        let mut c = Path::default();
        for _len in 0..64 {
            assert!(b_set.contains(&f.push(&mut c)));
        }
    }
}