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
//! Generic tree iterators.

use traits::{Node, AssociatedData};


/// An iterator over the objects in a tree.
pub struct Iter<'a, T: 'a> {
    nodes: Vec<(usize, &'a T)>,
}

impl<'a, T> Iter<'a, T> {
    /// Create a new iterator.
    pub fn new(tree: &'a T) -> Iter<'a, T> {
        Iter { nodes: vec![(0, tree)] }
    }
}

impl<'a, T> Iterator for Iter<'a, T>
    where T: Node<Container = Vec<T>>,
{
    type Item = &'a <T as Node>::Object;

    fn next(&mut self) -> Option<&'a <T as Node>::Object> {
        use traits::NodeState::*;
        match self.nodes.pop() {
            None => None,
            Some((count, node)) => match (count, node.state()) {
                (_, Empty) => self.next(),
                (_, Leaf(obj)) => Some(obj),
                (n, Branch(vec)) => {
                    if let Some(child) = vec.get(n) {
                        self.nodes.push((n + 1, node));
                        self.nodes.push((0, child));
                    }
                    self.next()
                },
            }
        }
    }
}


/// An iterator over the objects in a tree, that only recurses as deep as
/// specified by some predicate.
pub struct RecurseObjects<'a, T: 'a, R> {
    nodes: Vec<(usize, &'a T)>,
    recurse: R,
}

impl<'a, T, R> RecurseObjects<'a, T, R> {
    /// Create a new iterator.
    pub fn new(tree: &'a T, recurse: R) -> RecurseObjects<'a, T, R> {
        RecurseObjects { nodes: vec![(0, tree)], recurse: recurse }
    }
}

impl<'a, T, R> Iterator for RecurseObjects<'a, T, R>
    where T: Node<Container = Vec<T>>,
          R: FnMut(&T) -> bool,
{
    type Item = &'a <T as Node>::Object;

    fn next(&mut self) -> Option<&'a <T as Node>::Object> {
        use traits::NodeState::*;
        match self.nodes.pop() {
            None => None,
            Some((count, node)) => match (count, node.state()) {
                (_, Empty) => self.next(),
                (_, Leaf(obj)) => Some(obj),
                (n, Branch(vec)) => {
                    if let Some(child) = vec.get(n) {
                        if (self.recurse)(node) {
                            self.nodes.push((n + 1, node));
                            self.nodes.push((0, child));
                        }
                    }
                    self.next()
                },
            }
        }
    }
}


/// An iterator over the objects in a tree, that only recurses as deep as
/// specified by some predicate.
pub struct RecurseData<'a, T: 'a, R> {
    nodes: Vec<(Option<usize>, &'a T)>,
    recurse: R,
}

impl<'a, T, R> RecurseData<'a, T, R> {
    /// Create a new iterator.
    pub fn new(tree: &'a T, recurse: R) -> RecurseData<'a, T, R> {
        RecurseData { nodes: vec![(None, tree)], recurse: recurse }
    }
}

impl<'a, T, R> Iterator for RecurseData<'a, T, R>
    where T: Node<Container = Vec<T>> + AssociatedData,
          <T as Node>::Object: 'a,
          R: FnMut(&T) -> bool,
{
    type Item = &'a <T as AssociatedData>::Data;

    fn next(&mut self) -> Option<&'a <T as AssociatedData>::Data> {
        use traits::NodeState::*;
        match self.nodes.pop() {
            None => None,
            Some((count, node)) => match (count, node.state()) {
                (None, Branch(_)) => {
                    if (self.recurse)(node) {
                        self.nodes.push((Some(0), node));
                        self.next()
                    } else {
                        Some(node.data())
                    }
                }
                (Some(n), Branch(vec)) => {
                    if let Some(child) = vec.get(n) {
                        self.nodes.push((Some(n + 1), node));
                        self.nodes.push((None, child));
                    }
                    self.next()
                },
                _ => Some(node.data()),
            }
        }
    }
}


#[cfg(test)]
mod test {
    use nalgebra::{Point2, Origin};

    use partition::Ncube;
    use traits::{Positioned, Node};
    use pure_tree::PureTree;
    use super::*;

    #[test]
    fn iter_pure_tree() {
        let tree = PureTree::new(
            vec![
                Positioned { object: 1, position: Point2::new(-0.1, 1.0) },
                Positioned { object: 2, position: Point2::new(0.5, -0.3) },
            ].into_iter(),
            Ncube::new(Origin::origin(), 2.0)
        ).expect("Couldn't construct tree");
        let all: Vec<_> = Iter::new(&tree).collect();
        assert_eq!(all.len(), 2);
        assert!(all[0].object == 1 || all[1].object == 1);
    }

    #[test]
    fn recurse_objects_pure_tree() {
        let tree = PureTree::new(
            vec![
                Positioned { object: 1i32, position: Point2::new(-0.1, 0.8) },
                Positioned { object: 2, position: Point2::new(-0.2, 0.7) },
                Positioned { object: 3, position: Point2::new(0.5, -0.3) },
            ].into_iter(),
            Ncube::new(Origin::origin(), 2.0)
        ).expect("Couldn't construct tree");
        let all: Vec<_> =
            RecurseObjects::new(
                &tree,
                |n: &PureTree<Ncube<_, f64>, _>|
                    n.partition().width() > 1.5
            )
            .collect();
        assert_eq!(all.len(), 1);
        assert_eq!(all[0].object, 3);
    }
}