1 use bit_set::{self, BitSet};
2 use document::Document;
3 use node::Node;
4 use predicate::Predicate;
5 
6 #[derive(Clone, Debug, PartialEq)]
7 pub struct Selection<'a> {
8     document: &'a Document,
9     bit_set: BitSet,
10 }
11 
12 impl<'a> Selection<'a> {
new(document: &'a Document, bit_set: BitSet) -> Selection<'a>13     pub fn new(document: &'a Document, bit_set: BitSet) -> Selection<'a> {
14         Selection { document, bit_set }
15     }
16 
iter<'sel>(&'sel self) -> Iter<'sel, 'a>17     pub fn iter<'sel>(&'sel self) -> Iter<'sel, 'a> {
18         Iter {
19             selection: self,
20             inner: self.bit_set.iter(),
21         }
22     }
23 
filter<P: Predicate>(&self, p: P) -> Selection<'a>24     pub fn filter<P: Predicate>(&self, p: P) -> Selection<'a> {
25         Selection {
26             document: self.document,
27             bit_set: self
28                 .bit_set
29                 .iter()
30                 .filter(|&index| p.matches(&self.document.nth(index).unwrap()))
31                 .collect(),
32         }
33     }
34 
find<P: Predicate>(&self, p: P) -> Selection<'a>35     pub fn find<P: Predicate>(&self, p: P) -> Selection<'a> {
36         let mut bit_set = BitSet::new();
37 
38         for node in self {
39             recur(&node, &mut bit_set);
40         }
41 
42         return Selection {
43             document: self.document,
44             bit_set: bit_set
45                 .iter()
46                 .filter(|&index| p.matches(&self.document.nth(index).unwrap()))
47                 .collect(),
48         };
49 
50         fn recur(node: &Node, bit_set: &mut BitSet) {
51             if bit_set.contains(node.index()) {
52                 return;
53             }
54 
55             for child in node.children() {
56                 recur(&child, bit_set);
57                 bit_set.insert(child.index());
58             }
59         }
60     }
61 
parent(&self) -> Selection<'a>62     pub fn parent(&self) -> Selection<'a> {
63         Selection {
64             document: self.document,
65             bit_set: self
66                 .iter()
67                 .filter_map(|node| node.parent().map(|parent| parent.index()))
68                 .collect(),
69         }
70     }
71 
prev(&self) -> Selection<'a>72     pub fn prev(&self) -> Selection<'a> {
73         Selection {
74             document: self.document,
75             bit_set: self
76                 .iter()
77                 .filter_map(|node| node.prev().map(|prev| prev.index()))
78                 .collect(),
79         }
80     }
81 
next(&self) -> Selection<'a>82     pub fn next(&self) -> Selection<'a> {
83         Selection {
84             document: self.document,
85             bit_set: self
86                 .iter()
87                 .filter_map(|node| node.next().map(|next| next.index()))
88                 .collect(),
89         }
90     }
91 
parents(&self) -> Selection<'a>92     pub fn parents(&self) -> Selection<'a> {
93         let mut bit_set = BitSet::new();
94         for mut node in self {
95             while let Some(parent) = node.parent() {
96                 bit_set.insert(parent.index());
97                 node = parent;
98             }
99         }
100 
101         Selection {
102             document: self.document,
103             bit_set,
104         }
105     }
106 
children(&self) -> Selection<'a>107     pub fn children(&self) -> Selection<'a> {
108         let mut bit_set = BitSet::new();
109         for node in self {
110             for child in node.children() {
111                 bit_set.insert(child.index());
112             }
113         }
114 
115         Selection {
116             document: self.document,
117             bit_set,
118         }
119     }
120 
first(&self) -> Option<Node<'a>>121     pub fn first(&self) -> Option<Node<'a>> {
122         self.bit_set
123             .iter()
124             .next()
125             .map(|index| self.document.nth(index).unwrap())
126     }
127 
len(&self) -> usize128     pub fn len(&self) -> usize {
129         self.bit_set.len()
130     }
131 
is_empty(&self) -> bool132     pub fn is_empty(&self) -> bool {
133         self.bit_set.is_empty()
134     }
135 }
136 
137 #[derive(Clone)]
138 pub struct Iter<'sel, 'doc: 'sel> {
139     selection: &'sel Selection<'doc>,
140     inner: bit_set::Iter<'sel, u32>,
141 }
142 
143 impl<'sel, 'doc> Iterator for Iter<'sel, 'doc> {
144     type Item = Node<'doc>;
145 
next(&mut self) -> Option<Node<'doc>>146     fn next(&mut self) -> Option<Node<'doc>> {
147         self.inner
148             .next()
149             .map(|index| self.selection.document.nth(index).unwrap())
150     }
151 }
152 
153 impl<'sel, 'doc> IntoIterator for &'sel Selection<'doc> {
154     type Item = Node<'doc>;
155     type IntoIter = Iter<'sel, 'doc>;
156 
into_iter(self) -> Self::IntoIter157     fn into_iter(self) -> Self::IntoIter {
158         self.iter()
159     }
160 }
161