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
use bit_set::{self, BitSet};
use document::Document;
use node::Node;
use predicate::Predicate;

#[derive(Clone, Debug, PartialEq)]
pub struct Selection<'a> {
    document: &'a Document,
    bit_set: BitSet,
}

impl<'a> Selection<'a> {
    pub fn new(document: &'a Document, bit_set: BitSet) -> Selection<'a> {
        Selection {
            document: document,
            bit_set: bit_set,
        }
    }

    pub fn iter<'sel>(&'sel self) -> Iter<'sel, 'a> {
        Iter {
            selection: self,
            inner: self.bit_set.iter(),
        }
    }

    pub fn filter<P: Predicate>(&self, p: P) -> Selection<'a> {
        Selection {
            document: self.document,
            bit_set: self
                .bit_set
                .iter()
                .filter(|&index| p.matches(&self.document.nth(index).unwrap()))
                .collect(),
        }
    }

    pub fn find<P: Predicate>(&self, p: P) -> Selection<'a> {
        let mut bit_set = BitSet::new();

        for node in self {
            recur(&node, &mut bit_set);
        }

        return Selection {
            document: self.document,
            bit_set: bit_set
                .iter()
                .filter(|&index| p.matches(&self.document.nth(index).unwrap()))
                .collect(),
        };

        fn recur(node: &Node, bit_set: &mut BitSet) {
            if bit_set.contains(node.index()) {
                return;
            }

            for child in node.children() {
                recur(&child, bit_set);
                bit_set.insert(child.index());
            }
        }
    }

    pub fn parent(&self) -> Selection<'a> {
        Selection {
            document: self.document,
            bit_set: self
                .iter()
                .filter_map(|node| node.parent().map(|parent| parent.index()))
                .collect(),
        }
    }

    pub fn prev(&self) -> Selection<'a> {
        Selection {
            document: self.document,
            bit_set: self
                .iter()
                .filter_map(|node| node.prev().map(|prev| prev.index()))
                .collect(),
        }
    }

    pub fn next(&self) -> Selection<'a> {
        Selection {
            document: self.document,
            bit_set: self
                .iter()
                .filter_map(|node| node.next().map(|next| next.index()))
                .collect(),
        }
    }

    pub fn parents(&self) -> Selection<'a> {
        let mut bit_set = BitSet::new();
        for mut node in self {
            while let Some(parent) = node.parent() {
                bit_set.insert(parent.index());
                node = parent;
            }
        }

        Selection {
            document: self.document,
            bit_set: bit_set,
        }
    }

    pub fn children(&self) -> Selection<'a> {
        let mut bit_set = BitSet::new();
        for node in self {
            for child in node.children() {
                bit_set.insert(child.index());
            }
        }

        Selection {
            document: self.document,
            bit_set: bit_set,
        }
    }

    pub fn first(&self) -> Option<Node<'a>> {
        self.bit_set
            .iter()
            .next()
            .map(|index| self.document.nth(index).unwrap())
    }

    pub fn len(&self) -> usize {
        self.bit_set.len()
    }
}

#[derive(Clone)]
pub struct Iter<'sel, 'doc: 'sel> {
    selection: &'sel Selection<'doc>,
    inner: bit_set::Iter<'sel, u32>,
}

impl<'sel, 'doc> Iterator for Iter<'sel, 'doc> {
    type Item = Node<'doc>;

    fn next(&mut self) -> Option<Node<'doc>> {
        self.inner
            .next()
            .map(|index| self.selection.document.nth(index).unwrap())
    }
}

impl<'sel, 'doc> IntoIterator for &'sel Selection<'doc> {
    type Item = Node<'doc>;
    type IntoIter = Iter<'sel, 'doc>;

    fn into_iter(self) -> Self::IntoIter {
        self.iter()
    }
}