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
use crate::Tree;

/// The immutable Depth-First Iterator.
pub struct DfIter<'a,T>{
    pub (in crate) stack : Vec<&'a Tree<T>>
}

/// The mutable Depth-First Iterator.
pub struct DfIterMut<'a,T>{
    pub (in crate) stack : Vec<&'a mut Tree<T>>
}

impl<'a,T> DfIter<'a,T> {
    /// convert to a DfDepthIter
    pub fn depth(self) -> DfDepthIter<'a,T>{
        let mut s = self;
        DfDepthIter {
            stack : vec![(0,s.stack.pop().unwrap())]
        }
    }
}

impl<'a,T> DfIterMut<'a,T>{
    /// convert to a DfDepthIterMut
    pub fn depth(self) -> DfDepthIterMut<'a,T> {
        let mut s = self;
        DfDepthIterMut {
            stack: vec![(0,s.stack.pop().unwrap())],
        }
    }
}

impl<'a,T> Iterator for DfIter<'a,T>{
    type Item = &'a T;

    fn next(&mut self) -> Option<Self::Item> {
        if let Some(top) = self.stack.pop() {
            for child in top.children.iter().rev() {
                self.stack.push(child);
            }
            Some(&top.data)
        }else{
            Option::None
        }
    }
}

impl<'a,T> Iterator for DfIterMut<'a,T>{
    type Item = &'a mut T;

    fn next(&mut self) -> Option<Self::Item> {
        if let Some(top) = self.stack.pop() {
            for child in top.children.iter_mut().rev() {
                self.stack.push(child);
            }
            Some(&mut top.data)
        }else{
            Option::None
        }
    }
}

/// The immutable Depth-First Iterator with Depth information.
pub struct DfDepthIter<'a,T> {
    stack : Vec<(usize,&'a Tree<T>)>
}

/// The mutable Breadth-First Iterator with Depth information.
pub struct DfDepthIterMut<'a,T> {
    stack : Vec<(usize,&'a mut Tree<T>)>
}

impl<'a,T> Iterator for DfDepthIter<'a,T> {
    type Item = (usize,&'a T);

    fn next(&mut self) -> Option<Self::Item> {
        if let Some((dpth,top)) = self.stack.pop() {
            for child in top.children.iter().rev() {
                self.stack.push((dpth + 1,child));
            }
            Some((dpth,&top.data))
        }else{
            Option::None
        }
    }
}

impl<'a,T> Iterator for DfDepthIterMut<'a,T> {
    type Item = (usize,&'a mut T);

    fn next(&mut self) -> Option<Self::Item> {
        if let Some((dpth,top)) = self.stack.pop() {
            for child in top.children.iter_mut().rev() {
                self.stack.push((dpth + 1,child));
            }
            Some((dpth,&mut top.data))
        }else{
            Option::None
        }
    }
}