| 1 |
use crate::document::Document; |
| 2 |
use crate::node::Node; |
| 3 |
use crate::predicate::Predicate; |
| 4 |
use bit_set::{self, BitSet}; |
| 5 |
|
| 6 |
#[derive(Clone, Debug, PartialEq, Eq)] |
| 7 |
pub struct Selection<'a> { |
| 8 |
document: &'a Document, |
| 9 |
bit_set: BitSet, |
| 10 |
} |
| 11 |
|
| 12 |
impl<'a> Selection<'a> { |
| 13 |
pub fn new(document: &'a Document, bit_set: BitSet) -> Selection<'a> { |
| 14 |
Selection { document, bit_set } |
| 15 |
} |
| 16 |
|
| 17 |
pub fn iter<'sel>(&'sel self) -> Iter<'sel, 'a> { |
| 18 |
Iter { |
| 19 |
selection: self, |
| 20 |
inner: self.bit_set.iter(), |
| 21 |
} |
| 22 |
} |
| 23 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 128 |
pub fn last(&self) -> Option<Node<'a>> { |
| 129 |
self.bit_set |
| 130 |
.iter() |
| 131 |
.last() |
| 132 |
.map(|index| self.document.nth(index).unwrap()) |
| 133 |
} |
| 134 |
|
| 135 |
pub fn len(&self) -> usize { |
| 136 |
self.bit_set.len() |
| 137 |
} |
| 138 |
|
| 139 |
pub fn is_empty(&self) -> bool { |
| 140 |
self.bit_set.is_empty() |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
#[derive(Clone)] |
| 145 |
pub struct Iter<'sel, 'doc: 'sel> { |
| 146 |
selection: &'sel Selection<'doc>, |
| 147 |
inner: bit_set::Iter<'sel, u32>, |
| 148 |
} |
| 149 |
|
| 150 |
impl<'sel, 'doc: 'sel> std::fmt::Debug for Iter<'sel, 'doc> { |
| 151 |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 152 |
f.debug_struct("Iter") |
| 153 |
.field("selection", &self.selection) |
| 154 |
|
| 155 |
|
| 156 |
.finish() |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
impl<'sel, 'doc> Iterator for Iter<'sel, 'doc> { |
| 161 |
type Item = Node<'doc>; |
| 162 |
|
| 163 |
fn next(&mut self) -> Option<Node<'doc>> { |
| 164 |
self.inner |
| 165 |
.next() |
| 166 |
.map(|index| self.selection.document.nth(index).unwrap()) |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
impl<'sel, 'doc> IntoIterator for &'sel Selection<'doc> { |
| 171 |
type Item = Node<'doc>; |
| 172 |
type IntoIter = Iter<'sel, 'doc>; |
| 173 |
|
| 174 |
fn into_iter(self) -> Self::IntoIter { |
| 175 |
self.iter() |
| 176 |
} |
| 177 |
} |