00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00032 #ifndef tree_hh_
00033 #define tree_hh_
00034
00035 #include <cassert>
00036 #include <memory>
00037 #include <stdexcept>
00038 #include <iterator>
00039 #include <set>
00040 #include <queue>
00041 #include <algorithm>
00042 #include <cstddef>
00043
00044
00046 template<class T>
00047 class tree_node_ {
00048 public:
00049 tree_node_();
00050 tree_node_(const T&);
00051
00052 tree_node_<T> *parent;
00053 tree_node_<T> *first_child, *last_child;
00054 tree_node_<T> *prev_sibling, *next_sibling;
00055 T data;
00056 };
00057
00058 template<class T>
00059 tree_node_<T>::tree_node_()
00060 : parent(0), first_child(0), last_child(0), prev_sibling(0), next_sibling(0)
00061 {
00062 }
00063
00064 template<class T>
00065 tree_node_<T>::tree_node_(const T& val)
00066 : parent(0), first_child(0), last_child(0), prev_sibling(0), next_sibling(0), data(val)
00067 {
00068 }
00069
00070 template <class T, class tree_node_allocator = std::allocator<tree_node_<T> > >
00071 class tree {
00072 protected:
00073 typedef tree_node_<T> tree_node;
00074 public:
00076 typedef T value_type;
00077 bool ready_from_draw;
00078 bool need_layout;
00079
00080 class iterator_base;
00081 class pre_order_iterator;
00082 class post_order_iterator;
00083 class sibling_iterator;
00084 class leaf_iterator;
00085
00086 tree();
00087 tree(const T&);
00088 tree(const iterator_base&);
00089 tree(const tree<T, tree_node_allocator>&);
00090 ~tree();
00091 tree<T,tree_node_allocator>& operator=(const tree<T, tree_node_allocator>&);
00092
00094 #ifdef __SGI_STL_PORT
00095 class iterator_base : public stlport::bidirectional_iterator<T, ptrdiff_t> {
00096 #else
00097 class iterator_base {
00098 #endif
00099 public:
00100 typedef T value_type;
00101 typedef T* pointer;
00102 typedef T& reference;
00103 typedef size_t size_type;
00104 typedef ptrdiff_t difference_type;
00105 typedef std::bidirectional_iterator_tag iterator_category;
00106
00107 iterator_base();
00108 iterator_base(tree_node *);
00109
00110 T& operator*() const;
00111 T* operator->() const;
00112
00114 void skip_children();
00115 void skip_children(bool skip);
00117 unsigned int number_of_children() const;
00118
00119 sibling_iterator begin() const;
00120 sibling_iterator end() const;
00121
00122 tree_node *node;
00123 protected:
00124 bool skip_current_children_;
00125 };
00126
00128 class pre_order_iterator : public iterator_base {
00129 public:
00130 pre_order_iterator();
00131 pre_order_iterator(tree_node *);
00132 pre_order_iterator(const iterator_base&);
00133 pre_order_iterator(const sibling_iterator&);
00134
00135 bool operator==(const pre_order_iterator&) const;
00136 bool operator!=(const pre_order_iterator&) const;
00137 pre_order_iterator& operator++();
00138 pre_order_iterator& operator--();
00139 pre_order_iterator operator++(int);
00140 pre_order_iterator operator--(int);
00141 pre_order_iterator& operator+=(unsigned int);
00142 pre_order_iterator& operator-=(unsigned int);
00143 };
00144
00146 class post_order_iterator : public iterator_base {
00147 public:
00148 post_order_iterator();
00149 post_order_iterator(tree_node *);
00150 post_order_iterator(const iterator_base&);
00151 post_order_iterator(const sibling_iterator&);
00152
00153 bool operator==(const post_order_iterator&) const;
00154 bool operator!=(const post_order_iterator&) const;
00155 post_order_iterator& operator++();
00156 post_order_iterator& operator--();
00157 post_order_iterator operator++(int);
00158 post_order_iterator operator--(int);
00159 post_order_iterator& operator+=(unsigned int);
00160 post_order_iterator& operator-=(unsigned int);
00161
00163 void descend_all();
00164 };
00165
00167 class breadth_first_queued_iterator : public iterator_base {
00168 public:
00169 breadth_first_queued_iterator();
00170 breadth_first_queued_iterator(tree_node *);
00171 breadth_first_queued_iterator(const iterator_base&);
00172
00173 bool operator==(const breadth_first_queued_iterator&) const;
00174 bool operator!=(const breadth_first_queued_iterator&) const;
00175 breadth_first_queued_iterator& operator++();
00176 breadth_first_queued_iterator operator++(int);
00177 breadth_first_queued_iterator& operator+=(unsigned int);
00178
00179 private:
00180 std::queue<tree_node *> traversal_queue;
00181 };
00182
00184 typedef pre_order_iterator iterator;
00185 typedef breadth_first_queued_iterator breadth_first_iterator;
00186
00188 class fixed_depth_iterator : public iterator_base {
00189 public:
00190 fixed_depth_iterator();
00191 fixed_depth_iterator(tree_node *);
00192 fixed_depth_iterator(const iterator_base&);
00193 fixed_depth_iterator(const sibling_iterator&);
00194 fixed_depth_iterator(const fixed_depth_iterator&);
00195
00196 bool operator==(const fixed_depth_iterator&) const;
00197 bool operator!=(const fixed_depth_iterator&) const;
00198 fixed_depth_iterator& operator++();
00199 fixed_depth_iterator& operator--();
00200 fixed_depth_iterator operator++(int);
00201 fixed_depth_iterator operator--(int);
00202 fixed_depth_iterator& operator+=(unsigned int);
00203 fixed_depth_iterator& operator-=(unsigned int);
00204
00205 tree_node *top_node;
00206 };
00207
00209 class sibling_iterator : public iterator_base {
00210 public:
00211 sibling_iterator();
00212 sibling_iterator(tree_node *);
00213 sibling_iterator(const sibling_iterator&);
00214 sibling_iterator(const iterator_base&);
00215
00216 bool operator==(const sibling_iterator&) const;
00217 bool operator!=(const sibling_iterator&) const;
00218 sibling_iterator& operator++();
00219 sibling_iterator& operator--();
00220 sibling_iterator operator++(int);
00221 sibling_iterator operator--(int);
00222 sibling_iterator& operator+=(unsigned int);
00223 sibling_iterator& operator-=(unsigned int);
00224
00225 tree_node *range_first() const;
00226 tree_node *range_last() const;
00227 tree_node *parent_;
00228 private:
00229 void set_parent_();
00230 };
00231
00233 class leaf_iterator : public iterator_base {
00234 public:
00235 leaf_iterator();
00236 leaf_iterator(tree_node *, tree_node *top=0);
00237 leaf_iterator(const sibling_iterator&);
00238 leaf_iterator(const iterator_base&);
00239
00240 bool operator==(const leaf_iterator&) const;
00241 bool operator!=(const leaf_iterator&) const;
00242 leaf_iterator& operator++();
00243 leaf_iterator& operator--();
00244 leaf_iterator operator++(int);
00245 leaf_iterator operator--(int);
00246 leaf_iterator& operator+=(unsigned int);
00247 leaf_iterator& operator-=(unsigned int);
00248 private:
00249 tree_node *top_node;
00250 };
00251
00253 inline pre_order_iterator begin() const;
00255 inline pre_order_iterator end() const;
00257 post_order_iterator begin_post() const;
00259 post_order_iterator end_post() const;
00261 fixed_depth_iterator begin_fixed(const iterator_base&, unsigned int) const;
00263 fixed_depth_iterator end_fixed(const iterator_base&, unsigned int) const;
00265 breadth_first_queued_iterator begin_breadth_first() const;
00267 breadth_first_queued_iterator end_breadth_first() const;
00269 sibling_iterator begin(const iterator_base&) const;
00271 sibling_iterator end(const iterator_base&) const;
00273 leaf_iterator begin_leaf() const;
00275 leaf_iterator end_leaf() const;
00277 leaf_iterator begin_leaf(const iterator_base& top) const;
00279 leaf_iterator end_leaf(const iterator_base& top) const;
00280
00282 template<typename iter> static iter parent(iter);
00284 template<typename iter> iter previous_sibling(iter) const;
00286 template<typename iter> iter next_sibling(iter) const;
00288 template<typename iter> iter next_at_same_depth(iter) const;
00289
00291 void clear();
00293 template<typename iter> iter erase(iter);
00295 void erase_children(const iterator_base&);
00296
00298 void erase_branch(const iterator_base&);
00299
00301 template<typename iter> iter append_child(iter position);
00302 template<typename iter> iter prepend_child(iter position);
00304 template<typename iter> iter append_child(iter position, const T& x);
00305 template<typename iter> iter prepend_child(iter position, const T& x);
00307 template<typename iter> iter append_child(iter position, iter other_position);
00308 template<typename iter> iter prepend_child(iter position, iter other_position);
00310 template<typename iter> iter append_children(iter position, sibling_iterator from, sibling_iterator to);
00311 template<typename iter> iter prepend_children(iter position, sibling_iterator from, sibling_iterator to);
00312
00314 pre_order_iterator set_head(const T& x);
00316 template<typename iter> iter insert(iter position, const T& x);
00318 sibling_iterator insert(sibling_iterator position, const T& x);
00320 template<typename iter> iter insert_subtree(iter position, const iterator_base& subtree);
00322 template<typename iter> iter insert_after(iter position, const T& x);
00324 template<typename iter> iter insert_subtree_after(iter position, const iterator_base& subtree);
00325
00327 template<typename iter> iter replace(iter position, const T& x);
00329 template<typename iter> iter replace(iter position, const iterator_base& from);
00331 sibling_iterator replace(sibling_iterator orig_begin, sibling_iterator orig_end,
00332 sibling_iterator new_begin, sibling_iterator new_end);
00333
00335 template<typename iter> iter flatten(iter position);
00337 template<typename iter> iter reparent(iter position, sibling_iterator begin, sibling_iterator end);
00339 template<typename iter> iter reparent(iter position, iter from);
00340
00342 template<typename iter> iter wrap(iter position, const T& x);
00343
00345 template<typename iter> iter move_after(iter target, iter source);
00347 template<typename iter> iter move_before(iter target, iter source);
00348 sibling_iterator move_before(sibling_iterator target, sibling_iterator source);
00350 template<typename iter> iter move_ontop(iter target, iter source);
00351
00352 template<typename iter> iter move_ontop_same_branch(iter target, iter source);
00353
00354
00356 void merge(sibling_iterator, sibling_iterator, sibling_iterator, sibling_iterator,
00357 bool duplicate_leaves=false);
00359 void sort(sibling_iterator from, sibling_iterator to, bool deep=false);
00360 template<class StrictWeakOrdering>
00361 void sort(sibling_iterator from, sibling_iterator to, StrictWeakOrdering comp, bool deep=false);
00363 template<typename iter>
00364 bool equal(const iter& one, const iter& two, const iter& three) const;
00365 template<typename iter, class BinaryPredicate>
00366 bool equal(const iter& one, const iter& two, const iter& three, BinaryPredicate) const;
00367 template<typename iter>
00368 bool equal_subtree(const iter& one, const iter& two) const;
00369 template<typename iter, class BinaryPredicate>
00370 bool equal_subtree(const iter& one, const iter& two, BinaryPredicate) const;
00372 tree subtree(sibling_iterator from, sibling_iterator to) const;
00373 void subtree(tree&, sibling_iterator from, sibling_iterator to) const;
00375 void swap(sibling_iterator it);
00377 void swap(iterator, iterator);
00378
00380 size_t size() const;
00382 size_t size(const iterator_base&) const;
00384 bool empty() const;
00386 static int depth(const iterator_base&);
00387 static int depth(const iterator_base&, const iterator_base&);
00389 int max_depth() const;
00391 int max_depth(const iterator_base&) const;
00393 static unsigned int number_of_children(const iterator_base&);
00395 unsigned int number_of_siblings(const iterator_base&) const;
00397 bool is_in_subtree(const iterator_base& position, const iterator_base& begin,
00398 const iterator_base& end) const;
00400 bool is_valid(const iterator_base&) const;
00403 iterator lowest_common_ancestor(const iterator_base&, const iterator_base &) const;
00404
00406 unsigned int index(sibling_iterator it) const;
00408 static sibling_iterator child(const iterator_base& position, unsigned int);
00410 sibling_iterator sibling(const iterator_base& position, unsigned int);
00411
00414 void debug_verify_consistency() const;
00415
00417 class iterator_base_less {
00418 public:
00419 bool operator()(const typename tree<T, tree_node_allocator>::iterator_base& one,
00420 const typename tree<T, tree_node_allocator>::iterator_base& two) const
00421 {
00422 return one.node < two.node;
00423 }
00424 };
00425 tree_node *head, *feet;
00426 private:
00427 tree_node_allocator alloc_;
00428 void head_initialise_();
00429 void copy_(const tree<T, tree_node_allocator>& other);
00430
00432 template<class StrictWeakOrdering>
00433 class compare_nodes {
00434 public:
00435 compare_nodes(StrictWeakOrdering comp) : comp_(comp) {};
00436
00437 bool operator()(const tree_node *a, const tree_node *b)
00438 {
00439 return comp_(a->data, b->data);
00440 }
00441 private:
00442 StrictWeakOrdering comp_;
00443 };
00444 };
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488 template<class T, class tree_node_allocator>
00489 void tree<T, tree_node_allocator>::erase_branch(const iterator_base& it)
00490 {
00491
00492 if(it.node==0) return;
00493
00494 tree_node *cur=it.node;
00495
00496
00497 while(cur!=0 && number_of_siblings(cur)==0)
00498 cur=cur->parent;
00499
00500
00501
00502
00503 erase_children(cur);
00504 erase(iterator(cur));
00505 }
00506
00507 template <class T, class tree_node_allocator>
00508 tree<T, tree_node_allocator>::tree()
00509 {
00510 head_initialise_();
00511 }
00512
00513 template <class T, class tree_node_allocator>
00514 tree<T, tree_node_allocator>::tree(const T& x)
00515 {
00516 head_initialise_();
00517 set_head(x);
00518 }
00519
00520 template <class T, class tree_node_allocator>
00521 tree<T, tree_node_allocator>::tree(const iterator_base& other)
00522 {
00523 head_initialise_();
00524 set_head((*other));
00525 replace(begin(), other);
00526 }
00527
00528 template <class T, class tree_node_allocator>
00529 tree<T, tree_node_allocator>::~tree()
00530 {
00531 clear();
00532 alloc_.destroy(head);
00533 alloc_.destroy(feet);
00534 alloc_.deallocate(head,1);
00535 alloc_.deallocate(feet,1);
00536 }
00537
00538 template <class T, class tree_node_allocator>
00539 void tree<T, tree_node_allocator>::head_initialise_()
00540 {
00541 head = alloc_.allocate(1,0);
00542 feet = alloc_.allocate(1,0);
00543 alloc_.construct(head, tree_node_<T>());
00544 alloc_.construct(feet, tree_node_<T>());
00545
00546 head->parent=0;
00547 head->first_child=0;
00548 head->last_child=0;
00549 head->prev_sibling=0;
00550 head->next_sibling=feet;
00551
00552 feet->parent=0;
00553 feet->first_child=0;
00554 feet->last_child=0;
00555 feet->prev_sibling=head;
00556 feet->next_sibling=0;
00557 }
00558
00559 template <class T, class tree_node_allocator>
00560 tree<T,tree_node_allocator>& tree<T, tree_node_allocator>::operator=(const tree<T, tree_node_allocator>& other)
00561 {
00562 if(this != &other)
00563 copy_(other);
00564 return *this;
00565 }
00566
00567 template <class T, class tree_node_allocator>
00568 tree<T, tree_node_allocator>::tree(const tree<T, tree_node_allocator>& other)
00569 {
00570 head_initialise_();
00571 copy_(other);
00572 }
00573
00574 template <class T, class tree_node_allocator>
00575 void tree<T, tree_node_allocator>::copy_(const tree<T, tree_node_allocator>& other)
00576 {
00577 clear();
00578 pre_order_iterator it=other.begin(), to=begin();
00579 while(it!=other.end()) {
00580 to=insert(to, (*it));
00581 it.skip_children();
00582 ++it;
00583 }
00584 to=begin();
00585 it=other.begin();
00586 while(it!=other.end()) {
00587 to=replace(to, it);
00588 to.skip_children();
00589 it.skip_children();
00590 ++to;
00591 ++it;
00592 }
00593 }
00594
00595 template <class T, class tree_node_allocator>
00596 void tree<T, tree_node_allocator>::clear()
00597 {
00598 if(head)
00599 while(head->next_sibling!=feet)
00600 erase(pre_order_iterator(head->next_sibling));
00601 }
00602
00603 template<class T, class tree_node_allocator>
00604 void tree<T, tree_node_allocator>::erase_children(const iterator_base& it)
00605 {
00606 if(it.node==0) return;
00607
00608 tree_node *cur=it.node->first_child;
00609 tree_node *prev=0;
00610
00611 while(cur!=0) {
00612 prev=cur;
00613 cur=cur->next_sibling;
00614 erase_children(pre_order_iterator(prev));
00615
00616
00617
00618 alloc_.destroy(prev);
00619 alloc_.deallocate(prev,1);
00620 }
00621 it.node->first_child=0;
00622 it.node->last_child=0;
00623 }
00624
00625 template<class T, class tree_node_allocator>
00626 template<class iter>
00627 iter tree<T, tree_node_allocator>::erase(iter it)
00628 {
00629 tree_node *cur=it.node;
00630 assert(cur!=head);
00631 iter ret=it;
00632 ret.skip_children();
00633 ++ret;
00634 erase_children(it);
00635 if(cur->prev_sibling==0) {
00636 cur->parent->first_child=cur->next_sibling;
00637 }
00638 else {
00639 cur->prev_sibling->next_sibling=cur->next_sibling;
00640 }
00641 if(cur->next_sibling==0) {
00642 cur->parent->last_child=cur->prev_sibling;
00643 }
00644 else {
00645 cur->next_sibling->prev_sibling=cur->prev_sibling;
00646 }
00647
00648
00649
00650
00651 alloc_.destroy(cur);
00652 alloc_.deallocate(cur,1);
00653 return ret;
00654 }
00655
00656 template <class T, class tree_node_allocator>
00657 typename tree<T, tree_node_allocator>::pre_order_iterator tree<T, tree_node_allocator>::begin() const
00658 {
00659 return pre_order_iterator(head->next_sibling);
00660 }
00661
00662 template <class T, class tree_node_allocator>
00663 typename tree<T, tree_node_allocator>::pre_order_iterator tree<T, tree_node_allocator>::end() const
00664 {
00665 return pre_order_iterator(feet);
00666 }
00667
00668 template <class T, class tree_node_allocator>
00669 typename tree<T, tree_node_allocator>::breadth_first_queued_iterator tree<T, tree_node_allocator>::begin_breadth_first() const
00670 {
00671 return breadth_first_queued_iterator(head->next_sibling);
00672 }
00673
00674 template <class T, class tree_node_allocator>
00675 typename tree<T, tree_node_allocator>::breadth_first_queued_iterator tree<T, tree_node_allocator>::end_breadth_first() const
00676 {
00677 return breadth_first_queued_iterator();
00678 }
00679
00680 template <class T, class tree_node_allocator>
00681 typename tree<T, tree_node_allocator>::post_order_iterator tree<T, tree_node_allocator>::begin_post() const
00682 {
00683 tree_node *tmp=head->next_sibling;
00684 if(tmp!=feet) {
00685 while(tmp->first_child)
00686 tmp=tmp->first_child;
00687 }
00688 return post_order_iterator(tmp);
00689 }
00690
00691 template <class T, class tree_node_allocator>
00692 typename tree<T, tree_node_allocator>::post_order_iterator tree<T, tree_node_allocator>::end_post() const
00693 {
00694 return post_order_iterator(feet);
00695 }
00696
00697 template <class T, class tree_node_allocator>
00698 typename tree<T, tree_node_allocator>::fixed_depth_iterator tree<T, tree_node_allocator>::begin_fixed(const iterator_base& pos, unsigned int dp) const
00699 {
00700 typename tree<T, tree_node_allocator>::fixed_depth_iterator ret;
00701 ret.top_node=pos.node;
00702
00703 tree_node *tmp=pos.node;
00704 unsigned int curdepth=0;
00705 while(curdepth<dp) {
00706 while(tmp->first_child==0) {
00707 if(tmp->next_sibling==0) {
00708
00709 do {
00710 if(tmp==ret.top_node)
00711 throw std::range_error("tree: begin_fixed out of range");
00712 tmp=tmp->parent;
00713 if(tmp==0)
00714 throw std::range_error("tree: begin_fixed out of range");
00715 --curdepth;
00716 } while(tmp->next_sibling==0);
00717 }
00718 tmp=tmp->next_sibling;
00719 }
00720 tmp=tmp->first_child;
00721 ++curdepth;
00722 }
00723
00724 ret.node=tmp;
00725 return ret;
00726 }
00727
00728 template <class T, class tree_node_allocator>
00729 typename tree<T, tree_node_allocator>::fixed_depth_iterator tree<T, tree_node_allocator>::end_fixed(const iterator_base& pos, unsigned int dp) const
00730 {
00731 assert(1==0);
00732 tree_node *tmp=pos.node;
00733 unsigned int curdepth=1;
00734 while(curdepth<dp) {
00735 while(tmp->first_child==0) {
00736 tmp=tmp->next_sibling;
00737 if(tmp==0)
00738 throw std::range_error("tree: end_fixed out of range");
00739 }
00740 tmp=tmp->first_child;
00741 ++curdepth;
00742 }
00743 return tmp;
00744 }
00745
00746 template <class T, class tree_node_allocator>
00747 typename tree<T, tree_node_allocator>::sibling_iterator tree<T, tree_node_allocator>::begin(const iterator_base& pos) const
00748 {
00749 assert(pos.node!=0);
00750 if(pos.node->first_child==0) {
00751 return end(pos);
00752 }
00753 return pos.node->first_child;
00754 }
00755
00756 template <class T, class tree_node_allocator>
00757 typename tree<T, tree_node_allocator>::sibling_iterator tree<T, tree_node_allocator>::end(const iterator_base& pos) const
00758 {
00759 sibling_iterator ret(0);
00760 ret.parent_=pos.node;
00761 return ret;
00762 }
00763
00764 template <class T, class tree_node_allocator>
00765 typename tree<T, tree_node_allocator>::leaf_iterator tree<T, tree_node_allocator>::begin_leaf() const
00766 {
00767 tree_node *tmp=head->next_sibling;
00768 if(tmp!=feet) {
00769 while(tmp->first_child)
00770 tmp=tmp->first_child;
00771 }
00772 return leaf_iterator(tmp);
00773 }
00774
00775 template <class T, class tree_node_allocator>
00776 typename tree<T, tree_node_allocator>::leaf_iterator tree<T, tree_node_allocator>::end_leaf() const
00777 {
00778 return leaf_iterator(feet);
00779 }
00780
00781 template <class T, class tree_node_allocator>
00782 typename tree<T, tree_node_allocator>::leaf_iterator tree<T, tree_node_allocator>::begin_leaf(const iterator_base& top) const
00783 {
00784 tree_node *tmp=top.node;
00785 while(tmp->first_child)
00786 tmp=tmp->first_child;
00787 return leaf_iterator(tmp, top.node);
00788 }
00789
00790 template <class T, class tree_node_allocator>
00791 typename tree<T, tree_node_allocator>::leaf_iterator tree<T, tree_node_allocator>::end_leaf(const iterator_base& top) const
00792 {
00793 return leaf_iterator(top.node, top.node);
00794 }
00795
00796 template <class T, class tree_node_allocator>
00797 template <typename iter>
00798 iter tree<T, tree_node_allocator>::parent(iter position)
00799 {
00800 assert(position.node!=0);
00801 return iter(position.node->parent);
00802 }
00803
00804 template <class T, class tree_node_allocator>
00805 template <typename iter>
00806 iter tree<T, tree_node_allocator>::previous_sibling(iter position) const
00807 {
00808 assert(position.node!=0);
00809 iter ret(position);
00810 ret.node=position.node->prev_sibling;
00811 return ret;
00812 }
00813
00814 template <class T, class tree_node_allocator>
00815 template <typename iter>
00816 iter tree<T, tree_node_allocator>::next_sibling(iter position) const
00817 {
00818 assert(position.node!=0);
00819 iter ret(position);
00820 ret.node=position.node->next_sibling;
00821 return ret;
00822 }
00823
00824 template <class T, class tree_node_allocator>
00825 template <typename iter>
00826 iter tree<T, tree_node_allocator>::next_at_same_depth(iter position) const
00827 {
00828
00829
00830 typename tree<T, tree_node_allocator>::fixed_depth_iterator tmp(position.node);
00831
00832 ++tmp;
00833 return iter(tmp);
00834
00835
00836
00837
00838
00839
00840
00841
00842
00843
00844
00845
00846
00847
00848
00849
00850
00851
00852
00853
00854
00855
00856
00857
00858
00859
00860
00861
00862
00863
00864
00865
00866
00867 }
00868
00869 template <class T, class tree_node_allocator>
00870 template <typename iter>
00871 iter tree<T, tree_node_allocator>::append_child(iter position)
00872 {
00873 assert(position.node!=head);
00874 assert(position.node!=feet);
00875 assert(position.node);
00876
00877 tree_node *tmp=alloc_.allocate(1,0);
00878 alloc_.construct(tmp, tree_node_<T>());
00879
00880 tmp->first_child=0;
00881 tmp->last_child=0;
00882
00883 tmp->parent=position.node;
00884 if(position.node->last_child!=0) {
00885 position.node->last_child->next_sibling=tmp;
00886 }
00887 else {
00888 position.node->first_child=tmp;
00889 }
00890 tmp->prev_sibling=position.node->last_child;
00891 position.node->last_child=tmp;
00892 tmp->next_sibling=0;
00893 return tmp;
00894 }
00895
00896 template <class T, class tree_node_allocator>
00897 template <typename iter>
00898 iter tree<T, tree_node_allocator>::prepend_child(iter position)
00899 {
00900 assert(position.node!=head);
00901 assert(position.node!=feet);
00902 assert(position.node);
00903
00904 tree_node *tmp=alloc_.allocate(1,0);
00905 alloc_.construct(tmp, tree_node_<T>());
00906
00907 tmp->first_child=0;
00908 tmp->last_child=0;
00909
00910 tmp->parent=position.node;
00911 if(position.node->first_child!=0) {
00912 position.node->first_child->prev_sibling=tmp;
00913 }
00914 else {
00915 position.node->last_child=tmp;
00916 }
00917 tmp->next_sibling=position.node->first_child;
00918 position.node->prev_child=tmp;
00919 tmp->prev_sibling=0;
00920 return tmp;
00921 }
00922
00923 template <class T, class tree_node_allocator>
00924 template <class iter>
00925 iter tree<T, tree_node_allocator>::append_child(iter position, const T& x)
00926 {
00927
00928
00929
00930
00931 assert(position.node!=head);
00932 assert(position.node!=feet);
00933 assert(position.node);
00934
00935 tree_node* tmp = alloc_.allocate(1,0);
00936 alloc_.construct(tmp, x);
00937
00938 tmp->first_child=0;
00939 tmp->last_child=0;
00940
00941 tmp->parent=position.node;
00942 if(position.node->last_child!=0) {
00943 position.node->last_child->next_sibling=tmp;
00944 }
00945 else {
00946 position.node->first_child=tmp;
00947 }
00948 tmp->prev_sibling=position.node->last_child;
00949 position.node->last_child=tmp;
00950 tmp->next_sibling=0;
00951 return tmp;
00952 }
00953
00954 template <class T, class tree_node_allocator>
00955 template <class iter>
00956 iter tree<T, tree_node_allocator>::prepend_child(iter position, const T& x)
00957 {
00958 assert(position.node!=head);
00959 assert(position.node!=feet);
00960 assert(position.node);
00961
00962 tree_node* tmp = alloc_.allocate(1,0);
00963 alloc_.construct(tmp, x);
00964
00965 tmp->first_child=0;
00966 tmp->last_child=0;
00967
00968 tmp->parent=position.node;
00969 if(position.node->first_child!=0) {
00970 position.node->first_child->prev_sibling=tmp;
00971 }
00972 else {
00973 position.node->last_child=tmp;
00974 }
00975 tmp->next_sibling=position.node->first_child;
00976 position.node->first_child=tmp;
00977 tmp->prev_sibling=0;
00978 return tmp;
00979 }
00980
00981 template <class T, class tree_node_allocator>
00982 template <class iter>
00983 iter tree<T, tree_node_allocator>::append_child(iter position, iter other)
00984 {
00985 assert(position.node!=head);
00986 assert(position.node!=feet);
00987 assert(position.node);
00988
00989 sibling_iterator aargh=append_child(position, value_type());
00990 return replace(aargh, other);
00991 }
00992
00993 template <class T, class tree_node_allocator>
00994 template <class iter>
00995 iter tree<T, tree_node_allocator>::prepend_child(iter position, iter other)
00996 {
00997 assert(position.node!=head);
00998 assert(position.node!=feet);
00999 assert(position.node);
01000
01001 sibling_iterator aargh=prepend_child(position, value_type());
01002 return replace(aargh, other);
01003 }
01004
01005 template <class T, class tree_node_allocator>
01006 template <class iter>
01007 iter tree<T, tree_node_allocator>::append_children(iter position, sibling_iterator from, sibling_iterator to)
01008 {
01009 assert(position.node!=head);
01010 assert(position.node!=feet);
01011 assert(position.node);
01012
01013 iter ret=from;
01014
01015 while(from!=to) {
01016 insert_subtree(position.end(), from);
01017 ++from;
01018 }
01019 return ret;
01020 }
01021
01022 template <class T, class tree_node_allocator>
01023 template <class iter>
01024 iter tree<T, tree_node_allocator>::prepend_children(iter position, sibling_iterator from, sibling_iterator to)
01025 {
01026 assert(position.node!=head);
01027 assert(position.node!=feet);
01028 assert(position.node);
01029
01030 iter ret=from;
01031
01032 while(from!=to) {
01033 insert_subtree(position.begin(), from);
01034 ++from;
01035 }
01036 return ret;
01037 }
01038
01039 template <class T, class tree_node_allocator>
01040 typename tree<T, tree_node_allocator>::pre_order_iterator tree<T, tree_node_allocator>::set_head(const T& x)
01041 {
01042 assert(head->next_sibling==feet);
01043 return insert(iterator(feet), x);
01044 }
01045
01046 template <class T, class tree_node_allocator>
01047 template <class iter>
01048 iter tree<T, tree_node_allocator>::insert(iter position, const T& x)
01049 {
01050 if(position.node==0) {
01051 position.node=feet;
01052
01053 }
01054 tree_node* tmp = alloc_.allocate(1,0);
01055 alloc_.construct(tmp, x);
01056
01057 tmp->first_child=0;
01058 tmp->last_child=0;
01059
01060 tmp->parent=position.node->parent;
01061 tmp->next_sibling=position.node;
01062 tmp->prev_sibling=position.node->prev_sibling;
01063 position.node->prev_sibling=tmp;
01064
01065 if(tmp->prev_sibling==0) {
01066 if(tmp->parent)
01067 tmp->parent->first_child=tmp;
01068 }
01069 else
01070 tmp->prev_sibling->next_sibling=tmp;
01071 return tmp;
01072 }
01073
01074 template <class T, class tree_node_allocator>
01075 typename tree<T, tree_node_allocator>::sibling_iterator tree<T, tree_node_allocator>::insert(sibling_iterator position, const T& x)
01076 {
01077 tree_node* tmp = alloc_.allocate(1,0);
01078 alloc_.construct(tmp, x);
01079
01080 tmp->first_child=0;
01081 tmp->last_child=0;
01082
01083 tmp->next_sibling=position.node;
01084 if(position.node==0) {
01085 tmp->parent=position.parent_;
01086 tmp->prev_sibling=position.range_last();
01087 tmp->parent->last_child=tmp;
01088 }
01089 else {
01090 tmp->parent=position.node->parent;
01091 tmp->prev_sibling=position.node->prev_sibling;
01092 position.node->prev_sibling=tmp;
01093 }
01094
01095 if(tmp->prev_sibling==0) {
01096 if(tmp->parent)
01097 tmp->parent->first_child=tmp;
01098 }
01099 else
01100 tmp->prev_sibling->next_sibling=tmp;
01101 return tmp;
01102 }
01103
01104 template <class T, class tree_node_allocator>
01105 template <class iter>
01106 iter tree<T, tree_node_allocator>::insert_after(iter position, const T& x)
01107 {
01108 tree_node* tmp = alloc_.allocate(1,0);
01109 alloc_.construct(tmp, x);
01110
01111 tmp->first_child=0;
01112 tmp->last_child=0;
01113
01114 tmp->parent=position.node->parent;
01115 tmp->prev_sibling=position.node;
01116 tmp->next_sibling=position.node->next_sibling;
01117 position.node->next_sibling=tmp;
01118
01119 if(tmp->next_sibling==0) {
01120 if(tmp->parent)
01121 tmp->parent->last_child=tmp;
01122 }
01123 else {
01124 tmp->next_sibling->prev_sibling=tmp;
01125 }
01126 return tmp;
01127 }
01128
01129 template <class T, class tree_node_allocator>
01130 template <class iter>
01131 iter tree<T, tree_node_allocator>::insert_subtree(iter position, const iterator_base& subtree)
01132 {
01133
01134 iter it=insert(position, value_type());
01135
01136 return replace(it, subtree);
01137 }
01138
01139 template <class T, class tree_node_allocator>
01140 template <class iter>
01141 iter tree<T, tree_node_allocator>::insert_subtree_after(iter position, const iterator_base& subtree)
01142 {
01143
01144 iter it=insert_after(position, value_type());
01145
01146 return replace(it, subtree);
01147 }
01148
01149
01150
01151
01152
01153
01154
01155
01156
01157
01158
01159 template <class T, class tree_node_allocator>
01160 template <class iter>
01161 iter tree<T, tree_node_allocator>::replace(iter position, const T& x)
01162 {
01163
01164
01165 position.node->data=x;
01166
01167
01168 return position;
01169 }
01170
01171 template <class T, class tree_node_allocator>
01172 template <class iter>
01173 iter tree<T, tree_node_allocator>::replace(iter position, const iterator_base& from)
01174 {
01175 assert(position.node!=head);
01176 tree_node *current_from=from.node;
01177 tree_node *start_from=from.node;
01178 tree_node *current_to =position.node;
01179
01180
01181
01182 erase_children(position);
01183
01184 tree_node* tmp = alloc_.allocate(1,0);
01185 alloc_.construct(tmp, (*from));
01186
01187 tmp->first_child=0;
01188 tmp->last_child=0;
01189 if(current_to->prev_sibling==0) {
01190 if(current_to->parent!=0)
01191 current_to->parent->first_child=tmp;
01192 }
01193 else {
01194 current_to->prev_sibling->next_sibling=tmp;
01195 }
01196 tmp->prev_sibling=current_to->prev_sibling;
01197 if(current_to->next_sibling==0) {
01198 if(current_to->parent!=0)
01199 current_to->parent->last_child=tmp;
01200 }
01201 else {
01202 current_to->next_sibling->prev_sibling=tmp;
01203 }
01204 tmp->next_sibling=current_to->next_sibling;
01205 tmp->parent=current_to->parent;
01206
01207 alloc_.destroy(current_to);
01208 alloc_.deallocate(current_to,1);
01209 current_to=tmp;
01210
01211
01212 tree_node *last=from.node->next_sibling;
01213
01214 pre_order_iterator toit=tmp;
01215
01216 do {
01217 assert(current_from!=0);
01218 if(current_from->first_child != 0) {
01219 current_from=current_from->first_child;
01220 toit=append_child(toit, current_from->data);
01221 }
01222 else {
01223 while(current_from->next_sibling==0 && current_from!=start_from) {
01224 current_from=current_from->parent;
01225 toit=parent(toit);
01226 assert(current_from!=0);
01227 }
01228 current_from=current_from->next_sibling;
01229 if(current_from!=last) {
01230 toit=append_child(parent(toit), current_from->data);
01231 }
01232 }
01233 } while(current_from!=last);
01234
01235 return current_to;
01236 }
01237
01238 template <class T, class tree_node_allocator>
01239 typename tree<T, tree_node_allocator>::sibling_iterator tree<T, tree_node_allocator>::replace(
01240 sibling_iterator orig_begin,
01241 sibling_iterator orig_end,
01242 sibling_iterator new_begin,
01243 sibling_iterator new_end)
01244 {
01245 tree_node *orig_first=orig_begin.node;
01246 tree_node *new_first=new_begin.node;
01247 tree_node *orig_last=orig_first;
01248 while((++orig_begin)!=orig_end)
01249 orig_last=orig_last->next_sibling;
01250 tree_node *new_last=new_first;
01251 while((++new_begin)!=new_end)
01252 new_last=new_last->next_sibling;
01253
01254
01255 bool first=true;
01256 pre_order_iterator ret;
01257 while(1==1) {
01258 pre_order_iterator tt=insert_subtree(pre_order_iterator(orig_first), pre_order_iterator(new_first));
01259 if(first) {
01260 ret=tt;
01261 first=false;
01262 }
01263 if(new_first==new_last)
01264 break;
01265 new_first=new_first->next_sibling;
01266 }
01267
01268
01269 bool last=false;
01270 tree_node *next=orig_first;
01271 while(1==1) {
01272 if(next==orig_last)
01273 last=true;
01274 next=next->next_sibling;
01275 erase((pre_order_iterator)orig_first);
01276 if(last)
01277 break;
01278 orig_first=next;
01279 }
01280 return ret;
01281 }
01282
01283 template <class T, class tree_node_allocator>
01284 template <typename iter>
01285 iter tree<T, tree_node_allocator>::flatten(iter position)
01286 {
01287 if(position.node->first_child==0)
01288 return position;
01289
01290 tree_node *tmp=position.node->first_child;
01291 while(tmp) {
01292 tmp->parent=position.node->parent;
01293 tmp=tmp->next_sibling;
01294 }
01295 if(position.node->next_sibling) {
01296 position.node->last_child->next_sibling=position.node->next_sibling;
01297 position.node->next_sibling->prev_sibling=position.node->last_child;
01298 }
01299 else {
01300 position.node->parent->last_child=position.node->last_child;
01301 }
01302 position.node->next_sibling=position.node->first_child;
01303 position.node->next_sibling->prev_sibling=position.node;
01304 position.node->first_child=0;
01305 position.node->last_child=0;
01306
01307 return position;
01308 }
01309
01310
01311 template <class T, class tree_node_allocator>
01312 template <typename iter>
01313 iter tree<T, tree_node_allocator>::reparent(iter position, sibling_iterator begin, sibling_iterator end)
01314 {
01315 tree_node *first=begin.node;
01316 tree_node *last=first;
01317
01318 assert(first!=position.node);
01319
01320 if(begin==end) return begin;
01321
01322 while((++begin)!=end) {
01323 last=last->next_sibling;
01324 }
01325
01326 if(first->prev_sibling==0) {
01327 first->parent->first_child=last->next_sibling;
01328 }
01329 else {
01330 first->prev_sibling->next_sibling=last->next_sibling;
01331 }
01332 if(last->next_sibling==0) {
01333 last->parent->last_child=first->prev_sibling;
01334 }
01335 else {
01336 last->next_sibling->prev_sibling=first->prev_sibling;
01337 }
01338 if(position.node->first_child==0) {
01339 position.node->first_child=first;
01340 position.node->last_child=last;
01341 first->prev_sibling=0;
01342 }
01343 else {
01344 position.node->last_child->next_sibling=first;
01345 first->prev_sibling=position.node->last_child;
01346 position.node->last_child=last;
01347 }
01348 last->next_sibling=0;
01349
01350 tree_node *pos=first;
01351 for(;;) {
01352 pos->parent=position.node;
01353 if(pos==last) break;
01354 pos=pos->next_sibling;
01355 }
01356
01357 return first;
01358 }
01359
01360 template <class T, class tree_node_allocator>
01361 template <typename iter> iter tree<T, tree_node_allocator>::reparent(iter position, iter from)
01362 {
01363
01364 if(from.node->first_child==0)
01365 return position;
01366
01367 return reparent(position, from.node->first_child, end(from));
01368 }
01369
01370 template <class T, class tree_node_allocator>
01371 template <typename iter> iter tree<T, tree_node_allocator>::wrap(iter position, const T& x)
01372 {
01373 assert(position.node!=0);
01374 sibling_iterator fr=position, to=position;
01375 ++to;
01376 iter ret = insert(position, x);
01377 reparent(ret, fr, to);
01378 return ret;
01379 }
01380
01381 template <class T, class tree_node_allocator>
01382 template <typename iter> iter tree<T, tree_node_allocator>::move_after(iter target, iter source)
01383 {
01384 tree_node *dst=target.node;
01385 tree_node *src=source.node;
01386 assert(dst);
01387 assert(src);
01388
01389 if(dst==src) return source;
01390 if(dst->next_sibling)
01391 if(dst->next_sibling==src)
01392 return source;
01393
01394
01395 if(src->prev_sibling!=0) src->prev_sibling->next_sibling=src->next_sibling;
01396 else src->parent->first_child=src->next_sibling;
01397 if(src->next_sibling!=0) src->next_sibling->prev_sibling=src->prev_sibling;
01398 else src->parent->last_child=src->prev_sibling;
01399
01400
01401 if(dst->next_sibling!=0) dst->next_sibling->prev_sibling=src;
01402 else dst->parent->last_child=src;
01403 src->next_sibling=dst->next_sibling;
01404 dst->next_sibling=src;
01405 src->prev_sibling=dst;
01406 src->parent=dst->parent;
01407 return src;
01408 }
01409
01410 template <class T, class tree_node_allocator>
01411 template <typename iter> iter tree<T, tree_node_allocator>::move_before(iter target, iter source)
01412 {
01413 tree_node *dst=target.node;
01414 tree_node *src=source.node;
01415 assert(dst);
01416 assert(src);
01417
01418 if(dst==src) return source;
01419 if(dst->prev_sibling)
01420 if(dst->prev_sibling==src)
01421 return source;
01422
01423
01424 if(src->prev_sibling!=0) src->prev_sibling->next_sibling=src->next_sibling;
01425 else src->parent->first_child=src->next_sibling;
01426 if(src->next_sibling!=0) src->next_sibling->prev_sibling=src->prev_sibling;
01427 else src->parent->last_child=src->prev_sibling;
01428
01429
01430 if(dst->prev_sibling!=0) dst->prev_sibling->next_sibling=src;
01431 else dst->parent->first_child=src;
01432 src->prev_sibling=dst->prev_sibling;
01433 dst->prev_sibling=src;
01434 src->next_sibling=dst;
01435 src->parent=dst->parent;
01436 return src;
01437 }
01438
01439
01440 template <class T, class tree_node_allocator>
01441 typename tree<T, tree_node_allocator>::sibling_iterator tree<T, tree_node_allocator>::move_before(sibling_iterator target,
01442 sibling_iterator source)
01443 {
01444 tree_node *dst=target.node;
01445 tree_node *src=source.node;
01446 tree_node *dst_prev_sibling;
01447 if(dst==0) {
01448 dst_prev_sibling=target.parent_->last_child;
01449 assert(dst_prev_sibling);
01450 }
01451 else dst_prev_sibling=dst->prev_sibling;
01452 assert(src);
01453
01454 if(dst==src) return source;
01455 if(dst_prev_sibling)
01456 if(dst_prev_sibling==src)
01457 return source;
01458
01459
01460 if(src->prev_sibling!=0) src->prev_sibling->next_sibling=src->next_sibling;
01461 else src->parent->first_child=src->next_sibling;
01462 if(src->next_sibling!=0) src->next_sibling->prev_sibling=src->prev_sibling;
01463 else src->parent->last_child=src->prev_sibling;
01464
01465
01466 if(dst_prev_sibling!=0) dst_prev_sibling->next_sibling=src;
01467 else target.parent_->first_child=src;
01468 src->prev_sibling=dst_prev_sibling;
01469 if(dst) {
01470 dst->prev_sibling=src;
01471 src->parent=dst->parent;
01472 }
01473 src->next_sibling=dst;
01474 return src;
01475 }
01476
01477 template <class T, class tree_node_allocator>
01478 template <typename iter> iter tree<T, tree_node_allocator>::move_ontop(iter target, iter source)
01479 {
01480 tree_node *dst=target.node;
01481 tree_node *src=source.node;
01482 assert(dst);
01483 assert(src);
01484
01485 if(dst==src) return source;
01486
01487
01488
01489
01490
01491
01492 tree_node *b_prev_sibling=dst->prev_sibling;
01493 tree_node *b_next_sibling=dst->next_sibling;
01494 tree_node *b_parent=dst->parent;
01495
01496
01497 erase(target);
01498
01499
01500 if(src->prev_sibling!=0) src->prev_sibling->next_sibling=src->next_sibling;
01501 else src->parent->first_child=src->next_sibling;
01502 if(src->next_sibling!=0) src->next_sibling->prev_sibling=src->prev_sibling;
01503 else src->parent->last_child=src->prev_sibling;
01504
01505
01506 if(b_prev_sibling!=0) b_prev_sibling->next_sibling=src;
01507 else b_parent->first_child=src;
01508 if(b_next_sibling!=0) b_next_sibling->prev_sibling=src;
01509 else b_parent->last_child=src;
01510 src->prev_sibling=b_prev_sibling;
01511 src->next_sibling=b_next_sibling;
01512 src->parent=b_parent;
01513 return src;
01514 }
01515
01516 template <class T, class tree_node_allocator>
01517 template <typename iter> iter tree<T, tree_node_allocator>::move_ontop_same_branch(iter target, iter source)
01518 {
01519 tree_node *dst=target.node;
01520 tree_node *src=source.node;
01521 assert(dst);
01522 assert(src);
01523
01524 if(dst==src) return source;
01525
01526
01527
01528
01529
01530
01531 tree_node *b_prev_sibling=dst->prev_sibling;
01532 tree_node *b_next_sibling=dst->next_sibling;
01533 tree_node *b_parent=dst->parent;
01534
01535
01536
01537
01538 alloc_.destroy(target.node);
01539 alloc_.deallocate(target.node,1);
01540
01541
01542 if(src->prev_sibling!=0) src->prev_sibling->next_sibling=src->next_sibling;
01543 else src->parent->first_child=src->next_sibling;
01544 if(src->next_sibling!=0) src->next_sibling->prev_sibling=src->prev_sibling;
01545 else src->parent->last_child=src->prev_sibling;
01546
01547
01548 if(b_prev_sibling!=0) b_prev_sibling->next_sibling=src;
01549 else b_parent->first_child=src;
01550 if(b_next_sibling!=0) b_next_sibling->prev_sibling=src;
01551 else b_parent->last_child=src;
01552 src->prev_sibling=b_prev_sibling;
01553 src->next_sibling=b_next_sibling;
01554 src->parent=b_parent;
01555 return src;
01556 }
01557
01558 template <class T, class tree_node_allocator>
01559 void tree<T, tree_node_allocator>::merge(sibling_iterator to1, sibling_iterator to2,
01560 sibling_iterator from1, sibling_iterator from2,
01561 bool duplicate_leaves)
01562 {
01563 sibling_iterator fnd;
01564 while(from1!=from2) {
01565 if((fnd=std::find(to1, to2, (*from1))) != to2) {
01566 if(from1.begin()==from1.end()) {
01567 if(duplicate_leaves)
01568 append_child(parent(to1), (*from1));
01569 }
01570 else {
01571 merge(fnd.begin(), fnd.end(), from1.begin(), from1.end(), duplicate_leaves);
01572 }
01573 }
01574 else {
01575 insert_subtree(to2, from1);
01576 }
01577 ++from1;
01578 }
01579 }
01580
01581
01582 template <class T, class tree_node_allocator>
01583 void tree<T, tree_node_allocator>::sort(sibling_iterator from, sibling_iterator to, bool deep)
01584 {
01585 std::less<T> comp;
01586 sort(from, to, comp, deep);
01587 }
01588
01589 template <class T, class tree_node_allocator>
01590 template <class StrictWeakOrdering>
01591 void tree<T, tree_node_allocator>::sort(sibling_iterator from, sibling_iterator to,
01592 StrictWeakOrdering comp, bool deep)
01593 {
01594 if(from==to) return;
01595
01596
01597
01598 std::multiset<tree_node *, compare_nodes<StrictWeakOrdering> > nodes(comp);
01599 sibling_iterator it=from, it2=to;
01600 while(it != to) {
01601 nodes.insert(it.node);
01602 ++it;
01603 }
01604
01605 --it2;
01606
01607
01608 tree_node *prev=from.node->prev_sibling;
01609 tree_node *next=it2.node->next_sibling;
01610 typename std::multiset<tree_node *, compare_nodes<StrictWeakOrdering> >::iterator nit=nodes.begin(), eit=nodes.end();
01611 if(prev==0) {
01612 if((*nit)->parent!=0)
01613 (*nit)->parent->first_child=(*nit);
01614 }
01615 else prev->next_sibling=(*nit);
01616
01617 --eit;
01618 while(nit!=eit) {
01619 (*nit)->prev_sibling=prev;
01620 if(prev)
01621 prev->next_sibling=(*nit);
01622 prev=(*nit);
01623 ++nit;
01624 }
01625
01626 if(prev)
01627 prev->next_sibling=(*eit);
01628
01629
01630 (*eit)->next_sibling=next;
01631 (*eit)->prev_sibling=prev;
01632 if(next==0) {
01633 if((*eit)->parent!=0)
01634 (*eit)->parent->last_child=(*eit);
01635 }
01636 else next->prev_sibling=(*eit);
01637
01638 if(deep) {
01639 sibling_iterator bcs(*nodes.begin());
01640 sibling_iterator ecs(*eit);
01641 ++ecs;
01642 while(bcs!=ecs) {
01643 sort(begin(bcs), end(bcs), comp, deep);
01644 ++bcs;
01645 }
01646 }
01647 }
01648
01649 template <class T, class tree_node_allocator>
01650 template <typename iter>
01651 bool tree<T, tree_node_allocator>::equal(const iter& one_, const iter& two, const iter& three_) const
01652 {
01653 std::equal_to<T> comp;
01654 return equal(one_, two, three_, comp);
01655 }
01656
01657 template <class T, class tree_node_allocator>
01658 template <typename iter>
01659 bool tree<T, tree_node_allocator>::equal_subtree(const iter& one_, const iter& two_) const
01660 {
01661 std::equal_to<T> comp;
01662 return equal_subtree(one_, two_, comp);
01663 }
01664
01665 template <class T, class tree_node_allocator>
01666 template <typename iter, class BinaryPredicate>
01667 bool tree<T, tree_node_allocator>::equal(const iter& one_, const iter& two, const iter& three_, BinaryPredicate fun) const
01668 {
01669 pre_order_iterator one(one_), three(three_);
01670
01671
01672
01673 while(one!=two && is_valid(three)) {
01674 if(!fun(*one,*three))
01675 return false;
01676 if(one.number_of_children()!=three.number_of_children())
01677 return false;
01678 ++one;
01679 ++three;
01680 }
01681 return true;
01682 }
01683
01684 template <class T, class tree_node_allocator>
01685 template <typename iter, class BinaryPredicate>
01686 bool tree<T, tree_node_allocator>::equal_subtree(const iter& one_, const iter& two_, BinaryPredicate fun) const
01687 {
01688 pre_order_iterator one(one_), two(two_);
01689
01690 if(!fun(*one,*two)) return false;
01691 if(number_of_children(one)!=number_of_children(two)) return false;
01692 return equal(begin(one),end(one),begin(two),fun);
01693 }
01694
01695 template <class T, class tree_node_allocator>
01696 tree<T, tree_node_allocator> tree<T, tree_node_allocator>::subtree(sibling_iterator from, sibling_iterator to) const
01697 {
01698 tree tmp;
01699 tmp.set_head(value_type());
01700 tmp.replace(tmp.begin(), tmp.end(), from, to);
01701 return tmp;
01702 }
01703
01704 template <class T, class tree_node_allocator>
01705 void tree<T, tree_node_allocator>::subtree(tree& tmp, sibling_iterator from, sibling_iterator to) const
01706 {
01707 tmp.set_head(value_type());
01708 tmp.replace(tmp.begin(), tmp.end(), from, to);
01709 }
01710
01711 template <class T, class tree_node_allocator>
01712 size_t tree<T, tree_node_allocator>::size() const
01713 {
01714 size_t i=0;
01715 pre_order_iterator it=begin(), eit=end();
01716 while(it!=eit) {
01717 ++i;
01718 ++it;
01719 }
01720 return i;
01721 }
01722
01723 template <class T, class tree_node_allocator>
01724 size_t tree<T, tree_node_allocator>::size(const iterator_base& top) const
01725 {
01726 size_t i=0;
01727 pre_order_iterator it=top, eit=top;
01728 eit.skip_children();
01729 ++eit;
01730 while(it!=eit) {
01731 ++i;
01732 ++it;
01733 }
01734 return i;
01735 }
01736
01737 template <class T, class tree_node_allocator>
01738 bool tree<T, tree_node_allocator>::empty() const
01739 {
01740 pre_order_iterator it=begin(), eit=end();
01741 return (it==eit);
01742 }
01743
01744 template <class T, class tree_node_allocator>
01745 int tree<T, tree_node_allocator>::depth(const iterator_base& it)
01746 {
01747 tree_node* pos=it.node;
01748 assert(pos!=0);
01749 int ret=0;
01750 while(pos->parent!=0) {
01751 pos=pos->parent;
01752 ++ret;
01753 }
01754 return ret;
01755 }
01756
01757 template <class T, class tree_node_allocator>
01758 int tree<T, tree_node_allocator>::depth(const iterator_base& it, const iterator_base& root)
01759 {
01760 tree_node* pos=it.node;
01761 assert(pos!=0);
01762 int ret=0;
01763 while(pos->parent!=0 && pos!=root.node) {
01764 pos=pos->parent;
01765 ++ret;
01766 }
01767 return ret;
01768 }
01769
01770 template <class T, class tree_node_allocator>
01771 int tree<T, tree_node_allocator>::max_depth() const
01772 {
01773 int maxd=-1;
01774 for(tree_node *it = head->next_sibling; it!=feet; it=it->next_sibling)
01775 maxd=std::max(maxd, max_depth(it));
01776
01777 return maxd;
01778 }
01779
01780
01781 template <class T, class tree_node_allocator>
01782 int tree<T, tree_node_allocator>::max_depth(const iterator_base& pos) const
01783 {
01784 tree_node *tmp=pos.node;
01785
01786 if(tmp==0 || tmp==head || tmp==feet) return -1;
01787
01788 int curdepth=0, maxdepth=0;
01789 while(true) {
01790 while(tmp->first_child==0) {
01791 if(tmp==pos.node) return maxdepth;
01792 if(tmp->next_sibling==0) {
01793
01794 do {
01795 tmp=tmp->parent;
01796 if(tmp==0) return maxdepth;
01797 --curdepth;
01798 } while(tmp->next_sibling==0);
01799 }
01800 if(tmp==pos.node) return maxdepth;
01801 tmp=tmp->next_sibling;
01802 }
01803 tmp=tmp->first_child;
01804 ++curdepth;
01805 maxdepth=std::max(curdepth, maxdepth);
01806 }
01807 }
01808
01809 template <class T, class tree_node_allocator>
01810 unsigned int tree<T, tree_node_allocator>::number_of_children(const iterator_base& it)
01811 {
01812 tree_node *pos=it.node->first_child;
01813 if(pos==0) return 0;
01814
01815 unsigned int ret=1;
01816
01817
01818
01819
01820 while((pos=pos->next_sibling))
01821 ++ret;
01822 return ret;
01823 }
01824
01825 template <class T, class tree_node_allocator>
01826 unsigned int tree<T, tree_node_allocator>::number_of_siblings(const iterator_base& it) const
01827 {
01828 tree_node *pos=it.node;
01829 unsigned int ret=0;
01830
01831 while(pos->next_sibling &&
01832 pos->next_sibling!=head &&
01833 pos->next_sibling!=feet) {
01834 ++ret;
01835 pos=pos->next_sibling;
01836 }
01837
01838 pos=it.node;
01839 while(pos->prev_sibling &&
01840 pos->prev_sibling!=head &&
01841 pos->prev_sibling!=feet) {
01842 ++ret;
01843 pos=pos->prev_sibling;
01844 }
01845
01846 return ret;
01847 }
01848
01849 template <class T, class tree_node_allocator>
01850 void tree<T, tree_node_allocator>::swap(sibling_iterator it)
01851 {
01852 tree_node *nxt=it.node->next_sibling;
01853 if(nxt) {
01854 if(it.node->prev_sibling)
01855 it.node->prev_sibling->next_sibling=nxt;
01856 else
01857 it.node->parent->first_child=nxt;
01858 nxt->prev_sibling=it.node->prev_sibling;
01859 tree_node *nxtnxt=nxt->next_sibling;
01860 if(nxtnxt)
01861 nxtnxt->prev_sibling=it.node;
01862 else
01863 it.node->parent->last_child=it.node;
01864 nxt->next_sibling=it.node;
01865 it.node->prev_sibling=nxt;
01866 it.node->next_sibling=nxtnxt;
01867 }
01868 }
01869
01870 template <class T, class tree_node_allocator>
01871 void tree<T, tree_node_allocator>::swap(iterator one, iterator two)
01872 {
01873
01874 if(one.node->next_sibling==two.node) swap(one);
01875 else if(two.node->next_sibling==one.node) swap(two);
01876 else {
01877 tree_node *nxt1=one.node->next_sibling;
01878 tree_node *nxt2=two.node->next_sibling;
01879 tree_node *pre1=one.node->prev_sibling;
01880 tree_node *pre2=two.node->prev_sibling;
01881 tree_node *par1=one.node->parent;
01882 tree_node *par2=two.node->parent;
01883
01884
01885 one.node->parent=par2;
01886 one.node->next_sibling=nxt2;
01887 if(nxt2) nxt2->prev_sibling=one.node;
01888 else par2->last_child=one.node;
01889 one.node->prev_sibling=pre2;
01890 if(pre2) pre2->next_sibling=one.node;
01891 else par2->first_child=one.node;
01892
01893 two.node->parent=par1;
01894 two.node->next_sibling=nxt1;
01895 if(nxt1) nxt1->prev_sibling=two.node;
01896 else par1->last_child=two.node;
01897 two.node->prev_sibling=pre1;
01898 if(pre1) pre1->next_sibling=two.node;
01899 else par1->first_child=two.node;
01900 }
01901 }
01902
01903
01904
01905
01906
01907
01908
01909
01910
01911
01912
01913
01914
01915
01916
01917 template <class T, class tree_node_allocator>
01918 bool tree<T, tree_node_allocator>::is_in_subtree(const iterator_base& it, const iterator_base& begin,
01919 const iterator_base& end) const
01920 {
01921
01922 pre_order_iterator tmp=begin;
01923 while(tmp!=end) {
01924 if(tmp==it) return true;
01925 ++tmp;
01926 }
01927 return false;
01928 }
01929
01930 template <class T, class tree_node_allocator>
01931 bool tree<T, tree_node_allocator>::is_valid(const iterator_base& it) const
01932 {
01933 if(it.node==0 || it.node==feet || it.node==head) return false;
01934 else return true;
01935 }
01936
01937 template <class T, class tree_node_allocator>
01938 typename tree<T, tree_node_allocator>::iterator tree<T, tree_node_allocator>::lowest_common_ancestor(
01939 const iterator_base& one, const iterator_base& two) const
01940 {
01941 std::set<iterator, iterator_base_less> parents;
01942
01943
01944 iterator walk=one;
01945 do {
01946 walk=parent(walk);
01947 parents.insert(walk);
01948 } while( is_valid(parent(walk)) );
01949
01950
01951 walk=two;
01952 do {
01953 walk=parent(walk);
01954 if(parents.find(walk) != parents.end()) break;
01955 } while( is_valid(parent(walk)) );
01956
01957 return walk;
01958 }
01959
01960 template <class T, class tree_node_allocator>
01961 unsigned int tree<T, tree_node_allocator>::index(sibling_iterator it) const
01962 {
01963 unsigned int ind=0;
01964 if(it.node->parent==0) {
01965 while(it.node->prev_sibling!=head) {
01966 it.node=it.node->prev_sibling;
01967 ++ind;
01968 }
01969 }
01970 else {
01971 while(it.node->prev_sibling!=0) {
01972 it.node=it.node->prev_sibling;
01973 ++ind;
01974 }
01975 }
01976 return ind;
01977 }
01978
01979 template <class T, class tree_node_allocator>
01980 typename tree<T, tree_node_allocator>::sibling_iterator tree<T, tree_node_allocator>::sibling(const iterator_base& it, unsigned int num)
01981 {
01982 tree_node *tmp;
01983 if(it.node->parent==0) {
01984 tmp=head->next_sibling;
01985 while(num) {
01986 tmp = tmp->next_sibling;
01987 --num;
01988 }
01989 }
01990 else {
01991 tmp=it.node->parent->first_child;
01992 while(num) {
01993 assert(tmp!=0);
01994 tmp = tmp->next_sibling;
01995 --num;
01996 }
01997 }
01998 return tmp;
01999 }
02000
02001 template <class T, class tree_node_allocator>
02002 void tree<T, tree_node_allocator>::debug_verify_consistency() const
02003 {
02004 iterator it=begin();
02005 while(it!=end()) {
02006 if(it.node->parent!=0) {
02007 if(it.node->prev_sibling==0)
02008 assert(it.node->parent->first_child==it.node);
02009 else
02010 assert(it.node->prev_sibling->next_sibling==it.node);
02011 if(it.node->next_sibling==0)
02012 assert(it.node->parent->last_child==it.node);
02013 else
02014 assert(it.node->next_sibling->prev_sibling==it.node);
02015 }
02016 ++it;
02017 }
02018 }
02019
02020 template <class T, class tree_node_allocator>
02021 typename tree<T, tree_node_allocator>::sibling_iterator tree<T, tree_node_allocator>::child(const iterator_base& it, unsigned int num)
02022 {
02023 tree_node *tmp=it.node->first_child;
02024 while(num--) {
02025 assert(tmp!=0);
02026 tmp=tmp->next_sibling;
02027 }
02028 return tmp;
02029 }
02030
02031
02032
02033
02034
02035
02036 template <class T, class tree_node_allocator>
02037 tree<T, tree_node_allocator>::iterator_base::iterator_base()
02038 : node(0), skip_current_children_(false)
02039 {
02040 }
02041
02042 template <class T, class tree_node_allocator>
02043 tree<T, tree_node_allocator>::iterator_base::iterator_base(tree_node *tn)
02044 : node(tn), skip_current_children_(false)
02045 {
02046 }
02047
02048 template <class T, class tree_node_allocator>
02049 T& tree<T, tree_node_allocator>::iterator_base::operator*() const
02050 {
02051 return node->data;
02052 }
02053
02054 template <class T, class tree_node_allocator>
02055 T* tree<T, tree_node_allocator>::iterator_base::operator->() const
02056 {
02057 return &(node->data);
02058 }
02059
02060 template <class T, class tree_node_allocator>
02061 bool tree<T, tree_node_allocator>::post_order_iterator::operator!=(const post_order_iterator& other) const
02062 {
02063 if(other.node!=this->node) return true;
02064 else return false;
02065 }
02066
02067 template <class T, class tree_node_allocator>
02068 bool tree<T, tree_node_allocator>::post_order_iterator::operator==(const post_order_iterator& other) const
02069 {
02070 if(other.node==this->node) return true;
02071 else return false;
02072 }
02073
02074 template <class T, class tree_node_allocator>
02075 bool tree<T, tree_node_allocator>::pre_order_iterator::operator!=(const pre_order_iterator& other) const
02076 {
02077 if(other.node!=this->node) return true;
02078 else return false;
02079 }
02080
02081 template <class T, class tree_node_allocator>
02082 bool tree<T, tree_node_allocator>::pre_order_iterator::operator==(const pre_order_iterator& other) const
02083 {
02084 if(other.node==this->node) return true;
02085 else return false;
02086 }
02087
02088 template <class T, class tree_node_allocator>
02089 bool tree<T, tree_node_allocator>::sibling_iterator::operator!=(const sibling_iterator& other) const
02090 {
02091 if(other.node!=this->node) return true;
02092 else return false;
02093 }
02094
02095 template <class T, class tree_node_allocator>
02096 bool tree<T, tree_node_allocator>::sibling_iterator::operator==(const sibling_iterator& other) const
02097 {
02098 if(other.node==this->node) return true;
02099 else return false;
02100 }
02101
02102 template <class T, class tree_node_allocator>
02103 bool tree<T, tree_node_allocator>::leaf_iterator::operator!=(const leaf_iterator& other) const
02104 {
02105 if(other.node!=this->node) return true;
02106 else return false;
02107 }
02108
02109 template <class T, class tree_node_allocator>
02110 bool tree<T, tree_node_allocator>::leaf_iterator::operator==(const leaf_iterator& other) const
02111 {
02112 if(other.node==this->node && other.top_node==this->top_node) return true;
02113 else return false;
02114 }
02115
02116 template <class T, class tree_node_allocator>
02117 typename tree<T, tree_node_allocator>::sibling_iterator tree<T, tree_node_allocator>::iterator_base::begin() const
02118 {
02119 if(node->first_child==0)
02120 return end();
02121
02122 sibling_iterator ret(node->first_child);
02123 ret.parent_=this->node;
02124 return ret;
02125 }
02126
02127 template <class T, class tree_node_allocator>
02128 typename tree<T, tree_node_allocator>::sibling_iterator tree<T, tree_node_allocator>::iterator_base::end() const
02129 {
02130 sibling_iterator ret(0);
02131 ret.parent_=node;
02132 return ret;
02133 }
02134
02135 template <class T, class tree_node_allocator>
02136 void tree<T, tree_node_allocator>::iterator_base::skip_children()
02137 {
02138 skip_current_children_=true;
02139 }
02140
02141 template <class T, class tree_node_allocator>
02142 void tree<T, tree_node_allocator>::iterator_base::skip_children(bool skip)
02143 {
02144 skip_current_children_=skip;
02145 }
02146
02147 template <class T, class tree_node_allocator>
02148 unsigned int tree<T, tree_node_allocator>::iterator_base::number_of_children() const
02149 {
02150 tree_node *pos=node->first_child;
02151 if(pos==0) return 0;
02152
02153 unsigned int ret=1;
02154 while(pos!=node->last_child) {
02155 ++ret;
02156 pos=pos->next_sibling;
02157 }
02158 return ret;
02159 }
02160
02161
02162
02163
02164
02165 template <class T, class tree_node_allocator>
02166 tree<T, tree_node_allocator>::pre_order_iterator::pre_order_iterator()
02167 : iterator_base(0)
02168 {
02169 }
02170
02171 template <class T, class tree_node_allocator>
02172 tree<T, tree_node_allocator>::pre_order_iterator::pre_order_iterator(tree_node *tn)
02173 : iterator_base(tn)
02174 {
02175 }
02176
02177 template <class T, class tree_node_allocator>
02178 tree<T, tree_node_allocator>::pre_order_iterator::pre_order_iterator(const iterator_base &other)
02179 : iterator_base(other.node)
02180 {
02181 }
02182
02183 template <class T, class tree_node_allocator>
02184 tree<T, tree_node_allocator>::pre_order_iterator::pre_order_iterator(const sibling_iterator& other)
02185 : iterator_base(other.node)
02186 {
02187 if(this->node==0) {
02188 if(other.range_last()!=0)
02189 this->node=other.range_last();
02190 else
02191 this->node=other.parent_;
02192 this->skip_children();
02193 ++(*this);
02194 }
02195 }
02196
02197 template <class T, class tree_node_allocator>
02198 typename tree<T, tree_node_allocator>::pre_order_iterator& tree<T, tree_node_allocator>::pre_order_iterator::operator++()
02199 {
02200 assert(this->node!=0);
02201 if(!this->skip_current_children_ && this->node->first_child != 0) {
02202 this->node=this->node->first_child;
02203 }
02204 else {
02205 this->skip_current_children_=false;
02206 while(this->node->next_sibling==0) {
02207 this->node=this->node->parent;
02208 if(this->node==0)
02209 return *this;
02210 }
02211 this->node=this->node->next_sibling;
02212 }
02213 return *this;
02214 }
02215
02216 template <class T, class tree_node_allocator>
02217 typename tree<T, tree_node_allocator>::pre_order_iterator& tree<T, tree_node_allocator>::pre_order_iterator::operator--()
02218 {
02219 assert(this->node!=0);
02220 if(this->node->prev_sibling) {
02221 this->node=this->node->prev_sibling;
02222 while(this->node->last_child)
02223 this->node=this->node->last_child;
02224 }
02225 else {
02226 this->node=this->node->parent;
02227 if(this->node==0)
02228 return *this;
02229 }
02230 return *this;
02231 }
02232
02233 template <class T, class tree_node_allocator>
02234 typename tree<T, tree_node_allocator>::pre_order_iterator tree<T, tree_node_allocator>::pre_order_iterator::operator++(int)
02235 {
02236 pre_order_iterator copy = *this;
02237 ++(*this);
02238 return copy;
02239 }
02240
02241 template <class T, class tree_node_allocator>
02242 typename tree<T, tree_node_allocator>::pre_order_iterator tree<T, tree_node_allocator>::pre_order_iterator::operator--(int)
02243 {
02244 pre_order_iterator copy = *this;
02245 --(*this);
02246 return copy;
02247 }
02248
02249 template <class T, class tree_node_allocator>
02250 typename tree<T, tree_node_allocator>::pre_order_iterator& tree<T, tree_node_allocator>::pre_order_iterator::operator+=(unsigned int num)
02251 {
02252 while(num>0) {
02253 ++(*this);
02254 --num;
02255 }
02256 return (*this);
02257 }
02258
02259 template <class T, class tree_node_allocator>
02260 typename tree<T, tree_node_allocator>::pre_order_iterator& tree<T, tree_node_allocator>::pre_order_iterator::operator-=(unsigned int num)
02261 {
02262 while(num>0) {
02263 --(*this);
02264 --num;
02265 }
02266 return (*this);
02267 }
02268
02269
02270
02271
02272
02273 template <class T, class tree_node_allocator>
02274 tree<T, tree_node_allocator>::post_order_iterator::post_order_iterator()
02275 : iterator_base(0)
02276 {
02277 }
02278
02279 template <class T, class tree_node_allocator>
02280 tree<T, tree_node_allocator>::post_order_iterator::post_order_iterator(tree_node *tn)
02281 : iterator_base(tn)
02282 {
02283 }
02284
02285 template <class T, class tree_node_allocator>
02286 tree<T, tree_node_allocator>::post_order_iterator::post_order_iterator(const iterator_base &other)
02287 : iterator_base(other.node)
02288 {
02289 }
02290
02291 template <class T, class tree_node_allocator>
02292 tree<T, tree_node_allocator>::post_order_iterator::post_order_iterator(const sibling_iterator& other)
02293 : iterator_base(other.node)
02294 {
02295 if(this->node==0) {
02296 if(other.range_last()!=0)
02297 this->node=other.range_last();
02298 else
02299 this->node=other.parent_;
02300 this->skip_children();
02301 ++(*this);
02302 }
02303 }
02304
02305 template <class T, class tree_node_allocator>
02306 typename tree<T, tree_node_allocator>::post_order_iterator& tree<T, tree_node_allocator>::post_order_iterator::operator++()
02307 {
02308 assert(this->node!=0);
02309 if(this->node->next_sibling==0) {
02310 this->node=this->node->parent;
02311 this->skip_current_children_=false;
02312 }
02313 else {
02314 this->node=this->node->next_sibling;
02315 if(this->skip_current_children_) {
02316 this->skip_current_children_=false;
02317 }
02318 else {
02319 while(this->node->first_child)
02320 this->node=this->node->first_child;
02321 }
02322 }
02323 return *this;
02324 }
02325
02326 template <class T, class tree_node_allocator>
02327 typename tree<T, tree_node_allocator>::post_order_iterator& tree<T, tree_node_allocator>::post_order_iterator::operator--()
02328 {
02329 assert(this->node!=0);
02330 if(this->skip_current_children_ || this->node->last_child==0) {
02331 this->skip_current_children_=false;
02332 while(this->node->prev_sibling==0)
02333 this->node=this->node->parent;
02334 this->node=this->node->prev_sibling;
02335 }
02336 else {
02337 this->node=this->node->last_child;
02338 }
02339 return *this;
02340 }
02341
02342 template <class T, class tree_node_allocator>
02343 typename tree<T, tree_node_allocator>::post_order_iterator tree<T, tree_node_allocator>::post_order_iterator::operator++(int)
02344 {
02345 post_order_iterator copy = *this;
02346 ++(*this);
02347 return copy;
02348 }
02349
02350 template <class T, class tree_node_allocator>
02351 typename tree<T, tree_node_allocator>::post_order_iterator tree<T, tree_node_allocator>::post_order_iterator::operator--(int)
02352 {
02353 post_order_iterator copy = *this;
02354 --(*this);
02355 return copy;
02356 }
02357
02358
02359 template <class T, class tree_node_allocator>
02360 typename tree<T, tree_node_allocator>::post_order_iterator& tree<T, tree_node_allocator>::post_order_iterator::operator+=(unsigned int num)
02361 {
02362 while(num>0) {
02363 ++(*this);
02364 --num;
02365 }
02366 return (*this);
02367 }
02368
02369 template <class T, class tree_node_allocator>
02370 typename tree<T, tree_node_allocator>::post_order_iterator& tree<T, tree_node_allocator>::post_order_iterator::operator-=(unsigned int num)
02371 {
02372 while(num>0) {
02373 --(*this);
02374 --num;
02375 }
02376 return (*this);
02377 }
02378
02379 template <class T, class tree_node_allocator>
02380 void tree<T, tree_node_allocator>::post_order_iterator::descend_all()
02381 {
02382 assert(this->node!=0);
02383 while(this->node->first_child)
02384 this->node=this->node->first_child;
02385 }
02386
02387
02388
02389
02390 template <class T, class tree_node_allocator>
02391 tree<T, tree_node_allocator>::breadth_first_queued_iterator::breadth_first_queued_iterator()
02392 : iterator_base()
02393 {
02394 }
02395
02396 template <class T, class tree_node_allocator>
02397 tree<T, tree_node_allocator>::breadth_first_queued_iterator::breadth_first_queued_iterator(tree_node *tn)
02398 : iterator_base(tn)
02399 {
02400 traversal_queue.push(tn);
02401 }
02402
02403 template <class T, class tree_node_allocator>
02404 tree<T, tree_node_allocator>::breadth_first_queued_iterator::breadth_first_queued_iterator(const iterator_base& other)
02405 : iterator_base(other.node)
02406 {
02407 traversal_queue.push(other.node);
02408 }
02409
02410 template <class T, class tree_node_allocator>
02411 bool tree<T, tree_node_allocator>::breadth_first_queued_iterator::operator!=(const breadth_first_queued_iterator& other) const
02412 {
02413 if(other.node!=this->node) return true;
02414 else return false;
02415 }
02416
02417 template <class T, class tree_node_allocator>
02418 bool tree<T, tree_node_allocator>::breadth_first_queued_iterator::operator==(const breadth_first_queued_iterator& other) const
02419 {
02420 if(other.node==this->node) return true;
02421 else return false;
02422 }
02423
02424 template <class T, class tree_node_allocator>
02425 typename tree<T, tree_node_allocator>::breadth_first_queued_iterator& tree<T, tree_node_allocator>::breadth_first_queued_iterator::operator++()
02426 {
02427 assert(this->node!=0);
02428
02429
02430 sibling_iterator sib=this->begin();
02431 while(sib!=this->end()) {
02432 traversal_queue.push(sib.node);
02433 ++sib;
02434 }
02435 traversal_queue.pop();
02436 if(traversal_queue.size()>0)
02437 this->node=traversal_queue.front();
02438 else
02439 this->node=0;
02440 return (*this);
02441 }
02442
02443 template <class T, class tree_node_allocator>
02444 typename tree<T, tree_node_allocator>::breadth_first_queued_iterator tree<T, tree_node_allocator>::breadth_first_queued_iterator::operator++(int)
02445 {
02446 breadth_first_queued_iterator copy = *this;
02447 ++(*this);
02448 return copy;
02449 }
02450
02451 template <class T, class tree_node_allocator>
02452 typename tree<T, tree_node_allocator>::breadth_first_queued_iterator& tree<T, tree_node_allocator>::breadth_first_queued_iterator::operator+=(unsigned int num)
02453 {
02454 while(num>0) {
02455 ++(*this);
02456 --num;
02457 }
02458 return (*this);
02459 }
02460
02461
02462
02463
02464
02465 template <class T, class tree_node_allocator>
02466 tree<T, tree_node_allocator>::fixed_depth_iterator::fixed_depth_iterator()
02467 : iterator_base()
02468 {
02469 }
02470
02471 template <class T, class tree_node_allocator>
02472 tree<T, tree_node_allocator>::fixed_depth_iterator::fixed_depth_iterator(tree_node *tn)
02473 : iterator_base(tn), top_node(0)
02474 {
02475 }
02476
02477 template <class T, class tree_node_allocator>
02478 tree<T, tree_node_allocator>::fixed_depth_iterator::fixed_depth_iterator(const iterator_base& other)
02479 : iterator_base(other.node), top_node(0)
02480 {
02481 }
02482
02483 template <class T, class tree_node_allocator>
02484 tree<T, tree_node_allocator>::fixed_depth_iterator::fixed_depth_iterator(const sibling_iterator& other)
02485 : iterator_base(other.node), top_node(0)
02486 {
02487 }
02488
02489 template <class T, class tree_node_allocator>
02490 tree<T, tree_node_allocator>::fixed_depth_iterator::fixed_depth_iterator(const fixed_depth_iterator& other)
02491 : iterator_base(other.node), top_node(other.top_node)
02492 {
02493 }
02494
02495 template <class T, class tree_node_allocator>
02496 bool tree<T, tree_node_allocator>::fixed_depth_iterator::operator==(const fixed_depth_iterator& other) const
02497 {
02498 if(other.node==this->node && other.top_node==top_node) return true;
02499 else return false;
02500 }
02501
02502 template <class T, class tree_node_allocator>
02503 bool tree<T, tree_node_allocator>::fixed_depth_iterator::operator!=(const fixed_depth_iterator& other) const
02504 {
02505 if(other.node!=this->node || other.top_node!=top_node) return true;
02506 else return false;
02507 }
02508
02509 template <class T, class tree_node_allocator>
02510 typename tree<T, tree_node_allocator>::fixed_depth_iterator& tree<T, tree_node_allocator>::fixed_depth_iterator::operator++()
02511 {
02512 assert(this->node!=0);
02513
02514 if(this->node->next_sibling) {
02515 this->node=this->node->next_sibling;
02516 }
02517 else {
02518 int relative_depth=0;
02519 upper:
02520 do {
02521 if(this->node==this->top_node) {
02522 this->node=0;
02523 return *this;
02524 }
02525 this->node=this->node->parent;
02526 if(this->node==0) return *this;
02527 --relative_depth;
02528 } while(this->node->next_sibling==0);
02529 lower:
02530 this->node=this->node->next_sibling;
02531 while(this->node->first_child==0) {
02532 if(this->node->next_sibling==0)
02533 goto upper;
02534 this->node=this->node->next_sibling;
02535 if(this->node==0) return *this;
02536 }
02537 while(relative_depth<0 && this->node->first_child!=0) {
02538 this->node=this->node->first_child;
02539 ++relative_depth;
02540 }
02541 if(relative_depth<0) {
02542 if(this->node->next_sibling==0) goto upper;
02543 else goto lower;
02544 }
02545 }
02546 return *this;
02547 }
02548
02549 template <class T, class tree_node_allocator>
02550 typename tree<T, tree_node_allocator>::fixed_depth_iterator& tree<T, tree_node_allocator>::fixed_depth_iterator::operator--()
02551 {
02552 assert(this->node!=0);
02553
02554 if(this->node->prev_sibling) {
02555 this->node=this->node->prev_sibling;
02556 }
02557 else {
02558 int relative_depth=0;
02559 upper:
02560 do {
02561 if(this->node==this->top_node) {
02562 this->node=0;
02563 return *this;
02564 }
02565 this->node=this->node->parent;
02566 if(this->node==0) return *this;
02567 --relative_depth;
02568 } while(this->node->prev_sibling==0);
02569 lower:
02570 this->node=this->node->prev_sibling;
02571 while(this->node->last_child==0) {
02572 if(this->node->prev_sibling==0)
02573 goto upper;
02574 this->node=this->node->prev_sibling;
02575 if(this->node==0) return *this;
02576 }
02577 while(relative_depth<0 && this->node->last_child!=0) {
02578 this->node=this->node->last_child;
02579 ++relative_depth;
02580 }
02581 if(relative_depth<0) {
02582 if(this->node->prev_sibling==0) goto upper;
02583 else goto lower;
02584 }
02585 }
02586 return *this;
02587
02588
02589
02590
02591
02592
02593
02594
02595
02596
02597
02598
02599
02600
02601
02602
02603
02604
02605
02606
02607
02608
02609 }
02610
02611 template <class T, class tree_node_allocator>
02612 typename tree<T, tree_node_allocator>::fixed_depth_iterator tree<T, tree_node_allocator>::fixed_depth_iterator::operator++(int)
02613 {
02614 fixed_depth_iterator copy = *this;
02615 ++(*this);
02616 return copy;
02617 }
02618
02619 template <class T, class tree_node_allocator>
02620 typename tree<T, tree_node_allocator>::fixed_depth_iterator tree<T, tree_node_allocator>::fixed_depth_iterator::operator--(int)
02621 {
02622 fixed_depth_iterator copy = *this;
02623 --(*this);
02624 return copy;
02625 }
02626
02627 template <class T, class tree_node_allocator>
02628 typename tree<T, tree_node_allocator>::fixed_depth_iterator& tree<T, tree_node_allocator>::fixed_depth_iterator::operator-=(unsigned int num)
02629 {
02630 while(num>0) {
02631 --(*this);
02632 --(num);
02633 }
02634 return (*this);
02635 }
02636
02637 template <class T, class tree_node_allocator>
02638 typename tree<T, tree_node_allocator>::fixed_depth_iterator& tree<T, tree_node_allocator>::fixed_depth_iterator::operator+=(unsigned int num)
02639 {
02640 while(num>0) {
02641 ++(*this);
02642 --(num);
02643 }
02644 return *this;
02645 }
02646
02647
02648
02649
02650 template <class T, class tree_node_allocator>
02651 tree<T, tree_node_allocator>::sibling_iterator::sibling_iterator()
02652 : iterator_base()
02653 {
02654 set_parent_();
02655 }
02656
02657 template <class T, class tree_node_allocator>
02658 tree<T, tree_node_allocator>::sibling_iterator::sibling_iterator(tree_node *tn)
02659 : iterator_base(tn)
02660 {
02661 set_parent_();
02662 }
02663
02664 template <class T, class tree_node_allocator>
02665 tree<T, tree_node_allocator>::sibling_iterator::sibling_iterator(const iterator_base& other)
02666 : iterator_base(other.node)
02667 {
02668 set_parent_();
02669 }
02670
02671 template <class T, class tree_node_allocator>
02672 tree<T, tree_node_allocator>::sibling_iterator::sibling_iterator(const sibling_iterator& other)
02673 : iterator_base(other), parent_(other.parent_)
02674 {
02675 }
02676
02677 template <class T, class tree_node_allocator>
02678 void tree<T, tree_node_allocator>::sibling_iterator::set_parent_()
02679 {
02680 parent_=0;
02681 if(this->node==0) return;
02682 if(this->node->parent!=0)
02683 parent_=this->node->parent;
02684 }
02685
02686 template <class T, class tree_node_allocator>
02687 typename tree<T, tree_node_allocator>::sibling_iterator& tree<T, tree_node_allocator>::sibling_iterator::operator++()
02688 {
02689 if(this->node)
02690 this->node=this->node->next_sibling;
02691 return *this;
02692 }
02693
02694 template <class T, class tree_node_allocator>
02695 typename tree<T, tree_node_allocator>::sibling_iterator& tree<T, tree_node_allocator>::sibling_iterator::operator--()
02696 {
02697 if(this->node) this->node=this->node->prev_sibling;
02698 else {
02699 assert(parent_);
02700 this->node=parent_->last_child;
02701 }
02702 return *this;
02703 }
02704
02705 template <class T, class tree_node_allocator>
02706 typename tree<T, tree_node_allocator>::sibling_iterator tree<T, tree_node_allocator>::sibling_iterator::operator++(int)
02707 {
02708 sibling_iterator copy = *this;
02709 ++(*this);
02710 return copy;
02711 }
02712
02713 template <class T, class tree_node_allocator>
02714 typename tree<T, tree_node_allocator>::sibling_iterator tree<T, tree_node_allocator>::sibling_iterator::operator--(int)
02715 {
02716 sibling_iterator copy = *this;
02717 --(*this);
02718 return copy;
02719 }
02720
02721 template <class T, class tree_node_allocator>
02722 typename tree<T, tree_node_allocator>::sibling_iterator& tree<T, tree_node_allocator>::sibling_iterator::operator+=(unsigned int num)
02723 {
02724 while(num>0) {
02725 ++(*this);
02726 --num;
02727 }
02728 return (*this);
02729 }
02730
02731 template <class T, class tree_node_allocator>
02732 typename tree<T, tree_node_allocator>::sibling_iterator& tree<T, tree_node_allocator>::sibling_iterator::operator-=(unsigned int num)
02733 {
02734 while(num>0) {
02735 --(*this);
02736 --num;
02737 }
02738 return (*this);
02739 }
02740
02741 template <class T, class tree_node_allocator>
02742 typename tree<T, tree_node_allocator>::tree_node *tree<T, tree_node_allocator>::sibling_iterator::range_first() const
02743 {
02744 tree_node *tmp=parent_->first_child;
02745 return tmp;
02746 }
02747
02748 template <class T, class tree_node_allocator>
02749 typename tree<T, tree_node_allocator>::tree_node *tree<T, tree_node_allocator>::sibling_iterator::range_last() const
02750 {
02751 return parent_->last_child;
02752 }
02753
02754
02755
02756 template <class T, class tree_node_allocator>
02757 tree<T, tree_node_allocator>::leaf_iterator::leaf_iterator()
02758 : iterator_base(0), top_node(0)
02759 {
02760 }
02761
02762 template <class T, class tree_node_allocator>
02763 tree<T, tree_node_allocator>::leaf_iterator::leaf_iterator(tree_node *tn, tree_node *top)
02764 : iterator_base(tn), top_node(top)
02765 {
02766 }
02767
02768 template <class T, class tree_node_allocator>
02769 tree<T, tree_node_allocator>::leaf_iterator::leaf_iterator(const iterator_base &other)
02770 : iterator_base(other.node), top_node(0)
02771 {
02772 }
02773
02774 template <class T, class tree_node_allocator>
02775 tree<T, tree_node_allocator>::leaf_iterator::leaf_iterator(const sibling_iterator& other)
02776 : iterator_base(other.node), top_node(0)
02777 {
02778 if(this->node==0) {
02779 if(other.range_last()!=0)
02780 this->node=other.range_last();
02781 else
02782 this->node=other.parent_;
02783 ++(*this);
02784 }
02785 }
02786
02787 template <class T, class tree_node_allocator>
02788 typename tree<T, tree_node_allocator>::leaf_iterator& tree<T, tree_node_allocator>::leaf_iterator::operator++()
02789 {
02790 assert(this->node!=0);
02791 if(this->node->first_child!=0) {
02792 while(this->node->first_child)
02793 this->node=this->node->first_child;
02794 }
02795 else {
02796 while(this->node->next_sibling==0) {
02797 if (this->node->parent==0) return *this;
02798 this->node=this->node->parent;
02799 if (top_node != 0 && this->node==top_node) return *this;
02800 }
02801 this->node=this->node->next_sibling;
02802 while(this->node->first_child)
02803 this->node=this->node->first_child;
02804 }
02805 return *this;
02806 }
02807
02808 template <class T, class tree_node_allocator>
02809 typename tree<T, tree_node_allocator>::leaf_iterator& tree<T, tree_node_allocator>::leaf_iterator::operator--()
02810 {
02811 assert(this->node!=0);
02812 while (this->node->prev_sibling==0) {
02813 if (this->node->parent==0) return *this;
02814 this->node=this->node->parent;
02815 if (top_node !=0 && this->node==top_node) return *this;
02816 }
02817 this->node=this->node->prev_sibling;
02818 while(this->node->last_child)
02819 this->node=this->node->last_child;
02820 return *this;
02821 }
02822
02823 template <class T, class tree_node_allocator>
02824 typename tree<T, tree_node_allocator>::leaf_iterator tree<T, tree_node_allocator>::leaf_iterator::operator++(int)
02825 {
02826 leaf_iterator copy = *this;
02827 ++(*this);
02828 return copy;
02829 }
02830
02831 template <class T, class tree_node_allocator>
02832 typename tree<T, tree_node_allocator>::leaf_iterator tree<T, tree_node_allocator>::leaf_iterator::operator--(int)
02833 {
02834 leaf_iterator copy = *this;
02835 --(*this);
02836 return copy;
02837 }
02838
02839
02840 template <class T, class tree_node_allocator>
02841 typename tree<T, tree_node_allocator>::leaf_iterator& tree<T, tree_node_allocator>::leaf_iterator::operator+=(unsigned int num)
02842 {
02843 while(num>0) {
02844 ++(*this);
02845 --num;
02846 }
02847 return (*this);
02848 }
02849
02850 template <class T, class tree_node_allocator>
02851 typename tree<T, tree_node_allocator>::leaf_iterator& tree<T, tree_node_allocator>::leaf_iterator::operator-=(unsigned int num)
02852 {
02853 while(num>0) {
02854 --(*this);
02855 --num;
02856 }
02857 return (*this);
02858 }
02859
02860 #endif
02861
02862
02863
02864