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