1use std::{mem, fmt};
40use std::error;
41use std::collections::HashMap;
42use std::ops::{Index, IndexMut};
43
44pub use self::IndexTreeError::*;
45
46#[doc = "
47Use this for catching errors that
48can happen when using indextree_ng::Tree.
49"]
50#[derive(Debug, Clone, PartialEq, Eq, Copy)]
51pub enum IndexTreeError {
52 SameNodeErr,
54}
55
56impl fmt::Display for IndexTreeError {
57 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
58 fmt.write_str(error::Error::description(self))
59 }
60}
61
62impl error::Error for IndexTreeError {
63 fn description(&self) -> &str {
64 match *self {
65 SameNodeErr => "Tried to apply a method to the same node like node.append(node, arena)",
66 }
67 }
68}
69
70
71#[derive(PartialEq, Eq, Copy, Clone, Debug, Hash)]
72pub struct NodeId {
74 id: usize,
75}
76
77#[derive(Clone, Debug)]
78pub struct Node<T> {
80 parent: Option<NodeId>,
83 previous_sibling: Option<NodeId>,
84 next_sibling: Option<NodeId>,
85 first_child: Option<NodeId>,
86 last_child: Option<NodeId>,
87
88 pub data: T,
90}
91
92impl<T> fmt::Display for Node<T> {
93 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
94 write!(f, "Parent: {:?}, ", self.parent)?;
95 write!(f, "Previous sibling: {:?}, ", self.previous_sibling)?;
96 write!(f, "Next sibling: {:?}, ", self.next_sibling)?;
97 write!(f, "First child: {:?}, ", self.first_child)?;
98 write!(f, "Last child: {:?}", self.last_child)
99 }
100}
101
102#[derive(Clone, Debug)]
103pub struct Arena<T> {
105 nodes: Vec<Node<T>>,
106 lookup: HashMap<usize, usize>,
107}
108
109impl<T> Arena<T> {
110 pub fn new() -> Arena<T> {
112 Arena { nodes: Vec::new(),
113 lookup: HashMap::new(), }
114 }
115
116 pub fn new_node(&mut self, data: T) -> NodeId {
118 let next_index = self.nodes.len();
119 let next_id = self.lookup.keys().fold(0, |acc, &x| std::cmp::max(acc+1, x));
120 self.nodes.push(Node {
121 parent: None,
122 first_child: None,
123 last_child: None,
124 previous_sibling: None,
125 next_sibling: None,
126 data: data,
127 });
128 self.lookup.insert(next_id, next_index);
129 NodeId { id: next_id }
130 }
131
132 pub fn remove_node(&mut self, node: NodeId) {
134 let index = *self.lookup.get(&node.id).expect("No node for given NodeId");
135
136 let mut children: Vec<NodeId> = vec![];
137 for child in node.children(self) {
138 children.push(child.clone());
139 }
140 for child in children {
141 child.detach(self);
142 }
143
144 let highest_index = self.nodes.len() - 1;
145 let mut highest_index_id: usize = 0;
146 for (key, value) in self.lookup.iter() {
147 if *value == highest_index {
148 highest_index_id = *key;
149 break;
150 }
151 }
152
153 let _ = self.lookup.remove(&node.id);
154 let _ = self.nodes.swap_remove(index);
155 self.lookup.insert(highest_index_id, index);
156 }
157
158 pub fn len(&self) -> usize {
160 self.nodes.len()
161 }
162
163 pub fn is_empty(&self) -> bool {
165 if self.len() == 0 {
166 true
167 }
168 else {
169 false
170 }
171 }
172
173 fn get_pair_mut(&mut self, a: &NodeId, b: &NodeId) -> Result<(&mut Node<T>, &mut Node<T>), IndexTreeError> {
175 if a.id == b.id {
176 return Err(IndexTreeError::SameNodeErr);
177 }
178
179 let error_msg = "No node for NodeId";
180 let a_index = *self.lookup.get(&a.id).expect(error_msg);
181 let b_index = *self.lookup.get(&b.id).expect(error_msg);
182
183 let (xs, ys) = self.nodes.split_at_mut(std::cmp::max(a_index, b_index));
184 if a_index < b_index {
185 Ok((&mut xs[a_index], &mut ys[0]))
186 } else {
187 Ok((&mut ys[0], &mut xs[b_index]))
188 }
189 }
190}
191
192impl<T> Index<NodeId> for Arena<T> {
193 type Output = Node<T>;
194
195 fn index(&self, node: NodeId) -> &Node<T> {
196 let node_index = *self.lookup.get(&node.id).expect("No node for this NodeId");
197 &self.nodes[node_index]
198 }
199}
200
201impl<T> IndexMut<NodeId> for Arena<T> {
202 fn index_mut(&mut self, node: NodeId) -> &mut Node<T> {
203 let node_index = *self.lookup.get(&node.id).expect("No node for this NodeId");
204 &mut self.nodes[node_index]
205 }
206}
207
208impl<T> Node<T> {
209 pub fn parent(&self) -> Option<NodeId> {
211 self.parent
212 }
213
214 pub fn first_child(&self) -> Option<NodeId> {
216 self.first_child
217 }
218
219 pub fn last_child(&self) -> Option<NodeId> {
221 self.last_child
222 }
223
224 pub fn previous_sibling(&self) -> Option<NodeId> {
226 self.previous_sibling
227 }
228
229 pub fn next_sibling(&self) -> Option<NodeId> {
231 self.next_sibling
232 }
233}
234
235impl NodeId {
236 pub fn ancestors<T>(self, arena: &Arena<T>) -> Ancestors<T> {
240 Ancestors {
241 arena: arena,
242 node: Some(self),
243 }
244 }
245
246 pub fn preceding_siblings<T>(self, arena: &Arena<T>) -> PrecedingSiblings<T> {
250 PrecedingSiblings {
251 arena: arena,
252 node: Some(self),
253 }
254 }
255
256 pub fn following_siblings<T>(self, arena: &Arena<T>) -> FollowingSiblings<T> {
260 FollowingSiblings {
261 arena: arena,
262 node: Some(self),
263 }
264 }
265
266 pub fn children<T>(self, arena: &Arena<T>) -> Children<T> {
268 Children {
269 arena: arena,
270 node: arena[self].first_child,
271 }
272 }
273
274 pub fn reverse_children<T>(self, arena: &Arena<T>) -> ReverseChildren<T> {
276 ReverseChildren {
277 arena: arena,
278 node: arena[self].last_child,
279 }
280 }
281
282 pub fn descendants<T>(self, arena: &Arena<T>) -> Descendants<T> {
287 Descendants(self.traverse(arena))
288 }
289
290 pub fn traverse<T>(self, arena: &Arena<T>) -> Traverse<T> {
292 Traverse {
293 arena: arena,
294 root: self,
295 next: Some(NodeEdge::Start(self)),
296 }
297 }
298
299 pub fn reverse_traverse<T>(self, arena: &Arena<T>) -> ReverseTraverse<T> {
301 ReverseTraverse {
302 arena: arena,
303 root: self,
304 next: Some(NodeEdge::End(self)),
305 }
306 }
307
308 pub fn detach<T>(self, arena: &mut Arena<T>) {
310 let (parent, previous_sibling, next_sibling) = {
311 let node = &mut arena[self];
312 (node.parent.take(), node.previous_sibling.take(), node.next_sibling.take())
313 };
314
315 if let Some(next_sibling) = next_sibling {
316 arena[next_sibling].previous_sibling = previous_sibling;
317 } else if let Some(parent) = parent {
318 arena[parent].last_child = previous_sibling;
319 }
320
321 if let Some(previous_sibling) = previous_sibling {
322 arena[previous_sibling].next_sibling = next_sibling;
323 } else if let Some(parent) = parent {
324 arena[parent].first_child = next_sibling;
325 }
326 }
327
328 pub fn append<T>(self, new_child: NodeId, arena: &mut Arena<T>) -> Result<(), IndexTreeError> {
330 new_child.detach(arena);
331 let last_child_opt;
332 {
333 let (self_borrow , new_child_borrow) = arena.get_pair_mut(&self,
334 &new_child)?;
335 new_child_borrow.parent = Some(self);
336 last_child_opt = mem::replace(&mut self_borrow.last_child, Some(new_child));
337 if let Some(last_child) = last_child_opt {
338 new_child_borrow.previous_sibling = Some(last_child);
339 } else {
340 debug_assert!(self_borrow.first_child.is_none());
341 self_borrow.first_child = Some(new_child);
342 }
343 }
344 if let Some(last_child) = last_child_opt {
345 debug_assert!(arena[last_child].next_sibling.is_none());
346 arena[last_child].next_sibling = Some(new_child);
347 }
348
349 Ok(())
350 }
351
352 pub fn prepend<T>(self, new_child: NodeId, arena: &mut Arena<T>) -> Result<(), IndexTreeError> {
354 new_child.detach(arena);
355 let first_child_opt;
356 {
357 let (self_borrow, new_child_borrow) = arena.get_pair_mut(&self,
358 &new_child)?;
359
360 new_child_borrow.parent = Some(self);
361 first_child_opt = mem::replace(&mut self_borrow.first_child, Some(new_child));
362 if let Some(first_child) = first_child_opt {
363 new_child_borrow.next_sibling = Some(first_child);
364 } else {
365 self_borrow.last_child = Some(new_child);
366 debug_assert!(&self_borrow.first_child.is_none());
367 }
368 }
369 if let Some(first_child) = first_child_opt {
370 debug_assert!(arena[first_child].previous_sibling.is_none());
371 arena[first_child].previous_sibling = Some(new_child);
372 }
373
374 Ok(())
375 }
376
377 pub fn insert_after<T>(self, new_sibling: NodeId, arena: &mut Arena<T>) -> Result<(), IndexTreeError> {
379 new_sibling.detach(arena);
380 let next_sibling_opt;
381 let parent_opt;
382 {
383 let (self_borrow, new_sibling_borrow) = arena.get_pair_mut(&self,
384 &new_sibling)?;
385
386 parent_opt = self_borrow.parent;
387 new_sibling_borrow.parent = parent_opt;
388 new_sibling_borrow.previous_sibling = Some(self);
389 next_sibling_opt = mem::replace(&mut self_borrow.next_sibling, Some(new_sibling));
390 if let Some(next_sibling) = next_sibling_opt {
391 new_sibling_borrow.next_sibling = Some(next_sibling);
392 }
393 }
394 if let Some(next_sibling) = next_sibling_opt {
395 debug_assert!(arena[next_sibling].previous_sibling.unwrap() == self);
396 arena[next_sibling].previous_sibling = Some(new_sibling);
397 } else if let Some(parent) = parent_opt {
398 debug_assert!(arena[parent].last_child.unwrap() == self);
399 arena[parent].last_child = Some(new_sibling);
400 }
401
402 Ok(())
403 }
404
405 pub fn insert_before<T>(self, new_sibling: NodeId, arena: &mut Arena<T>) -> Result<(), IndexTreeError> {
407 new_sibling.detach(arena);
408 let previous_sibling_opt;
409 let parent_opt;
410 {
411 let (self_borrow, new_sibling_borrow) = arena.get_pair_mut(&self,
412 &new_sibling)?;
413 parent_opt = self_borrow.parent;
414 new_sibling_borrow.parent = parent_opt;
415 new_sibling_borrow.next_sibling = Some(self);
416 previous_sibling_opt = mem::replace(&mut self_borrow.previous_sibling, Some(new_sibling));
417 if let Some(previous_sibling) = previous_sibling_opt {
418 new_sibling_borrow.previous_sibling = Some(previous_sibling);
419 }
420 }
421 if let Some(previous_sibling) = previous_sibling_opt {
422 debug_assert!(arena[previous_sibling].next_sibling.unwrap() == self);
423 arena[previous_sibling].next_sibling = Some(new_sibling);
424 } else if let Some(parent) = parent_opt {
425 debug_assert!(arena[parent].first_child.unwrap() == self);
426 arena[parent].first_child = Some(new_sibling);
427 }
428
429 Ok(())
430 }
431}
432
433macro_rules! impl_node_iterator {
434 ($name: ident, $next: expr) => {
435 impl<'a, T> Iterator for $name<'a, T> {
436 type Item = NodeId;
437
438 fn next(&mut self) -> Option<NodeId> {
439 match self.node.take() {
440 Some(node) => {
441 self.node = $next(&self.arena[node]);
442 Some(node)
443 }
444 None => None
445 }
446 }
447 }
448 }
449}
450
451pub struct Ancestors<'a, T: 'a> {
453 arena: &'a Arena<T>,
454 node: Option<NodeId>,
455}
456impl_node_iterator!(Ancestors, |node: &Node<T>| node.parent);
457
458pub struct PrecedingSiblings<'a, T: 'a> {
460 arena: &'a Arena<T>,
461 node: Option<NodeId>,
462}
463impl_node_iterator!(PrecedingSiblings, |node: &Node<T>| node.previous_sibling);
464
465pub struct FollowingSiblings<'a, T: 'a> {
467 arena: &'a Arena<T>,
468 node: Option<NodeId>,
469}
470impl_node_iterator!(FollowingSiblings, |node: &Node<T>| node.next_sibling);
471
472pub struct Children<'a, T: 'a> {
474 arena: &'a Arena<T>,
475 node: Option<NodeId>,
476}
477impl_node_iterator!(Children, |node: &Node<T>| node.next_sibling);
478
479pub struct ReverseChildren<'a, T: 'a> {
481 arena: &'a Arena<T>,
482 node: Option<NodeId>,
483}
484impl_node_iterator!(ReverseChildren, |node: &Node<T>| node.previous_sibling);
485
486pub struct Descendants<'a, T: 'a>(Traverse<'a, T>);
488
489impl<'a, T> Iterator for Descendants<'a, T> {
490 type Item = NodeId;
491
492 fn next(&mut self) -> Option<NodeId> {
493 loop {
494 match self.0.next() {
495 Some(NodeEdge::Start(node)) => return Some(node),
496 Some(NodeEdge::End(_)) => {}
497 None => return None,
498 }
499 }
500 }
501}
502
503#[derive(Debug, Clone)]
504pub enum NodeEdge<T> {
506 Start(T),
509
510 End(T),
513}
514
515
516pub struct Traverse<'a, T: 'a> {
518 arena: &'a Arena<T>,
519 root: NodeId,
520 next: Option<NodeEdge<NodeId>>,
521}
522
523impl<'a, T> Iterator for Traverse<'a, T> {
524 type Item = NodeEdge<NodeId>;
525
526 fn next(&mut self) -> Option<NodeEdge<NodeId>> {
527 match self.next.take() {
528 Some(item) => {
529 self.next = match item {
530 NodeEdge::Start(node) => {
531 match self.arena[node].first_child {
532 Some(first_child) => Some(NodeEdge::Start(first_child)),
533 None => Some(NodeEdge::End(node)),
534 }
535 }
536 NodeEdge::End(node) => {
537 if node == self.root {
538 None
539 } else {
540 match self.arena[node].next_sibling {
541 Some(next_sibling) => Some(NodeEdge::Start(next_sibling)),
542 None => {
543 match self.arena[node].parent {
544 Some(parent) => Some(NodeEdge::End(parent)),
545
546 None => None,
551 }
552 }
553 }
554 }
555 }
556 };
557 Some(item)
558 }
559 None => None,
560 }
561 }
562}
563
564pub struct ReverseTraverse<'a, T: 'a> {
566 arena: &'a Arena<T>,
567 root: NodeId,
568 next: Option<NodeEdge<NodeId>>,
569}
570
571impl<'a, T> Iterator for ReverseTraverse<'a, T> {
572 type Item = NodeEdge<NodeId>;
573
574 fn next(&mut self) -> Option<NodeEdge<NodeId>> {
575 match self.next.take() {
576 Some(item) => {
577 self.next = match item {
578 NodeEdge::End(node) => {
579 match self.arena[node].last_child {
580 Some(last_child) => Some(NodeEdge::End(last_child)),
581 None => Some(NodeEdge::Start(node)),
582 }
583 }
584 NodeEdge::Start(node) => {
585 if node == self.root {
586 None
587 } else {
588 match self.arena[node].previous_sibling {
589 Some(previous_sibling) => Some(NodeEdge::End(previous_sibling)),
590 None => {
591 match self.arena[node].parent {
592 Some(parent) => Some(NodeEdge::Start(parent)),
593
594 None => None,
599 }
600 }
601 }
602 }
603 }
604 };
605 Some(item)
606 }
607 None => None,
608 }
609 }
610}