liborigin2 29/08/2011

/build/liborigin2/src/liborigin2/tree.hh
Go to the documentation of this file.
1
2 // STL-like templated tree class.
3 //
4 // Copyright (C) 2001-2011 Kasper Peeters <kasper@phi-sci.com>
5 // Distributed under the GNU General Public License version 3.
6 //
7 // When used together with the htmlcxx library to create
8 // HTML::Node template instances, the GNU Lesser General Public
9 // version 2 applies. Special permission to use tree.hh under
10 // the LGPL for other projects can be requested from the author.
11
28 #ifndef tree_hh_
29 #define tree_hh_
30
31 #include <cassert>
32 #include <memory>
33 #include <stdexcept>
34 #include <iterator>
35 #include <set>
36 #include <queue>
37 #include <algorithm>
38 #include <cstddef>
39
40
42 template<class T>
43 class tree_node_ { // size: 5*4=20 bytes (on 32 bit arch), can be reduced by 8.
44 public:
45 tree_node_();
46 tree_node_(const T&);
47
48 tree_node_<T> *parent;
49 tree_node_<T> *first_child, *last_child;
50 tree_node_<T> *prev_sibling, *next_sibling;
51 T data;
52 }; // __attribute__((packed));
53
54 template<class T>
55 tree_node_<T>::tree_node_()
56 : parent(0), first_child(0), last_child(0), prev_sibling(0), next_sibling(0)
57 {
58 }
59
60 template<class T>
61 tree_node_<T>::tree_node_(const T& val)
62 : parent(0), first_child(0), last_child(0), prev_sibling(0), next_sibling(0), data(val)
63 {
64 }
65
66 template <class T, class tree_node_allocator = std::allocator<tree_node_<T> > >
67 class tree {
68 protected:
69 typedef tree_node_<T> tree_node;
70 public:
72 typedef T value_type;
73
74 class iterator_base;
75 class pre_order_iterator;
76 class post_order_iterator;
77 class sibling_iterator;
78 class leaf_iterator;
79
80 tree();
81 tree(const T&);
82 tree(const iterator_base&);
83 tree(const tree<T, tree_node_allocator>&);
84 ~tree();
85 tree<T,tree_node_allocator>& operator=(const tree<T, tree_node_allocator>&);
86
88 #ifdef __SGI_STL_PORT
89 class iterator_base : public stlport::bidirectional_iterator<T, ptrdiff_t> {
90 #else
91 class iterator_base {
92 #endif
93 public:
94 typedef T value_type;
95 typedef T* pointer;
96 typedef T& reference;
97 typedef size_t size_type;
98 typedef ptrdiff_t difference_type;
99 typedef std::bidirectional_iterator_tag iterator_category;
100
101 iterator_base();
102 iterator_base(tree_node *);
103
104 T& operator*() const;
105 T* operator->() const;
106
108 void skip_children();
109 void skip_children(bool skip);
111 unsigned int number_of_children() const;
112
113 sibling_iterator begin() const;
114 sibling_iterator end() const;
115
116 tree_node *node;
117 protected:
118 bool skip_current_children_;
119 };
120
122 class pre_order_iterator : public iterator_base {
123 public:
124 pre_order_iterator();
125 pre_order_iterator(tree_node *);
126 pre_order_iterator(const iterator_base&);
127 pre_order_iterator(const sibling_iterator&);
128
129 bool operator==(const pre_order_iterator&) const;
130 bool operator!=(const pre_order_iterator&) const;
131 pre_order_iterator& operator++();
132 pre_order_iterator& operator--();
133 pre_order_iterator operator++(int);
134 pre_order_iterator operator--(int);
135 pre_order_iterator& operator+=(unsigned int);
136 pre_order_iterator& operator-=(unsigned int);
137 };
138
140 class post_order_iterator : public iterator_base {
141 public:
142 post_order_iterator();
143 post_order_iterator(tree_node *);
144 post_order_iterator(const iterator_base&);
145 post_order_iterator(const sibling_iterator&);
146
147 bool operator==(const post_order_iterator&) const;
148 bool operator!=(const post_order_iterator&) const;
149 post_order_iterator& operator++();
150 post_order_iterator& operator--();
151 post_order_iterator operator++(int);
152 post_order_iterator operator--(int);
153 post_order_iterator& operator+=(unsigned int);
154 post_order_iterator& operator-=(unsigned int);
155
157 void descend_all();
158 };
159
161 class breadth_first_queued_iterator : public iterator_base {
162 public:
163 breadth_first_queued_iterator();
164 breadth_first_queued_iterator(tree_node *);
165 breadth_first_queued_iterator(const iterator_base&);
166
167 bool operator==(const breadth_first_queued_iterator&) const;
168 bool operator!=(const breadth_first_queued_iterator&) const;
169 breadth_first_queued_iterator& operator++();
170 breadth_first_queued_iterator operator++(int);
171 breadth_first_queued_iterator& operator+=(unsigned int);
172
173 private:
174 std::queue<tree_node *> traversal_queue;
175 };
176
178 typedef pre_order_iterator iterator;
179 typedef breadth_first_queued_iterator breadth_first_iterator;
180
182 class fixed_depth_iterator : public iterator_base {
183 public:
184 fixed_depth_iterator();
185 fixed_depth_iterator(tree_node *);
186 fixed_depth_iterator(const iterator_base&);
187 fixed_depth_iterator(const sibling_iterator&);
188 fixed_depth_iterator(const fixed_depth_iterator&);
189
190 bool operator==(const fixed_depth_iterator&) const;
191 bool operator!=(const fixed_depth_iterator&) const;
192 fixed_depth_iterator& operator++();
193 fixed_depth_iterator& operator--();
194 fixed_depth_iterator operator++(int);
195 fixed_depth_iterator operator--(int);
196 fixed_depth_iterator& operator+=(unsigned int);
197 fixed_depth_iterator& operator-=(unsigned int);
198
199 tree_node *top_node;
200 };
201
203 class sibling_iterator : public iterator_base {
204 public:
205 sibling_iterator();
206 sibling_iterator(tree_node *);
207 sibling_iterator(const sibling_iterator&);
208 sibling_iterator(const iterator_base&);
209
210 bool operator==(const sibling_iterator&) const;
211 bool operator!=(const sibling_iterator&) const;
212 sibling_iterator& operator++();
213 sibling_iterator& operator--();
214 sibling_iterator operator++(int);
215 sibling_iterator operator--(int);
216 sibling_iterator& operator+=(unsigned int);
217 sibling_iterator& operator-=(unsigned int);
218
219 tree_node *range_first() const;
220 tree_node *range_last() const;
221 tree_node *parent_;
222 private:
223 void set_parent_();
224 };
225
227 class leaf_iterator : public iterator_base {
228 public:
229 leaf_iterator();
230 leaf_iterator(tree_node *, tree_node *top=0);
231 leaf_iterator(const sibling_iterator&);
232 leaf_iterator(const iterator_base&);
233
234 bool operator==(const leaf_iterator&) const;
235 bool operator!=(const leaf_iterator&) const;
236 leaf_iterator& operator++();
237 leaf_iterator& operator--();
238 leaf_iterator operator++(int);
239 leaf_iterator operator--(int);
240 leaf_iterator& operator+=(unsigned int);
241 leaf_iterator& operator-=(unsigned int);
242 private:
243 tree_node *top_node;
244 };
245
247 inline pre_order_iterator begin() const;
249 inline pre_order_iterator end() const;
251 post_order_iterator begin_post() const;
253 post_order_iterator end_post() const;
255 fixed_depth_iterator begin_fixed(const iterator_base&, unsigned int) const;
257 fixed_depth_iterator end_fixed(const iterator_base&, unsigned int) const;
259 breadth_first_queued_iterator begin_breadth_first() const;
261 breadth_first_queued_iterator end_breadth_first() const;
263 sibling_iterator begin(const iterator_base&) const;
265 sibling_iterator end(const iterator_base&) const;
267 leaf_iterator begin_leaf() const;
269 leaf_iterator end_leaf() const;
271 leaf_iterator begin_leaf(const iterator_base& top) const;
273 leaf_iterator end_leaf(const iterator_base& top) const;
274
276 template<typename iter> static iter parent(iter);
278 template<typename iter> iter previous_sibling(iter) const;
280 template<typename iter> iter next_sibling(iter) const;
282 template<typename iter> iter next_at_same_depth(iter) const;
283
285 void clear();
287 template<typename iter> iter erase(iter);
289 void erase_children(const iterator_base&);
290
292 template<typename iter> iter append_child(iter position);
293 template<typename iter> iter prepend_child(iter position);
295 template<typename iter> iter append_child(iter position, const T& x);
296 template<typename iter> iter prepend_child(iter position, const T& x);
298 template<typename iter> iter append_child(iter position, iter other_position);
299 template<typename iter> iter prepend_child(iter position, iter other_position);
301 template<typename iter> iter append_children(iter position, sibling_iterator from, sibling_iterator to);
302 template<typename iter> iter prepend_children(iter position, sibling_iterator from, sibling_iterator to);
303
305 pre_order_iterator set_head(const T& x);
307 template<typename iter> iter insert(iter position, const T& x);
309 sibling_iterator insert(sibling_iterator position, const T& x);
311 template<typename iter> iter insert_subtree(iter position, const iterator_base& subtree);
313 template<typename iter> iter insert_after(iter position, const T& x);
315 template<typename iter> iter insert_subtree_after(iter position, const iterator_base& subtree);
316
318 template<typename iter> iter replace(iter position, const T& x);
320 template<typename iter> iter replace(iter position, const iterator_base& from);
322 sibling_iterator replace(sibling_iterator orig_begin, sibling_iterator orig_end,
323 sibling_iterator new_begin, sibling_iterator new_end);
324
326 template<typename iter> iter flatten(iter position);
328 template<typename iter> iter reparent(iter position, sibling_iterator begin, sibling_iterator end);
330 template<typename iter> iter reparent(iter position, iter from);
331
333 template<typename iter> iter wrap(iter position, const T& x);
334
336 template<typename iter> iter move_after(iter target, iter source);
338 template<typename iter> iter move_before(iter target, iter source);
339 sibling_iterator move_before(sibling_iterator target, sibling_iterator source);
341 template<typename iter> iter move_ontop(iter target, iter source);
342
344 void merge(sibling_iterator, sibling_iterator, sibling_iterator, sibling_iterator,
345 bool duplicate_leaves=false);
347 void sort(sibling_iterator from, sibling_iterator to, bool deep=false);
348 template<class StrictWeakOrdering>
349 void sort(sibling_iterator from, sibling_iterator to, StrictWeakOrdering comp, bool deep=false);
351 template<typename iter>
352 bool equal(const iter& one, const iter& two, const iter& three) const;
353 template<typename iter, class BinaryPredicate>
354 bool equal(const iter& one, const iter& two, const iter& three, BinaryPredicate) const;
355 template<typename iter>
356 bool equal_subtree(const iter& one, const iter& two) const;
357 template<typename iter, class BinaryPredicate>
358 bool equal_subtree(const iter& one, const iter& two, BinaryPredicate) const;
360 tree subtree(sibling_iterator from, sibling_iterator to) const;
361 void subtree(tree&, sibling_iterator from, sibling_iterator to) const;
363 void swap(sibling_iterator it);
365 void swap(iterator, iterator);
366
368 size_t size() const;
370 size_t size(const iterator_base&) const;
372 bool empty() const;
374 static int depth(const iterator_base&);
375 static int depth(const iterator_base&, const iterator_base&);
377 int max_depth() const;
379 int max_depth(const iterator_base&) const;
381 static unsigned int number_of_children(const iterator_base&);
383 unsigned int number_of_siblings(const iterator_base&) const;
385 bool is_in_subtree(const iterator_base& position, const iterator_base& begin,
386 const iterator_base& end) const;
388 bool is_valid(const iterator_base&) const;
391 iterator lowest_common_ancestor(const iterator_base&, const iterator_base &) const;
392
394 unsigned int index(sibling_iterator it) const;
396 static sibling_iterator child(const iterator_base& position, unsigned int);
398 sibling_iterator sibling(const iterator_base& position, unsigned int);
399
402 void debug_verify_consistency() const;
403
405 class iterator_base_less {
406 public:
407 bool operator()(const typename tree<T, tree_node_allocator>::iterator_base& one,
408 const typename tree<T, tree_node_allocator>::iterator_base& two) const
409 {
410 return one.node < two.node;
411 }
412 };
413 tree_node *head, *feet; // head/feet are always dummy; if an iterator points to them it is invalid
414 private:
415 tree_node_allocator alloc_;
416 void head_initialise_();
417 void copy_(const tree<T, tree_node_allocator>& other);
418
420 template<class StrictWeakOrdering>
421 class compare_nodes {
422 public:
423 compare_nodes(StrictWeakOrdering comp) : comp_(comp) {};
424
425 bool operator()(const tree_node *a, const tree_node *b)
426 {
427 return comp_(a->data, b->data);
428 }
429 private:
430 StrictWeakOrdering comp_;
431 };
432 };
433
434 //template <class T, class tree_node_allocator>
435 //class iterator_base_less {
436 // public:
437 // bool operator()(const typename tree<T, tree_node_allocator>::iterator_base& one,
438 // const typename tree<T, tree_node_allocator>::iterator_base& two) const
439 // {
440 // txtout << "operatorclass<" << one.node < two.node << std::endl;
441 // return one.node < two.node;
442 // }
443 //};
444
445 // template <class T, class tree_node_allocator>
446 // bool operator<(const typename tree<T, tree_node_allocator>::iterator& one,
447 // const typename tree<T, tree_node_allocator>::iterator& two)
448 // {
449 // txtout << "operator< " << one.node < two.node << std::endl;
450 // if(one.node < two.node) return true;
451 // return false;
452 // }
453 //
454 // template <class T, class tree_node_allocator>
455 // bool operator==(const typename tree<T, tree_node_allocator>::iterator& one,
456 // const typename tree<T, tree_node_allocator>::iterator& two)
457 // {
458 // txtout << "operator== " << one.node == two.node << std::endl;
459 // if(one.node == two.node) return true;
460 // return false;
461 // }
462 //
463 // template <class T, class tree_node_allocator>
464 // bool operator>(const typename tree<T, tree_node_allocator>::iterator_base& one,
465 // const typename tree<T, tree_node_allocator>::iterator_base& two)
466 // {
467 // txtout << "operator> " << one.node < two.node << std::endl;
468 // if(one.node > two.node) return true;
469 // return false;
470 // }
471
472
473
474 // Tree
475
476 template <class T, class tree_node_allocator>
477 tree<T, tree_node_allocator>::tree()
478 {
479 head_initialise_();
480 }
481
482 template <class T, class tree_node_allocator>
483 tree<T, tree_node_allocator>::tree(const T& x)
484 {
485 head_initialise_();
486 set_head(x);
487 }
488
489 template <class T, class tree_node_allocator>
490 tree<T, tree_node_allocator>::tree(const iterator_base& other)
491 {
492 head_initialise_();
493 set_head((*other));
494 replace(begin(), other);
495 }
496
497 template <class T, class tree_node_allocator>
498 tree<T, tree_node_allocator>::~tree()
499 {
500 clear();
501 alloc_.destroy(head);
502 alloc_.destroy(feet);
503 alloc_.deallocate(head,1);
504 alloc_.deallocate(feet,1);
505 }
506
507 template <class T, class tree_node_allocator>
508 void tree<T, tree_node_allocator>::head_initialise_()
509 {
510 head = alloc_.allocate(1,0); // MSVC does not have default second argument
511 feet = alloc_.allocate(1,0);
512 alloc_.construct(head, tree_node_<T>());
513 alloc_.construct(feet, tree_node_<T>());
514
515 head->parent=0;
516 head->first_child=0;
517 head->last_child=0;
518 head->prev_sibling=0; //head;
519 head->next_sibling=feet; //head;
520
521 feet->parent=0;
522 feet->first_child=0;
523 feet->last_child=0;
524 feet->prev_sibling=head;
525 feet->next_sibling=0;
526 }
527
528 template <class T, class tree_node_allocator>
529 tree<T,tree_node_allocator>& tree<T, tree_node_allocator>::operator=(const tree<T, tree_node_allocator>& other)
530 {
531 if(this != &other)
532 copy_(other);
533 return *this;
534 }
535
536 template <class T, class tree_node_allocator>
537 tree<T, tree_node_allocator>::tree(const tree<T, tree_node_allocator>& other)
538 {
539 head_initialise_();
540 copy_(other);
541 }
542
543 template <class T, class tree_node_allocator>
544 void tree<T, tree_node_allocator>::copy_(const tree<T, tree_node_allocator>& other)
545 {
546 clear();
547 pre_order_iterator it=other.begin(), to=begin();
548 while(it!=other.end()) {
549 to=insert(to, (*it));
550 it.skip_children();
551 ++it;
552 }
553 to=begin();
554 it=other.begin();
555 while(it!=other.end()) {
556 to=replace(to, it);
557 to.skip_children();
558 it.skip_children();
559 ++to;
560 ++it;
561 }
562 }
563
564 template <class T, class tree_node_allocator>
565 void tree<T, tree_node_allocator>::clear()
566 {
567 if(head)
568 while(head->next_sibling!=feet)
569 erase(pre_order_iterator(head->next_sibling));
570 }
571
572 template<class T, class tree_node_allocator>
573 void tree<T, tree_node_allocator>::erase_children(const iterator_base& it)
574 {
575 // std::cout << "erase_children " << it.node << std::endl;
576 if(it.node==0) return;
577
578 tree_node *cur=it.node->first_child;
579 tree_node *prev=0;
580
581 while(cur!=0) {
582 prev=cur;
583 cur=cur->next_sibling;
584 erase_children(pre_order_iterator(prev));
585 // kp::destructor(&prev->data);
586 alloc_.destroy(prev);
587 alloc_.deallocate(prev,1);
588 }
589 it.node->first_child=0;
590 it.node->last_child=0;
591 // std::cout << "exit" << std::endl;
592 }
593
594 template<class T, class tree_node_allocator>
595 template<class iter>
596 iter tree<T, tree_node_allocator>::erase(iter it)
597 {
598 tree_node *cur=it.node;
599 assert(cur!=head);
600 iter ret=it;
601 ret.skip_children();
602 ++ret;
603 erase_children(it);
604 if(cur->prev_sibling==0) {
605 cur->parent->first_child=cur->next_sibling;
606 }
607 else {
608 cur->prev_sibling->next_sibling=cur->next_sibling;
609 }
610 if(cur->next_sibling==0) {
611 cur->parent->last_child=cur->prev_sibling;
612 }
613 else {
614 cur->next_sibling->prev_sibling=cur->prev_sibling;
615 }
616
617 // kp::destructor(&cur->data);
618 alloc_.destroy(cur);
619 alloc_.deallocate(cur,1);
620 return ret;
621 }
622
623 template <class T, class tree_node_allocator>
624 typename tree<T, tree_node_allocator>::pre_order_iterator tree<T, tree_node_allocator>::begin() const
625 {
626 return pre_order_iterator(head->next_sibling);
627 }
628
629 template <class T, class tree_node_allocator>
630 typename tree<T, tree_node_allocator>::pre_order_iterator tree<T, tree_node_allocator>::end() const
631 {
632 return pre_order_iterator(feet);
633 }
634
635 template <class T, class tree_node_allocator>
636 typename tree<T, tree_node_allocator>::breadth_first_queued_iterator tree<T, tree_node_allocator>::begin_breadth_first() const
637 {
638 return breadth_first_queued_iterator(head->next_sibling);
639 }
640
641 template <class T, class tree_node_allocator>
642 typename tree<T, tree_node_allocator>::breadth_first_queued_iterator tree<T, tree_node_allocator>::end_breadth_first() const
643 {
644 return breadth_first_queued_iterator();
645 }
646
647 template <class T, class tree_node_allocator>
648 typename tree<T, tree_node_allocator>::post_order_iterator tree<T, tree_node_allocator>::begin_post() const
649 {
650 tree_node *tmp=head->next_sibling;
651 if(tmp!=feet) {
652 while(tmp->first_child)
653 tmp=tmp->first_child;
654 }
655 return post_order_iterator(tmp);
656 }
657
658 template <class T, class tree_node_allocator>
659 typename tree<T, tree_node_allocator>::post_order_iterator tree<T, tree_node_allocator>::end_post() const
660 {
661 return post_order_iterator(feet);
662 }
663
664 template <class T, class tree_node_allocator>
665 typename tree<T, tree_node_allocator>::fixed_depth_iterator tree<T, tree_node_allocator>::begin_fixed(const iterator_base& pos, unsigned int dp) const
666 {
667 typename tree<T, tree_node_allocator>::fixed_depth_iterator ret;
668 ret.top_node=pos.node;
669
670 tree_node *tmp=pos.node;
671 unsigned int curdepth=0;
672 while(curdepth<dp) { // go down one level
673 while(tmp->first_child==0) {
674 if(tmp->next_sibling==0) {
675 // try to walk up and then right again
676 do {
677 if(tmp==ret.top_node)
678 throw std::range_error("tree: begin_fixed out of range");
679 tmp=tmp->parent;
680 if(tmp==0)
681 throw std::range_error("tree: begin_fixed out of range");
682 --curdepth;
683 } while(tmp->next_sibling==0);
684 }
685 tmp=tmp->next_sibling;
686 }
687 tmp=tmp->first_child;
688 ++curdepth;
689 }
690
691 ret.node=tmp;
692 return ret;
693 }
694
695 template <class T, class tree_node_allocator>
696 typename tree<T, tree_node_allocator>::fixed_depth_iterator tree<T, tree_node_allocator>::end_fixed(const iterator_base& pos, unsigned int dp) const
697 {
698 assert(1==0); // FIXME: not correct yet: use is_valid() as a temporary workaround
699 tree_node *tmp=pos.node;
700 unsigned int curdepth=1;
701 while(curdepth<dp) { // go down one level
702 while(tmp->first_child==0) {
703 tmp=tmp->next_sibling;
704 if(tmp==0)
705 throw std::range_error("tree: end_fixed out of range");
706 }
707 tmp=tmp->first_child;
708 ++curdepth;
709 }
710 return tmp;
711 }
712
713 template <class T, class tree_node_allocator>
714 typename tree<T, tree_node_allocator>::sibling_iterator tree<T, tree_node_allocator>::begin(const iterator_base& pos) const
715 {
716 assert(pos.node!=0);
717 if(pos.node->first_child==0) {
718 return end(pos);
719 }
720 return pos.node->first_child;
721 }
722
723 template <class T, class tree_node_allocator>
724 typename tree<T, tree_node_allocator>::sibling_iterator tree<T, tree_node_allocator>::end(const iterator_base& pos) const
725 {
726 sibling_iterator ret(0);
727 ret.parent_=pos.node;
728 return ret;
729 }
730
731 template <class T, class tree_node_allocator>
732 typename tree<T, tree_node_allocator>::leaf_iterator tree<T, tree_node_allocator>::begin_leaf() const
733 {
734 tree_node *tmp=head->next_sibling;
735 if(tmp!=feet) {
736 while(tmp->first_child)
737 tmp=tmp->first_child;
738 }
739 return leaf_iterator(tmp);
740 }
741
742 template <class T, class tree_node_allocator>
743 typename tree<T, tree_node_allocator>::leaf_iterator tree<T, tree_node_allocator>::end_leaf() const
744 {
745 return leaf_iterator(feet);
746 }
747
748 template <class T, class tree_node_allocator>
749 typename tree<T, tree_node_allocator>::leaf_iterator tree<T, tree_node_allocator>::begin_leaf(const iterator_base& top) const
750 {
751 tree_node *tmp=top.node;
752 while(tmp->first_child)
753 tmp=tmp->first_child;
754 return leaf_iterator(tmp, top.node);
755 }
756
757 template <class T, class tree_node_allocator>
758 typename tree<T, tree_node_allocator>::leaf_iterator tree<T, tree_node_allocator>::end_leaf(const iterator_base& top) const
759 {
760 return leaf_iterator(top.node, top.node);
761 }
762
763 template <class T, class tree_node_allocator>
764 template <typename iter>
765 iter tree<T, tree_node_allocator>::parent(iter position)
766 {
767 assert(position.node!=0);
768 return iter(position.node->parent);
769 }
770
771 template <class T, class tree_node_allocator>
772 template <typename iter>
773 iter tree<T, tree_node_allocator>::previous_sibling(iter position) const
774 {
775 assert(position.node!=0);
776 iter ret(position);
777 ret.node=position.node->prev_sibling;
778 return ret;
779 }
780
781 template <class T, class tree_node_allocator>
782 template <typename iter>
783 iter tree<T, tree_node_allocator>::next_sibling(iter position) const
784 {
785 assert(position.node!=0);
786 iter ret(position);
787 ret.node=position.node->next_sibling;
788 return ret;
789 }
790
791 template <class T, class tree_node_allocator>
792 template <typename iter>
793 iter tree<T, tree_node_allocator>::next_at_same_depth(iter position) const
794 {
795 // We make use of a temporary fixed_depth iterator to implement this.
796
797 typename tree<T, tree_node_allocator>::fixed_depth_iterator tmp(position.node);
798
799 ++tmp;
800 return iter(tmp);
801
802 // assert(position.node!=0);
803 // iter ret(position);
804 //
805 // if(position.node->next_sibling) {
806 // ret.node=position.node->next_sibling;
807 // }
808 // else {
809 // int relative_depth=0;
810 // upper:
811 // do {
812 // ret.node=ret.node->parent;
813 // if(ret.node==0) return ret;
814 // --relative_depth;
815 // } while(ret.node->next_sibling==0);
816 // lower:
817 // ret.node=ret.node->next_sibling;
818 // while(ret.node->first_child==0) {
819 // if(ret.node->next_sibling==0)
820 // goto upper;
821 // ret.node=ret.node->next_sibling;
822 // if(ret.node==0) return ret;
823 // }
824 // while(relative_depth<0 && ret.node->first_child!=0) {
825 // ret.node=ret.node->first_child;
826 // ++relative_depth;
827 // }
828 // if(relative_depth<0) {
829 // if(ret.node->next_sibling==0) goto upper;
830 // else goto lower;
831 // }
832 // }
833 // return ret;
834 }
835
836 template <class T, class tree_node_allocator>
837 template <typename iter>
838 iter tree<T, tree_node_allocator>::append_child(iter position)
839 {
840 assert(position.node!=head);
841 assert(position.node!=feet);
842 assert(position.node);
843
844 tree_node *tmp=alloc_.allocate(1,0);
845 alloc_.construct(tmp, tree_node_<T>());
846 // kp::constructor(&tmp->data);
847 tmp->first_child=0;
848 tmp->last_child=0;
849
850 tmp->parent=position.node;
851 if(position.node->last_child!=0) {
852 position.node->last_child->next_sibling=tmp;
853 }
854 else {
855 position.node->first_child=tmp;
856 }
857 tmp->prev_sibling=position.node->last_child;
858 position.node->last_child=tmp;
859 tmp->next_sibling=0;
860 return tmp;
861 }
862
863 template <class T, class tree_node_allocator>
864 template <typename iter>
865 iter tree<T, tree_node_allocator>::prepend_child(iter position)
866 {
867 assert(position.node!=head);
868 assert(position.node!=feet);
869 assert(position.node);
870
871 tree_node *tmp=alloc_.allocate(1,0);
872 alloc_.construct(tmp, tree_node_<T>());
873 // kp::constructor(&tmp->data);
874 tmp->first_child=0;
875 tmp->last_child=0;
876
877 tmp->parent=position.node;
878 if(position.node->first_child!=0) {
879 position.node->first_child->prev_sibling=tmp;
880 }
881 else {
882 position.node->last_child=tmp;
883 }
884 tmp->next_sibling=position.node->first_child;
885 position.node->prev_child=tmp;
886 tmp->prev_sibling=0;
887 return tmp;
888 }
889
890 template <class T, class tree_node_allocator>
891 template <class iter>
892 iter tree<T, tree_node_allocator>::append_child(iter position, const T& x)
893 {
894 // If your program fails here you probably used 'append_child' to add the top
895 // node to an empty tree. From version 1.45 the top element should be added
896 // using 'insert'. See the documentation for further information, and sorry about
897 // the API change.
898 assert(position.node!=head);
899 assert(position.node!=feet);
900 assert(position.node);
901
902 tree_node* tmp = alloc_.allocate(1,0);
903 alloc_.construct(tmp, x);
904 // kp::constructor(&tmp->data, x);
905 tmp->first_child=0;
906 tmp->last_child=0;
907
908 tmp->parent=position.node;
909 if(position.node->last_child!=0) {
910 position.node->last_child->next_sibling=tmp;
911 }
912 else {
913 position.node->first_child=tmp;
914 }
915 tmp->prev_sibling=position.node->last_child;
916 position.node->last_child=tmp;
917 tmp->next_sibling=0;
918 return tmp;
919 }
920
921 template <class T, class tree_node_allocator>
922 template <class iter>
923 iter tree<T, tree_node_allocator>::prepend_child(iter position, const T& x)
924 {
925 assert(position.node!=head);
926 assert(position.node!=feet);
927 assert(position.node);
928
929 tree_node* tmp = alloc_.allocate(1,0);
930 alloc_.construct(tmp, x);
931 // kp::constructor(&tmp->data, x);
932 tmp->first_child=0;
933 tmp->last_child=0;
934
935 tmp->parent=position.node;
936 if(position.node->first_child!=0) {
937 position.node->first_child->prev_sibling=tmp;
938 }
939 else {
940 position.node->last_child=tmp;
941 }
942 tmp->next_sibling=position.node->first_child;
943 position.node->first_child=tmp;
944 tmp->prev_sibling=0;
945 return tmp;
946 }
947
948 template <class T, class tree_node_allocator>
949 template <class iter>
950 iter tree<T, tree_node_allocator>::append_child(iter position, iter other)
951 {
952 assert(position.node!=head);
953 assert(position.node!=feet);
954 assert(position.node);
955
956 sibling_iterator aargh=append_child(position, value_type());
957 return replace(aargh, other);
958 }
959
960 template <class T, class tree_node_allocator>
961 template <class iter>
962 iter tree<T, tree_node_allocator>::prepend_child(iter position, iter other)
963 {
964 assert(position.node!=head);
965 assert(position.node!=feet);
966 assert(position.node);
967
968 sibling_iterator aargh=prepend_child(position, value_type());
969 return replace(aargh, other);
970 }
971
972 template <class T, class tree_node_allocator>
973 template <class iter>
974 iter tree<T, tree_node_allocator>::append_children(iter position, sibling_iterator from, sibling_iterator to)
975 {
976 assert(position.node!=head);
977 assert(position.node!=feet);
978 assert(position.node);
979
980 iter ret=from;
981
982 while(from!=to) {
983 insert_subtree(position.end(), from);
984 ++from;
985 }
986 return ret;
987 }
988
989 template <class T, class tree_node_allocator>
990 template <class iter>
991 iter tree<T, tree_node_allocator>::prepend_children(iter position, sibling_iterator from, sibling_iterator to)
992 {
993 assert(position.node!=head);
994 assert(position.node!=feet);
995 assert(position.node);
996
997 iter ret=from;
998
999 while(from!=to) {
1000 insert_subtree(position.begin(), from);
1001 ++from;
1002 }
1003 return ret;
1004 }
1005
1006 template <class T, class tree_node_allocator>
1007 typename tree<T, tree_node_allocator>::pre_order_iterator tree<T, tree_node_allocator>::set_head(const T& x)
1008 {
1009 assert(head->next_sibling==feet);
1010 return insert(iterator(feet), x);
1011 }
1012
1013 template <class T, class tree_node_allocator>
1014 template <class iter>
1015 iter tree<T, tree_node_allocator>::insert(iter position, const T& x)
1016 {
1017 if(position.node==0) {
1018 position.node=feet; // Backward compatibility: when calling insert on a null node,
1019 // insert before the feet.
1020 }
1021 tree_node* tmp = alloc_.allocate(1,0);
1022 alloc_.construct(tmp, x);
1023 // kp::constructor(&tmp->data, x);
1024 tmp->first_child=0;
1025 tmp->last_child=0;
1026
1027 tmp->parent=position.node->parent;
1028 tmp->next_sibling=position.node;
1029 tmp->prev_sibling=position.node->prev_sibling;
1030 position.node->prev_sibling=tmp;
1031
1032 if(tmp->prev_sibling==0) {
1033 if(tmp->parent) // when inserting nodes at the head, there is no parent
1034 tmp->parent->first_child=tmp;
1035 }
1036 else
1037 tmp->prev_sibling->next_sibling=tmp;
1038 return tmp;
1039 }
1040
1041 template <class T, class tree_node_allocator>
1042 typename tree<T, tree_node_allocator>::sibling_iterator tree<T, tree_node_allocator>::insert(sibling_iterator position, const T& x)
1043 {
1044 tree_node* tmp = alloc_.allocate(1,0);
1045 alloc_.construct(tmp, x);
1046 // kp::constructor(&tmp->data, x);
1047 tmp->first_child=0;
1048 tmp->last_child=0;
1049
1050 tmp->next_sibling=position.node;
1051 if(position.node==0) { // iterator points to end of a subtree
1052 tmp->parent=position.parent_;
1053 tmp->prev_sibling=position.range_last();
1054 tmp->parent->last_child=tmp;
1055 }
1056 else {
1057 tmp->parent=position.node->parent;
1058 tmp->prev_sibling=position.node->prev_sibling;
1059 position.node->prev_sibling=tmp;
1060 }
1061
1062 if(tmp->prev_sibling==0) {
1063 if(tmp->parent) // when inserting nodes at the head, there is no parent
1064 tmp->parent->first_child=tmp;
1065 }
1066 else
1067 tmp->prev_sibling->next_sibling=tmp;
1068 return tmp;
1069 }
1070
1071 template <class T, class tree_node_allocator>
1072 template <class iter>
1073 iter tree<T, tree_node_allocator>::insert_after(iter position, const T& x)
1074 {
1075 tree_node* tmp = alloc_.allocate(1,0);
1076 alloc_.construct(tmp, x);
1077 // kp::constructor(&tmp->data, x);
1078 tmp->first_child=0;
1079 tmp->last_child=0;
1080
1081 tmp->parent=position.node->parent;
1082 tmp->prev_sibling=position.node;
1083 tmp->next_sibling=position.node->next_sibling;
1084 position.node->next_sibling=tmp;
1085
1086 if(tmp->next_sibling==0) {
1087 if(tmp->parent) // when inserting nodes at the head, there is no parent
1088 tmp->parent->last_child=tmp;
1089 }
1090 else {
1091 tmp->next_sibling->prev_sibling=tmp;
1092 }
1093 return tmp;
1094 }
1095
1096 template <class T, class tree_node_allocator>
1097 template <class iter>
1098 iter tree<T, tree_node_allocator>::insert_subtree(iter position, const iterator_base& subtree)
1099 {
1100 // insert dummy
1101 iter it=insert(position, value_type());
1102 // replace dummy with subtree
1103 return replace(it, subtree);
1104 }
1105
1106 template <class T, class tree_node_allocator>
1107 template <class iter>
1108 iter tree<T, tree_node_allocator>::insert_subtree_after(iter position, const iterator_base& subtree)
1109 {
1110 // insert dummy
1111 iter it=insert_after(position, value_type());
1112 // replace dummy with subtree
1113 return replace(it, subtree);
1114 }
1115
1116 // template <class T, class tree_node_allocator>
1117 // template <class iter>
1118 // iter tree<T, tree_node_allocator>::insert_subtree(sibling_iterator position, iter subtree)
1119 // {
1120 // // insert dummy
1121 // iter it(insert(position, value_type()));
1122 // // replace dummy with subtree
1123 // return replace(it, subtree);
1124 // }
1125
1126 template <class T, class tree_node_allocator>
1127 template <class iter>
1128 iter tree<T, tree_node_allocator>::replace(iter position, const T& x)
1129 {
1130 // kp::destructor(&position.node->data);
1131 // kp::constructor(&position.node->data, x);
1132 position.node->data=x;
1133 // alloc_.destroy(position.node);
1134 // alloc_.construct(position.node, x);
1135 return position;
1136 }
1137
1138 template <class T, class tree_node_allocator>
1139 template <class iter>
1140 iter tree<T, tree_node_allocator>::replace(iter position, const iterator_base& from)
1141 {
1142 assert(position.node!=head);
1143 tree_node *current_from=from.node;
1144 tree_node *start_from=from.node;
1145 tree_node *current_to =position.node;
1146
1147 // replace the node at position with head of the replacement tree at from
1148 // std::cout << "warning!" << position.node << std::endl;
1149 erase_children(position);
1150 // std::cout << "no warning!" << std::endl;
1151 tree_node* tmp = alloc_.allocate(1,0);
1152 alloc_.construct(tmp, (*from));
1153 // kp::constructor(&tmp->data, (*from));
1154 tmp->first_child=0;
1155 tmp->last_child=0;
1156 if(current_to->prev_sibling==0) {
1157 if(current_to->parent!=0)
1158 current_to->parent->first_child=tmp;
1159 }
1160 else {
1161 current_to->prev_sibling->next_sibling=tmp;
1162 }
1163 tmp->prev_sibling=current_to->prev_sibling;
1164 if(current_to->next_sibling==0) {
1165 if(current_to->parent!=0)
1166 current_to->parent->last_child=tmp;
1167 }
1168 else {
1169 current_to->next_sibling->prev_sibling=tmp;
1170 }
1171 tmp->next_sibling=current_to->next_sibling;
1172 tmp->parent=current_to->parent;
1173 // kp::destructor(&current_to->data);
1174 alloc_.destroy(current_to);
1175 alloc_.deallocate(current_to,1);
1176 current_to=tmp;
1177
1178 // only at this stage can we fix 'last'
1179 tree_node *last=from.node->next_sibling;
1180
1181 pre_order_iterator toit=tmp;
1182 // copy all children
1183 do {
1184 assert(current_from!=0);
1185 if(current_from->first_child != 0) {
1186 current_from=current_from->first_child;
1187 toit=append_child(toit, current_from->data);
1188 }
1189 else {
1190 while(current_from->next_sibling==0 && current_from!=start_from) {
1191 current_from=current_from->parent;
1192 toit=parent(toit);
1193 assert(current_from!=0);
1194 }
1195 current_from=current_from->next_sibling;
1196 if(current_from!=last) {
1197 toit=append_child(parent(toit), current_from->data);
1198 }
1199 }
1200 } while(current_from!=last);
1201
1202 return current_to;
1203 }
1204
1205 template <class T, class tree_node_allocator>
1206 typename tree<T, tree_node_allocator>::sibling_iterator tree<T, tree_node_allocator>::replace(
1207 sibling_iterator orig_begin,
1208 sibling_iterator orig_end,
1209 sibling_iterator new_begin,
1210 sibling_iterator new_end)
1211 {
1212 tree_node *orig_first=orig_begin.node;
1213 tree_node *new_first=new_begin.node;
1214 tree_node *orig_last=orig_first;
1215 while((++orig_begin)!=orig_end)
1216 orig_last=orig_last->next_sibling;
1217 tree_node *new_last=new_first;
1218 while((++new_begin)!=new_end)
1219 new_last=new_last->next_sibling;
1220
1221 // insert all siblings in new_first..new_last before orig_first
1222 bool first=true;
1223 pre_order_iterator ret;
1224 while(1==1) {
1225 pre_order_iterator tt=insert_subtree(pre_order_iterator(orig_first), pre_order_iterator(new_first));
1226 if(first) {
1227 ret=tt;
1228 first=false;
1229 }
1230 if(new_first==new_last)
1231 break;
1232 new_first=new_first->next_sibling;
1233 }
1234
1235 // erase old range of siblings
1236 bool last=false;
1237 tree_node *next=orig_first;
1238 while(1==1) {
1239 if(next==orig_last)
1240 last=true;
1241 next=next->next_sibling;
1242 erase((pre_order_iterator)orig_first);
1243 if(last)
1244 break;
1245 orig_first=next;
1246 }
1247 return ret;
1248 }
1249
1250 template <class T, class tree_node_allocator>
1251 template <typename iter>
1252 iter tree<T, tree_node_allocator>::flatten(iter position)
1253 {
1254 if(position.node->first_child==0)
1255 return position;
1256
1257 tree_node *tmp=position.node->first_child;
1258 while(tmp) {
1259 tmp->parent=position.node->parent;
1260 tmp=tmp->next_sibling;
1261 }
1262 if(position.node->next_sibling) {
1263 position.node->last_child->next_sibling=position.node->next_sibling;
1264 position.node->next_sibling->prev_sibling=position.node->last_child;
1265 }
1266 else {
1267 position.node->parent->last_child=position.node->last_child;
1268 }
1269 position.node->next_sibling=position.node->first_child;
1270 position.node->next_sibling->prev_sibling=position.node;
1271 position.node->first_child=0;
1272 position.node->last_child=0;
1273
1274 return position;
1275 }
1276
1277
1278 template <class T, class tree_node_allocator>
1279 template <typename iter>
1280 iter tree<T, tree_node_allocator>::reparent(iter position, sibling_iterator begin, sibling_iterator end)
1281 {
1282 tree_node *first=begin.node;
1283 tree_node *last=first;
1284
1285 assert(first!=position.node);
1286
1287 if(begin==end) return begin;
1288 // determine last node
1289 while((++begin)!=end) {
1290 last=last->next_sibling;
1291 }
1292 // move subtree
1293 if(first->prev_sibling==0) {
1294 first->parent->first_child=last->next_sibling;
1295 }
1296 else {
1297 first->prev_sibling->next_sibling=last->next_sibling;
1298 }
1299 if(last->next_sibling==0) {
1300 last->parent->last_child=first->prev_sibling;
1301 }
1302 else {
1303 last->next_sibling->prev_sibling=first->prev_sibling;
1304 }
1305 if(position.node->first_child==0) {
1306 position.node->first_child=first;
1307 position.node->last_child=last;
1308 first->prev_sibling=0;
1309 }
1310 else {
1311 position.node->last_child->next_sibling=first;
1312 first->prev_sibling=position.node->last_child;
1313 position.node->last_child=last;
1314 }
1315 last->next_sibling=0;
1316
1317 tree_node *pos=first;
1318 for(;;) {
1319 pos->parent=position.node;
1320 if(pos==last) break;
1321 pos=pos->next_sibling;
1322 }
1323
1324 return first;
1325 }
1326
1327 template <class T, class tree_node_allocator>
1328 template <typename iter> iter tree<T, tree_node_allocator>::reparent(iter position, iter from)
1329 {
1330 if(from.node->first_child==0) return position;
1331 return reparent(position, from.node->first_child, end(from));
1332 }
1333
1334 template <class T, class tree_node_allocator>
1335 template <typename iter> iter tree<T, tree_node_allocator>::wrap(iter position, const T& x)
1336 {
1337 assert(position.node!=0);
1338 sibling_iterator fr=position, to=position;
1339 ++to;
1340 iter ret = insert(position, x);
1341 reparent(ret, fr, to);
1342 return ret;
1343 }
1344
1345 template <class T, class tree_node_allocator>
1346 template <typename iter> iter tree<T, tree_node_allocator>::move_after(iter target, iter source)
1347 {
1348 tree_node *dst=target.node;
1349 tree_node *src=source.node;
1350 assert(dst);
1351 assert(src);
1352
1353 if(dst==src) return source;
1354 if(dst->next_sibling)
1355 if(dst->next_sibling==src) // already in the right spot
1356 return source;
1357
1358 // take src out of the tree
1359 if(src->prev_sibling!=0) src->prev_sibling->next_sibling=src->next_sibling;
1360 else src->parent->first_child=src->next_sibling;
1361 if(src->next_sibling!=0) src->next_sibling->prev_sibling=src->prev_sibling;
1362 else src->parent->last_child=src->prev_sibling;
1363
1364 // connect it to the new point
1365 if(dst->next_sibling!=0) dst->next_sibling->prev_sibling=src;
1366 else dst->parent->last_child=src;
1367 src->next_sibling=dst->next_sibling;
1368 dst->next_sibling=src;
1369 src->prev_sibling=dst;
1370 src->parent=dst->parent;
1371 return src;
1372 }
1373
1374 template <class T, class tree_node_allocator>
1375 template <typename iter> iter tree<T, tree_node_allocator>::move_before(iter target, iter source)
1376 {
1377 tree_node *dst=target.node;
1378 tree_node *src=source.node;
1379 assert(dst);
1380 assert(src);
1381
1382 if(dst==src) return source;
1383 if(dst->prev_sibling)
1384 if(dst->prev_sibling==src) // already in the right spot
1385 return source;
1386
1387 // take src out of the tree
1388 if(src->prev_sibling!=0) src->prev_sibling->next_sibling=src->next_sibling;
1389 else src->parent->first_child=src->next_sibling;
1390 if(src->next_sibling!=0) src->next_sibling->prev_sibling=src->prev_sibling;
1391 else src->parent->last_child=src->prev_sibling;
1392
1393 // connect it to the new point
1394 if(dst->prev_sibling!=0) dst->prev_sibling->next_sibling=src;
1395 else dst->parent->first_child=src;
1396 src->prev_sibling=dst->prev_sibling;
1397 dst->prev_sibling=src;
1398 src->next_sibling=dst;
1399 src->parent=dst->parent;
1400 return src;
1401 }
1402
1403 // specialisation for sibling_iterators
1404 template <class T, class tree_node_allocator>
1405 typename tree<T, tree_node_allocator>::sibling_iterator tree<T, tree_node_allocator>::move_before(sibling_iterator target,
1406 sibling_iterator source)
1407 {
1408 tree_node *dst=target.node;
1409 tree_node *src=source.node;
1410 tree_node *dst_prev_sibling;
1411 if(dst==0) { // must then be an end iterator
1412 dst_prev_sibling=target.parent_->last_child;
1413 assert(dst_prev_sibling);
1414 }
1415 else dst_prev_sibling=dst->prev_sibling;
1416 assert(src);
1417
1418 if(dst==src) return source;
1419 if(dst_prev_sibling)
1420 if(dst_prev_sibling==src) // already in the right spot
1421 return source;
1422
1423 // take src out of the tree
1424 if(src->prev_sibling!=0) src->prev_sibling->next_sibling=src->next_sibling;
1425 else src->parent->first_child=src->next_sibling;
1426 if(src->next_sibling!=0) src->next_sibling->prev_sibling=src->prev_sibling;
1427 else src->parent->last_child=src->prev_sibling;
1428
1429 // connect it to the new point
1430 if(dst_prev_sibling!=0) dst_prev_sibling->next_sibling=src;
1431 else target.parent_->first_child=src;
1432 src->prev_sibling=dst_prev_sibling;
1433 if(dst) {
1434 dst->prev_sibling=src;
1435 src->parent=dst->parent;
1436 }
1437 src->next_sibling=dst;
1438 return src;
1439 }
1440
1441 template <class T, class tree_node_allocator>
1442 template <typename iter> iter tree<T, tree_node_allocator>::move_ontop(iter target, iter source)
1443 {
1444 tree_node *dst=target.node;
1445 tree_node *src=source.node;
1446 assert(dst);
1447 assert(src);
1448
1449 if(dst==src) return source;
1450
1451 // if(dst==src->prev_sibling) {
1452 //
1453 // }
1454
1455 // remember connection points
1456 tree_node *b_prev_sibling=dst->prev_sibling;
1457 tree_node *b_next_sibling=dst->next_sibling;
1458 tree_node *b_parent=dst->parent;
1459
1460 // remove target
1461 erase(target);
1462
1463 // take src out of the tree
1464 if(src->prev_sibling!=0) src->prev_sibling->next_sibling=src->next_sibling;
1465 else src->parent->first_child=src->next_sibling;
1466 if(src->next_sibling!=0) src->next_sibling->prev_sibling=src->prev_sibling;
1467 else src->parent->last_child=src->prev_sibling;
1468
1469 // connect it to the new point
1470 if(b_prev_sibling!=0) b_prev_sibling->next_sibling=src;
1471 else b_parent->first_child=src;
1472 if(b_next_sibling!=0) b_next_sibling->prev_sibling=src;
1473 else b_parent->last_child=src;
1474 src->prev_sibling=b_prev_sibling;
1475 src->next_sibling=b_next_sibling;
1476 src->parent=b_parent;
1477 return src;
1478 }
1479
1480 template <class T, class tree_node_allocator>
1481 void tree<T, tree_node_allocator>::merge(sibling_iterator to1, sibling_iterator to2,
1482 sibling_iterator from1, sibling_iterator from2,
1483 bool duplicate_leaves)
1484 {
1485 sibling_iterator fnd;
1486 while(from1!=from2) {
1487 if((fnd=std::find(to1, to2, (*from1))) != to2) { // element found
1488 if(from1.begin()==from1.end()) { // full depth reached
1489 if(duplicate_leaves)
1490 append_child(parent(to1), (*from1));
1491 }
1492 else { // descend further
1493 merge(fnd.begin(), fnd.end(), from1.begin(), from1.end(), duplicate_leaves);
1494 }
1495 }
1496 else { // element missing
1497 insert_subtree(to2, from1);
1498 }
1499 ++from1;
1500 }
1501 }
1502
1503
1504 template <class T, class tree_node_allocator>
1505 void tree<T, tree_node_allocator>::sort(sibling_iterator from, sibling_iterator to, bool deep)
1506 {
1507 std::less<T> comp;
1508 sort(from, to, comp, deep);
1509 }
1510
1511 template <class T, class tree_node_allocator>
1512 template <class StrictWeakOrdering>
1513 void tree<T, tree_node_allocator>::sort(sibling_iterator from, sibling_iterator to,
1514 StrictWeakOrdering comp, bool deep)
1515 {
1516 if(from==to) return;
1517 // make list of sorted nodes
1518 // CHECK: if multiset stores equivalent nodes in the order in which they
1519 // are inserted, then this routine should be called 'stable_sort'.
1520 std::multiset<tree_node *, compare_nodes<StrictWeakOrdering> > nodes(comp);
1521 sibling_iterator it=from, it2=to;
1522 while(it != to) {
1523 nodes.insert(it.node);
1524 ++it;
1525 }
1526 // reassemble
1527 --it2;
1528
1529 // prev and next are the nodes before and after the sorted range
1530 tree_node *prev=from.node->prev_sibling;
1531 tree_node *next=it2.node->next_sibling;
1532 typename std::multiset<tree_node *, compare_nodes<StrictWeakOrdering> >::iterator nit=nodes.begin(), eit=nodes.end();
1533 if(prev==0) {
1534 if((*nit)->parent!=0) // to catch "sorting the head" situations, when there is no parent
1535 (*nit)->parent->first_child=(*nit);
1536 }
1537 else prev->next_sibling=(*nit);
1538
1539 --eit;
1540 while(nit!=eit) {
1541 (*nit)->prev_sibling=prev;
1542 if(prev)
1543 prev->next_sibling=(*nit);
1544 prev=(*nit);
1545 ++nit;
1546 }
1547 // prev now points to the last-but-one node in the sorted range
1548 if(prev)
1549 prev->next_sibling=(*eit);
1550
1551 // eit points to the last node in the sorted range.
1552 (*eit)->next_sibling=next;
1553 (*eit)->prev_sibling=prev; // missed in the loop above
1554 if(next==0) {
1555 if((*eit)->parent!=0) // to catch "sorting the head" situations, when there is no parent
1556 (*eit)->parent->last_child=(*eit);
1557 }
1558 else next->prev_sibling=(*eit);
1559
1560 if(deep) { // sort the children of each node too
1561 sibling_iterator bcs(*nodes.begin());
1562 sibling_iterator ecs(*eit);
1563 ++ecs;
1564 while(bcs!=ecs) {
1565 sort(begin(bcs), end(bcs), comp, deep);
1566 ++bcs;
1567 }
1568 }
1569 }
1570
1571 template <class T, class tree_node_allocator>
1572 template <typename iter>
1573 bool tree<T, tree_node_allocator>::equal(const iter& one_, const iter& two, const iter& three_) const
1574 {
1575 std::equal_to<T> comp;
1576 return equal(one_, two, three_, comp);
1577 }
1578
1579 template <class T, class tree_node_allocator>
1580 template <typename iter>
1581 bool tree<T, tree_node_allocator>::equal_subtree(const iter& one_, const iter& two_) const
1582 {
1583 std::equal_to<T> comp;
1584 return equal_subtree(one_, two_, comp);
1585 }
1586
1587 template <class T, class tree_node_allocator>
1588 template <typename iter, class BinaryPredicate>
1589 bool tree<T, tree_node_allocator>::equal(const iter& one_, const iter& two, const iter& three_, BinaryPredicate fun) const
1590 {
1591 pre_order_iterator one(one_), three(three_);
1592
1593 // if(one==two && is_valid(three) && three.number_of_children()!=0)
1594 // return false;
1595 while(one!=two && is_valid(three)) {
1596 if(!fun(*one,*three))
1597 return false;
1598 if(one.number_of_children()!=three.number_of_children())
1599 return false;
1600 ++one;
1601 ++three;
1602 }
1603 return true;
1604 }
1605
1606 template <class T, class tree_node_allocator>
1607 template <typename iter, class BinaryPredicate>
1608 bool tree<T, tree_node_allocator>::equal_subtree(const iter& one_, const iter& two_, BinaryPredicate fun) const
1609 {
1610 pre_order_iterator one(one_), two(two_);
1611
1612 if(!fun(*one,*two)) return false;
1613 if(number_of_children(one)!=number_of_children(two)) return false;
1614 return equal(begin(one),end(one),begin(two),fun);
1615 }
1616
1617 template <class T, class tree_node_allocator>
1618 tree<T, tree_node_allocator> tree<T, tree_node_allocator>::subtree(sibling_iterator from, sibling_iterator to) const
1619 {
1620 tree tmp;
1621 tmp.set_head(value_type());
1622 tmp.replace(tmp.begin(), tmp.end(), from, to);
1623 return tmp;
1624 }
1625
1626 template <class T, class tree_node_allocator>
1627 void tree<T, tree_node_allocator>::subtree(tree& tmp, sibling_iterator from, sibling_iterator to) const
1628 {
1629 tmp.set_head(value_type());
1630 tmp.replace(tmp.begin(), tmp.end(), from, to);
1631 }
1632
1633 template <class T, class tree_node_allocator>
1634 size_t tree<T, tree_node_allocator>::size() const
1635 {
1636 size_t i=0;
1637 pre_order_iterator it=begin(), eit=end();
1638 while(it!=eit) {
1639 ++i;
1640 ++it;
1641 }
1642 return i;
1643 }
1644
1645 template <class T, class tree_node_allocator>
1646 size_t tree<T, tree_node_allocator>::size(const iterator_base& top) const
1647 {
1648 size_t i=0;
1649 pre_order_iterator it=top, eit=top;
1650 eit.skip_children();
1651 ++eit;
1652 while(it!=eit) {
1653 ++i;
1654 ++it;
1655 }
1656 return i;
1657 }
1658
1659 template <class T, class tree_node_allocator>
1660 bool tree<T, tree_node_allocator>::empty() const
1661 {
1662 pre_order_iterator it=begin(), eit=end();
1663 return (it==eit);
1664 }
1665
1666 template <class T, class tree_node_allocator>
1667 int tree<T, tree_node_allocator>::depth(const iterator_base& it)
1668 {
1669 tree_node* pos=it.node;
1670 assert(pos!=0);
1671 int ret=0;
1672 while(pos->parent!=0) {
1673 pos=pos->parent;
1674 ++ret;
1675 }
1676 return ret;
1677 }
1678
1679 template <class T, class tree_node_allocator>
1680 int tree<T, tree_node_allocator>::depth(const iterator_base& it, const iterator_base& root)
1681 {
1682 tree_node* pos=it.node;
1683 assert(pos!=0);
1684 int ret=0;
1685 while(pos->parent!=0 && pos!=root.node) {
1686 pos=pos->parent;
1687 ++ret;
1688 }
1689 return ret;
1690 }
1691
1692 template <class T, class tree_node_allocator>
1693 int tree<T, tree_node_allocator>::max_depth() const
1694 {
1695 int maxd=-1;
1696 for(tree_node *it = head->next_sibling; it!=feet; it=it->next_sibling)
1697 maxd=std::max(maxd, max_depth(it));
1698
1699 return maxd;
1700 }
1701
1702
1703 template <class T, class tree_node_allocator>
1704 int tree<T, tree_node_allocator>::max_depth(const iterator_base& pos) const
1705 {
1706 tree_node *tmp=pos.node;
1707
1708 if(tmp==0 || tmp==head || tmp==feet) return -1;
1709
1710 int curdepth=0, maxdepth=0;
1711 while(true) { // try to walk the bottom of the tree
1712 while(tmp->first_child==0) {
1713 if(tmp==pos.node) return maxdepth;
1714 if(tmp->next_sibling==0) {
1715 // try to walk up and then right again
1716 do {
1717 tmp=tmp->parent;
1718 if(tmp==0) return maxdepth;
1719 --curdepth;
1720 } while(tmp->next_sibling==0);
1721 }
1722 if(tmp==pos.node) return maxdepth;
1723 tmp=tmp->next_sibling;
1724 }
1725 tmp=tmp->first_child;
1726 ++curdepth;
1727 maxdepth=std::max(curdepth, maxdepth);
1728 }
1729 }
1730
1731 template <class T, class tree_node_allocator>
1732 unsigned int tree<T, tree_node_allocator>::number_of_children(const iterator_base& it)
1733 {
1734 tree_node *pos=it.node->first_child;
1735 if(pos==0) return 0;
1736
1737 unsigned int ret=1;
1738 // while(pos!=it.node->last_child) {
1739 // ++ret;
1740 // pos=pos->next_sibling;
1741 // }
1742 while((pos=pos->next_sibling))
1743 ++ret;
1744 return ret;
1745 }
1746
1747 template <class T, class tree_node_allocator>
1748 unsigned int tree<T, tree_node_allocator>::number_of_siblings(const iterator_base& it) const
1749 {
1750 tree_node *pos=it.node;
1751 unsigned int ret=0;
1752 // count forward
1753 while(pos->next_sibling &&
1754 pos->next_sibling!=head &&
1755 pos->next_sibling!=feet) {
1756 ++ret;
1757 pos=pos->next_sibling;
1758 }
1759 // count backward
1760 pos=it.node;
1761 while(pos->prev_sibling &&
1762 pos->prev_sibling!=head &&
1763 pos->prev_sibling!=feet) {
1764 ++ret;
1765 pos=pos->prev_sibling;
1766 }
1767
1768 return ret;
1769 }
1770
1771 template <class T, class tree_node_allocator>
1772 void tree<T, tree_node_allocator>::swap(sibling_iterator it)
1773 {
1774 tree_node *nxt=it.node->next_sibling;
1775 if(nxt) {
1776 if(it.node->prev_sibling)
1777 it.node->prev_sibling->next_sibling=nxt;
1778 else
1779 it.node->parent->first_child=nxt;
1780 nxt->prev_sibling=it.node->prev_sibling;
1781 tree_node *nxtnxt=nxt->next_sibling;
1782 if(nxtnxt)
1783 nxtnxt->prev_sibling=it.node;
1784 else
1785 it.node->parent->last_child=it.node;
1786 nxt->next_sibling=it.node;
1787 it.node->prev_sibling=nxt;
1788 it.node->next_sibling=nxtnxt;
1789 }
1790 }
1791
1792 template <class T, class tree_node_allocator>
1793 void tree<T, tree_node_allocator>::swap(iterator one, iterator two)
1794 {
1795 // if one and two are adjacent siblings, use the sibling swap
1796 if(one.node->next_sibling==two.node) swap(one);
1797 else if(two.node->next_sibling==one.node) swap(two);
1798 else {
1799 tree_node *nxt1=one.node->next_sibling;
1800 tree_node *nxt2=two.node->next_sibling;
1801 tree_node *pre1=one.node->prev_sibling;
1802 tree_node *pre2=two.node->prev_sibling;
1803 tree_node *par1=one.node->parent;
1804 tree_node *par2=two.node->parent;
1805
1806 // reconnect
1807 one.node->parent=par2;
1808 one.node->next_sibling=nxt2;
1809 if(nxt2) nxt2->prev_sibling=one.node;
1810 else par2->last_child=one.node;
1811 one.node->prev_sibling=pre2;
1812 if(pre2) pre2->next_sibling=one.node;
1813 else par2->first_child=one.node;
1814
1815 two.node->parent=par1;
1816 two.node->next_sibling=nxt1;
1817 if(nxt1) nxt1->prev_sibling=two.node;
1818 else par1->last_child=two.node;
1819 two.node->prev_sibling=pre1;
1820 if(pre1) pre1->next_sibling=two.node;
1821 else par1->first_child=two.node;
1822 }
1823 }
1824
1825 // template <class BinaryPredicate>
1826 // tree<T, tree_node_allocator>::iterator tree<T, tree_node_allocator>::find_subtree(
1827 // sibling_iterator subfrom, sibling_iterator subto, iterator from, iterator to,
1828 // BinaryPredicate fun) const
1829 // {
1830 // assert(1==0); // this routine is not finished yet.
1831 // while(from!=to) {
1832 // if(fun(*subfrom, *from)) {
1833 //
1834 // }
1835 // }
1836 // return to;
1837 // }
1838
1839 template <class T, class tree_node_allocator>
1840 bool tree<T, tree_node_allocator>::is_in_subtree(const iterator_base& it, const iterator_base& begin,
1841 const iterator_base& end) const
1842 {
1843 // FIXME: this should be optimised.
1844 pre_order_iterator tmp=begin;
1845 while(tmp!=end) {
1846 if(tmp==it) return true;
1847 ++tmp;
1848 }
1849 return false;
1850 }
1851
1852 template <class T, class tree_node_allocator>
1853 bool tree<T, tree_node_allocator>::is_valid(const iterator_base& it) const
1854 {
1855 if(it.node==0 || it.node==feet || it.node==head) return false;
1856 else return true;
1857 }
1858
1859 template <class T, class tree_node_allocator>
1860 typename tree<T, tree_node_allocator>::iterator tree<T, tree_node_allocator>::lowest_common_ancestor(
1861 const iterator_base& one, const iterator_base& two) const
1862 {
1863 std::set<iterator, iterator_base_less> parents;
1864
1865 // Walk up from 'one' storing all parents.
1866 iterator walk=one;
1867 do {
1868 walk=parent(walk);
1869 parents.insert(walk);
1870 } while( is_valid(parent(walk)) );
1871
1872 // Walk up from 'two' until we encounter a node in parents.
1873 walk=two;
1874 do {
1875 walk=parent(walk);
1876 if(parents.find(walk) != parents.end()) break;
1877 } while( is_valid(parent(walk)) );
1878
1879 return walk;
1880 }
1881
1882 template <class T, class tree_node_allocator>
1883 unsigned int tree<T, tree_node_allocator>::index(sibling_iterator it) const
1884 {
1885 unsigned int ind=0;
1886 if(it.node->parent==0) {
1887 while(it.node->prev_sibling!=head) {
1888 it.node=it.node->prev_sibling;
1889 ++ind;
1890 }
1891 }
1892 else {
1893 while(it.node->prev_sibling!=0) {
1894 it.node=it.node->prev_sibling;
1895 ++ind;
1896 }
1897 }
1898 return ind;
1899 }
1900
1901 template <class T, class tree_node_allocator>
1902 typename tree<T, tree_node_allocator>::sibling_iterator tree<T, tree_node_allocator>::sibling(const iterator_base& it, unsigned int num)
1903 {
1904 tree_node *tmp;
1905 if(it.node->parent==0) {
1906 tmp=head->next_sibling;
1907 while(num) {
1908 tmp = tmp->next_sibling;
1909 --num;
1910 }
1911 }
1912 else {
1913 tmp=it.node->parent->first_child;
1914 while(num) {
1915 assert(tmp!=0);
1916 tmp = tmp->next_sibling;
1917 --num;
1918 }
1919 }
1920 return tmp;
1921 }
1922
1923 template <class T, class tree_node_allocator>
1924 void tree<T, tree_node_allocator>::debug_verify_consistency() const
1925 {
1926 iterator it=begin();
1927 while(it!=end()) {
1928 if(it.node->parent!=0) {
1929 if(it.node->prev_sibling==0)
1930 assert(it.node->parent->first_child==it.node);
1931 else
1932 assert(it.node->prev_sibling->next_sibling==it.node);
1933 if(it.node->next_sibling==0)
1934 assert(it.node->parent->last_child==it.node);
1935 else
1936 assert(it.node->next_sibling->prev_sibling==it.node);
1937 }
1938 ++it;
1939 }
1940 }
1941
1942 template <class T, class tree_node_allocator>
1943 typename tree<T, tree_node_allocator>::sibling_iterator tree<T, tree_node_allocator>::child(const iterator_base& it, unsigned int num)
1944 {
1945 tree_node *tmp=it.node->first_child;
1946 while(num--) {
1947 assert(tmp!=0);
1948 tmp=tmp->next_sibling;
1949 }
1950 return tmp;
1951 }
1952
1953
1954
1955
1956 // Iterator base
1957
1958 template <class T, class tree_node_allocator>
1959 tree<T, tree_node_allocator>::iterator_base::iterator_base()
1960 : node(0), skip_current_children_(false)
1961 {
1962 }
1963
1964 template <class T, class tree_node_allocator>
1965 tree<T, tree_node_allocator>::iterator_base::iterator_base(tree_node *tn)
1966 : node(tn), skip_current_children_(false)
1967 {
1968 }
1969
1970 template <class T, class tree_node_allocator>
1971 T& tree<T, tree_node_allocator>::iterator_base::operator*() const
1972 {
1973 return node->data;
1974 }
1975
1976 template <class T, class tree_node_allocator>
1977 T* tree<T, tree_node_allocator>::iterator_base::operator->() const
1978 {
1979 return &(node->data);
1980 }
1981
1982 template <class T, class tree_node_allocator>
1983 bool tree<T, tree_node_allocator>::post_order_iterator::operator!=(const post_order_iterator& other) const
1984 {
1985 if(other.node!=this->node) return true;
1986 else return false;
1987 }
1988
1989 template <class T, class tree_node_allocator>
1990 bool tree<T, tree_node_allocator>::post_order_iterator::operator==(const post_order_iterator& other) const
1991 {
1992 if(other.node==this->node) return true;
1993 else return false;
1994 }
1995
1996 template <class T, class tree_node_allocator>
1997 bool tree<T, tree_node_allocator>::pre_order_iterator::operator!=(const pre_order_iterator& other) const
1998 {
1999 if(other.node!=this->node) return true;
2000 else return false;
2001 }
2002
2003 template <class T, class tree_node_allocator>
2004 bool tree<T, tree_node_allocator>::pre_order_iterator::operator==(const pre_order_iterator& other) const
2005 {
2006 if(other.node==this->node) return true;
2007 else return false;
2008 }
2009
2010 template <class T, class tree_node_allocator>
2011 bool tree<T, tree_node_allocator>::sibling_iterator::operator!=(const sibling_iterator& other) const
2012 {
2013 if(other.node!=this->node) return true;
2014 else return false;
2015 }
2016
2017 template <class T, class tree_node_allocator>
2018 bool tree<T, tree_node_allocator>::sibling_iterator::operator==(const sibling_iterator& other) const
2019 {
2020 if(other.node==this->node) return true;
2021 else return false;
2022 }
2023
2024 template <class T, class tree_node_allocator>
2025 bool tree<T, tree_node_allocator>::leaf_iterator::operator!=(const leaf_iterator& other) const
2026 {
2027 if(other.node!=this->node) return true;
2028 else return false;
2029 }
2030
2031 template <class T, class tree_node_allocator>
2032 bool tree<T, tree_node_allocator>::leaf_iterator::operator==(const leaf_iterator& other) const
2033 {
2034 if(other.node==this->node && other.top_node==this->top_node) return true;
2035 else return false;
2036 }
2037
2038 template <class T, class tree_node_allocator>
2039 typename tree<T, tree_node_allocator>::sibling_iterator tree<T, tree_node_allocator>::iterator_base::begin() const
2040 {
2041 if(node->first_child==0)
2042 return end();
2043
2044 sibling_iterator ret(node->first_child);
2045 ret.parent_=this->node;
2046 return ret;
2047 }
2048
2049 template <class T, class tree_node_allocator>
2050 typename tree<T, tree_node_allocator>::sibling_iterator tree<T, tree_node_allocator>::iterator_base::end() const
2051 {
2052 sibling_iterator ret(0);
2053 ret.parent_=node;
2054 return ret;
2055 }
2056
2057 template <class T, class tree_node_allocator>
2058 void tree<T, tree_node_allocator>::iterator_base::skip_children()
2059 {
2060 skip_current_children_=true;
2061 }
2062
2063 template <class T, class tree_node_allocator>
2064 void tree<T, tree_node_allocator>::iterator_base::skip_children(bool skip)
2065 {
2066 skip_current_children_=skip;
2067 }
2068
2069 template <class T, class tree_node_allocator>
2070 unsigned int tree<T, tree_node_allocator>::iterator_base::number_of_children() const
2071 {
2072 tree_node *pos=node->first_child;
2073 if(pos==0) return 0;
2074
2075 unsigned int ret=1;
2076 while(pos!=node->last_child) {
2077 ++ret;
2078 pos=pos->next_sibling;
2079 }
2080 return ret;
2081 }
2082
2083
2084
2085 // Pre-order iterator
2086
2087 template <class T, class tree_node_allocator>
2088 tree<T, tree_node_allocator>::pre_order_iterator::pre_order_iterator()
2089 : iterator_base(0)
2090 {
2091 }
2092
2093 template <class T, class tree_node_allocator>
2094 tree<T, tree_node_allocator>::pre_order_iterator::pre_order_iterator(tree_node *tn)
2095 : iterator_base(tn)
2096 {
2097 }
2098
2099 template <class T, class tree_node_allocator>
2100 tree<T, tree_node_allocator>::pre_order_iterator::pre_order_iterator(const iterator_base &other)
2101 : iterator_base(other.node)
2102 {
2103 }
2104
2105 template <class T, class tree_node_allocator>
2106 tree<T, tree_node_allocator>::pre_order_iterator::pre_order_iterator(const sibling_iterator& other)
2107 : iterator_base(other.node)
2108 {
2109 if(this->node==0) {
2110 if(other.range_last()!=0)
2111 this->node=other.range_last();
2112 else
2113 this->node=other.parent_;
2114 this->skip_children();
2115 ++(*this);
2116 }
2117 }
2118
2119 template <class T, class tree_node_allocator>
2120 typename tree<T, tree_node_allocator>::pre_order_iterator& tree<T, tree_node_allocator>::pre_order_iterator::operator++()
2121 {
2122 assert(this->node!=0);
2123 if(!this->skip_current_children_ && this->node->first_child != 0) {
2124 this->node=this->node->first_child;
2125 }
2126 else {
2127 this->skip_current_children_=false;
2128 while(this->node->next_sibling==0) {
2129 this->node=this->node->parent;
2130 if(this->node==0)
2131 return *this;
2132 }
2133 this->node=this->node->next_sibling;
2134 }
2135 return *this;
2136 }
2137
2138 template <class T, class tree_node_allocator>
2139 typename tree<T, tree_node_allocator>::pre_order_iterator& tree<T, tree_node_allocator>::pre_order_iterator::operator--()
2140 {
2141 assert(this->node!=0);
2142 if(this->node->prev_sibling) {
2143 this->node=this->node->prev_sibling;
2144 while(this->node->last_child)
2145 this->node=this->node->last_child;
2146 }
2147 else {
2148 this->node=this->node->parent;
2149 if(this->node==0)
2150 return *this;
2151 }
2152 return *this;
2153 }
2154
2155 template <class T, class tree_node_allocator>
2156 typename tree<T, tree_node_allocator>::pre_order_iterator tree<T, tree_node_allocator>::pre_order_iterator::operator++(int)
2157 {
2158 pre_order_iterator copy = *this;
2159 ++(*this);
2160 return copy;
2161 }
2162
2163 template <class T, class tree_node_allocator>
2164 typename tree<T, tree_node_allocator>::pre_order_iterator tree<T, tree_node_allocator>::pre_order_iterator::operator--(int)
2165 {
2166 pre_order_iterator copy = *this;
2167 --(*this);
2168 return copy;
2169 }
2170
2171 template <class T, class tree_node_allocator>
2172 typename tree<T, tree_node_allocator>::pre_order_iterator& tree<T, tree_node_allocator>::pre_order_iterator::operator+=(unsigned int num)
2173 {
2174 while(num>0) {
2175 ++(*this);
2176 --num;
2177 }
2178 return (*this);
2179 }
2180
2181 template <class T, class tree_node_allocator>
2182 typename tree<T, tree_node_allocator>::pre_order_iterator& tree<T, tree_node_allocator>::pre_order_iterator::operator-=(unsigned int num)
2183 {
2184 while(num>0) {
2185 --(*this);
2186 --num;
2187 }
2188 return (*this);
2189 }
2190
2191
2192
2193 // Post-order iterator
2194
2195 template <class T, class tree_node_allocator>
2196 tree<T, tree_node_allocator>::post_order_iterator::post_order_iterator()
2197 : iterator_base(0)
2198 {
2199 }
2200
2201 template <class T, class tree_node_allocator>
2202 tree<T, tree_node_allocator>::post_order_iterator::post_order_iterator(tree_node *tn)
2203 : iterator_base(tn)
2204 {
2205 }
2206
2207 template <class T, class tree_node_allocator>
2208 tree<T, tree_node_allocator>::post_order_iterator::post_order_iterator(const iterator_base &other)
2209 : iterator_base(other.node)
2210 {
2211 }
2212
2213 template <class T, class tree_node_allocator>
2214 tree<T, tree_node_allocator>::post_order_iterator::post_order_iterator(const sibling_iterator& other)
2215 : iterator_base(other.node)
2216 {
2217 if(this->node==0) {
2218 if(other.range_last()!=0)
2219 this->node=other.range_last();
2220 else
2221 this->node=other.parent_;
2222 this->skip_children();
2223 ++(*this);
2224 }
2225 }
2226
2227 template <class T, class tree_node_allocator>
2228 typename tree<T, tree_node_allocator>::post_order_iterator& tree<T, tree_node_allocator>::post_order_iterator::operator++()
2229 {
2230 assert(this->node!=0);
2231 if(this->node->next_sibling==0) {
2232 this->node=this->node->parent;
2233 this->skip_current_children_=false;
2234 }
2235 else {
2236 this->node=this->node->next_sibling;
2237 if(this->skip_current_children_) {
2238 this->skip_current_children_=false;
2239 }
2240 else {
2241 while(this->node->first_child)
2242 this->node=this->node->first_child;
2243 }
2244 }
2245 return *this;
2246 }
2247
2248 template <class T, class tree_node_allocator>
2249 typename tree<T, tree_node_allocator>::post_order_iterator& tree<T, tree_node_allocator>::post_order_iterator::operator--()
2250 {
2251 assert(this->node!=0);
2252 if(this->skip_current_children_ || this->node->last_child==0) {
2253 this->skip_current_children_=false;
2254 while(this->node->prev_sibling==0)
2255 this->node=this->node->parent;
2256 this->node=this->node->prev_sibling;
2257 }
2258 else {
2259 this->node=this->node->last_child;
2260 }
2261 return *this;
2262 }
2263
2264 template <class T, class tree_node_allocator>
2265 typename tree<T, tree_node_allocator>::post_order_iterator tree<T, tree_node_allocator>::post_order_iterator::operator++(int)
2266 {
2267 post_order_iterator copy = *this;
2268 ++(*this);
2269 return copy;
2270 }
2271
2272 template <class T, class tree_node_allocator>
2273 typename tree<T, tree_node_allocator>::post_order_iterator tree<T, tree_node_allocator>::post_order_iterator::operator--(int)
2274 {
2275 post_order_iterator copy = *this;
2276 --(*this);
2277 return copy;
2278 }
2279
2280
2281 template <class T, class tree_node_allocator>
2282 typename tree<T, tree_node_allocator>::post_order_iterator& tree<T, tree_node_allocator>::post_order_iterator::operator+=(unsigned int num)
2283 {
2284 while(num>0) {
2285 ++(*this);
2286 --num;
2287 }
2288 return (*this);
2289 }
2290
2291 template <class T, class tree_node_allocator>
2292 typename tree<T, tree_node_allocator>::post_order_iterator& tree<T, tree_node_allocator>::post_order_iterator::operator-=(unsigned int num)
2293 {
2294 while(num>0) {
2295 --(*this);
2296 --num;
2297 }
2298 return (*this);
2299 }
2300
2301 template <class T, class tree_node_allocator>
2302 void tree<T, tree_node_allocator>::post_order_iterator::descend_all()
2303 {
2304 assert(this->node!=0);
2305 while(this->node->first_child)
2306 this->node=this->node->first_child;
2307 }
2308
2309
2310 // Breadth-first iterator
2311
2312 template <class T, class tree_node_allocator>
2313 tree<T, tree_node_allocator>::breadth_first_queued_iterator::breadth_first_queued_iterator()
2314 : iterator_base()
2315 {
2316 }
2317
2318 template <class T, class tree_node_allocator>
2319 tree<T, tree_node_allocator>::breadth_first_queued_iterator::breadth_first_queued_iterator(tree_node *tn)
2320 : iterator_base(tn)
2321 {
2322 traversal_queue.push(tn);
2323 }
2324
2325 template <class T, class tree_node_allocator>
2326 tree<T, tree_node_allocator>::breadth_first_queued_iterator::breadth_first_queued_iterator(const iterator_base& other)
2327 : iterator_base(other.node)
2328 {
2329 traversal_queue.push(other.node);
2330 }
2331
2332 template <class T, class tree_node_allocator>
2333 bool tree<T, tree_node_allocator>::breadth_first_queued_iterator::operator!=(const breadth_first_queued_iterator& other) const
2334 {
2335 if(other.node!=this->node) return true;
2336 else return false;
2337 }
2338
2339 template <class T, class tree_node_allocator>
2340 bool tree<T, tree_node_allocator>::breadth_first_queued_iterator::operator==(const breadth_first_queued_iterator& other) const
2341 {
2342 if(other.node==this->node) return true;
2343 else return false;
2344 }
2345
2346 template <class T, class tree_node_allocator>
2347 typename tree<T, tree_node_allocator>::breadth_first_queued_iterator& tree<T, tree_node_allocator>::breadth_first_queued_iterator::operator++()
2348 {
2349 assert(this->node!=0);
2350
2351 // Add child nodes and pop current node
2352 sibling_iterator sib=this->begin();
2353 while(sib!=this->end()) {
2354 traversal_queue.push(sib.node);
2355 ++sib;
2356 }
2357 traversal_queue.pop();
2358 if(traversal_queue.size()>0)
2359 this->node=traversal_queue.front();
2360 else
2361 this->node=0;
2362 return (*this);
2363 }
2364
2365 template <class T, class tree_node_allocator>
2366 typename tree<T, tree_node_allocator>::breadth_first_queued_iterator tree<T, tree_node_allocator>::breadth_first_queued_iterator::operator++(int)
2367 {
2368 breadth_first_queued_iterator copy = *this;
2369 ++(*this);
2370 return copy;
2371 }
2372
2373 template <class T, class tree_node_allocator>
2374 typename tree<T, tree_node_allocator>::breadth_first_queued_iterator& tree<T, tree_node_allocator>::breadth_first_queued_iterator::operator+=(unsigned int num)
2375 {
2376 while(num>0) {
2377 ++(*this);
2378 --num;
2379 }
2380 return (*this);
2381 }
2382
2383
2384
2385 // Fixed depth iterator
2386
2387 template <class T, class tree_node_allocator>
2388 tree<T, tree_node_allocator>::fixed_depth_iterator::fixed_depth_iterator()
2389 : iterator_base()
2390 {
2391 }
2392
2393 template <class T, class tree_node_allocator>
2394 tree<T, tree_node_allocator>::fixed_depth_iterator::fixed_depth_iterator(tree_node *tn)
2395 : iterator_base(tn), top_node(0)
2396 {
2397 }
2398
2399 template <class T, class tree_node_allocator>
2400 tree<T, tree_node_allocator>::fixed_depth_iterator::fixed_depth_iterator(const iterator_base& other)
2401 : iterator_base(other.node), top_node(0)
2402 {
2403 }
2404
2405 template <class T, class tree_node_allocator>
2406 tree<T, tree_node_allocator>::fixed_depth_iterator::fixed_depth_iterator(const sibling_iterator& other)
2407 : iterator_base(other.node), top_node(0)
2408 {
2409 }
2410
2411 template <class T, class tree_node_allocator>
2412 tree<T, tree_node_allocator>::fixed_depth_iterator::fixed_depth_iterator(const fixed_depth_iterator& other)
2413 : iterator_base(other.node), top_node(other.top_node)
2414 {
2415 }
2416
2417 template <class T, class tree_node_allocator>
2418 bool tree<T, tree_node_allocator>::fixed_depth_iterator::operator==(const fixed_depth_iterator& other) const
2419 {
2420 if(other.node==this->node && other.top_node==top_node) return true;
2421 else return false;
2422 }
2423
2424 template <class T, class tree_node_allocator>
2425 bool tree<T, tree_node_allocator>::fixed_depth_iterator::operator!=(const fixed_depth_iterator& other) const
2426 {
2427 if(other.node!=this->node || other.top_node!=top_node) return true;
2428 else return false;
2429 }
2430
2431 template <class T, class tree_node_allocator>
2432 typename tree<T, tree_node_allocator>::fixed_depth_iterator& tree<T, tree_node_allocator>::fixed_depth_iterator::operator++()
2433 {
2434 assert(this->node!=0);
2435
2436 if(this->node->next_sibling) {
2437 this->node=this->node->next_sibling;
2438 }
2439 else {
2440 int relative_depth=0;
2441 upper:
2442 do {
2443 if(this->node==this->top_node) {
2444 this->node=0; // FIXME: return a proper fixed_depth end iterator once implemented
2445 return *this;
2446 }
2447 this->node=this->node->parent;
2448 if(this->node==0) return *this;
2449 --relative_depth;
2450 } while(this->node->next_sibling==0);
2451 lower:
2452 this->node=this->node->next_sibling;
2453 while(this->node->first_child==0) {
2454 if(this->node->next_sibling==0)
2455 goto upper;
2456 this->node=this->node->next_sibling;
2457 if(this->node==0) return *this;
2458 }
2459 while(relative_depth<0 && this->node->first_child!=0) {
2460 this->node=this->node->first_child;
2461 ++relative_depth;
2462 }
2463 if(relative_depth<0) {
2464 if(this->node->next_sibling==0) goto upper;
2465 else goto lower;
2466 }
2467 }
2468 return *this;
2469 }
2470
2471 template <class T, class tree_node_allocator>
2472 typename tree<T, tree_node_allocator>::fixed_depth_iterator& tree<T, tree_node_allocator>::fixed_depth_iterator::operator--()
2473 {
2474 assert(this->node!=0);
2475
2476 if(this->node->prev_sibling) {
2477 this->node=this->node->prev_sibling;
2478 }
2479 else {
2480 int relative_depth=0;
2481 upper:
2482 do {
2483 if(this->node==this->top_node) {
2484 this->node=0;
2485 return *this;
2486 }
2487 this->node=this->node->parent;
2488 if(this->node==0) return *this;
2489 --relative_depth;
2490 } while(this->node->prev_sibling==0);
2491 lower:
2492 this->node=this->node->prev_sibling;
2493 while(this->node->last_child==0) {
2494 if(this->node->prev_sibling==0)
2495 goto upper;
2496 this->node=this->node->prev_sibling;
2497 if(this->node==0) return *this;
2498 }
2499 while(relative_depth<0 && this->node->last_child!=0) {
2500 this->node=this->node->last_child;
2501 ++relative_depth;
2502 }
2503 if(relative_depth<0) {
2504 if(this->node->prev_sibling==0) goto upper;
2505 else goto lower;
2506 }
2507 }
2508 return *this;
2509
2510 //
2511 //
2512 // assert(this->node!=0);
2513 // if(this->node->prev_sibling!=0) {
2514 // this->node=this->node->prev_sibling;
2515 // assert(this->node!=0);
2516 // if(this->node->parent==0 && this->node->prev_sibling==0) // head element
2517 // this->node=0;
2518 // }
2519 // else {
2520 // tree_node *par=this->node->parent;
2521 // do {
2522 // par=par->prev_sibling;
2523 // if(par==0) { // FIXME: need to keep track of this!
2524 // this->node=0;
2525 // return *this;
2526 // }
2527 // } while(par->last_child==0);
2528 // this->node=par->last_child;
2529 // }
2530 // return *this;
2531 }
2532
2533 template <class T, class tree_node_allocator>
2534 typename tree<T, tree_node_allocator>::fixed_depth_iterator tree<T, tree_node_allocator>::fixed_depth_iterator::operator++(int)
2535 {
2536 fixed_depth_iterator copy = *this;
2537 ++(*this);
2538 return copy;
2539 }
2540
2541 template <class T, class tree_node_allocator>
2542 typename tree<T, tree_node_allocator>::fixed_depth_iterator tree<T, tree_node_allocator>::fixed_depth_iterator::operator--(int)
2543 {
2544 fixed_depth_iterator copy = *this;
2545 --(*this);
2546 return copy;
2547 }
2548
2549 template <class T, class tree_node_allocator>
2550 typename tree<T, tree_node_allocator>::fixed_depth_iterator& tree<T, tree_node_allocator>::fixed_depth_iterator::operator-=(unsigned int num)
2551 {
2552 while(num>0) {
2553 --(*this);
2554 --(num);
2555 }
2556 return (*this);
2557 }
2558
2559 template <class T, class tree_node_allocator>
2560 typename tree<T, tree_node_allocator>::fixed_depth_iterator& tree<T, tree_node_allocator>::fixed_depth_iterator::operator+=(unsigned int num)
2561 {
2562 while(num>0) {
2563 ++(*this);
2564 --(num);
2565 }
2566 return *this;
2567 }
2568
2569
2570 // Sibling iterator
2571
2572 template <class T, class tree_node_allocator>
2573 tree<T, tree_node_allocator>::sibling_iterator::sibling_iterator()
2574 : iterator_base()
2575 {
2576 set_parent_();
2577 }
2578
2579 template <class T, class tree_node_allocator>
2580 tree<T, tree_node_allocator>::sibling_iterator::sibling_iterator(tree_node *tn)
2581 : iterator_base(tn)
2582 {
2583 set_parent_();
2584 }
2585
2586 template <class T, class tree_node_allocator>
2587 tree<T, tree_node_allocator>::sibling_iterator::sibling_iterator(const iterator_base& other)
2588 : iterator_base(other.node)
2589 {
2590 set_parent_();
2591 }
2592
2593 template <class T, class tree_node_allocator>
2594 tree<T, tree_node_allocator>::sibling_iterator::sibling_iterator(const sibling_iterator& other)
2595 : iterator_base(other), parent_(other.parent_)
2596 {
2597 }
2598
2599 template <class T, class tree_node_allocator>
2600 void tree<T, tree_node_allocator>::sibling_iterator::set_parent_()
2601 {
2602 parent_=0;
2603 if(this->node==0) return;
2604 if(this->node->parent!=0)
2605 parent_=this->node->parent;
2606 }
2607
2608 template <class T, class tree_node_allocator>
2609 typename tree<T, tree_node_allocator>::sibling_iterator& tree<T, tree_node_allocator>::sibling_iterator::operator++()
2610 {
2611 if(this->node)
2612 this->node=this->node->next_sibling;
2613 return *this;
2614 }
2615
2616 template <class T, class tree_node_allocator>
2617 typename tree<T, tree_node_allocator>::sibling_iterator& tree<T, tree_node_allocator>::sibling_iterator::operator--()
2618 {
2619 if(this->node) this->node=this->node->prev_sibling;
2620 else {
2621 assert(parent_);
2622 this->node=parent_->last_child;
2623 }
2624 return *this;
2625 }
2626
2627 template <class T, class tree_node_allocator>
2628 typename tree<T, tree_node_allocator>::sibling_iterator tree<T, tree_node_allocator>::sibling_iterator::operator++(int)
2629 {
2630 sibling_iterator copy = *this;
2631 ++(*this);
2632 return copy;
2633 }
2634
2635 template <class T, class tree_node_allocator>
2636 typename tree<T, tree_node_allocator>::sibling_iterator tree<T, tree_node_allocator>::sibling_iterator::operator--(int)
2637 {
2638 sibling_iterator copy = *this;
2639 --(*this);
2640 return copy;
2641 }
2642
2643 template <class T, class tree_node_allocator>
2644 typename tree<T, tree_node_allocator>::sibling_iterator& tree<T, tree_node_allocator>::sibling_iterator::operator+=(unsigned int num)
2645 {
2646 while(num>0) {
2647 ++(*this);
2648 --num;
2649 }
2650 return (*this);
2651 }
2652
2653 template <class T, class tree_node_allocator>
2654 typename tree<T, tree_node_allocator>::sibling_iterator& tree<T, tree_node_allocator>::sibling_iterator::operator-=(unsigned int num)
2655 {
2656 while(num>0) {
2657 --(*this);
2658 --num;
2659 }
2660 return (*this);
2661 }
2662
2663 template <class T, class tree_node_allocator>
2664 typename tree<T, tree_node_allocator>::tree_node *tree<T, tree_node_allocator>::sibling_iterator::range_first() const
2665 {
2666 tree_node *tmp=parent_->first_child;
2667 return tmp;
2668 }
2669
2670 template <class T, class tree_node_allocator>
2671 typename tree<T, tree_node_allocator>::tree_node *tree<T, tree_node_allocator>::sibling_iterator::range_last() const
2672 {
2673 return parent_->last_child;
2674 }
2675
2676 // Leaf iterator
2677
2678 template <class T, class tree_node_allocator>
2679 tree<T, tree_node_allocator>::leaf_iterator::leaf_iterator()
2680 : iterator_base(0), top_node(0)
2681 {
2682 }
2683
2684 template <class T, class tree_node_allocator>
2685 tree<T, tree_node_allocator>::leaf_iterator::leaf_iterator(tree_node *tn, tree_node *top)
2686 : iterator_base(tn), top_node(top)
2687 {
2688 }
2689
2690 template <class T, class tree_node_allocator>
2691 tree<T, tree_node_allocator>::leaf_iterator::leaf_iterator(const iterator_base &other)
2692 : iterator_base(other.node), top_node(0)
2693 {
2694 }
2695
2696 template <class T, class tree_node_allocator>
2697 tree<T, tree_node_allocator>::leaf_iterator::leaf_iterator(const sibling_iterator& other)
2698 : iterator_base(other.node), top_node(0)
2699 {
2700 if(this->node==0) {
2701 if(other.range_last()!=0)
2702 this->node=other.range_last();
2703 else
2704 this->node=other.parent_;
2705 ++(*this);
2706 }
2707 }
2708
2709 template <class T, class tree_node_allocator>
2710 typename tree<T, tree_node_allocator>::leaf_iterator& tree<T, tree_node_allocator>::leaf_iterator::operator++()
2711 {
2712 assert(this->node!=0);
2713 if(this->node->first_child!=0) { // current node is no longer leaf (children got added)
2714 while(this->node->first_child)
2715 this->node=this->node->first_child;
2716 }
2717 else {
2718 while(this->node->next_sibling==0) {
2719 if (this->node->parent==0) return *this;
2720 this->node=this->node->parent;
2721 if (top_node != 0 && this->node==top_node) return *this;
2722 }
2723 this->node=this->node->next_sibling;
2724 while(this->node->first_child)
2725 this->node=this->node->first_child;
2726 }
2727 return *this;
2728 }
2729
2730 template <class T, class tree_node_allocator>
2731 typename tree<T, tree_node_allocator>::leaf_iterator& tree<T, tree_node_allocator>::leaf_iterator::operator--()
2732 {
2733 assert(this->node!=0);
2734 while (this->node->prev_sibling==0) {
2735 if (this->node->parent==0) return *this;
2736 this->node=this->node->parent;
2737 if (top_node !=0 && this->node==top_node) return *this;
2738 }
2739 this->node=this->node->prev_sibling;
2740 while(this->node->last_child)
2741 this->node=this->node->last_child;
2742 return *this;
2743 }
2744
2745 template <class T, class tree_node_allocator>
2746 typename tree<T, tree_node_allocator>::leaf_iterator tree<T, tree_node_allocator>::leaf_iterator::operator++(int)
2747 {
2748 leaf_iterator copy = *this;
2749 ++(*this);
2750 return copy;
2751 }
2752
2753 template <class T, class tree_node_allocator>
2754 typename tree<T, tree_node_allocator>::leaf_iterator tree<T, tree_node_allocator>::leaf_iterator::operator--(int)
2755 {
2756 leaf_iterator copy = *this;
2757 --(*this);
2758 return copy;
2759 }
2760
2761
2762 template <class T, class tree_node_allocator>
2763 typename tree<T, tree_node_allocator>::leaf_iterator& tree<T, tree_node_allocator>::leaf_iterator::operator+=(unsigned int num)
2764 {
2765 while(num>0) {
2766 ++(*this);
2767 --num;
2768 }
2769 return (*this);
2770 }
2771
2772 template <class T, class tree_node_allocator>
2773 typename tree<T, tree_node_allocator>::leaf_iterator& tree<T, tree_node_allocator>::leaf_iterator::operator-=(unsigned int num)
2774 {
2775 while(num>0) {
2776 --(*this);
2777 --num;
2778 }
2779 return (*this);
2780 }
2781
2782 #endif
2783
2784 // Local variables:
2785 // default-tab-width: 3
2786 // End:
tree::post_order_iterator::operator!=
bool operator!=(const post_order_iterator &) const
Definition: tree.hh:1983
tree::breadth_first_queued_iterator::operator!=
bool operator!=(const breadth_first_queued_iterator &) const
Definition: tree.hh:2333
tree::clear
void clear()
Erase all nodes of the tree.
Definition: tree.hh:565
tree::replace
iter replace(iter position, const T &x)
Replace node at &#39;position&#39; with other node (keeping same children); &#39;position&#39; becomes invalid...
Definition: tree.hh:1128
tree::breadth_first_queued_iterator
Breadth-first iterator, using a queue.
Definition: tree.hh:161
tree::sibling_iterator::operator-=
sibling_iterator & operator-=(unsigned int)
Definition: tree.hh:2654
tree::breadth_first_queued_iterator::operator++
breadth_first_queued_iterator & operator++()
Definition: tree.hh:2347
tree::swap
void swap(sibling_iterator it)
Exchange the node (plus subtree) with its sibling node (do nothing if no sibling present).
Definition: tree.hh:1772
tree::fixed_depth_iterator::operator-=
fixed_depth_iterator & operator-=(unsigned int)
Definition: tree.hh:2550
tree::iterator_base::number_of_children
unsigned int number_of_children() const
Number of children of the node pointed to by the iterator.
Definition: tree.hh:2070
tree::end_breadth_first
breadth_first_queued_iterator end_breadth_first() const
Return breadth-first end iterator.
Definition: tree.hh:642
tree::pre_order_iterator::operator++
pre_order_iterator & operator++()
Definition: tree.hh:2120
tree::leaf_iterator::operator++
leaf_iterator & operator++()
Definition: tree.hh:2710
tree::post_order_iterator::operator==
bool operator==(const post_order_iterator &) const
Definition: tree.hh:1990
tree::size
size_t size() const
Count the total number of nodes.
Definition: tree.hh:1634
tree::debug_verify_consistency
void debug_verify_consistency() const
Definition: tree.hh:1924
tree::breadth_first_queued_iterator::breadth_first_queued_iterator
breadth_first_queued_iterator()
Definition: tree.hh:2313
tree::erase_children
void erase_children(const iterator_base &)
Erase all children of the node pointed to by iterator.
Definition: tree.hh:573
tree::iterator_base::pointer
T * pointer
Definition: tree.hh:95
tree_node_::prev_sibling
tree_node_< T > * prev_sibling
Definition: tree.hh:50
tree::pre_order_iterator::operator!=
bool operator!=(const pre_order_iterator &) const
Definition: tree.hh:1997
tree::tree
tree()
Definition: tree.hh:477
tree::reparent
iter reparent(iter position, sibling_iterator begin, sibling_iterator end)
Move nodes in range to be children of &#39;position&#39;.
Definition: tree.hh:1280
tree::sibling_iterator::range_first
tree_node * range_first() const
Definition: tree.hh:2664
tree::leaf_iterator::operator!=
bool operator!=(const leaf_iterator &) const
Definition: tree.hh:2025
tree::erase
iter erase(iter)
Erase element at position pointed to by iterator, return incremented iterator.
Definition: tree.hh:596
tree::sibling_iterator::range_last
tree_node * range_last() const
Definition: tree.hh:2671
tree::iterator_base::iterator_base
iterator_base()
Definition: tree.hh:1959
tree::begin
pre_order_iterator begin() const
Return iterator to the beginning of the tree.
Definition: tree.hh:624
tree::pre_order_iterator::operator--
pre_order_iterator & operator--()
Definition: tree.hh:2139
tree::iterator_base::size_type
size_t size_type
Definition: tree.hh:97
tree::fixed_depth_iterator::operator++
fixed_depth_iterator & operator++()
Definition: tree.hh:2432
tree::end_leaf
leaf_iterator end_leaf() const
Return leaf end iterator for entire tree.
Definition: tree.hh:743
tree::iterator_base::end
sibling_iterator end() const
Definition: tree.hh:2050
tree::is_in_subtree
bool is_in_subtree(const iterator_base &position, const iterator_base &begin, const iterator_base &end) const
Determine whether node at position is in the subtrees with root in the range.
Definition: tree.hh:1840
tree::set_head
pre_order_iterator set_head(const T &x)
Short-hand to insert topmost node in otherwise empty tree.
Definition: tree.hh:1007
tree::iterator_base_less::operator()
bool operator()(const typename tree< T, tree_node_allocator >::iterator_base &one, const typename tree< T, tree_node_allocator >::iterator_base &two) const
Definition: tree.hh:407
tree::~tree
~tree()
Definition: tree.hh:498
tree::alloc_
tree_node_allocator alloc_
Definition: tree.hh:415
tree::copy_
void copy_(const tree< T, tree_node_allocator > &other)
Definition: tree.hh:544
tree::iterator_base::skip_children
void skip_children()
When called, the next increment/decrement skips children of this node.
Definition: tree.hh:2058
tree::flatten
iter flatten(iter position)
Move all children of node at &#39;position&#39; to be siblings, returns position.
Definition: tree.hh:1252
tree::number_of_children
static unsigned int number_of_children(const iterator_base &)
Count the number of children of node at position.
Definition: tree.hh:1732
tree::number_of_siblings
unsigned int number_of_siblings(const iterator_base &) const
Count the number of siblings (left and right) of node at iterator. Total nodes at this level is +1...
Definition: tree.hh:1748
tree::pre_order_iterator::pre_order_iterator
pre_order_iterator()
Definition: tree.hh:2088
tree::sibling_iterator::operator+=
sibling_iterator & operator+=(unsigned int)
Definition: tree.hh:2644
tree::iterator_base::node
tree_node * node
Definition: tree.hh:116
tree::depth
static int depth(const iterator_base &)
Compute the depth to the root or to a fixed other iterator.
Definition: tree.hh:1667
tree::post_order_iterator::operator--
post_order_iterator & operator--()
Definition: tree.hh:2249
tree::breadth_first_queued_iterator::operator==
bool operator==(const breadth_first_queued_iterator &) const
Definition: tree.hh:2340
tree::head
tree_node * head
Definition: tree.hh:413
tree::compare_nodes
Comparator class for two nodes of a tree (used for sorting and searching).
Definition: tree.hh:421
tree::move_before
iter move_before(iter target, iter source)
Move &#39;source&#39; node (plus its children) to become the previous sibling of &#39;target&#39;.
Definition: tree.hh:1375
tree::fixed_depth_iterator::operator!=
bool operator!=(const fixed_depth_iterator &) const
Definition: tree.hh:2425
tree::sibling_iterator::operator++
sibling_iterator & operator++()
Definition: tree.hh:2609
tree::fixed_depth_iterator::operator==
bool operator==(const fixed_depth_iterator &) const
Definition: tree.hh:2418
tree_node_::next_sibling
tree_node_< T > * next_sibling
Definition: tree.hh:50
tree::iterator_base::value_type
T value_type
Definition: tree.hh:94
tree::post_order_iterator::operator+=
post_order_iterator & operator+=(unsigned int)
Definition: tree.hh:2282
tree::next_sibling
iter next_sibling(iter) const
Return iterator to the next sibling of a node.
Definition: tree.hh:783
tree::sibling_iterator
Iterator which traverses only the nodes which are siblings of each other.
Definition: tree.hh:203
tree::post_order_iterator::post_order_iterator
post_order_iterator()
Definition: tree.hh:2196
tree::begin_breadth_first
breadth_first_queued_iterator begin_breadth_first() const
Return breadth-first iterator to the first node at a given depth.
Definition: tree.hh:636
tree::sibling
sibling_iterator sibling(const iterator_base &position, unsigned int)
Return iterator to the sibling indicated by index.
Definition: tree.hh:1902
tree_node_::first_child
tree_node_< T > * first_child
Definition: tree.hh:49
tree::fixed_depth_iterator::fixed_depth_iterator
fixed_depth_iterator()
Definition: tree.hh:2388
tree::sibling_iterator::operator--
sibling_iterator & operator--()
Definition: tree.hh:2617
tree::feet
tree_node * feet
Definition: tree.hh:413
tree::child
static sibling_iterator child(const iterator_base &position, unsigned int)
Inverse of &#39;index&#39;: return the n-th child of the node at position.
Definition: tree.hh:1943
tree::equal_subtree
bool equal_subtree(const iter &one, const iter &two) const
Definition: tree.hh:1581
tree_node_::data
T data
Definition: tree.hh:51
tree::lowest_common_ancestor
iterator lowest_common_ancestor(const iterator_base &, const iterator_base &) const
Definition: tree.hh:1860
tree::insert_after
iter insert_after(iter position, const T &x)
Insert node as next sibling of node pointed to by position.
Definition: tree.hh:1073
tree::begin_fixed
fixed_depth_iterator begin_fixed(const iterator_base &, unsigned int) const
Return fixed-depth iterator to the first node at a given depth from the given iterator.
Definition: tree.hh:665
tree_node_
A node in the tree, combining links to other nodes as well as the actual data.
Definition: tree.hh:43
tree::sibling_iterator::operator==
bool operator==(const sibling_iterator &) const
Definition: tree.hh:2018
tree::leaf_iterator::leaf_iterator
leaf_iterator()
Definition: tree.hh:2679
tree::begin_post
post_order_iterator begin_post() const
Return post-order iterator to the beginning of the tree.
Definition: tree.hh:648
tree::sibling_iterator::parent_
tree_node * parent_
Definition: tree.hh:221
tree::iterator_base
Base class for iterators, only pointers stored, no traversal logic.
Definition: tree.hh:91
tree::pre_order_iterator::operator-=
pre_order_iterator & operator-=(unsigned int)
Definition: tree.hh:2182
tree_node_::last_child
tree_node_< T > * last_child
Definition: tree.hh:49
tree::insert_subtree
iter insert_subtree(iter position, const iterator_base &subtree)
Insert node (with children) pointed to by subtree as previous sibling of node pointed to by position...
Definition: tree.hh:1098
tree::iterator_base::begin
sibling_iterator begin() const
Definition: tree.hh:2039
tree::leaf_iterator
Iterator which traverses only the leaves.
Definition: tree.hh:227
tree::breadth_first_iterator
breadth_first_queued_iterator breadth_first_iterator
Definition: tree.hh:179
tree::previous_sibling
iter previous_sibling(iter) const
Return iterator to the previous sibling of a node.
Definition: tree.hh:773
tree::end
pre_order_iterator end() const
Return iterator to the end of the tree.
Definition: tree.hh:630
tree::leaf_iterator::operator-=
leaf_iterator & operator-=(unsigned int)
Definition: tree.hh:2773
tree::compare_nodes::operator()
bool operator()(const tree_node *a, const tree_node *b)
Definition: tree.hh:425
tree::post_order_iterator::descend_all
void descend_all()
Set iterator to the first child as deep as possible down the tree.
Definition: tree.hh:2302
tree::sibling_iterator::sibling_iterator
sibling_iterator()
Definition: tree.hh:2573
tree::iterator_base::operator->
T * operator->() const
Definition: tree.hh:1977
tree::next_at_same_depth
iter next_at_same_depth(iter) const
Return iterator to the next node at a given depth.
Definition: tree.hh:793
tree::leaf_iterator::operator+=
leaf_iterator & operator+=(unsigned int)
Definition: tree.hh:2763
tree::merge
void merge(sibling_iterator, sibling_iterator, sibling_iterator, sibling_iterator, bool duplicate_leaves=false)
Merge with other tree, creating new branches and leaves only if they are not already present...
Definition: tree.hh:1481
tree::fixed_depth_iterator::operator--
fixed_depth_iterator & operator--()
Definition: tree.hh:2472
tree::post_order_iterator
Depth-first iterator, first accessing the children, then the node itself.
Definition: tree.hh:140
tree::value_type
T value_type
Value of the data stored at a node.
Definition: tree.hh:72
tree_node_::tree_node_
tree_node_()
Definition: tree.hh:55
tree::post_order_iterator::operator++
post_order_iterator & operator++()
Definition: tree.hh:2228
tree::iterator_base::difference_type
ptrdiff_t difference_type
Definition: tree.hh:98
tree::end_post
post_order_iterator end_post() const
Return post-order end iterator of the tree.
Definition: tree.hh:659
tree::breadth_first_queued_iterator::operator+=
breadth_first_queued_iterator & operator+=(unsigned int)
Definition: tree.hh:2374
tree::append_child
iter append_child(iter position)
Insert empty node as last/first child of node pointed to by position.
Definition: tree.hh:838
tree::leaf_iterator::operator==
bool operator==(const leaf_iterator &) const
Definition: tree.hh:2032
tree::insert
iter insert(iter position, const T &x)
Insert node as previous sibling of node pointed to by position.
Definition: tree.hh:1015
tree::equal
bool equal(const iter &one, const iter &two, const iter &three) const
Compare two ranges of nodes (compares nodes as well as tree structure).
Definition: tree.hh:1573
tree::iterator_base::skip_current_children_
bool skip_current_children_
Definition: tree.hh:118
tree::end_fixed
fixed_depth_iterator end_fixed(const iterator_base &, unsigned int) const
Return fixed-depth end iterator.
Definition: tree.hh:696
tree::iterator_base::iterator_category
std::bidirectional_iterator_tag iterator_category
Definition: tree.hh:99
tree::post_order_iterator::operator-=
post_order_iterator & operator-=(unsigned int)
Definition: tree.hh:2292
tree::iterator_base::operator*
T & operator*() const
Definition: tree.hh:1971
tree::subtree
tree subtree(sibling_iterator from, sibling_iterator to) const
Extract a new tree formed by the range of siblings plus all their children.
Definition: tree.hh:1618
tree::sibling_iterator::operator!=
bool operator!=(const sibling_iterator &) const
Definition: tree.hh:2011
tree::sort
void sort(sibling_iterator from, sibling_iterator to, bool deep=false)
Sort (std::sort only moves values of nodes, this one moves children as well).
Definition: tree.hh:1505
tree::index
unsigned int index(sibling_iterator it) const
Determine the index of a node in the range of siblings to which it belongs.
Definition: tree.hh:1883
tree
Definition: tree.hh:67
tree::wrap
iter wrap(iter position, const T &x)
Replace node with a new node, making the old node a child of the new node.
Definition: tree.hh:1335
tree::fixed_depth_iterator
Iterator which traverses only the nodes at a given depth from the root.
Definition: tree.hh:182
tree::tree_node
tree_node_< T > tree_node
Definition: tree.hh:69
tree::head_initialise_
void head_initialise_()
Definition: tree.hh:508
tree::parent
static iter parent(iter)
Return iterator to the parent of a node.
Definition: tree.hh:765
tree::pre_order_iterator::operator==
bool operator==(const pre_order_iterator &) const
Definition: tree.hh:2004
tree::move_ontop
iter move_ontop(iter target, iter source)
Move &#39;source&#39; node (plus its children) to become the node at &#39;target&#39; (erasing the node at &#39;target&#39;)...
Definition: tree.hh:1442
tree::insert_subtree_after
iter insert_subtree_after(iter position, const iterator_base &subtree)
Insert node (with children) pointed to by subtree as next sibling of node pointed to by position...
Definition: tree.hh:1108
tree_node_::parent
tree_node_< T > * parent
Definition: tree.hh:48
tree::leaf_iterator::top_node
tree_node * top_node
Definition: tree.hh:243
tree::pre_order_iterator
Depth-first iterator, first accessing the node, then its children.
Definition: tree.hh:122
tree::leaf_iterator::operator--
leaf_iterator & operator--()
Definition: tree.hh:2731
tree::is_valid
bool is_valid(const iterator_base &) const
Determine whether the iterator is an &#39;end&#39; iterator and thus not actually pointing to a node...
Definition: tree.hh:1853
tree::fixed_depth_iterator::operator+=
fixed_depth_iterator & operator+=(unsigned int)
Definition: tree.hh:2560
tree::iterator_base_less
Comparator class for iterators (compares pointer values; why doesn&#39;t this work automatically?)
Definition: tree.hh:405
tree::prepend_children
iter prepend_children(iter position, sibling_iterator from, sibling_iterator to)
Definition: tree.hh:991
tree::fixed_depth_iterator::top_node
tree_node * top_node
Definition: tree.hh:199
tree::append_children
iter append_children(iter position, sibling_iterator from, sibling_iterator to)
Append the nodes in the from-to range (plus their children) as last/first children of position...
Definition: tree.hh:974
tree::prepend_child
iter prepend_child(iter position)
Definition: tree.hh:865
tree::compare_nodes::compare_nodes
compare_nodes(StrictWeakOrdering comp)
Definition: tree.hh:423
tree::compare_nodes::comp_
StrictWeakOrdering comp_
Definition: tree.hh:430
tree::sibling_iterator::set_parent_
void set_parent_()
Definition: tree.hh:2600
tree::pre_order_iterator::operator+=
pre_order_iterator & operator+=(unsigned int)
Definition: tree.hh:2172
tree::move_after
iter move_after(iter target, iter source)
Move &#39;source&#39; node (plus its children) to become the next sibling of &#39;target&#39;.
Definition: tree.hh:1346
tree::operator=
tree< T, tree_node_allocator > & operator=(const tree< T, tree_node_allocator > &)
Definition: tree.hh:529
tree::iterator
pre_order_iterator iterator
The default iterator types throughout the tree class.
Definition: tree.hh:178
tree::max_depth
int max_depth() const
Determine the maximal depth of the tree. An empty tree has max_depth=-1.
Definition: tree.hh:1693
tree::breadth_first_queued_iterator::traversal_queue
std::queue< tree_node * > traversal_queue
Definition: tree.hh:174
tree::begin_leaf
leaf_iterator begin_leaf() const
Return leaf iterator to the first leaf of the tree.
Definition: tree.hh:732
tree::iterator_base::reference
T & reference
Definition: tree.hh:96
tree::empty
bool empty() const
Check if tree is empty.
Definition: tree.hh:1660