GiNaCRA  0.6.4
tree.h
Go to the documentation of this file.
00001 //  STL-like templated tree class.
00002 //
00003 // Copyright (C) 2001-2011 Kasper Peeters <kasper* @phi-sci.com>
00004 // Distributed under the GNU General Public License version 3.
00005 //
00006 // When used together with the htmlcxx library to create
00007 // HTML::Node template instances, the GNU Lesser General Public
00008 // version 2 applies. Special permission to use tree.hh under
00009 // the LGPL for other projects can be requested from the author.
00010 
00036 #ifndef TREE_H
00037 #define TREE_H
00038 
00039 #include <cassert>
00040 #include <memory>
00041 #include <stdexcept>
00042 #include <iterator>
00043 #include <set>
00044 #include <queue>
00045 #include <algorithm>
00046 #include <cstddef>
00047 
00048 namespace GiNaCRA
00049 {
00050 
00052 
00053 template<class T>
00054 class tree_node_ // size: 5*4=20 bytes (on 32 bit arch), can be reduced by 8.
00055 {
00056 public:
00057     tree_node_( );
00058     tree_node_( const T& );
00059 
00060     tree_node_<T> *parent;
00061     tree_node_<T> *first_child, *last_child;
00062     tree_node_<T> *prev_sibling, *next_sibling;
00063     T data;
00064 }; // __attribute__((packed));
00065 
00066 template<class T>
00067 tree_node_<T>
00068 ::tree_node_( )
00069 : parent( 0 ), first_child( 0 ), last_child( 0 ), prev_sibling( 0 ), next_sibling( 0 ){ }
00070 
00071 template<class T>
00072 tree_node_<T>
00073 ::tree_node_( const T& val )
00074 : parent( 0 ), first_child( 0 ), last_child( 0 ), prev_sibling( 0 ), next_sibling( 0 ), data( val ){ }
00075 
00076 template <class T, class tree_node_allocator = std::allocator<tree_node_<T> > >
00077 class tree
00078 {
00079 protected:
00080     typedef tree_node_<T> tree_node;
00081 public:
00083     typedef T value_type;
00084 
00085     class iterator_base;
00086     class pre_order_iterator;
00087     class post_order_iterator;
00088     class sibling_iterator;
00089     class leaf_iterator;
00090 
00091     tree( );
00092     tree( const T& );
00093     tree( const iterator_base& );
00094     tree( const tree<T, tree_node_allocator>& );
00095     ~tree( );
00096     tree<T, tree_node_allocator>& operator=( const tree<T, tree_node_allocator>& );
00097 
00099 #ifdef __SGI_STL_PORT
00100 
00101     class iterator_base : public stlport::bidirectional_iterator<T, ptrdiff_t>
00102     {
00103 #else
00104 
00105     class iterator_base
00106     {
00107 #endif
00108     public:
00109         typedef T value_type;
00110         typedef T* pointer;
00111         typedef T& reference;
00112         typedef size_t size_type;
00113         typedef ptrdiff_t difference_type;
00114         typedef std::bidirectional_iterator_tag iterator_category;
00115 
00116         iterator_base( );
00117         iterator_base( tree_node* );
00118 
00119         T& operator*( ) const;
00120         T* operator->( ) const;
00121 
00123         void skip_children( );
00124         void skip_children( bool skip );
00126         unsigned int number_of_children( ) const;
00127 
00128         sibling_iterator begin( ) const;
00129         sibling_iterator end( ) const;
00130 
00131         tree_node* node;
00132     protected:
00133         bool skip_current_children_;
00134     };
00135 
00137 
00138     class pre_order_iterator : public iterator_base
00139     {
00140     public:
00141         pre_order_iterator( );
00142         pre_order_iterator( tree_node* );
00143         pre_order_iterator( const iterator_base& );
00144         pre_order_iterator( const sibling_iterator& );
00145 
00146         bool operator==( const pre_order_iterator& ) const;
00147         bool operator!=( const pre_order_iterator& ) const;
00148         pre_order_iterator& operator++( );
00149         pre_order_iterator& operator--( );
00150         pre_order_iterator operator++( int );
00151         pre_order_iterator operator--( int );
00152         pre_order_iterator& operator+=( unsigned int );
00153         pre_order_iterator& operator-=( unsigned int );
00154     };
00155 
00157 
00158     class post_order_iterator : public iterator_base
00159     {
00160     public:
00161         post_order_iterator( );
00162         post_order_iterator( tree_node* );
00163         post_order_iterator( const iterator_base& );
00164         post_order_iterator( const sibling_iterator& );
00165 
00166         bool operator==( const post_order_iterator& ) const;
00167         bool operator!=( const post_order_iterator& ) const;
00168         post_order_iterator& operator++( );
00169         post_order_iterator& operator--( );
00170         post_order_iterator operator++( int );
00171         post_order_iterator operator--( int );
00172         post_order_iterator& operator+=( unsigned int );
00173         post_order_iterator& operator-=( unsigned int );
00174 
00176         void descend_all( );
00177     };
00178 
00180 
00181     class breadth_first_queued_iterator : public iterator_base
00182     {
00183     public:
00184         breadth_first_queued_iterator( );
00185         breadth_first_queued_iterator( tree_node* );
00186         breadth_first_queued_iterator( const iterator_base& );
00187 
00188         bool operator==( const breadth_first_queued_iterator& ) const;
00189         bool operator!=( const breadth_first_queued_iterator& ) const;
00190         breadth_first_queued_iterator& operator++( );
00191         breadth_first_queued_iterator operator++( int );
00192         breadth_first_queued_iterator& operator+=( unsigned int );
00193 
00194     private:
00195         std::queue<tree_node*> traversal_queue;
00196     };
00197 
00199     typedef pre_order_iterator iterator;
00200     typedef breadth_first_queued_iterator breadth_first_iterator;
00201 
00203 
00204     class fixed_depth_iterator : public iterator_base
00205     {
00206     public:
00207         fixed_depth_iterator( );
00208         fixed_depth_iterator( tree_node* );
00209         fixed_depth_iterator( const iterator_base& );
00210         fixed_depth_iterator( const sibling_iterator& );
00211         fixed_depth_iterator( const fixed_depth_iterator& );
00212 
00213         bool operator==( const fixed_depth_iterator& ) const;
00214         bool operator!=( const fixed_depth_iterator& ) const;
00215         fixed_depth_iterator& operator++( );
00216         fixed_depth_iterator& operator--( );
00217         fixed_depth_iterator operator++( int );
00218         fixed_depth_iterator operator--( int );
00219         fixed_depth_iterator& operator+=( unsigned int );
00220         fixed_depth_iterator& operator-=( unsigned int );
00221 
00222         tree_node* top_node;
00223     };
00224 
00226 
00227     class sibling_iterator : public iterator_base
00228     {
00229     public:
00230         sibling_iterator( );
00231         sibling_iterator( tree_node* );
00232         sibling_iterator( const sibling_iterator& );
00233         sibling_iterator( const iterator_base& );
00234 
00235         bool operator==( const sibling_iterator& ) const;
00236         bool operator!=( const sibling_iterator& ) const;
00237         sibling_iterator& operator++( );
00238         sibling_iterator& operator--( );
00239         sibling_iterator operator++( int );
00240         sibling_iterator operator--( int );
00241         sibling_iterator& operator+=( unsigned int );
00242         sibling_iterator& operator-=( unsigned int );
00243 
00244         tree_node* range_first( ) const;
00245         tree_node* range_last( ) const;
00246         tree_node* parent_;
00247     private:
00248         void set_parent_( );
00249     };
00250 
00252 
00253     class leaf_iterator : public iterator_base
00254     {
00255     public:
00256         leaf_iterator( );
00257         leaf_iterator( tree_node*, tree_node* top = 0 );
00258         leaf_iterator( const sibling_iterator& );
00259         leaf_iterator( const iterator_base& );
00260 
00261         bool operator==( const leaf_iterator& ) const;
00262         bool operator!=( const leaf_iterator& ) const;
00263         leaf_iterator& operator++( );
00264         leaf_iterator& operator--( );
00265         leaf_iterator operator++( int );
00266         leaf_iterator operator--( int );
00267         leaf_iterator& operator+=( unsigned int );
00268         leaf_iterator& operator-=( unsigned int );
00269     private:
00270         tree_node* top_node;
00271     };
00272 
00274     inline pre_order_iterator begin( ) const;
00276     inline pre_order_iterator end( ) const;
00278     post_order_iterator begin_post( ) const;
00280     post_order_iterator end_post( ) const;
00282     fixed_depth_iterator begin_fixed( const iterator_base&, unsigned int ) const;
00284     fixed_depth_iterator end_fixed( const iterator_base&, unsigned int ) const;
00286     breadth_first_queued_iterator begin_breadth_first( ) const;
00288     breadth_first_queued_iterator end_breadth_first( ) const;
00290     sibling_iterator begin( const iterator_base& ) const;
00292     sibling_iterator end( const iterator_base& ) const;
00294     leaf_iterator begin_leaf( ) const;
00296     leaf_iterator end_leaf( ) const;
00298     leaf_iterator begin_leaf( const iterator_base& top ) const;
00300     leaf_iterator end_leaf( const iterator_base& top ) const;
00301 
00303     template<typename iter> static iter parent( iter );
00305     template<typename iter> iter previous_sibling( iter ) const;
00307     template<typename iter> iter next_sibling( iter ) const;
00309     template<typename iter> iter next_at_same_depth( iter ) const;
00310 
00312     void clear( );
00314     template<typename iter> iter erase( iter );
00316     void erase_children( const iterator_base& );
00317 
00319     template<typename iter> iter append_child( iter position );
00320     template<typename iter> iter prepend_child( iter position );
00322     template<typename iter> iter append_child( iter position, const T& x );
00323     template<typename iter> iter prepend_child( iter position, const T& x );
00325     template<typename iter> iter append_child( iter position, iter other_position );
00326     template<typename iter> iter prepend_child( iter position, iter other_position );
00328     template<typename iter> iter append_children( iter position, sibling_iterator from, sibling_iterator to );
00329     template<typename iter> iter prepend_children( iter position, sibling_iterator from, sibling_iterator to );
00330 
00332     pre_order_iterator set_head( const T& x );
00334     template<typename iter> iter insert( iter position, const T& x );
00336     sibling_iterator insert( sibling_iterator position, const T& x );
00338     template<typename iter> iter insert_subtree( iter position, const iterator_base& subtree );
00340     template<typename iter> iter insert_after( iter position, const T& x );
00342     template<typename iter> iter insert_subtree_after( iter position, const iterator_base& subtree );
00343 
00345     template<typename iter> iter replace( iter position, const T& x );
00347     template<typename iter> iter replace( iter position, const iterator_base& from );
00349     sibling_iterator replace( sibling_iterator orig_begin, sibling_iterator orig_end,
00350                               sibling_iterator new_begin, sibling_iterator new_end );
00351 
00353     template<typename iter> iter flatten( iter position );
00355     template<typename iter> iter reparent( iter position, sibling_iterator begin, sibling_iterator end );
00357     template<typename iter> iter reparent( iter position, iter from );
00358 
00360     template<typename iter> iter wrap( iter position, const T& x );
00361 
00363     template<typename iter> iter move_after( iter target, iter source );
00365     template<typename iter> iter move_before( iter target, iter source );
00366     sibling_iterator move_before( sibling_iterator target, sibling_iterator source );
00368     template<typename iter> iter move_ontop( iter target, iter source );
00369 
00371     void merge( sibling_iterator, sibling_iterator, sibling_iterator, sibling_iterator,
00372                 bool duplicate_leaves = false );
00374     void sort( sibling_iterator from, sibling_iterator to, bool deep = false );
00375     template<class StrictWeakOrdering>
00376     void sort( sibling_iterator from, sibling_iterator to, StrictWeakOrdering comp, bool deep = false );
00378     template<typename iter>
00379     bool equal( const iter& one, const iter& two, const iter& three ) const;
00380     template<typename iter, class BinaryPredicate>
00381     bool equal( const iter& one, const iter& two, const iter& three, BinaryPredicate ) const;
00382     template<typename iter>
00383     bool equal_subtree( const iter& one, const iter& two ) const;
00384     template<typename iter, class BinaryPredicate>
00385     bool equal_subtree( const iter& one, const iter& two, BinaryPredicate ) const;
00387     tree subtree( sibling_iterator from, sibling_iterator to ) const;
00388     void subtree( tree&, sibling_iterator from, sibling_iterator to ) const;
00390     void swap( sibling_iterator it );
00392     void swap( iterator, iterator );
00393 
00395     size_t size( ) const;
00397     size_t size( const iterator_base& ) const;
00399     bool empty( ) const;
00401     static int depth( const iterator_base& );
00402     static int depth( const iterator_base&, const iterator_base& );
00404     int max_depth( ) const;
00406     int max_depth( const iterator_base& ) const;
00408     static unsigned int number_of_children( const iterator_base& );
00410     unsigned int number_of_siblings( const iterator_base& ) const;
00412     bool is_in_subtree( const iterator_base& position, const iterator_base& begin,
00413                         const iterator_base& end ) const;
00415     bool is_valid( const iterator_base& ) const;
00418     iterator lowest_common_ancestor( const iterator_base&, const iterator_base& ) const;
00419 
00421     unsigned int index( sibling_iterator it ) const;
00423     static sibling_iterator child( const iterator_base& position, unsigned int );
00425     sibling_iterator sibling( const iterator_base& position, unsigned int );
00426 
00429     void
00430     debug_verify_consistency( ) const;
00431 
00433 
00434     class iterator_base_less
00435     {
00436     public:
00437 
00438         bool operator( )( const typename tree<T, tree_node_allocator>::iterator_base& one,
00439                           const typename tree<T, tree_node_allocator>::iterator_base& two ) const
00440         {
00441             return one.node < two.node;
00442         }
00443     };
00444     tree_node* head, *feet; // head/feet are always dummy; if an iterator points to them it is invalid
00445 private:
00446     tree_node_allocator alloc_;
00447     void head_initialise_( );
00448     void
00449     copy_( const tree<T, tree_node_allocator>& other );
00450 
00452 
00453     template<class StrictWeakOrdering>
00454     class compare_nodes
00455     {
00456     public:
00457 
00458         compare_nodes( StrictWeakOrdering comp ) : comp_( comp ){ };
00459 
00460         bool operator( )( const tree_node* a, const tree_node* b )
00461         {
00462             return comp_( a->data, b->data );
00463         }
00464     private:
00465         StrictWeakOrdering comp_;
00466     };
00467 };
00468 
00469 //template <class T, class tree_node_allocator>
00470 //class iterator_base_less {
00471 //  public:
00472 //      bool operator()(const typename tree<T, tree_node_allocator>::iterator_base& one,
00473 //                        const typename tree<T, tree_node_allocator>::iterator_base& two) const
00474 //          {
00475 //          txtout << "operatorclass<" << one.node < two.node << std::endl;
00476 //          return one.node < two.node;
00477 //          }
00478 //};
00479 
00480 // template <class T, class tree_node_allocator>
00481 // bool operator<(const typename tree<T, tree_node_allocator>::iterator& one,
00482 //                  const typename tree<T, tree_node_allocator>::iterator& two)
00483 //  {
00484 //  txtout << "operator< " << one.node < two.node << std::endl;
00485 //  if(one.node < two.node) return true;
00486 //  return false;
00487 //  }
00488 //
00489 // template <class T, class tree_node_allocator>
00490 // bool operator==(const typename tree<T, tree_node_allocator>::iterator& one,
00491 //                  const typename tree<T, tree_node_allocator>::iterator& two)
00492 //  {
00493 //  txtout << "operator== " << one.node == two.node << std::endl;
00494 //  if(one.node == two.node) return true;
00495 //  return false;
00496 //  }
00497 //
00498 // template <class T, class tree_node_allocator>
00499 // bool operator>(const typename tree<T, tree_node_allocator>::iterator_base& one,
00500 //                  const typename tree<T, tree_node_allocator>::iterator_base& two)
00501 //  {
00502 //  txtout << "operator> " << one.node < two.node << std::endl;
00503 //  if(one.node > two.node) return true;
00504 //  return false;
00505 //  }
00506 
00507 
00508 
00509 // Tree
00510 
00511 template <class T, class tree_node_allocator>
00512 tree<T, tree_node_allocator>
00513 ::tree( )
00514 {
00515     head_initialise_( );
00516 }
00517 
00518 template <class T, class tree_node_allocator>
00519 tree<T, tree_node_allocator>
00520 ::tree( const T& x )
00521 {
00522     head_initialise_( );
00523     set_head( x );
00524 }
00525 
00526 template <class T, class tree_node_allocator>
00527 tree<T, tree_node_allocator>
00528 ::tree( const iterator_base& other )
00529 {
00530     head_initialise_( );
00531     set_head( ( *other ) );
00532     replace( begin( ), other );
00533 }
00534 
00535 template <class T, class tree_node_allocator>
00536 tree<T, tree_node_allocator>
00537 ::~tree( )
00538 {
00539     clear( );
00540     alloc_.destroy( head );
00541     alloc_.destroy( feet );
00542     alloc_.deallocate( head, 1 );
00543     alloc_.deallocate( feet, 1 );
00544 }
00545 
00546 template <class T, class tree_node_allocator>
00547 void tree<T, tree_node_allocator>
00548 ::head_initialise_( )
00549 {
00550     head = alloc_.allocate( 1, 0 ); // MSVC does not have default second argument
00551     feet = alloc_.allocate( 1, 0 );
00552     alloc_.construct( head, tree_node_<T > ( ) );
00553     alloc_.construct( feet, tree_node_<T > ( ) );
00554 
00555     head->parent = 0;
00556     head->first_child = 0;
00557     head->last_child = 0;
00558     head->prev_sibling = 0; //head;
00559     head->next_sibling = feet; //head;
00560 
00561     feet->parent = 0;
00562     feet->first_child = 0;
00563     feet->last_child = 0;
00564     feet->prev_sibling = head;
00565     feet->next_sibling = 0;
00566 }
00567 
00568 template <class T, class tree_node_allocator>
00569 tree<T, tree_node_allocator>& tree<T, tree_node_allocator>::operator=( const tree<T, tree_node_allocator>& other )
00570 {
00571     if( this != &other )
00572         copy_( other );
00573 
00574     return *this;
00575 }
00576 
00577 template <class T, class tree_node_allocator>
00578 tree<T, tree_node_allocator>
00579 ::tree( const tree<T, tree_node_allocator>& other )
00580 {
00581     head_initialise_( );
00582     copy_( other );
00583 }
00584 
00585 template <class T, class tree_node_allocator>
00586 void tree<T, tree_node_allocator>
00587 ::copy_( const tree<T, tree_node_allocator>& other )
00588 {
00589     clear( );
00590     pre_order_iterator it = other.begin( ), to = begin( );
00591 
00592     while( it != other.end( ) )
00593     {
00594         to = insert( to, ( *it ) );
00595         it.skip_children( );
00596         ++it;
00597     }
00598 
00599     to = begin( );
00600     it = other.begin( );
00601 
00602     while( it != other.end( ) )
00603     {
00604         to = replace( to, it );
00605         to.skip_children( );
00606         it.skip_children( );
00607         ++to;
00608         ++it;
00609     }
00610 }
00611 
00612 template <class T, class tree_node_allocator>
00613 void tree<T, tree_node_allocator>
00614 ::clear( )
00615 {
00616     if( head )
00617         while( head->next_sibling != feet )
00618             erase( pre_order_iterator( head->next_sibling ) );
00619 }
00620 
00621 template<class T, class tree_node_allocator>
00622 void tree<T, tree_node_allocator>
00623 ::erase_children( const iterator_base& it )
00624 {
00625     //  std::cout << "erase_children " << it.node << std::endl;
00626     if( it.node == 0 ) return;
00627 
00628     tree_node* cur = it.node->first_child;
00629     tree_node* prev = 0;
00630 
00631     while( cur != 0 )
00632     {
00633         prev = cur;
00634         cur = cur->next_sibling;
00635         erase_children( pre_order_iterator( prev ) );
00636         //      kp::destructor(&prev->data);
00637         alloc_.destroy( prev );
00638         alloc_.deallocate( prev, 1 );
00639     }
00640 
00641     it.node->first_child = 0;
00642     it.node->last_child = 0;
00643     //  std::cout << "exit" << std::endl;
00644 }
00645 
00646 template<class T, class tree_node_allocator>
00647 template<class iter>
00648 iter tree<T, tree_node_allocator>
00649 ::erase( iter it )
00650 {
00651     tree_node* cur = it.node;
00652     assert( cur != head );
00653     iter ret = it;
00654     ret.skip_children( );
00655     ++ret;
00656     erase_children( it );
00657 
00658     if( cur->prev_sibling == 0 )
00659     {
00660         cur->parent->first_child = cur->next_sibling;
00661     }
00662 
00663     else
00664     {
00665         cur->prev_sibling->next_sibling = cur->next_sibling;
00666     }
00667 
00668     if( cur->next_sibling == 0 )
00669     {
00670         cur->parent->last_child = cur->prev_sibling;
00671     }
00672 
00673     else
00674     {
00675         cur->next_sibling->prev_sibling = cur->prev_sibling;
00676     }
00677 
00678     //  kp::destructor(&cur->data);
00679     alloc_.destroy( cur );
00680     alloc_.deallocate( cur, 1 );
00681     return ret;
00682 }
00683 
00684 template <class T, class tree_node_allocator>
00685 typename tree<T, tree_node_allocator>::pre_order_iterator tree<T, tree_node_allocator>
00686 ::begin( ) const
00687 {
00688     return pre_order_iterator( head->next_sibling );
00689 }
00690 
00691 template <class T, class tree_node_allocator>
00692 typename tree<T, tree_node_allocator>::pre_order_iterator tree<T, tree_node_allocator>
00693 ::end( ) const
00694 {
00695     return pre_order_iterator( feet );
00696 }
00697 
00698 template <class T, class tree_node_allocator>
00699 typename tree<T, tree_node_allocator>::breadth_first_queued_iterator tree<T, tree_node_allocator>
00700 ::begin_breadth_first( ) const
00701 {
00702     return breadth_first_queued_iterator( head->next_sibling );
00703 }
00704 
00705 template <class T, class tree_node_allocator>
00706 typename tree<T, tree_node_allocator>::breadth_first_queued_iterator tree<T, tree_node_allocator>
00707 ::end_breadth_first( ) const
00708 {
00709     return breadth_first_queued_iterator( );
00710 }
00711 
00712 template <class T, class tree_node_allocator>
00713 typename tree<T, tree_node_allocator>::post_order_iterator tree<T, tree_node_allocator>
00714 ::begin_post( ) const
00715 {
00716     tree_node* tmp = head->next_sibling;
00717 
00718     if( tmp != feet )
00719     {
00720         while( tmp->first_child )
00721             tmp = tmp->first_child;
00722     }
00723 
00724     return post_order_iterator( tmp );
00725 }
00726 
00727 template <class T, class tree_node_allocator>
00728 typename tree<T, tree_node_allocator>::post_order_iterator tree<T, tree_node_allocator>
00729 ::end_post( ) const
00730 {
00731     return post_order_iterator( feet );
00732 }
00733 
00734 template <class T, class tree_node_allocator>
00735 typename tree<T, tree_node_allocator>::fixed_depth_iterator tree<T, tree_node_allocator>
00736 ::begin_fixed( const iterator_base& pos, unsigned int dp ) const
00737 {
00738     typename tree<T, tree_node_allocator>::fixed_depth_iterator ret;
00739     ret.top_node = pos.node;
00740 
00741     tree_node* tmp = pos.node;
00742     unsigned int curdepth = 0;
00743 
00744     while( curdepth < dp ) // go down one level
00745     {
00746         while( tmp->first_child == 0 )
00747         {
00748             if( tmp->next_sibling == 0 )
00749             {
00750                 // try to walk up and then right again
00751                 do
00752                 {
00753                     if( tmp == ret.top_node )
00754                         throw std::range_error( "tree: begin_fixed out of range" );
00755 
00756                     tmp = tmp->parent;
00757 
00758                     if( tmp == 0 )
00759                         throw std::range_error( "tree: begin_fixed out of range" );
00760 
00761                     --curdepth;
00762                 }
00763                 while( tmp->next_sibling == 0 );
00764             }
00765 
00766             tmp = tmp->next_sibling;
00767         }
00768 
00769         tmp = tmp->first_child;
00770         ++curdepth;
00771     }
00772 
00773     ret.node = tmp;
00774     return ret;
00775 }
00776 
00777 template <class T, class tree_node_allocator>
00778 typename tree<T, tree_node_allocator>::fixed_depth_iterator tree<T, tree_node_allocator>
00779 ::end_fixed( const iterator_base& pos, unsigned int dp ) const
00780 {
00781     assert( 1 == 0 ); // FIXME: not correct yet: use is_valid() as a temporary workaround
00782     tree_node* tmp = pos.node;
00783     unsigned int curdepth = 1;
00784 
00785     while( curdepth < dp ) // go down one level
00786     {
00787         while( tmp->first_child == 0 )
00788         {
00789             tmp = tmp->next_sibling;
00790 
00791             if( tmp == 0 )
00792                 throw std::range_error( "tree: end_fixed out of range" );
00793         }
00794 
00795         tmp = tmp->first_child;
00796         ++curdepth;
00797     }
00798 
00799     return tmp;
00800 }
00801 
00802 template <class T, class tree_node_allocator>
00803 typename tree<T, tree_node_allocator>::sibling_iterator tree<T, tree_node_allocator>
00804 ::begin( const iterator_base& pos ) const
00805 {
00806     assert( pos.node != 0 );
00807 
00808     if( pos.node->first_child == 0 )
00809     {
00810         return end( pos );
00811     }
00812 
00813     return pos.node->first_child;
00814 }
00815 
00816 template <class T, class tree_node_allocator>
00817 typename tree<T, tree_node_allocator>::sibling_iterator tree<T, tree_node_allocator>
00818 ::end( const iterator_base& pos ) const
00819 {
00820     sibling_iterator ret( 0 );
00821     ret.parent_ = pos.node;
00822     return ret;
00823 }
00824 
00825 template <class T, class tree_node_allocator>
00826 typename tree<T, tree_node_allocator>::leaf_iterator tree<T, tree_node_allocator>
00827 ::begin_leaf( ) const
00828 {
00829     tree_node* tmp = head->next_sibling;
00830 
00831     if( tmp != feet )
00832     {
00833         while( tmp->first_child )
00834             tmp = tmp->first_child;
00835     }
00836 
00837     return leaf_iterator( tmp );
00838 }
00839 
00840 template <class T, class tree_node_allocator>
00841 typename tree<T, tree_node_allocator>::leaf_iterator tree<T, tree_node_allocator>
00842 ::end_leaf( ) const
00843 {
00844     return leaf_iterator( feet );
00845 }
00846 
00847 template <class T, class tree_node_allocator>
00848 typename tree<T, tree_node_allocator>::leaf_iterator tree<T, tree_node_allocator>
00849 ::begin_leaf( const iterator_base& top ) const
00850 {
00851     tree_node* tmp = top.node;
00852 
00853     while( tmp->first_child )
00854         tmp = tmp->first_child;
00855 
00856     return leaf_iterator( tmp, top.node );
00857 }
00858 
00859 template <class T, class tree_node_allocator>
00860 typename tree<T, tree_node_allocator>::leaf_iterator tree<T, tree_node_allocator>
00861 ::end_leaf( const iterator_base& top ) const
00862 {
00863     return leaf_iterator( top.node, top.node );
00864 }
00865 
00866 template <class T, class tree_node_allocator>
00867 template <typename iter>
00868 iter tree<T, tree_node_allocator>
00869 ::parent( iter position )
00870 {
00871     assert( position.node != 0 );
00872     return iter( position.node->parent );
00873 }
00874 
00875 template <class T, class tree_node_allocator>
00876 template <typename iter>
00877 iter tree<T, tree_node_allocator>
00878 ::previous_sibling( iter position ) const
00879 {
00880     assert( position.node != 0 );
00881     iter ret( position );
00882     ret.node = position.node->prev_sibling;
00883     return ret;
00884 }
00885 
00886 template <class T, class tree_node_allocator>
00887 template <typename iter>
00888 iter tree<T, tree_node_allocator>
00889 ::next_sibling( iter position ) const
00890 {
00891     assert( position.node != 0 );
00892     iter ret( position );
00893     ret.node = position.node->next_sibling;
00894     return ret;
00895 }
00896 
00897 template <class T, class tree_node_allocator>
00898 template <typename iter>
00899 iter tree<T, tree_node_allocator>
00900 ::next_at_same_depth( iter position ) const
00901 {
00902     // We make use of a temporary fixed_depth iterator to implement this.
00903 
00904     typename tree<T, tree_node_allocator>::fixed_depth_iterator tmp( position.node );
00905 
00906     ++tmp;
00907     return iter( tmp );
00908 
00909     //  assert(position.node!=0);
00910     //  iter ret(position);
00911     //
00912     //  if(position.node->next_sibling) {
00913     //      ret.node=position.node->next_sibling;
00914     //      }
00915     //  else {
00916     //      int relative_depth=0;
00917     //     upper:
00918     //      do {
00919     //          ret.node=ret.node->parent;
00920     //          if(ret.node==0) return ret;
00921     //          --relative_depth;
00922     //          } while(ret.node->next_sibling==0);
00923     //     lower:
00924     //      ret.node=ret.node->next_sibling;
00925     //      while(ret.node->first_child==0) {
00926     //          if(ret.node->next_sibling==0)
00927     //              goto upper;
00928     //          ret.node=ret.node->next_sibling;
00929     //          if(ret.node==0) return ret;
00930     //          }
00931     //      while(relative_depth<0 && ret.node->first_child!=0) {
00932     //          ret.node=ret.node->first_child;
00933     //          ++relative_depth;
00934     //          }
00935     //      if(relative_depth<0) {
00936     //          if(ret.node->next_sibling==0) goto upper;
00937     //          else                          goto lower;
00938     //          }
00939     //      }
00940     //  return ret;
00941 }
00942 
00943 template <class T, class tree_node_allocator>
00944 template <typename iter>
00945 iter tree<T, tree_node_allocator>
00946 ::append_child( iter position )
00947 {
00948     assert( position.node != head );
00949     assert( position.node != feet );
00950     assert( position.node );
00951 
00952     tree_node* tmp = alloc_.allocate( 1, 0 );
00953     alloc_.construct( tmp, tree_node_<T > ( ) );
00954     //  kp::constructor(&tmp->data);
00955     tmp->first_child = 0;
00956     tmp->last_child = 0;
00957 
00958     tmp->parent = position.node;
00959 
00960     if( position.node->last_child != 0 )
00961     {
00962         position.node->last_child->next_sibling = tmp;
00963     }
00964 
00965     else
00966     {
00967         position.node->first_child = tmp;
00968     }
00969 
00970     tmp->prev_sibling = position.node->last_child;
00971     position.node->last_child = tmp;
00972     tmp->next_sibling = 0;
00973     return tmp;
00974 }
00975 
00976 template <class T, class tree_node_allocator>
00977 template <typename iter>
00978 iter tree<T, tree_node_allocator>
00979 ::prepend_child( iter position )
00980 {
00981     assert( position.node != head );
00982     assert( position.node != feet );
00983     assert( position.node );
00984 
00985     tree_node* tmp = alloc_.allocate( 1, 0 );
00986     alloc_.construct( tmp, tree_node_<T > ( ) );
00987     //  kp::constructor(&tmp->data);
00988     tmp->first_child = 0;
00989     tmp->last_child = 0;
00990 
00991     tmp->parent = position.node;
00992 
00993     if( position.node->first_child != 0 )
00994     {
00995         position.node->first_child->prev_sibling = tmp;
00996     }
00997 
00998     else
00999     {
01000         position.node->last_child = tmp;
01001     }
01002 
01003     tmp->next_sibling = position.node->first_child;
01004     position.node->prev_child = tmp;
01005     tmp->prev_sibling = 0;
01006     return tmp;
01007 }
01008 
01009 template <class T, class tree_node_allocator>
01010 template <class iter>
01011 iter tree<T, tree_node_allocator>
01012 ::append_child( iter position, const T& x )
01013 {
01014     // If your program fails here you probably used 'append_child' to add the top
01015     // node to an empty tree. From version 1.45 the top element should be added
01016     // using 'insert'. See the documentation for further information, and sorry about
01017     // the API change.
01018     assert( position.node != head );
01019     assert( position.node != feet );
01020     assert( position.node );
01021 
01022     tree_node* tmp = alloc_.allocate( 1, 0 );
01023     alloc_.construct( tmp, x );
01024     //  kp::constructor(&tmp->data, x);
01025     tmp->first_child = 0;
01026     tmp->last_child = 0;
01027 
01028     tmp->parent = position.node;
01029 
01030     if( position.node->last_child != 0 )
01031     {
01032         position.node->last_child->next_sibling = tmp;
01033     }
01034 
01035     else
01036     {
01037         position.node->first_child = tmp;
01038     }
01039 
01040     tmp->prev_sibling = position.node->last_child;
01041     position.node->last_child = tmp;
01042     tmp->next_sibling = 0;
01043     return tmp;
01044 }
01045 
01046 template <class T, class tree_node_allocator>
01047 template <class iter>
01048 iter tree<T, tree_node_allocator>
01049 ::prepend_child( iter position, const T& x )
01050 {
01051     assert( position.node != head );
01052     assert( position.node != feet );
01053     assert( position.node );
01054 
01055     tree_node* tmp = alloc_.allocate( 1, 0 );
01056     alloc_.construct( tmp, x );
01057     //  kp::constructor(&tmp->data, x);
01058     tmp->first_child = 0;
01059     tmp->last_child = 0;
01060 
01061     tmp->parent = position.node;
01062 
01063     if( position.node->first_child != 0 )
01064     {
01065         position.node->first_child->prev_sibling = tmp;
01066     }
01067 
01068     else
01069     {
01070         position.node->last_child = tmp;
01071     }
01072 
01073     tmp->next_sibling = position.node->first_child;
01074     position.node->first_child = tmp;
01075     tmp->prev_sibling = 0;
01076     return tmp;
01077 }
01078 
01079 template <class T, class tree_node_allocator>
01080 template <class iter>
01081 iter tree<T, tree_node_allocator>
01082 ::append_child( iter position, iter other )
01083 {
01084     assert( position.node != head );
01085     assert( position.node != feet );
01086     assert( position.node );
01087 
01088     sibling_iterator aargh = append_child( position, value_type( ) );
01089     return replace( aargh, other );
01090 }
01091 
01092 template <class T, class tree_node_allocator>
01093 template <class iter>
01094 iter tree<T, tree_node_allocator>
01095 ::prepend_child( iter position, iter other )
01096 {
01097     assert( position.node != head );
01098     assert( position.node != feet );
01099     assert( position.node );
01100 
01101     sibling_iterator aargh = prepend_child( position, value_type( ) );
01102     return replace( aargh, other );
01103 }
01104 
01105 template <class T, class tree_node_allocator>
01106 template <class iter>
01107 iter tree<T, tree_node_allocator>
01108 ::append_children( iter position, sibling_iterator from, sibling_iterator to )
01109 {
01110     assert( position.node != head );
01111     assert( position.node != feet );
01112     assert( position.node );
01113 
01114     iter ret = from;
01115 
01116     while( from != to )
01117     {
01118         insert_subtree( position.end( ), from );
01119         ++from;
01120     }
01121 
01122     return ret;
01123 }
01124 
01125 template <class T, class tree_node_allocator>
01126 template <class iter>
01127 iter tree<T, tree_node_allocator>
01128 ::prepend_children( iter position, sibling_iterator from, sibling_iterator to )
01129 {
01130     assert( position.node != head );
01131     assert( position.node != feet );
01132     assert( position.node );
01133 
01134     iter ret = from;
01135 
01136     while( from != to )
01137     {
01138         insert_subtree( position.begin( ), from );
01139         ++from;
01140     }
01141 
01142     return ret;
01143 }
01144 
01145 template <class T, class tree_node_allocator>
01146 typename tree<T, tree_node_allocator>::pre_order_iterator tree<T, tree_node_allocator>
01147 ::set_head( const T& x )
01148 {
01149     assert( head->next_sibling == feet );
01150     return insert( iterator( feet ), x );
01151 }
01152 
01153 template <class T, class tree_node_allocator>
01154 template <class iter>
01155 iter tree<T, tree_node_allocator>
01156 ::insert( iter position, const T& x )
01157 {
01158     if( position.node == 0 )
01159     {
01160         position.node = feet; // Backward compatibility: when calling insert on a null node,
01161         // insert before the feet.
01162     }
01163 
01164     tree_node* tmp = alloc_.allocate( 1, 0 );
01165     alloc_.construct( tmp, x );
01166     //  kp::constructor(&tmp->data, x);
01167     tmp->first_child = 0;
01168     tmp->last_child = 0;
01169 
01170     tmp->parent = position.node->parent;
01171     tmp->next_sibling = position.node;
01172     tmp->prev_sibling = position.node->prev_sibling;
01173     position.node->prev_sibling = tmp;
01174 
01175     if( tmp->prev_sibling == 0 )
01176     {
01177         if( tmp->parent ) // when inserting nodes at the head, there is no parent
01178             tmp->parent->first_child = tmp;
01179     }
01180 
01181     else
01182         tmp->prev_sibling->next_sibling = tmp;
01183 
01184     return tmp;
01185 }
01186 
01187 template <class T, class tree_node_allocator>
01188 typename tree<T, tree_node_allocator>::sibling_iterator tree<T, tree_node_allocator>
01189 ::insert( sibling_iterator position, const T& x )
01190 {
01191     tree_node* tmp = alloc_.allocate( 1, 0 );
01192     alloc_.construct( tmp, x );
01193     //  kp::constructor(&tmp->data, x);
01194     tmp->first_child = 0;
01195     tmp->last_child = 0;
01196 
01197     tmp->next_sibling = position.node;
01198 
01199     if( position.node == 0 ) // iterator points to end of a subtree
01200     {
01201         tmp->parent = position.parent_;
01202         tmp->prev_sibling = position.range_last( );
01203         tmp->parent->last_child = tmp;
01204     }
01205 
01206     else
01207     {
01208         tmp->parent = position.node->parent;
01209         tmp->prev_sibling = position.node->prev_sibling;
01210         position.node->prev_sibling = tmp;
01211     }
01212 
01213     if( tmp->prev_sibling == 0 )
01214     {
01215         if( tmp->parent ) // when inserting nodes at the head, there is no parent
01216             tmp->parent->first_child = tmp;
01217     }
01218 
01219     else
01220         tmp->prev_sibling->next_sibling = tmp;
01221 
01222     return tmp;
01223 }
01224 
01225 template <class T, class tree_node_allocator>
01226 template <class iter>
01227 iter tree<T, tree_node_allocator>
01228 ::insert_after( iter position, const T& x )
01229 {
01230     tree_node* tmp = alloc_.allocate( 1, 0 );
01231     alloc_.construct( tmp, x );
01232     //  kp::constructor(&tmp->data, x);
01233     tmp->first_child = 0;
01234     tmp->last_child = 0;
01235 
01236     tmp->parent = position.node->parent;
01237     tmp->prev_sibling = position.node;
01238     tmp->next_sibling = position.node->next_sibling;
01239     position.node->next_sibling = tmp;
01240 
01241     if( tmp->next_sibling == 0 )
01242     {
01243         if( tmp->parent ) // when inserting nodes at the head, there is no parent
01244             tmp->parent->last_child = tmp;
01245     }
01246 
01247     else
01248     {
01249         tmp->next_sibling->prev_sibling = tmp;
01250     }
01251 
01252     return tmp;
01253 }
01254 
01255 template <class T, class tree_node_allocator>
01256 template <class iter>
01257 iter tree<T, tree_node_allocator>
01258 ::insert_subtree( iter position, const iterator_base& subtree )
01259 {
01260     // insert dummy
01261     iter it = insert( position, value_type( ) );
01262     // replace dummy with subtree
01263     return replace( it, subtree );
01264 }
01265 
01266 template <class T, class tree_node_allocator>
01267 template <class iter>
01268 iter tree<T, tree_node_allocator>
01269 ::insert_subtree_after( iter position, const iterator_base& subtree )
01270 {
01271     // insert dummy
01272     iter it = insert_after( position, value_type( ) );
01273     // replace dummy with subtree
01274     return replace( it, subtree );
01275 }
01276 
01277 // template <class T, class tree_node_allocator>
01278 // template <class iter>
01279 // iter tree<T, tree_node_allocator>::insert_subtree(sibling_iterator position, iter subtree)
01280 //  {
01281 //  // insert dummy
01282 //  iter it(insert(position, value_type()));
01283 //  // replace dummy with subtree
01284 //  return replace(it, subtree);
01285 //  }
01286 
01287 template <class T, class tree_node_allocator>
01288 template <class iter>
01289 iter tree<T, tree_node_allocator>
01290 ::replace( iter position, const T& x )
01291 {
01292     //  kp::destructor(&position.node->data);
01293     //  kp::constructor(&position.node->data, x);
01294     position.node->data = x;
01295     //  alloc_.destroy(position.node);
01296     //  alloc_.construct(position.node, x);
01297     return position;
01298 }
01299 
01300 template <class T, class tree_node_allocator>
01301 template <class iter>
01302 iter tree<T, tree_node_allocator>
01303 ::replace( iter position, const iterator_base& from )
01304 {
01305     assert( position.node != head );
01306     tree_node* current_from = from.node;
01307     tree_node* start_from = from.node;
01308     tree_node* current_to = position.node;
01309 
01310     // replace the node at position with head of the replacement tree at from
01311     //  std::cout << "warning!" << position.node << std::endl;
01312     erase_children( position );
01313     //  std::cout << "no warning!" << std::endl;
01314     tree_node* tmp = alloc_.allocate( 1, 0 );
01315     alloc_.construct( tmp, ( *from ) );
01316     //  kp::constructor(&tmp->data, (*from));
01317     tmp->first_child = 0;
01318     tmp->last_child = 0;
01319 
01320     if( current_to->prev_sibling == 0 )
01321     {
01322         if( current_to->parent != 0 )
01323             current_to->parent->first_child = tmp;
01324     }
01325 
01326     else
01327     {
01328         current_to->prev_sibling->next_sibling = tmp;
01329     }
01330 
01331     tmp->prev_sibling = current_to->prev_sibling;
01332 
01333     if( current_to->next_sibling == 0 )
01334     {
01335         if( current_to->parent != 0 )
01336             current_to->parent->last_child = tmp;
01337     }
01338 
01339     else
01340     {
01341         current_to->next_sibling->prev_sibling = tmp;
01342     }
01343 
01344     tmp->next_sibling = current_to->next_sibling;
01345     tmp->parent = current_to->parent;
01346     //  kp::destructor(&current_to->data);
01347     alloc_.destroy( current_to );
01348     alloc_.deallocate( current_to, 1 );
01349     current_to = tmp;
01350 
01351     // only at this stage can we fix 'last'
01352     tree_node* last = from.node->next_sibling;
01353 
01354     pre_order_iterator toit = tmp;
01355 
01356     // copy all children
01357     do
01358     {
01359         assert( current_from != 0 );
01360 
01361         if( current_from->first_child != 0 )
01362         {
01363             current_from = current_from->first_child;
01364             toit = append_child( toit, current_from->data );
01365         }
01366 
01367         else
01368         {
01369             while( current_from->next_sibling == 0 && current_from != start_from )
01370             {
01371                 current_from = current_from->parent;
01372                 toit = parent( toit );
01373                 assert( current_from != 0 );
01374             }
01375 
01376             current_from = current_from->next_sibling;
01377 
01378             if( current_from != last )
01379             {
01380                 toit = append_child( parent( toit ), current_from->data );
01381             }
01382         }
01383     }
01384     while( current_from != last );
01385 
01386     return current_to;
01387 }
01388 
01389 template <class T, class tree_node_allocator>
01390 typename tree<T, tree_node_allocator>::sibling_iterator tree<T, tree_node_allocator>
01391 ::replace(
01392          sibling_iterator orig_begin,
01393          sibling_iterator orig_end,
01394          sibling_iterator new_begin,
01395          sibling_iterator new_end )
01396 {
01397     tree_node* orig_first = orig_begin.node;
01398     tree_node* new_first = new_begin.node;
01399     tree_node* orig_last = orig_first;
01400 
01401     while( ( ++orig_begin ) != orig_end )
01402         orig_last = orig_last->next_sibling;
01403 
01404     tree_node* new_last = new_first;
01405 
01406     while( ( ++new_begin ) != new_end )
01407         new_last = new_last->next_sibling;
01408 
01409     // insert all siblings in new_first..new_last before orig_first
01410     bool first = true;
01411     pre_order_iterator ret;
01412 
01413     while( 1 == 1 )
01414     {
01415         pre_order_iterator tt = insert_subtree( pre_order_iterator( orig_first ), pre_order_iterator( new_first ) );
01416 
01417         if( first )
01418         {
01419             ret = tt;
01420             first = false;
01421         }
01422 
01423         if( new_first == new_last )
01424             break;
01425 
01426         new_first = new_first->next_sibling;
01427     }
01428 
01429     // erase old range of siblings
01430     bool last = false;
01431     tree_node* next = orig_first;
01432 
01433     while( 1 == 1 )
01434     {
01435         if( next == orig_last )
01436             last = true;
01437 
01438         next = next->next_sibling;
01439         erase( ( pre_order_iterator ) orig_first );
01440 
01441         if( last )
01442             break;
01443 
01444         orig_first = next;
01445     }
01446 
01447     return ret;
01448 }
01449 
01450 template <class T, class tree_node_allocator>
01451 template <typename iter>
01452 iter tree<T, tree_node_allocator>
01453 ::flatten( iter position )
01454 {
01455     if( position.node->first_child == 0 )
01456         return position;
01457 
01458     tree_node* tmp = position.node->first_child;
01459 
01460     while( tmp )
01461     {
01462         tmp->parent = position.node->parent;
01463         tmp = tmp->next_sibling;
01464     }
01465 
01466     if( position.node->next_sibling )
01467     {
01468         position.node->last_child->next_sibling = position.node->next_sibling;
01469         position.node->next_sibling->prev_sibling = position.node->last_child;
01470     }
01471 
01472     else
01473     {
01474         position.node->parent->last_child = position.node->last_child;
01475     }
01476 
01477     position.node->next_sibling = position.node->first_child;
01478     position.node->next_sibling->prev_sibling = position.node;
01479     position.node->first_child = 0;
01480     position.node->last_child = 0;
01481 
01482     return position;
01483 }
01484 
01485 template <class T, class tree_node_allocator>
01486 template <typename iter>
01487 iter tree<T, tree_node_allocator>
01488 ::reparent( iter position, sibling_iterator begin, sibling_iterator end )
01489 {
01490     tree_node* first = begin.node;
01491     tree_node* last = first;
01492 
01493     assert( first != position.node );
01494 
01495     if( begin == end ) return begin;
01496 
01497     // determine last node
01498     while( ( ++begin ) != end )
01499     {
01500         last = last->next_sibling;
01501     }
01502 
01503     // move subtree
01504     if( first->prev_sibling == 0 )
01505     {
01506         first->parent->first_child = last->next_sibling;
01507     }
01508 
01509     else
01510     {
01511         first->prev_sibling->next_sibling = last->next_sibling;
01512     }
01513 
01514     if( last->next_sibling == 0 )
01515     {
01516         last->parent->last_child = first->prev_sibling;
01517     }
01518 
01519     else
01520     {
01521         last->next_sibling->prev_sibling = first->prev_sibling;
01522     }
01523 
01524     if( position.node->first_child == 0 )
01525     {
01526         position.node->first_child = first;
01527         position.node->last_child = last;
01528         first->prev_sibling = 0;
01529     }
01530 
01531     else
01532     {
01533         position.node->last_child->next_sibling = first;
01534         first->prev_sibling = position.node->last_child;
01535         position.node->last_child = last;
01536     }
01537 
01538     last->next_sibling = 0;
01539 
01540     tree_node* pos = first;
01541 
01542     for(;; )
01543     {
01544         pos->parent = position.node;
01545 
01546         if( pos == last ) break;
01547 
01548         pos = pos->next_sibling;
01549     }
01550 
01551     return first;
01552 }
01553 
01554 template <class T, class tree_node_allocator>
01555 template <typename iter> iter tree<T, tree_node_allocator>
01556 ::reparent( iter position, iter from )
01557 {
01558     if( from.node->first_child == 0 ) return position;
01559 
01560     return reparent( position, from.node->first_child, end( from ) );
01561 }
01562 
01563 template <class T, class tree_node_allocator>
01564 template <typename iter> iter tree<T, tree_node_allocator>
01565 ::wrap( iter position, const T& x )
01566 {
01567     assert( position.node != 0 );
01568     sibling_iterator fr = position, to = position;
01569     ++to;
01570     iter ret = insert( position, x );
01571     reparent( ret, fr, to );
01572     return ret;
01573 }
01574 
01575 template <class T, class tree_node_allocator>
01576 template <typename iter> iter tree<T, tree_node_allocator>
01577 ::move_after( iter target, iter source )
01578 {
01579     tree_node* dst = target.node;
01580     tree_node* src = source.node;
01581     assert( dst );
01582     assert( src );
01583 
01584     if( dst == src ) return source;
01585 
01586     if( dst->next_sibling )
01587         if( dst->next_sibling == src ) // already in the right spot
01588             return source;
01589 
01590     // take src out of the tree
01591     if( src->prev_sibling != 0 ) src->prev_sibling->next_sibling = src->next_sibling;
01592 
01593     else src->parent->first_child = src->next_sibling;
01594 
01595     if( src->next_sibling != 0 ) src->next_sibling->prev_sibling = src->prev_sibling;
01596 
01597     else src->parent->last_child = src->prev_sibling;
01598 
01599     // connect it to the new point
01600     if( dst->next_sibling != 0 ) dst->next_sibling->prev_sibling = src;
01601 
01602     else dst->parent->last_child = src;
01603 
01604     src->next_sibling = dst->next_sibling;
01605     dst->next_sibling = src;
01606     src->prev_sibling = dst;
01607     src->parent = dst->parent;
01608     return src;
01609 }
01610 
01611 template <class T, class tree_node_allocator>
01612 template <typename iter> iter tree<T, tree_node_allocator>
01613 ::move_before( iter target, iter source )
01614 {
01615     tree_node* dst = target.node;
01616     tree_node* src = source.node;
01617     assert( dst );
01618     assert( src );
01619 
01620     if( dst == src ) return source;
01621 
01622     if( dst->prev_sibling )
01623         if( dst->prev_sibling == src ) // already in the right spot
01624             return source;
01625 
01626     // take src out of the tree
01627     if( src->prev_sibling != 0 ) src->prev_sibling->next_sibling = src->next_sibling;
01628 
01629     else src->parent->first_child = src->next_sibling;
01630 
01631     if( src->next_sibling != 0 ) src->next_sibling->prev_sibling = src->prev_sibling;
01632 
01633     else src->parent->last_child = src->prev_sibling;
01634 
01635     // connect it to the new point
01636     if( dst->prev_sibling != 0 ) dst->prev_sibling->next_sibling = src;
01637 
01638     else dst->parent->first_child = src;
01639 
01640     src->prev_sibling = dst->prev_sibling;
01641     dst->prev_sibling = src;
01642     src->next_sibling = dst;
01643     src->parent = dst->parent;
01644     return src;
01645 }
01646 
01647 // specialisation for sibling_iterators
01648 
01649 template <class T, class tree_node_allocator>
01650 typename tree<T, tree_node_allocator>::sibling_iterator tree<T, tree_node_allocator>
01651 ::move_before( sibling_iterator target,
01652              sibling_iterator source )
01653 {
01654     tree_node* dst = target.node;
01655     tree_node* src = source.node;
01656     tree_node* dst_prev_sibling;
01657 
01658     if( dst == 0 ) // must then be an end iterator
01659     {
01660         dst_prev_sibling = target.parent_->last_child;
01661         assert( dst_prev_sibling );
01662     }
01663 
01664     else dst_prev_sibling = dst->prev_sibling;
01665 
01666     assert( src );
01667 
01668     if( dst == src ) return source;
01669 
01670     if( dst_prev_sibling )
01671         if( dst_prev_sibling == src ) // already in the right spot
01672             return source;
01673 
01674     // take src out of the tree
01675     if( src->prev_sibling != 0 ) src->prev_sibling->next_sibling = src->next_sibling;
01676 
01677     else src->parent->first_child = src->next_sibling;
01678 
01679     if( src->next_sibling != 0 ) src->next_sibling->prev_sibling = src->prev_sibling;
01680 
01681     else src->parent->last_child = src->prev_sibling;
01682 
01683     // connect it to the new point
01684     if( dst_prev_sibling != 0 ) dst_prev_sibling->next_sibling = src;
01685 
01686     else target.parent_->first_child = src;
01687 
01688     src->prev_sibling = dst_prev_sibling;
01689 
01690     if( dst )
01691     {
01692         dst->prev_sibling = src;
01693         src->parent = dst->parent;
01694     }
01695 
01696     src->next_sibling = dst;
01697     return src;
01698 }
01699 
01700 template <class T, class tree_node_allocator>
01701 template <typename iter> iter tree<T, tree_node_allocator>
01702 ::move_ontop( iter target, iter source )
01703 {
01704     tree_node* dst = target.node;
01705     tree_node* src = source.node;
01706     assert( dst );
01707     assert( src );
01708 
01709     if( dst == src ) return source;
01710 
01711     //  if(dst==src->prev_sibling) {
01712     //
01713     //      }
01714 
01715     // remember connection points
01716     tree_node* b_prev_sibling = dst->prev_sibling;
01717     tree_node* b_next_sibling = dst->next_sibling;
01718     tree_node* b_parent = dst->parent;
01719 
01720     // remove target
01721     erase( target );
01722 
01723     // take src out of the tree
01724     if( src->prev_sibling != 0 ) src->prev_sibling->next_sibling = src->next_sibling;
01725 
01726     else src->parent->first_child = src->next_sibling;
01727 
01728     if( src->next_sibling != 0 ) src->next_sibling->prev_sibling = src->prev_sibling;
01729 
01730     else src->parent->last_child = src->prev_sibling;
01731 
01732     // connect it to the new point
01733     if( b_prev_sibling != 0 ) b_prev_sibling->next_sibling = src;
01734 
01735     else b_parent->first_child = src;
01736 
01737     if( b_next_sibling != 0 ) b_next_sibling->prev_sibling = src;
01738 
01739     else b_parent->last_child = src;
01740 
01741     src->prev_sibling = b_prev_sibling;
01742     src->next_sibling = b_next_sibling;
01743     src->parent = b_parent;
01744     return src;
01745 }
01746 
01747 template <class T, class tree_node_allocator>
01748 void tree<T, tree_node_allocator>
01749 ::merge( sibling_iterator to1, sibling_iterator to2,
01750        sibling_iterator from1, sibling_iterator from2,
01751        bool duplicate_leaves )
01752 {
01753     sibling_iterator fnd;
01754 
01755     while( from1 != from2 )
01756     {
01757         if( ( fnd = std::find( to1, to2, ( *from1 ) ) ) != to2 ) // element found
01758         {
01759             if( from1.begin( ) == from1.end( ) ) // full depth reached
01760             {
01761                 if( duplicate_leaves )
01762                     append_child( parent( to1 ), ( *from1 ) );
01763             }
01764 
01765             else // descend further
01766             {
01767                 merge( fnd.begin( ), fnd.end( ), from1.begin( ), from1.end( ), duplicate_leaves );
01768             }
01769         }
01770 
01771         else // element missing
01772         {
01773             insert_subtree( to2, from1 );
01774         }
01775 
01776         ++from1;
01777     }
01778 }
01779 
01780 template <class T, class tree_node_allocator>
01781 void tree<T, tree_node_allocator>
01782 ::sort( sibling_iterator from, sibling_iterator to, bool deep )
01783 {
01784     std::less<T> comp;
01785     sort( from, to, comp, deep );
01786 }
01787 
01788 template <class T, class tree_node_allocator>
01789 template <class StrictWeakOrdering>
01790 void tree<T, tree_node_allocator>
01791 ::sort( sibling_iterator from, sibling_iterator to,
01792       StrictWeakOrdering comp, bool deep )
01793 {
01794     if( from == to ) return;
01795 
01796     // make list of sorted nodes
01797     // CHECK: if multiset stores equivalent nodes in the order in which they
01798     // are inserted, then this routine should be called 'stable_sort'.
01799     std::multiset<tree_node*, compare_nodes<StrictWeakOrdering> > nodes( comp );
01800     sibling_iterator it = from, it2 = to;
01801 
01802     while( it != to )
01803     {
01804         nodes.insert( it.node );
01805         ++it;
01806     }
01807 
01808     // reassemble
01809     --it2;
01810 
01811     // prev and next are the nodes before and after the sorted range
01812     tree_node* prev = from.node->prev_sibling;
01813     tree_node* next = it2.node->next_sibling;
01814     typename std::multiset<tree_node*, compare_nodes<StrictWeakOrdering> >::iterator nit = nodes.begin( ), eit = nodes.end( );
01815 
01816     if( prev == 0 )
01817     {
01818         if( ( *nit )->parent != 0 ) // to catch "sorting the head" situations, when there is no parent
01819             ( *nit )->parent->first_child = ( *nit );
01820     }
01821 
01822     else prev->next_sibling = ( *nit );
01823 
01824     --eit;
01825 
01826     while( nit != eit )
01827     {
01828         ( *nit )->prev_sibling = prev;
01829 
01830         if( prev )
01831             prev->next_sibling = ( *nit );
01832 
01833         prev = ( *nit );
01834         ++nit;
01835     }
01836 
01837     // prev now points to the last-but-one node in the sorted range
01838     if( prev )
01839         prev->next_sibling = ( *eit );
01840 
01841     // eit points to the last node in the sorted range.
01842     ( *eit )->next_sibling = next;
01843     ( *eit )->prev_sibling = prev; // missed in the loop above
01844 
01845     if( next == 0 )
01846     {
01847         if( ( *eit )->parent != 0 ) // to catch "sorting the head" situations, when there is no parent
01848             ( *eit )->parent->last_child = ( *eit );
01849     }
01850 
01851     else next->prev_sibling = ( *eit );
01852 
01853     if( deep ) // sort the children of each node too
01854     {
01855         sibling_iterator bcs( *nodes.begin( ) );
01856         sibling_iterator ecs( *eit );
01857         ++ecs;
01858 
01859         while( bcs != ecs )
01860         {
01861             sort( begin( bcs ), end( bcs ), comp, deep );
01862             ++bcs;
01863         }
01864     }
01865 }
01866 
01867 template <class T, class tree_node_allocator>
01868 template <typename iter>
01869 bool tree<T, tree_node_allocator>
01870 ::equal( const iter& one_, const iter& two, const iter& three_ ) const
01871 {
01872     std::equal_to<T> comp;
01873     return equal( one_, two, three_, comp );
01874 }
01875 
01876 template <class T, class tree_node_allocator>
01877 template <typename iter>
01878 bool tree<T, tree_node_allocator>
01879 ::equal_subtree( const iter& one_, const iter& two_ ) const
01880 {
01881     std::equal_to<T> comp;
01882     return equal_subtree( one_, two_, comp );
01883 }
01884 
01885 template <class T, class tree_node_allocator>
01886 template <typename iter, class BinaryPredicate>
01887 bool tree<T, tree_node_allocator>
01888 ::equal( const iter& one_, const iter& two, const iter& three_, BinaryPredicate fun ) const
01889 {
01890     pre_order_iterator one( one_ ), three( three_ );
01891 
01892     //  if(one==two && is_valid(three) && three.number_of_children()!=0)
01893     //      return false;
01894     while( one != two && is_valid( three ) )
01895     {
01896         if( !fun( *one, *three ) )
01897             return false;
01898 
01899         if( one.number_of_children( ) != three.number_of_children( ) )
01900             return false;
01901 
01902         ++one;
01903         ++three;
01904     }
01905 
01906     return true;
01907 }
01908 
01909 template <class T, class tree_node_allocator>
01910 template <typename iter, class BinaryPredicate>
01911 bool tree<T, tree_node_allocator>
01912 ::equal_subtree( const iter& one_, const iter& two_, BinaryPredicate fun ) const
01913 {
01914     pre_order_iterator one( one_ ), two( two_ );
01915 
01916     if( !fun( *one, *two ) ) return false;
01917 
01918     if( number_of_children( one ) != number_of_children( two ) ) return false;
01919 
01920     return equal( begin( one ), end( one ), begin( two ), fun );
01921 }
01922 
01923 template <class T, class tree_node_allocator>
01924 tree<T, tree_node_allocator> tree<T, tree_node_allocator>
01925 ::subtree( sibling_iterator from, sibling_iterator to ) const
01926 {
01927     tree tmp;
01928     tmp.set_head( value_type( ) );
01929     tmp.replace( tmp.begin( ), tmp.end( ), from, to );
01930     return tmp;
01931 }
01932 
01933 template <class T, class tree_node_allocator>
01934 void tree<T, tree_node_allocator>
01935 ::subtree( tree& tmp, sibling_iterator from, sibling_iterator to ) const
01936 {
01937     tmp.set_head( value_type( ) );
01938     tmp.replace( tmp.begin( ), tmp.end( ), from, to );
01939 }
01940 
01941 template <class T, class tree_node_allocator>
01942 size_t tree<T, tree_node_allocator>
01943 ::size( ) const
01944 {
01945     size_t i = 0;
01946     pre_order_iterator it = begin( ), eit = end( );
01947 
01948     while( it != eit )
01949     {
01950         ++i;
01951         ++it;
01952     }
01953 
01954     return i;
01955 }
01956 
01957 template <class T, class tree_node_allocator>
01958 size_t tree<T, tree_node_allocator>
01959 ::size( const iterator_base& top ) const
01960 {
01961     size_t i = 0;
01962     pre_order_iterator it = top, eit = top;
01963     eit.skip_children( );
01964     ++eit;
01965 
01966     while( it != eit )
01967     {
01968         ++i;
01969         ++it;
01970     }
01971 
01972     return i;
01973 }
01974 
01975 template <class T, class tree_node_allocator>
01976 bool tree<T, tree_node_allocator>
01977 ::empty( ) const
01978 {
01979     pre_order_iterator it = begin( ), eit = end( );
01980     return (it == eit );
01981 }
01982 
01983 template <class T, class tree_node_allocator>
01984 int tree<T, tree_node_allocator>
01985 ::depth( const iterator_base& it )
01986 {
01987     tree_node* pos = it.node;
01988     assert( pos != 0 );
01989     int ret = 0;
01990 
01991     while( pos->parent != 0 )
01992     {
01993         pos = pos->parent;
01994         ++ret;
01995     }
01996 
01997     return ret;
01998 }
01999 
02000 template <class T, class tree_node_allocator>
02001 int tree<T, tree_node_allocator>
02002 ::depth( const iterator_base& it, const iterator_base& root )
02003 {
02004     tree_node* pos = it.node;
02005     assert( pos != 0 );
02006     int ret = 0;
02007 
02008     while( pos->parent != 0 && pos != root.node )
02009     {
02010         pos = pos->parent;
02011         ++ret;
02012     }
02013 
02014     return ret;
02015 }
02016 
02017 template <class T, class tree_node_allocator>
02018 int tree<T, tree_node_allocator>
02019 ::max_depth( ) const
02020 {
02021     int maxd = -1;
02022 
02023     for( tree_node* it = head->next_sibling; it != feet; it = it->next_sibling )
02024         maxd = std::max( maxd, max_depth( it ) );
02025 
02026     return maxd;
02027 }
02028 
02029 template <class T, class tree_node_allocator>
02030 int tree<T, tree_node_allocator>
02031 ::max_depth( const iterator_base& pos ) const
02032 {
02033     tree_node* tmp = pos.node;
02034 
02035     if( tmp == 0 || tmp == head || tmp == feet ) return -1;
02036 
02037     int curdepth = 0, maxdepth = 0;
02038 
02039     while( true ) // try to walk the bottom of the tree
02040     {
02041         while( tmp->first_child == 0 )
02042         {
02043             if( tmp == pos.node ) return maxdepth;
02044 
02045             if( tmp->next_sibling == 0 )
02046             {
02047                 // try to walk up and then right again
02048                 do
02049                 {
02050                     tmp = tmp->parent;
02051 
02052                     if( tmp == 0 ) return maxdepth;
02053 
02054                     --curdepth;
02055                 }
02056                 while( tmp->next_sibling == 0 );
02057             }
02058 
02059             if( tmp == pos.node ) return maxdepth;
02060 
02061             tmp = tmp->next_sibling;
02062         }
02063 
02064         tmp = tmp->first_child;
02065         ++curdepth;
02066         maxdepth = std::max( curdepth, maxdepth );
02067     }
02068 }
02069 
02070 template <class T, class tree_node_allocator>
02071 unsigned int tree<T, tree_node_allocator>
02072 ::number_of_children( const iterator_base& it )
02073 {
02074     tree_node* pos = it.node->first_child;
02075 
02076     if( pos == 0 ) return 0;
02077 
02078     unsigned int ret = 1;
02079 
02080     //    while(pos!=it.node->last_child) {
02081     //        ++ret;
02082     //        pos=pos->next_sibling;
02083     //        }
02084     while( ( pos = pos->next_sibling ) )
02085         ++ret;
02086 
02087     return ret;
02088 }
02089 
02090 template <class T, class tree_node_allocator>
02091 unsigned int tree<T, tree_node_allocator>
02092 ::number_of_siblings( const iterator_base& it ) const
02093 {
02094     tree_node* pos = it.node;
02095     unsigned int ret = 0;
02096 
02097     // count forward
02098     while( pos->next_sibling &&
02099            pos->next_sibling != head &&
02100            pos->next_sibling != feet )
02101     {
02102         ++ret;
02103         pos = pos->next_sibling;
02104     }
02105 
02106     // count backward
02107     pos = it.node;
02108 
02109     while( pos->prev_sibling &&
02110            pos->prev_sibling != head &&
02111            pos->prev_sibling != feet )
02112     {
02113         ++ret;
02114         pos = pos->prev_sibling;
02115     }
02116 
02117     return ret;
02118 }
02119 
02120 template <class T, class tree_node_allocator>
02121 void tree<T, tree_node_allocator>
02122 ::swap( sibling_iterator it )
02123 {
02124     tree_node* nxt = it.node->next_sibling;
02125 
02126     if( nxt )
02127     {
02128         if( it.node->prev_sibling )
02129             it.node->prev_sibling->next_sibling = nxt;
02130 
02131         else
02132             it.node->parent->first_child = nxt;
02133 
02134         nxt->prev_sibling = it.node->prev_sibling;
02135         tree_node* nxtnxt = nxt->next_sibling;
02136 
02137         if( nxtnxt )
02138             nxtnxt->prev_sibling = it.node;
02139 
02140         else
02141             it.node->parent->last_child = it.node;
02142 
02143         nxt->next_sibling = it.node;
02144         it.node->prev_sibling = nxt;
02145         it.node->next_sibling = nxtnxt;
02146     }
02147 }
02148 
02149 template <class T, class tree_node_allocator>
02150 void tree<T, tree_node_allocator>
02151 ::swap( iterator one, iterator two )
02152 {
02153     // if one and two are adjacent siblings, use the sibling swap
02154     if( one.node->next_sibling == two.node ) swap( one );
02155 
02156     else if( two.node->next_sibling == one.node ) swap( two );
02157 
02158     else
02159     {
02160         tree_node* nxt1 = one.node->next_sibling;
02161         tree_node* nxt2 = two.node->next_sibling;
02162         tree_node* pre1 = one.node->prev_sibling;
02163         tree_node* pre2 = two.node->prev_sibling;
02164         tree_node* par1 = one.node->parent;
02165         tree_node* par2 = two.node->parent;
02166 
02167         // reconnect
02168         one.node->parent = par2;
02169         one.node->next_sibling = nxt2;
02170 
02171         if( nxt2 ) nxt2->prev_sibling = one.node;
02172 
02173         else par2->last_child = one.node;
02174 
02175         one.node->prev_sibling = pre2;
02176 
02177         if( pre2 ) pre2->next_sibling = one.node;
02178 
02179         else par2->first_child = one.node;
02180 
02181         two.node->parent = par1;
02182         two.node->next_sibling = nxt1;
02183 
02184         if( nxt1 ) nxt1->prev_sibling = two.node;
02185 
02186         else par1->last_child = two.node;
02187 
02188         two.node->prev_sibling = pre1;
02189 
02190         if( pre1 ) pre1->next_sibling = two.node;
02191 
02192         else par1->first_child = two.node;
02193     }
02194 }
02195 
02196 // template <class BinaryPredicate>
02197 // tree<T, tree_node_allocator>::iterator tree<T, tree_node_allocator>::find_subtree(
02198 //  sibling_iterator subfrom, sibling_iterator subto, iterator from, iterator to,
02199 //  BinaryPredicate fun) const
02200 //  {
02201 //  assert(1==0); // this routine is not finished yet.
02202 //  while(from!=to) {
02203 //      if(fun(*subfrom, *from)) {
02204 //
02205 //          }
02206 //      }
02207 //  return to;
02208 //  }
02209 
02210 template <class T, class tree_node_allocator>
02211 bool tree<T, tree_node_allocator>
02212 ::is_in_subtree( const iterator_base& it, const iterator_base& begin,
02213                const iterator_base& end ) const
02214 {
02215     // FIXME: this should be optimised.
02216     pre_order_iterator tmp = begin;
02217 
02218     while( tmp != end )
02219     {
02220         if( tmp == it ) return true;
02221 
02222         ++tmp;
02223     }
02224 
02225     return false;
02226 }
02227 
02228 template <class T, class tree_node_allocator>
02229 bool tree<T, tree_node_allocator>
02230 ::is_valid( const iterator_base& it ) const
02231 {
02232     if( it.node == 0 || it.node == feet || it.node == head ) return false;
02233 
02234     else return true;
02235 }
02236 
02237 template <class T, class tree_node_allocator>
02238 typename tree<T, tree_node_allocator>::iterator tree<T, tree_node_allocator>
02239 ::lowest_common_ancestor(
02240                         const iterator_base& one, const iterator_base& two ) const
02241 {
02242     std::set<iterator, iterator_base_less> parents;
02243 
02244     // Walk up from 'one' storing all parents.
02245     iterator walk = one;
02246 
02247     do
02248     {
02249         walk = parent( walk );
02250         parents.insert( walk );
02251     }
02252     while( is_valid( parent( walk ) ) );
02253 
02254     // Walk up from 'two' until we encounter a node in parents.
02255     walk = two;
02256 
02257     do
02258     {
02259         walk = parent( walk );
02260 
02261         if( parents.find( walk ) != parents.end( ) ) break;
02262     }
02263     while( is_valid( parent( walk ) ) );
02264 
02265     return walk;
02266 }
02267 
02268 template <class T, class tree_node_allocator>
02269 unsigned int tree<T, tree_node_allocator>
02270 ::index( sibling_iterator it ) const
02271 {
02272     unsigned int ind = 0;
02273 
02274     if( it.node->parent == 0 )
02275     {
02276         while( it.node->prev_sibling != head )
02277         {
02278             it.node = it.node->prev_sibling;
02279             ++ind;
02280         }
02281     }
02282 
02283     else
02284     {
02285         while( it.node->prev_sibling != 0 )
02286         {
02287             it.node = it.node->prev_sibling;
02288             ++ind;
02289         }
02290     }
02291 
02292     return ind;
02293 }
02294 
02295 template <class T, class tree_node_allocator>
02296 typename tree<T, tree_node_allocator>::sibling_iterator tree<T, tree_node_allocator>
02297 ::sibling( const iterator_base& it, unsigned int num )
02298 {
02299     tree_node* tmp;
02300 
02301     if( it.node->parent == 0 )
02302     {
02303         tmp = head->next_sibling;
02304 
02305         while( num )
02306         {
02307             tmp = tmp->next_sibling;
02308             --num;
02309         }
02310     }
02311 
02312     else
02313     {
02314         tmp = it.node->parent->first_child;
02315 
02316         while( num )
02317         {
02318             assert( tmp != 0 );
02319             tmp = tmp->next_sibling;
02320             --num;
02321         }
02322     }
02323 
02324     return tmp;
02325 }
02326 
02327 template <class T, class tree_node_allocator>
02328 void tree<T, tree_node_allocator>
02329 ::debug_verify_consistency( ) const
02330 {
02331     iterator it = begin( );
02332 
02333     while( it != end( ) )
02334     {
02335         if( it.node->parent != 0 )
02336         {
02337             if( it.node->prev_sibling == 0 )
02338                 assert( it.node->parent->first_child == it.node );
02339 
02340             else
02341                 assert( it.node->prev_sibling->next_sibling == it.node );
02342 
02343             if( it.node->next_sibling == 0 )
02344                 assert( it.node->parent->last_child == it.node );
02345 
02346             else
02347                 assert( it.node->next_sibling->prev_sibling == it.node );
02348         }
02349 
02350         ++it;
02351     }
02352 }
02353 
02354 template <class T, class tree_node_allocator>
02355 typename tree<T, tree_node_allocator>::sibling_iterator tree<T, tree_node_allocator>
02356 ::child( const iterator_base& it, unsigned int num )
02357 {
02358     tree_node* tmp = it.node->first_child;
02359 
02360     while( num-- )
02361     {
02362         assert( tmp != 0 );
02363         tmp = tmp->next_sibling;
02364     }
02365 
02366     return tmp;
02367 }
02368 
02369 
02370 
02371 
02372 // Iterator base
02373 
02374 template <class T, class tree_node_allocator>
02375 tree<T, tree_node_allocator>
02376 ::iterator_base::iterator_base( )
02377 : node( 0 ), skip_current_children_( false ){ }
02378 
02379 template <class T, class tree_node_allocator>
02380 tree<T, tree_node_allocator>
02381 ::iterator_base::iterator_base( tree_node* tn )
02382 : node( tn ), skip_current_children_( false ){ }
02383 
02384 template <class T, class tree_node_allocator>
02385 T& tree<T, tree_node_allocator>::iterator_base::operator*( ) const
02386 {
02387     return node->data;
02388 }
02389 
02390 template <class T, class tree_node_allocator>
02391 T* tree<T, tree_node_allocator>::iterator_base::operator->( ) const
02392 {
02393     return &( node->data );
02394 }
02395 
02396 template <class T, class tree_node_allocator>
02397 bool tree<T, tree_node_allocator>::post_order_iterator::operator!=( const post_order_iterator& other ) const
02398 {
02399     if( other.node != this->node ) return true;
02400 
02401     else return false;
02402 }
02403 
02404 template <class T, class tree_node_allocator>
02405 bool tree<T, tree_node_allocator>::post_order_iterator::operator==( const post_order_iterator& other ) const
02406 {
02407     if( other.node == this->node ) return true;
02408 
02409     else return false;
02410 }
02411 
02412 template <class T, class tree_node_allocator>
02413 bool tree<T, tree_node_allocator>::pre_order_iterator::operator!=( const pre_order_iterator& other ) const
02414 {
02415     if( other.node != this->node ) return true;
02416 
02417     else return false;
02418 }
02419 
02420 template <class T, class tree_node_allocator>
02421 bool tree<T, tree_node_allocator>::pre_order_iterator::operator==( const pre_order_iterator& other ) const
02422 {
02423     if( other.node == this->node ) return true;
02424 
02425     else return false;
02426 }
02427 
02428 template <class T, class tree_node_allocator>
02429 bool tree<T, tree_node_allocator>::sibling_iterator::operator!=( const sibling_iterator& other ) const
02430 {
02431     if( other.node != this->node ) return true;
02432 
02433     else return false;
02434 }
02435 
02436 template <class T, class tree_node_allocator>
02437 bool tree<T, tree_node_allocator>::sibling_iterator::operator==( const sibling_iterator& other ) const
02438 {
02439     if( other.node == this->node ) return true;
02440 
02441     else return false;
02442 }
02443 
02444 template <class T, class tree_node_allocator>
02445 bool tree<T, tree_node_allocator>::leaf_iterator::operator!=( const leaf_iterator& other ) const
02446 {
02447     if( other.node != this->node ) return true;
02448 
02449     else return false;
02450 }
02451 
02452 template <class T, class tree_node_allocator>
02453 bool tree<T, tree_node_allocator>::leaf_iterator::operator==( const leaf_iterator& other ) const
02454 {
02455     if( other.node == this->node && other.top_node == this->top_node ) return true;
02456 
02457     else return false;
02458 }
02459 
02460 template <class T, class tree_node_allocator>
02461 typename tree<T, tree_node_allocator>::sibling_iterator tree<T, tree_node_allocator>
02462 ::iterator_base::begin( ) const
02463 {
02464     if( node->first_child == 0 )
02465         return end( );
02466 
02467     sibling_iterator ret( node->first_child );
02468     ret.parent_ = this->node;
02469     return ret;
02470 }
02471 
02472 template <class T, class tree_node_allocator>
02473 typename tree<T, tree_node_allocator>::sibling_iterator tree<T, tree_node_allocator>
02474 ::iterator_base::end( ) const
02475 {
02476     sibling_iterator ret( 0 );
02477     ret.parent_ = node;
02478     return ret;
02479 }
02480 
02481 template <class T, class tree_node_allocator>
02482 void tree<T, tree_node_allocator>
02483 ::iterator_base::skip_children( )
02484 {
02485     skip_current_children_ = true;
02486 }
02487 
02488 template <class T, class tree_node_allocator>
02489 void tree<T, tree_node_allocator>
02490 ::iterator_base::skip_children( bool skip )
02491 {
02492     skip_current_children_ = skip;
02493 }
02494 
02495 template <class T, class tree_node_allocator>
02496 unsigned int tree<T, tree_node_allocator>
02497 ::iterator_base::number_of_children( ) const
02498 {
02499     tree_node* pos = node->first_child;
02500 
02501     if( pos == 0 ) return 0;
02502 
02503     unsigned int ret = 1;
02504 
02505     while( pos != node->last_child )
02506     {
02507         ++ret;
02508         pos = pos->next_sibling;
02509     }
02510 
02511     return ret;
02512 }
02513 
02514 
02515 
02516 // Pre-order iterator
02517 
02518 template <class T, class tree_node_allocator>
02519 tree<T, tree_node_allocator>
02520 ::pre_order_iterator::pre_order_iterator( )
02521 : iterator_base( 0 ){ }
02522 
02523 template <class T, class tree_node_allocator>
02524 tree<T, tree_node_allocator>
02525 ::pre_order_iterator::pre_order_iterator( tree_node* tn )
02526 : iterator_base( tn ){ }
02527 
02528 template <class T, class tree_node_allocator>
02529 tree<T, tree_node_allocator>
02530 ::pre_order_iterator::pre_order_iterator( const iterator_base& other )
02531 : iterator_base( other.node ){ }
02532 
02533 template <class T, class tree_node_allocator>
02534 tree<T, tree_node_allocator>
02535 ::pre_order_iterator::pre_order_iterator( const sibling_iterator& other )
02536 : iterator_base( other.node )
02537 {
02538     if( this->node == 0 )
02539     {
02540         if( other.range_last( ) != 0 )
02541             this->node = other.range_last( );
02542 
02543         else
02544             this->node = other.parent_;
02545 
02546         this->skip_children( );
02547         ++( *this );
02548     }
02549 }
02550 
02551 template <class T, class tree_node_allocator>
02552 typename tree<T, tree_node_allocator>::pre_order_iterator& tree<T, tree_node_allocator>::pre_order_iterator::operator++( )
02553 {
02554     assert( this->node != 0 );
02555 
02556     if( !this->skip_current_children_ && this->node->first_child != 0 )
02557     {
02558         this->node = this->node->first_child;
02559     }
02560 
02561     else
02562     {
02563         this->skip_current_children_ = false;
02564 
02565         while( this->node->next_sibling == 0 )
02566         {
02567             this->node = this->node->parent;
02568 
02569             if( this->node == 0 )
02570                 return *this;
02571         }
02572 
02573         this->node = this->node->next_sibling;
02574     }
02575 
02576     return *this;
02577 }
02578 
02579 template <class T, class tree_node_allocator>
02580 typename tree<T, tree_node_allocator>::pre_order_iterator& tree<T, tree_node_allocator>::pre_order_iterator::operator--( )
02581 {
02582     assert( this->node != 0 );
02583 
02584     if( this->node->prev_sibling )
02585     {
02586         this->node = this->node->prev_sibling;
02587 
02588         while( this->node->last_child )
02589             this->node = this->node->last_child;
02590     }
02591 
02592     else
02593     {
02594         this->node = this->node->parent;
02595 
02596         if( this->node == 0 )
02597             return *this;
02598     }
02599 
02600     return *this;
02601 }
02602 
02603 template <class T, class tree_node_allocator>
02604 typename tree<T, tree_node_allocator>::pre_order_iterator tree<T, tree_node_allocator>::pre_order_iterator::operator++( int )
02605 {
02606     pre_order_iterator copy = *this;
02607     ++( *this );
02608     return copy;
02609 }
02610 
02611 template <class T, class tree_node_allocator>
02612 typename tree<T, tree_node_allocator>::pre_order_iterator tree<T, tree_node_allocator>::pre_order_iterator::operator--( int )
02613 {
02614     pre_order_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>::pre_order_iterator& tree<T, tree_node_allocator>::pre_order_iterator::operator+=( unsigned int num )
02621 {
02622     while( num > 0 )
02623     {
02624         ++( *this );
02625         --num;
02626     }
02627 
02628     return (*this );
02629 }
02630 
02631 template <class T, class tree_node_allocator>
02632 typename tree<T, tree_node_allocator>::pre_order_iterator& tree<T, tree_node_allocator>::pre_order_iterator::operator-=( unsigned int num )
02633 {
02634     while( num > 0 )
02635     {
02636         --( *this );
02637         --num;
02638     }
02639 
02640     return (*this );
02641 }
02642 
02643 
02644 
02645 // Post-order iterator
02646 
02647 template <class T, class tree_node_allocator>
02648 tree<T, tree_node_allocator>
02649 ::post_order_iterator::post_order_iterator( )
02650 : iterator_base( 0 ){ }
02651 
02652 template <class T, class tree_node_allocator>
02653 tree<T, tree_node_allocator>
02654 ::post_order_iterator::post_order_iterator( tree_node* tn )
02655 : iterator_base( tn ){ }
02656 
02657 template <class T, class tree_node_allocator>
02658 tree<T, tree_node_allocator>
02659 ::post_order_iterator::post_order_iterator( const iterator_base& other )
02660 : iterator_base( other.node ){ }
02661 
02662 template <class T, class tree_node_allocator>
02663 tree<T, tree_node_allocator>
02664 ::post_order_iterator::post_order_iterator( const sibling_iterator& other )
02665 : iterator_base( other.node )
02666 {
02667     if( this->node == 0 )
02668     {
02669         if( other.range_last( ) != 0 )
02670             this->node = other.range_last( );
02671 
02672         else
02673             this->node = other.parent_;
02674 
02675         this->skip_children( );
02676         ++( *this );
02677     }
02678 }
02679 
02680 template <class T, class tree_node_allocator>
02681 typename tree<T, tree_node_allocator>::post_order_iterator& tree<T, tree_node_allocator>::post_order_iterator::operator++( )
02682 {
02683     assert( this->node != 0 );
02684 
02685     if( this->node->next_sibling == 0 )
02686     {
02687         this->node = this->node->parent;
02688         this->skip_current_children_ = false;
02689     }
02690 
02691     else
02692     {
02693         this->node = this->node->next_sibling;
02694 
02695         if( this->skip_current_children_ )
02696         {
02697             this->skip_current_children_ = false;
02698         }
02699 
02700         else
02701         {
02702             while( this->node->first_child )
02703                 this->node = this->node->first_child;
02704         }
02705     }
02706 
02707     return *this;
02708 }
02709 
02710 template <class T, class tree_node_allocator>
02711 typename tree<T, tree_node_allocator>::post_order_iterator& tree<T, tree_node_allocator>::post_order_iterator::operator--( )
02712 {
02713     assert( this->node != 0 );
02714 
02715     if( this->skip_current_children_ || this->node->last_child == 0 )
02716     {
02717         this->skip_current_children_ = false;
02718 
02719         while( this->node->prev_sibling == 0 )
02720             this->node = this->node->parent;
02721 
02722         this->node = this->node->prev_sibling;
02723     }
02724 
02725     else
02726     {
02727         this->node = this->node->last_child;
02728     }
02729 
02730     return *this;
02731 }
02732 
02733 template <class T, class tree_node_allocator>
02734 typename tree<T, tree_node_allocator>::post_order_iterator tree<T, tree_node_allocator>::post_order_iterator::operator++( int )
02735 {
02736     post_order_iterator copy = *this;
02737     ++( *this );
02738     return copy;
02739 }
02740 
02741 template <class T, class tree_node_allocator>
02742 typename tree<T, tree_node_allocator>::post_order_iterator tree<T, tree_node_allocator>::post_order_iterator::operator--( int )
02743 {
02744     post_order_iterator copy = *this;
02745     --( *this );
02746     return copy;
02747 }
02748 
02749 template <class T, class tree_node_allocator>
02750 typename tree<T, tree_node_allocator>::post_order_iterator& tree<T, tree_node_allocator>::post_order_iterator::operator+=( unsigned int num )
02751 {
02752     while( num > 0 )
02753     {
02754         ++( *this );
02755         --num;
02756     }
02757 
02758     return (*this );
02759 }
02760 
02761 template <class T, class tree_node_allocator>
02762 typename tree<T, tree_node_allocator>::post_order_iterator& tree<T, tree_node_allocator>::post_order_iterator::operator-=( unsigned int num )
02763 {
02764     while( num > 0 )
02765     {
02766         --( *this );
02767         --num;
02768     }
02769 
02770     return (*this );
02771 }
02772 
02773 template <class T, class tree_node_allocator>
02774 void tree<T, tree_node_allocator>
02775 ::post_order_iterator::descend_all( )
02776 {
02777     assert( this->node != 0 );
02778 
02779     while( this->node->first_child )
02780         this->node = this->node->first_child;
02781 }
02782 
02783 
02784 // Breadth-first iterator
02785 
02786 template <class T, class tree_node_allocator>
02787 tree<T, tree_node_allocator>
02788 ::breadth_first_queued_iterator::breadth_first_queued_iterator( )
02789 : iterator_base( ){ }
02790 
02791 template <class T, class tree_node_allocator>
02792 tree<T, tree_node_allocator>
02793 ::breadth_first_queued_iterator::breadth_first_queued_iterator( tree_node* tn )
02794 : iterator_base( tn )
02795 {
02796     traversal_queue.push( tn );
02797 }
02798 
02799 template <class T, class tree_node_allocator>
02800 tree<T, tree_node_allocator>
02801 ::breadth_first_queued_iterator::breadth_first_queued_iterator( const iterator_base& other )
02802 : iterator_base( other.node )
02803 {
02804     traversal_queue.push( other.node );
02805 }
02806 
02807 template <class T, class tree_node_allocator>
02808 bool tree<T, tree_node_allocator>::breadth_first_queued_iterator::operator!=( const breadth_first_queued_iterator& other ) const
02809 {
02810     if( other.node != this->node ) return true;
02811 
02812     else return false;
02813 }
02814 
02815 template <class T, class tree_node_allocator>
02816 bool tree<T, tree_node_allocator>::breadth_first_queued_iterator::operator==( const breadth_first_queued_iterator& other ) const
02817 {
02818     if( other.node == this->node ) return true;
02819 
02820     else return false;
02821 }
02822 
02823 template <class T, class tree_node_allocator>
02824 typename tree<T, tree_node_allocator>::breadth_first_queued_iterator& tree<T, tree_node_allocator>::breadth_first_queued_iterator::operator++( )
02825 {
02826     assert( this->node != 0 );
02827 
02828     // Add child nodes and pop current node
02829     sibling_iterator sib = this->begin( );
02830 
02831     while( sib != this->end( ) )
02832     {
02833         traversal_queue.push( sib.node );
02834         ++sib;
02835     }
02836 
02837     traversal_queue.pop( );
02838 
02839     if( traversal_queue.size( ) > 0 )
02840         this->node = traversal_queue.front( );
02841 
02842     else
02843         this->node = 0;
02844 
02845     return (*this );
02846 }
02847 
02848 template <class T, class tree_node_allocator>
02849 typename tree<T, tree_node_allocator>::breadth_first_queued_iterator tree<T, tree_node_allocator>::breadth_first_queued_iterator::operator++( int )
02850 {
02851     breadth_first_queued_iterator copy = *this;
02852     ++( *this );
02853     return copy;
02854 }
02855 
02856 template <class T, class tree_node_allocator>
02857 typename tree<T, tree_node_allocator>::breadth_first_queued_iterator& tree<T, tree_node_allocator>::breadth_first_queued_iterator::operator+=( unsigned int num )
02858 {
02859     while( num > 0 )
02860     {
02861         ++( *this );
02862         --num;
02863     }
02864 
02865     return (*this );
02866 }
02867 
02868 
02869 
02870 // Fixed depth iterator
02871 
02872 template <class T, class tree_node_allocator>
02873 tree<T, tree_node_allocator>
02874 ::fixed_depth_iterator::fixed_depth_iterator( )
02875 : iterator_base( ){ }
02876 
02877 template <class T, class tree_node_allocator>
02878 tree<T, tree_node_allocator>
02879 ::fixed_depth_iterator::fixed_depth_iterator( tree_node* tn )
02880 : iterator_base( tn ), top_node( 0 ){ }
02881 
02882 template <class T, class tree_node_allocator>
02883 tree<T, tree_node_allocator>
02884 ::fixed_depth_iterator::fixed_depth_iterator( const iterator_base& other )
02885 : iterator_base( other.node ), top_node( 0 ){ }
02886 
02887 template <class T, class tree_node_allocator>
02888 tree<T, tree_node_allocator>
02889 ::fixed_depth_iterator::fixed_depth_iterator( const sibling_iterator& other )
02890 : iterator_base( other.node ), top_node( 0 ){ }
02891 
02892 template <class T, class tree_node_allocator>
02893 tree<T, tree_node_allocator>
02894 ::fixed_depth_iterator::fixed_depth_iterator( const fixed_depth_iterator& other )
02895 : iterator_base( other.node ), top_node( other.top_node ){ }
02896 
02897 template <class T, class tree_node_allocator>
02898 bool tree<T, tree_node_allocator>::fixed_depth_iterator::operator==( const fixed_depth_iterator& other ) const
02899 {
02900     if( other.node == this->node && other.top_node == top_node ) return true;
02901 
02902     else return false;
02903 }
02904 
02905 template <class T, class tree_node_allocator>
02906 bool tree<T, tree_node_allocator>::fixed_depth_iterator::operator!=( const fixed_depth_iterator& other ) const
02907 {
02908     if( other.node != this->node || other.top_node != top_node ) return true;
02909 
02910     else return false;
02911 }
02912 
02913 template <class T, class tree_node_allocator>
02914 typename tree<T, tree_node_allocator>::fixed_depth_iterator& tree<T, tree_node_allocator>::fixed_depth_iterator::operator++( )
02915 {
02916     assert( this->node != 0 );
02917 
02918     if( this->node->next_sibling )
02919     {
02920         this->node = this->node->next_sibling;
02921     }
02922 
02923     else
02924     {
02925         int relative_depth = 0;
02926 upper:
02927 
02928         do
02929         {
02930             if( this->node == this->top_node )
02931             {
02932                 this->node = 0; // FIXME: return a proper fixed_depth end iterator once implemented
02933                 return *this;
02934             }
02935 
02936             this->node = this->node->parent;
02937 
02938             if( this->node == 0 ) return *this;
02939 
02940             --relative_depth;
02941         }
02942         while( this->node->next_sibling == 0 );
02943 
02944 lower:
02945         this->node = this->node->next_sibling;
02946 
02947         while( this->node->first_child == 0 )
02948         {
02949             if( this->node->next_sibling == 0 )
02950                 goto upper;
02951 
02952             this->node = this->node->next_sibling;
02953 
02954             if( this->node == 0 ) return *this;
02955         }
02956 
02957         while( relative_depth < 0 && this->node->first_child != 0 )
02958         {
02959             this->node = this->node->first_child;
02960             ++relative_depth;
02961         }
02962 
02963         if( relative_depth < 0 )
02964         {
02965             if( this->node->next_sibling == 0 ) goto upper;
02966 
02967             else goto lower;
02968         }
02969     }
02970 
02971     return *this;
02972 }
02973 
02974 template <class T, class tree_node_allocator>
02975 typename tree<T, tree_node_allocator>::fixed_depth_iterator& tree<T, tree_node_allocator>::fixed_depth_iterator::operator--( )
02976 {
02977     assert( this->node != 0 );
02978 
02979     if( this->node->prev_sibling )
02980     {
02981         this->node = this->node->prev_sibling;
02982     }
02983 
02984     else
02985     {
02986         int relative_depth = 0;
02987 upper:
02988 
02989         do
02990         {
02991             if( this->node == this->top_node )
02992             {
02993                 this->node = 0;
02994                 return *this;
02995             }
02996 
02997             this->node = this->node->parent;
02998 
02999             if( this->node == 0 ) return *this;
03000 
03001             --relative_depth;
03002         }
03003         while( this->node->prev_sibling == 0 );
03004 
03005 lower:
03006         this->node = this->node->prev_sibling;
03007 
03008         while( this->node->last_child == 0 )
03009         {
03010             if( this->node->prev_sibling == 0 )
03011                 goto upper;
03012 
03013             this->node = this->node->prev_sibling;
03014 
03015             if( this->node == 0 ) return *this;
03016         }
03017 
03018         while( relative_depth < 0 && this->node->last_child != 0 )
03019         {
03020             this->node = this->node->last_child;
03021             ++relative_depth;
03022         }
03023 
03024         if( relative_depth < 0 )
03025         {
03026             if( this->node->prev_sibling == 0 ) goto upper;
03027 
03028             else goto lower;
03029         }
03030     }
03031 
03032     return *this;
03033 
03034     //
03035     //
03036     //  assert(this->node!=0);
03037     //  if(this->node->prev_sibling!=0) {
03038     //      this->node=this->node->prev_sibling;
03039     //      assert(this->node!=0);
03040     //      if(this->node->parent==0 && this->node->prev_sibling==0) // head element
03041     //          this->node=0;
03042     //      }
03043     //  else {
03044     //      tree_node *par=this->node->parent;
03045     //      do {
03046     //          par=par->prev_sibling;
03047     //          if(par==0) { // FIXME: need to keep track of this!
03048     //              this->node=0;
03049     //              return *this;
03050     //              }
03051     //          } while(par->last_child==0);
03052     //      this->node=par->last_child;
03053     //      }
03054     //  return *this;
03055 }
03056 
03057 template <class T, class tree_node_allocator>
03058 typename tree<T, tree_node_allocator>::fixed_depth_iterator tree<T, tree_node_allocator>::fixed_depth_iterator::operator++( int )
03059 {
03060     fixed_depth_iterator copy = *this;
03061     ++( *this );
03062     return copy;
03063 }
03064 
03065 template <class T, class tree_node_allocator>
03066 typename tree<T, tree_node_allocator>::fixed_depth_iterator tree<T, tree_node_allocator>::fixed_depth_iterator::operator--( int )
03067 {
03068     fixed_depth_iterator copy = *this;
03069     --( *this );
03070     return copy;
03071 }
03072 
03073 template <class T, class tree_node_allocator>
03074 typename tree<T, tree_node_allocator>::fixed_depth_iterator& tree<T, tree_node_allocator>::fixed_depth_iterator::operator-=( unsigned int num )
03075 {
03076     while( num > 0 )
03077     {
03078         --( *this );
03079         --( num );
03080     }
03081 
03082     return (*this );
03083 }
03084 
03085 template <class T, class tree_node_allocator>
03086 typename tree<T, tree_node_allocator>::fixed_depth_iterator& tree<T, tree_node_allocator>::fixed_depth_iterator::operator+=( unsigned int num )
03087 {
03088     while( num > 0 )
03089     {
03090         ++( *this );
03091         --( num );
03092     }
03093 
03094     return *this;
03095 }
03096 
03097 
03098 // Sibling iterator
03099 
03100 template <class T, class tree_node_allocator>
03101 tree<T, tree_node_allocator>
03102 ::sibling_iterator::sibling_iterator( )
03103 : iterator_base( )
03104 {
03105     set_parent_( );
03106 }
03107 
03108 template <class T, class tree_node_allocator>
03109 tree<T, tree_node_allocator>
03110 ::sibling_iterator::sibling_iterator( tree_node* tn )
03111 : iterator_base( tn )
03112 {
03113     set_parent_( );
03114 }
03115 
03116 template <class T, class tree_node_allocator>
03117 tree<T, tree_node_allocator>
03118 ::sibling_iterator::sibling_iterator( const iterator_base& other )
03119 : iterator_base( other.node )
03120 {
03121     set_parent_( );
03122 }
03123 
03124 template <class T, class tree_node_allocator>
03125 tree<T, tree_node_allocator>
03126 ::sibling_iterator::sibling_iterator( const sibling_iterator& other )
03127 : iterator_base( other ), parent_( other.parent_ ){ }
03128 
03129 template <class T, class tree_node_allocator>
03130 void tree<T, tree_node_allocator>
03131 ::sibling_iterator::set_parent_( )
03132 {
03133     parent_ = 0;
03134 
03135     if( this->node == 0 ) return;
03136 
03137     if( this->node->parent != 0 )
03138         parent_ = this->node->parent;
03139 }
03140 
03141 template <class T, class tree_node_allocator>
03142 typename tree<T, tree_node_allocator>::sibling_iterator& tree<T, tree_node_allocator>::sibling_iterator::operator++( )
03143 {
03144     if( this->node )
03145         this->node = this->node->next_sibling;
03146 
03147     return *this;
03148 }
03149 
03150 template <class T, class tree_node_allocator>
03151 typename tree<T, tree_node_allocator>::sibling_iterator& tree<T, tree_node_allocator>::sibling_iterator::operator--( )
03152 {
03153     if( this->node ) this->node = this->node->prev_sibling;
03154 
03155     else
03156     {
03157         assert( parent_ );
03158         this->node = parent_->last_child;
03159     }
03160 
03161     return *this;
03162 }
03163 
03164 template <class T, class tree_node_allocator>
03165 typename tree<T, tree_node_allocator>::sibling_iterator tree<T, tree_node_allocator>::sibling_iterator::operator++( int )
03166 {
03167     sibling_iterator copy = *this;
03168     ++( *this );
03169     return copy;
03170 }
03171 
03172 template <class T, class tree_node_allocator>
03173 typename tree<T, tree_node_allocator>::sibling_iterator tree<T, tree_node_allocator>::sibling_iterator::operator--( int )
03174 {
03175     sibling_iterator copy = *this;
03176     --( *this );
03177     return copy;
03178 }
03179 
03180 template <class T, class tree_node_allocator>
03181 typename tree<T, tree_node_allocator>::sibling_iterator& tree<T, tree_node_allocator>::sibling_iterator::operator+=( unsigned int num )
03182 {
03183     while( num > 0 )
03184     {
03185         ++( *this );
03186         --num;
03187     }
03188 
03189     return (*this );
03190 }
03191 
03192 template <class T, class tree_node_allocator>
03193 typename tree<T, tree_node_allocator>::sibling_iterator& tree<T, tree_node_allocator>::sibling_iterator::operator-=( unsigned int num )
03194 {
03195     while( num > 0 )
03196     {
03197         --( *this );
03198         --num;
03199     }
03200 
03201     return (*this );
03202 }
03203 
03204 template <class T, class tree_node_allocator>
03205 typename tree<T, tree_node_allocator>::tree_node* tree<T, tree_node_allocator>
03206 ::sibling_iterator::range_first( ) const
03207 {
03208     tree_node* tmp = parent_->first_child;
03209     return tmp;
03210 }
03211 
03212 template <class T, class tree_node_allocator>
03213 typename tree<T, tree_node_allocator>::tree_node* tree<T, tree_node_allocator>
03214 ::sibling_iterator::range_last( ) const
03215 {
03216     return parent_->last_child;
03217 }
03218 
03219 // Leaf iterator
03220 
03221 template <class T, class tree_node_allocator>
03222 tree<T, tree_node_allocator>
03223 ::leaf_iterator::leaf_iterator( )
03224 : iterator_base( 0 ), top_node( 0 ){ }
03225 
03226 template <class T, class tree_node_allocator>
03227 tree<T, tree_node_allocator>
03228 ::leaf_iterator::leaf_iterator( tree_node* tn, tree_node* top )
03229 : iterator_base( tn ), top_node( top ){ }
03230 
03231 template <class T, class tree_node_allocator>
03232 tree<T, tree_node_allocator>
03233 ::leaf_iterator::leaf_iterator( const iterator_base& other )
03234 : iterator_base( other.node ), top_node( 0 ){ }
03235 
03236 template <class T, class tree_node_allocator>
03237 tree<T, tree_node_allocator>
03238 ::leaf_iterator::leaf_iterator( const sibling_iterator& other )
03239 : iterator_base( other.node ), top_node( 0 )
03240 {
03241     if( this->node == 0 )
03242     {
03243         if( other.range_last( ) != 0 )
03244             this->node = other.range_last( );
03245 
03246         else
03247             this->node = other.parent_;
03248 
03249         ++( *this );
03250     }
03251 }
03252 
03253 template <class T, class tree_node_allocator>
03254 typename tree<T, tree_node_allocator>::leaf_iterator& tree<T, tree_node_allocator>::leaf_iterator::operator++( )
03255 {
03256     assert( this->node != 0 );
03257 
03258     if( this->node->first_child != 0 ) // current node is no longer leaf (children got added)
03259     {
03260         while( this->node->first_child )
03261             this->node = this->node->first_child;
03262     }
03263 
03264     else
03265     {
03266         while( this->node->next_sibling == 0 )
03267         {
03268             if( this->node->parent == 0 ) return *this;
03269 
03270             this->node = this->node->parent;
03271 
03272             if( top_node != 0 && this->node == top_node ) return *this;
03273         }
03274 
03275         this->node = this->node->next_sibling;
03276 
03277         while( this->node->first_child )
03278             this->node = this->node->first_child;
03279     }
03280 
03281     return *this;
03282 }
03283 
03284 template <class T, class tree_node_allocator>
03285 typename tree<T, tree_node_allocator>::leaf_iterator& tree<T, tree_node_allocator>::leaf_iterator::operator--( )
03286 {
03287     assert( this->node != 0 );
03288 
03289     while( this->node->prev_sibling == 0 )
03290     {
03291         if( this->node->parent == 0 ) return *this;
03292 
03293         this->node = this->node->parent;
03294 
03295         if( top_node != 0 && this->node == top_node ) return *this;
03296     }
03297 
03298     this->node = this->node->prev_sibling;
03299 
03300     while( this->node->last_child )
03301         this->node = this->node->last_child;
03302 
03303     return *this;
03304 }
03305 
03306 template <class T, class tree_node_allocator>
03307 typename tree<T, tree_node_allocator>::leaf_iterator tree<T, tree_node_allocator>::leaf_iterator::operator++( int )
03308 {
03309     leaf_iterator copy = *this;
03310     ++( *this );
03311     return copy;
03312 }
03313 
03314 template <class T, class tree_node_allocator>
03315 typename tree<T, tree_node_allocator>::leaf_iterator tree<T, tree_node_allocator>::leaf_iterator::operator--( int )
03316 {
03317     leaf_iterator copy = *this;
03318     --( *this );
03319     return copy;
03320 }
03321 
03322 template <class T, class tree_node_allocator>
03323 typename tree<T, tree_node_allocator>::leaf_iterator& tree<T, tree_node_allocator>::leaf_iterator::operator+=( unsigned int num )
03324 {
03325     while( num > 0 )
03326     {
03327         ++( *this );
03328         --num;
03329     }
03330 
03331     return (*this );
03332 }
03333 
03334 template <class T, class tree_node_allocator>
03335 typename tree<T, tree_node_allocator>::leaf_iterator& tree<T, tree_node_allocator>::leaf_iterator::operator-=( unsigned int num )
03336 {
03337     while( num > 0 )
03338     {
03339         --( *this );
03340         --num;
03341     }
03342 
03343     return (*this );
03344 }
03345 
03346 }
03347 
03348 #endif
03349 
03350 // Local variables:
03351 // default-tab-width: 3
03352 // End: