File indexing completed on 2024-04-21 14:47:36
0001 /* 0002 0003 $Id: tree.hh,v 1.147 2007/10/19 11:24:24 peekas Exp $ 0004 0005 STL-like templated tree class. 0006 Copyright (C) 2001-2006 Kasper Peeters <kasper.peeters@aei.mpg.de>. 0007 0008 */ 0009 0010 /** \mainpage tree.hh 0011 \author Kasper Peeters 0012 \version 2.4 0013 \date 18-Oct-2007 0014 \see http://www.aei.mpg.de/~peekas/tree/ 0015 \see http://www.aei.mpg.de/~peekas/tree/ChangeLog 0016 0017 The tree.hh library for C++ provides an STL-like container class 0018 for n-ary trees, templated over the data stored at the 0019 nodes. Various types of iterators are provided (post-order, 0020 pre-order, and others). Where possible the access methods are 0021 compatible with the STL or alternative algorithms are 0022 available. 0023 */ 0024 0025 0026 /* 0027 The tree.hh code is free software; you can redistribute it and/or modify 0028 it under the terms of the GNU General Public License as published by 0029 the Free Software Foundation; version 2 or 3. 0030 0031 This program is distributed in the hope that it will be useful, 0032 but WITHOUT ANY WARRANTY; without even the implied warranty of 0033 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0034 GNU General Public License for more details. 0035 0036 You should have received a copy of the GNU General Public License 0037 along with this program; if not, write to the Free Software 0038 Foundation, Inc., 51 Franklin Street, Fifth Floor, 0039 Boston, MA 02110-1301 USA 0040 */ 0041 0042 /** \todo 0043 - New-style move members are not completely finished yet. 0044 - It would be good to have an iterator which can iterate over all 0045 nodes below a given node. 0046 - Fixed depth iterators do not iterate over the entire range if there 0047 are 'holes' in the tree. 0048 - If a range uses const iter_base& as end iterator, things will 0049 inevitably go wrong, because upcast from iter_base to a non-sibling_iter 0050 is incorrect. This upcast should be removed (and then all illegal uses 0051 as previously in 'equal' will be flagged by the compiler). This requires 0052 new copy constructors though. 0053 - There's a bug in replace(sibling_iterator, ...) when the ranges 0054 sit next to each other. Turned up in append_child(iter,iter) 0055 but has been avoided now. 0056 - "std::operator<" does not work correctly on our iterators, and for some 0057 reason a globally defined template operator< did not get picked up. 0058 Using a comparison class now, but this should be investigated. 0059 */ 0060 0061 #ifndef tree_hh_ 0062 #define tree_hh_ 0063 0064 #include <cassert> 0065 #include <memory> 0066 #include <stdexcept> 0067 #include <iterator> 0068 #include <set> 0069 #include <queue> 0070 #include <iostream> 0071 0072 // HP-style construct/destroy have gone from the standard, 0073 // so here is a copy. 0074 0075 namespace kp { 0076 0077 template <class T1, class T2> 0078 void constructor(T1* p, T2& val) 0079 { 0080 new ((void *) p) T1(val); 0081 } 0082 0083 template <class T1> 0084 void constructor(T1* p) 0085 { 0086 new ((void *) p) T1; 0087 } 0088 0089 template <class T1> 0090 void destructor(T1* p) 0091 { 0092 p->~T1(); 0093 } 0094 0095 }; 0096 0097 /// A node in the tree, combining links to other nodes as well as the actual data. 0098 template<class T> 0099 class tree_node_ { // size: 5*4=20 bytes (on 32 bit arch), can be reduced by 8. 0100 public: 0101 tree_node_<T> *parent; 0102 tree_node_<T> *first_child, *last_child; 0103 tree_node_<T> *prev_sibling, *next_sibling; 0104 T data; 0105 }; // __attribute__((packed)); 0106 0107 template <class T, class tree_node_allocator = std::allocator<tree_node_<T> > > 0108 class tree { 0109 protected: 0110 typedef tree_node_<T> tree_node; 0111 public: 0112 /// Value of the data stored at a node. 0113 typedef T value_type; 0114 0115 class iterator_base; 0116 class pre_order_iterator; 0117 class post_order_iterator; 0118 class sibling_iterator; 0119 class leaf_iterator; 0120 0121 tree(); 0122 tree(const T&); 0123 tree(const iterator_base&); 0124 tree(const tree<T, tree_node_allocator>&); 0125 ~tree(); 0126 void operator=(const tree<T, tree_node_allocator>&); 0127 0128 /// Base class for iterators, only pointers stored, no traversal logic. 0129 #ifdef __SGI_STL_PORT 0130 class iterator_base : public stlport::bidirectional_iterator<T, ptrdiff_t> { 0131 #else 0132 class iterator_base { 0133 #endif 0134 public: 0135 typedef T value_type; 0136 typedef T* pointer; 0137 typedef T& reference; 0138 typedef size_t size_type; 0139 typedef ptrdiff_t difference_type; 0140 typedef std::bidirectional_iterator_tag iterator_category; 0141 0142 iterator_base(); 0143 iterator_base(tree_node *); 0144 0145 T& operator*() const; 0146 T* operator->() const; 0147 0148 /// When called, the next increment/decrement skips children of this node. 0149 void skip_children(); 0150 /// Number of children of the node pointed to by the iterator. 0151 unsigned int number_of_children() const; 0152 0153 sibling_iterator begin() const; 0154 sibling_iterator end() const; 0155 0156 tree_node *node; 0157 protected: 0158 bool skip_current_children_; 0159 }; 0160 0161 /// Depth-first iterator, first accessing the node, then its children. 0162 class pre_order_iterator : public iterator_base { 0163 public: 0164 pre_order_iterator(); 0165 pre_order_iterator(tree_node *); 0166 pre_order_iterator(const iterator_base&); 0167 pre_order_iterator(const sibling_iterator&); 0168 0169 bool operator==(const pre_order_iterator&) const; 0170 bool operator!=(const pre_order_iterator&) const; 0171 pre_order_iterator& operator++(); 0172 pre_order_iterator& operator--(); 0173 pre_order_iterator operator++(int); 0174 pre_order_iterator operator--(int); 0175 pre_order_iterator& operator+=(unsigned int); 0176 pre_order_iterator& operator-=(unsigned int); 0177 }; 0178 0179 /// Depth-first iterator, first accessing the children, then the node itself. 0180 class post_order_iterator : public iterator_base { 0181 public: 0182 post_order_iterator(); 0183 post_order_iterator(tree_node *); 0184 post_order_iterator(const iterator_base&); 0185 post_order_iterator(const sibling_iterator&); 0186 0187 bool operator==(const post_order_iterator&) const; 0188 bool operator!=(const post_order_iterator&) const; 0189 post_order_iterator& operator++(); 0190 post_order_iterator& operator--(); 0191 post_order_iterator operator++(int); 0192 post_order_iterator operator--(int); 0193 post_order_iterator& operator+=(unsigned int); 0194 post_order_iterator& operator-=(unsigned int); 0195 0196 /// Set iterator to the first child as deep as possible down the tree. 0197 void descend_all(); 0198 }; 0199 0200 /// Breadth-first iterator, using a queue 0201 class breadth_first_queued_iterator : public iterator_base { 0202 public: 0203 breadth_first_queued_iterator(); 0204 breadth_first_queued_iterator(tree_node *); 0205 breadth_first_queued_iterator(const iterator_base&); 0206 0207 bool operator==(const breadth_first_queued_iterator&) const; 0208 bool operator!=(const breadth_first_queued_iterator&) const; 0209 breadth_first_queued_iterator& operator++(); 0210 breadth_first_queued_iterator operator++(int); 0211 breadth_first_queued_iterator& operator+=(unsigned int); 0212 0213 private: 0214 std::queue<tree_node *> traversal_queue; 0215 }; 0216 0217 /// The default iterator types throughout the tree class. 0218 typedef pre_order_iterator iterator; 0219 typedef breadth_first_queued_iterator breadth_first_iterator; 0220 0221 /// Iterator which traverses only the nodes at a given depth from the root. 0222 class fixed_depth_iterator : public iterator_base { 0223 public: 0224 fixed_depth_iterator(); 0225 fixed_depth_iterator(tree_node *); 0226 fixed_depth_iterator(const iterator_base&); 0227 fixed_depth_iterator(const sibling_iterator&); 0228 fixed_depth_iterator(const fixed_depth_iterator&); 0229 0230 bool operator==(const fixed_depth_iterator&) const; 0231 bool operator!=(const fixed_depth_iterator&) const; 0232 fixed_depth_iterator& operator++(); 0233 fixed_depth_iterator& operator--(); 0234 fixed_depth_iterator operator++(int); 0235 fixed_depth_iterator operator--(int); 0236 fixed_depth_iterator& operator+=(unsigned int); 0237 fixed_depth_iterator& operator-=(unsigned int); 0238 0239 tree_node *first_parent_; 0240 private: 0241 void set_first_parent_(); 0242 void find_leftmost_parent_(); 0243 }; 0244 0245 /// Iterator which traverses only the nodes which are siblings of each other. 0246 class sibling_iterator : public iterator_base { 0247 public: 0248 sibling_iterator(); 0249 sibling_iterator(tree_node *); 0250 sibling_iterator(const sibling_iterator&); 0251 sibling_iterator(const iterator_base&); 0252 0253 bool operator==(const sibling_iterator&) const; 0254 bool operator!=(const sibling_iterator&) const; 0255 sibling_iterator& operator++(); 0256 sibling_iterator& operator--(); 0257 sibling_iterator operator++(int); 0258 sibling_iterator operator--(int); 0259 sibling_iterator& operator+=(unsigned int); 0260 sibling_iterator& operator-=(unsigned int); 0261 0262 tree_node *range_first() const; 0263 tree_node *range_last() const; 0264 tree_node *parent_; 0265 private: 0266 void set_parent_(); 0267 }; 0268 0269 /// Iterator which traverses only the leaves. 0270 class leaf_iterator : public iterator_base { 0271 public: 0272 leaf_iterator(); 0273 leaf_iterator(tree_node *); 0274 leaf_iterator(const sibling_iterator&); 0275 leaf_iterator(const iterator_base&); 0276 0277 bool operator==(const leaf_iterator&) const; 0278 bool operator!=(const leaf_iterator&) const; 0279 leaf_iterator& operator++(); 0280 leaf_iterator& operator--(); 0281 leaf_iterator operator++(int); 0282 leaf_iterator operator--(int); 0283 leaf_iterator& operator+=(unsigned int); 0284 leaf_iterator& operator-=(unsigned int); 0285 }; 0286 0287 /// Return iterator to the beginning of the tree. 0288 inline pre_order_iterator begin() const; 0289 /// Return iterator to the end of the tree. 0290 inline pre_order_iterator end() const; 0291 /// Return post-order iterator to the beginning of the tree. 0292 post_order_iterator begin_post() const; 0293 /// Return post-order iterator to the end of the tree. 0294 post_order_iterator end_post() const; 0295 /// Return fixed-depth iterator to the first node at a given depth from the given iterator. 0296 fixed_depth_iterator begin_fixed(const iterator_base&, unsigned int) const; 0297 /// Return fixed-depth iterator to end of the nodes at given depth from the given iterator. 0298 fixed_depth_iterator end_fixed(const iterator_base&, unsigned int) const; 0299 /// Return breadth-first iterator to the first node at a given depth. 0300 breadth_first_queued_iterator begin_breadth_first() const; 0301 /// Return breadth-first iterator to end of the nodes at given depth. 0302 breadth_first_queued_iterator end_breadth_first() const; 0303 /// Return sibling iterator to the first child of given node. 0304 sibling_iterator begin(const iterator_base&) const; 0305 /// Return sibling iterator to the end of the children of a given node. 0306 sibling_iterator end(const iterator_base&) const; 0307 /// Return leaf iterator to the first leaf of the tree. 0308 leaf_iterator begin_leaf() const; 0309 /// Return leaf iterator to the last leaf of the tree. 0310 leaf_iterator end_leaf() const; 0311 0312 /// Return iterator to the parent of a node. 0313 template<typename iter> static iter parent(iter); 0314 /// Return iterator to the previous sibling of a node. 0315 template<typename iter> iter previous_sibling(iter) const; 0316 /// Return iterator to the next sibling of a node. 0317 template<typename iter> iter next_sibling(iter) const; 0318 /// Return iterator to the next node at a given depth. 0319 template<typename iter> iter next_at_same_depth(iter) const; 0320 0321 /// Erase all nodes of the tree. 0322 void clear(); 0323 /// Erase element at position pointed to by iterator, return incremented iterator. 0324 template<typename iter> iter erase(iter); 0325 /// Erase all children of the node pointed to by iterator. 0326 void erase_children(const iterator_base&); 0327 0328 /// Insert empty node as last/first child of node pointed to by position. 0329 template<typename iter> iter append_child(iter position); 0330 template<typename iter> iter prepend_child(iter position); 0331 /// Insert node as last/first child of node pointed to by position. 0332 template<typename iter> iter append_child(iter position, const T& x); 0333 template<typename iter> iter prepend_child(iter position, const T& x); 0334 /// Append the node (plus its children) at other_position as last/first child of position. 0335 template<typename iter> iter append_child(iter position, iter other_position); 0336 template<typename iter> iter prepend_child(iter position, iter other_position); 0337 /// Append the nodes in the from-to range (plus their children) as last/first children of position. 0338 template<typename iter> iter append_children(iter position, sibling_iterator from, sibling_iterator to); 0339 template<typename iter> iter prepend_children(iter position, sibling_iterator from, sibling_iterator to); 0340 0341 /// Short-hand to insert topmost node in otherwise empty tree. 0342 pre_order_iterator set_head(const T& x); 0343 /// Insert node as previous sibling of node pointed to by position. 0344 template<typename iter> iter insert(iter position, const T& x); 0345 /// Specialisation of previous member. 0346 sibling_iterator insert(sibling_iterator position, const T& x); 0347 /// Insert node (with children) pointed to by subtree as previous sibling of node pointed to by position. 0348 template<typename iter> iter insert_subtree(iter position, const iterator_base& subtree); 0349 /// Insert node as next sibling of node pointed to by position. 0350 template<typename iter> iter insert_after(iter position, const T& x); 0351 /// Insert node (with children) pointed to by subtree as next sibling of node pointed to by position. 0352 template<typename iter> iter insert_subtree_after(iter position, const iterator_base& subtree); 0353 0354 /// Replace node at 'position' with other node (keeping same children); 'position' becomes invalid. 0355 template<typename iter> iter replace(iter position, const T& x); 0356 /// Replace node at 'position' with subtree starting at 'from' (do not erase subtree at 'from'); see above. 0357 template<typename iter> iter replace(iter position, const iterator_base& from); 0358 /// Replace string of siblings (plus their children) with copy of a new string (with children); see above 0359 sibling_iterator replace(sibling_iterator orig_begin, sibling_iterator orig_end, 0360 sibling_iterator new_begin, sibling_iterator new_end); 0361 0362 /// Move all children of node at 'position' to be siblings, returns position. 0363 template<typename iter> iter flatten(iter position); 0364 /// Move nodes in range to be children of 'position'. 0365 template<typename iter> iter reparent(iter position, sibling_iterator begin, sibling_iterator end); 0366 /// Move all child nodes of 'from' to be children of 'position'. 0367 template<typename iter> iter reparent(iter position, iter from); 0368 0369 /// Replace node with a new node, making the old node a child of the new node. 0370 template<typename iter> iter wrap(iter position, const T& x); 0371 0372 /// Move 'source' node (plus its children) to become the next sibling of 'target'. 0373 template<typename iter> iter move_after(iter target, iter source); 0374 /// Move 'source' node (plus its children) to become the previous sibling of 'target'. 0375 template<typename iter> iter move_before(iter target, iter source); 0376 sibling_iterator move_before(sibling_iterator target, sibling_iterator source); 0377 /// Move 'source' node (plus its children) to become the node at 'target' (erasing the node at 'target'). 0378 template<typename iter> iter move_ontop(iter target, iter source); 0379 0380 /// Merge with other tree, creating new branches and leaves only if they are not already present. 0381 void merge(sibling_iterator, sibling_iterator, sibling_iterator, sibling_iterator, 0382 bool duplicate_leaves=false); 0383 /// Sort (std::sort only moves values of nodes, this one moves children as well). 0384 void sort(sibling_iterator from, sibling_iterator to, bool deep=false); 0385 template<class StrictWeakOrdering> 0386 void sort(sibling_iterator from, sibling_iterator to, StrictWeakOrdering comp, bool deep=false); 0387 /// Compare two ranges of nodes (compares nodes as well as tree structure). 0388 template<typename iter> 0389 bool equal(const iter& one, const iter& two, const iter& three) const; 0390 template<typename iter, class BinaryPredicate> 0391 bool equal(const iter& one, const iter& two, const iter& three, BinaryPredicate) const; 0392 template<typename iter> 0393 bool equal_subtree(const iter& one, const iter& two) const; 0394 template<typename iter, class BinaryPredicate> 0395 bool equal_subtree(const iter& one, const iter& two, BinaryPredicate) const; 0396 /// Extract a new tree formed by the range of siblings plus all their children. 0397 tree subtree(sibling_iterator from, sibling_iterator to) const; 0398 void subtree(tree&, sibling_iterator from, sibling_iterator to) const; 0399 /// Exchange the node (plus subtree) with its sibling node (do nothing if no sibling present). 0400 void swap(sibling_iterator it); 0401 /// Exchange two nodes (plus subtrees) 0402 void swap(iterator, iterator); 0403 0404 /// Count the total number of nodes. 0405 int size() const; 0406 /// Count the total number of nodes below the indicated node (plus one). 0407 int size(const iterator_base&) const; 0408 /// Check if tree is empty. 0409 bool empty() const; 0410 /// Compute the depth to the root. 0411 int depth(const iterator_base&) const; 0412 /// Determine the maximal depth of the tree. 0413 int max_depth() const; 0414 /// Determine the maximal depth of the tree below a given one. 0415 int max_depth(const iterator_base&) const; 0416 /// Count the number of children of node at position. 0417 static unsigned int number_of_children(const iterator_base&); 0418 /// Count the number of 'next' siblings of node at iterator. 0419 unsigned int number_of_siblings(const iterator_base&) const; 0420 /// Determine whether node at position is in the subtrees with root in the range. 0421 bool is_in_subtree(const iterator_base& position, const iterator_base& begin, 0422 const iterator_base& end) const; 0423 /// Determine whether the iterator is an 'end' iterator and thus not actually pointing to a node. 0424 bool is_valid(const iterator_base&) const; 0425 0426 /// Determine the index of a node in the range of siblings to which it belongs. 0427 unsigned int index(sibling_iterator it) const; 0428 /// Inverse of 'index': return the n-th child of the node at position. 0429 sibling_iterator child(const iterator_base& position, unsigned int) const; 0430 0431 /// Comparator class for iterators (compares pointer values; why doesn't this work automatically?) 0432 class iterator_base_less { 0433 public: 0434 bool operator()(const typename tree<T, tree_node_allocator>::iterator_base& one, 0435 const typename tree<T, tree_node_allocator>::iterator_base& two) const 0436 { 0437 return one.node < two.node; 0438 } 0439 }; 0440 tree_node *head, *feet; // head/feet are always dummy; if an iterator points to them it is invalid 0441 private: 0442 tree_node_allocator alloc_; 0443 void head_initialise_(); 0444 void copy_(const tree<T, tree_node_allocator>& other); 0445 0446 /// Comparator class for two nodes of a tree (used for sorting and searching). 0447 template<class StrictWeakOrdering> 0448 class compare_nodes { 0449 public: 0450 compare_nodes(StrictWeakOrdering comp) : comp_(comp) {}; 0451 0452 bool operator()(const tree_node *a, const tree_node *b) 0453 { 0454 static StrictWeakOrdering comp; 0455 return comp(a->data, b->data); 0456 } 0457 private: 0458 StrictWeakOrdering comp_; 0459 }; 0460 }; 0461 0462 //template <class T, class tree_node_allocator> 0463 //class iterator_base_less { 0464 // public: 0465 // bool operator()(const typename tree<T, tree_node_allocator>::iterator_base& one, 0466 // const typename tree<T, tree_node_allocator>::iterator_base& two) const 0467 // { 0468 // txtout << "operatorclass<" << one.node < two.node << std::endl; 0469 // return one.node < two.node; 0470 // } 0471 //}; 0472 0473 // template <class T, class tree_node_allocator> 0474 // bool operator<(const typename tree<T, tree_node_allocator>::iterator& one, 0475 // const typename tree<T, tree_node_allocator>::iterator& two) 0476 // { 0477 // txtout << "operator< " << one.node < two.node << std::endl; 0478 // if(one.node < two.node) return true; 0479 // return false; 0480 // } 0481 // 0482 // template <class T, class tree_node_allocator> 0483 // bool operator==(const typename tree<T, tree_node_allocator>::iterator& one, 0484 // const typename tree<T, tree_node_allocator>::iterator& two) 0485 // { 0486 // txtout << "operator== " << one.node == two.node << std::endl; 0487 // if(one.node == two.node) return true; 0488 // return false; 0489 // } 0490 // 0491 // template <class T, class tree_node_allocator> 0492 // bool operator>(const typename tree<T, tree_node_allocator>::iterator_base& one, 0493 // const typename tree<T, tree_node_allocator>::iterator_base& two) 0494 // { 0495 // txtout << "operator> " << one.node < two.node << std::endl; 0496 // if(one.node > two.node) return true; 0497 // return false; 0498 // } 0499 0500 0501 0502 // Tree 0503 0504 template <class T, class tree_node_allocator> 0505 tree<T, tree_node_allocator>::tree() 0506 { 0507 head_initialise_(); 0508 } 0509 0510 template <class T, class tree_node_allocator> 0511 tree<T, tree_node_allocator>::tree(const T& x) 0512 { 0513 head_initialise_(); 0514 set_head(x); 0515 } 0516 0517 template <class T, class tree_node_allocator> 0518 tree<T, tree_node_allocator>::tree(const iterator_base& other) 0519 { 0520 head_initialise_(); 0521 set_head((*other)); 0522 replace(begin(), other); 0523 } 0524 0525 template <class T, class tree_node_allocator> 0526 tree<T, tree_node_allocator>::~tree() 0527 { 0528 clear(); 0529 alloc_.deallocate(head,1); 0530 alloc_.deallocate(feet,1); 0531 } 0532 0533 template <class T, class tree_node_allocator> 0534 void tree<T, tree_node_allocator>::head_initialise_() 0535 { 0536 head = alloc_.allocate(1,nullptr); // MSVC does not have default second argument 0537 feet = alloc_.allocate(1,nullptr); 0538 0539 head->parent=nullptr; 0540 head->first_child=nullptr; 0541 head->last_child=nullptr; 0542 head->prev_sibling=nullptr; //head; 0543 head->next_sibling=feet; //head; 0544 0545 feet->parent=nullptr; 0546 feet->first_child=nullptr; 0547 feet->last_child=nullptr; 0548 feet->prev_sibling=head; 0549 feet->next_sibling=nullptr; 0550 } 0551 0552 template <class T, class tree_node_allocator> 0553 void tree<T, tree_node_allocator>::operator=(const tree<T, tree_node_allocator>& other) 0554 { 0555 copy_(other); 0556 } 0557 0558 template <class T, class tree_node_allocator> 0559 tree<T, tree_node_allocator>::tree(const tree<T, tree_node_allocator>& other) 0560 { 0561 head_initialise_(); 0562 copy_(other); 0563 } 0564 0565 template <class T, class tree_node_allocator> 0566 void tree<T, tree_node_allocator>::copy_(const tree<T, tree_node_allocator>& other) 0567 { 0568 clear(); 0569 pre_order_iterator it=other.begin(), to=begin(); 0570 while(it!=other.end()) { 0571 to=insert(to, (*it)); 0572 it.skip_children(); 0573 ++it; 0574 } 0575 to=begin(); 0576 it=other.begin(); 0577 while(it!=other.end()) { 0578 to=replace(to, it); 0579 to.skip_children(); 0580 it.skip_children(); 0581 ++to; 0582 ++it; 0583 } 0584 } 0585 0586 template <class T, class tree_node_allocator> 0587 void tree<T, tree_node_allocator>::clear() 0588 { 0589 if(head) 0590 while(head->next_sibling!=feet) 0591 erase(pre_order_iterator(head->next_sibling)); 0592 } 0593 0594 template<class T, class tree_node_allocator> 0595 void tree<T, tree_node_allocator>::erase_children(const iterator_base& it) 0596 { 0597 // std::cout << "erase_children " << it.node << std::endl; 0598 if(it.node==nullptr) return; 0599 0600 tree_node *cur=it.node->first_child; 0601 tree_node *prev=nullptr; 0602 0603 while(cur!=nullptr) { 0604 prev=cur; 0605 cur=cur->next_sibling; 0606 erase_children(pre_order_iterator(prev)); 0607 kp::destructor(&prev->data); 0608 alloc_.deallocate(prev,1); 0609 } 0610 it.node->first_child=nullptr; 0611 it.node->last_child=nullptr; 0612 // std::cout << "exit" << std::endl; 0613 } 0614 0615 template<class T, class tree_node_allocator> 0616 template<class iter> 0617 iter tree<T, tree_node_allocator>::erase(iter it) 0618 { 0619 tree_node *cur=it.node; 0620 assert(cur!=head); 0621 iter ret=it; 0622 ret.skip_children(); 0623 ++ret; 0624 erase_children(it); 0625 if(cur->prev_sibling==nullptr) { 0626 cur->parent->first_child=cur->next_sibling; 0627 } 0628 else { 0629 cur->prev_sibling->next_sibling=cur->next_sibling; 0630 } 0631 if(cur->next_sibling==nullptr) { 0632 cur->parent->last_child=cur->prev_sibling; 0633 } 0634 else { 0635 cur->next_sibling->prev_sibling=cur->prev_sibling; 0636 } 0637 0638 kp::destructor(&cur->data); 0639 alloc_.deallocate(cur,1); 0640 return ret; 0641 } 0642 0643 template <class T, class tree_node_allocator> 0644 typename tree<T, tree_node_allocator>::pre_order_iterator tree<T, tree_node_allocator>::begin() const 0645 { 0646 return pre_order_iterator(head->next_sibling); 0647 } 0648 0649 template <class T, class tree_node_allocator> 0650 typename tree<T, tree_node_allocator>::pre_order_iterator tree<T, tree_node_allocator>::end() const 0651 { 0652 return pre_order_iterator(feet); 0653 } 0654 0655 template <class T, class tree_node_allocator> 0656 typename tree<T, tree_node_allocator>::breadth_first_queued_iterator tree<T, tree_node_allocator>::begin_breadth_first() const 0657 { 0658 return breadth_first_queued_iterator(head->next_sibling); 0659 } 0660 0661 template <class T, class tree_node_allocator> 0662 typename tree<T, tree_node_allocator>::breadth_first_queued_iterator tree<T, tree_node_allocator>::end_breadth_first() const 0663 { 0664 return breadth_first_queued_iterator(); 0665 } 0666 0667 template <class T, class tree_node_allocator> 0668 typename tree<T, tree_node_allocator>::post_order_iterator tree<T, tree_node_allocator>::begin_post() const 0669 { 0670 tree_node *tmp=head->next_sibling; 0671 if(tmp!=feet) { 0672 while(tmp->first_child) 0673 tmp=tmp->first_child; 0674 } 0675 return post_order_iterator(tmp); 0676 } 0677 0678 template <class T, class tree_node_allocator> 0679 typename tree<T, tree_node_allocator>::post_order_iterator tree<T, tree_node_allocator>::end_post() const 0680 { 0681 return post_order_iterator(feet); 0682 } 0683 0684 template <class T, class tree_node_allocator> 0685 typename tree<T, tree_node_allocator>::fixed_depth_iterator tree<T, tree_node_allocator>::begin_fixed(const iterator_base& pos, unsigned int dp) const 0686 { 0687 tree_node *tmp=pos.node; 0688 unsigned int curdepth=0; 0689 while(curdepth<dp) { // go down one level 0690 while(tmp->first_child==0) { 0691 if(tmp->next_sibling==0) { 0692 // try to walk up and then right again 0693 do { 0694 tmp=tmp->parent; 0695 if(tmp==0) 0696 throw std::range_error("tree: begin_fixed out of range"); 0697 --curdepth; 0698 } while(tmp->next_sibling==0); 0699 } 0700 tmp=tmp->next_sibling; 0701 } 0702 tmp=tmp->first_child; 0703 ++curdepth; 0704 } 0705 return tmp; 0706 } 0707 0708 template <class T, class tree_node_allocator> 0709 typename tree<T, tree_node_allocator>::fixed_depth_iterator tree<T, tree_node_allocator>::end_fixed(const iterator_base& pos, unsigned int dp) const 0710 { 0711 assert(1==0); // FIXME: not correct yet: use is_valid() as a temporary workaround 0712 tree_node *tmp=pos.node; 0713 unsigned int curdepth=1; 0714 while(curdepth<dp) { // go down one level 0715 while(tmp->first_child==0) { 0716 tmp=tmp->next_sibling; 0717 if(tmp==0) 0718 throw std::range_error("tree: end_fixed out of range"); 0719 } 0720 tmp=tmp->first_child; 0721 ++curdepth; 0722 } 0723 return tmp; 0724 } 0725 0726 template <class T, class tree_node_allocator> 0727 typename tree<T, tree_node_allocator>::sibling_iterator tree<T, tree_node_allocator>::begin(const iterator_base& pos) const 0728 { 0729 assert(pos.node!=nullptr); 0730 if(pos.node->first_child==nullptr) { 0731 return end(pos); 0732 } 0733 return pos.node->first_child; 0734 } 0735 0736 template <class T, class tree_node_allocator> 0737 typename tree<T, tree_node_allocator>::sibling_iterator tree<T, tree_node_allocator>::end(const iterator_base& pos) const 0738 { 0739 sibling_iterator ret(nullptr); 0740 ret.parent_=pos.node; 0741 return ret; 0742 } 0743 0744 template <class T, class tree_node_allocator> 0745 typename tree<T, tree_node_allocator>::leaf_iterator tree<T, tree_node_allocator>::begin_leaf() const 0746 { 0747 tree_node *tmp=head->next_sibling; 0748 if(tmp!=feet) { 0749 while(tmp->first_child) 0750 tmp=tmp->first_child; 0751 } 0752 return leaf_iterator(tmp); 0753 } 0754 0755 template <class T, class tree_node_allocator> 0756 typename tree<T, tree_node_allocator>::leaf_iterator tree<T, tree_node_allocator>::end_leaf() const 0757 { 0758 return leaf_iterator(feet); 0759 } 0760 0761 template <class T, class tree_node_allocator> 0762 template <typename iter> 0763 iter tree<T, tree_node_allocator>::parent(iter position) 0764 { 0765 assert(position.node!=0); 0766 return iter(position.node->parent); 0767 } 0768 0769 template <class T, class tree_node_allocator> 0770 template <typename iter> 0771 iter tree<T, tree_node_allocator>::previous_sibling(iter position) const 0772 { 0773 assert(position.node!=0); 0774 iter ret(position); 0775 ret.node=position.node->prev_sibling; 0776 return ret; 0777 } 0778 0779 template <class T, class tree_node_allocator> 0780 template <typename iter> 0781 iter tree<T, tree_node_allocator>::next_sibling(iter position) const 0782 { 0783 assert(position.node!=0); 0784 iter ret(position); 0785 ret.node=position.node->next_sibling; 0786 return ret; 0787 } 0788 0789 template <class T, class tree_node_allocator> 0790 template <typename iter> 0791 iter tree<T, tree_node_allocator>::next_at_same_depth(iter position) const 0792 { 0793 assert(position.node!=0); 0794 iter ret(position); 0795 0796 if(position.node->next_sibling) { 0797 ret.node=position.node->next_sibling; 0798 } 0799 else { 0800 int relative_depth=0; 0801 upper: 0802 do { 0803 ret.node=ret.node->parent; 0804 if(ret.node==0) return ret; 0805 --relative_depth; 0806 } while(ret.node->next_sibling==0); 0807 lower: 0808 ret.node=ret.node->next_sibling; 0809 while(ret.node->first_child==0) { 0810 if(ret.node->next_sibling==0) 0811 goto upper; 0812 ret.node=ret.node->next_sibling; 0813 if(ret.node==0) return ret; 0814 } 0815 while(relative_depth<0 && ret.node->first_child!=0) { 0816 ret.node=ret.node->first_child; 0817 ++relative_depth; 0818 } 0819 if(relative_depth<0) { 0820 if(ret.node->next_sibling==0) goto upper; 0821 else goto lower; 0822 } 0823 } 0824 return ret; 0825 } 0826 0827 template <class T, class tree_node_allocator> 0828 template <typename iter> 0829 iter tree<T, tree_node_allocator>::append_child(iter position) 0830 { 0831 assert(position.node!=head); 0832 assert(position.node); 0833 0834 tree_node *tmp=alloc_.allocate(1,0); 0835 kp::constructor(&tmp->data); 0836 tmp->first_child=0; 0837 tmp->last_child=0; 0838 0839 tmp->parent=position.node; 0840 if(position.node->last_child!=0) { 0841 position.node->last_child->next_sibling=tmp; 0842 } 0843 else { 0844 position.node->first_child=tmp; 0845 } 0846 tmp->prev_sibling=position.node->last_child; 0847 position.node->last_child=tmp; 0848 tmp->next_sibling=0; 0849 return tmp; 0850 } 0851 0852 template <class T, class tree_node_allocator> 0853 template <typename iter> 0854 iter tree<T, tree_node_allocator>::prepend_child(iter position) 0855 { 0856 assert(position.node!=head); 0857 assert(position.node); 0858 0859 tree_node *tmp=alloc_.allocate(1,0); 0860 kp::constructor(&tmp->data); 0861 tmp->first_child=0; 0862 tmp->last_child=0; 0863 0864 tmp->parent=position.node; 0865 if(position.node->first_child!=0) { 0866 position.node->first_child->prev_sibling=tmp; 0867 } 0868 else { 0869 position.node->last_child=tmp; 0870 } 0871 tmp->next_sibling=position.node->first_child; 0872 position.node->prev_child=tmp; 0873 tmp->prev_sibling=0; 0874 return tmp; 0875 } 0876 0877 template <class T, class tree_node_allocator> 0878 template <class iter> 0879 iter tree<T, tree_node_allocator>::append_child(iter position, const T& x) 0880 { 0881 // If your program fails here you probably used 'append_child' to add the top 0882 // node to an empty tree. From version 1.45 the top element should be added 0883 // using 'insert'. See the documentation for further information, and sorry about 0884 // the API change. 0885 assert(position.node!=head); 0886 assert(position.node); 0887 0888 tree_node* tmp = alloc_.allocate(1,nullptr); 0889 kp::constructor(&tmp->data, x); 0890 tmp->first_child=nullptr; 0891 tmp->last_child=nullptr; 0892 0893 tmp->parent=position.node; 0894 if(position.node->last_child!=nullptr) { 0895 position.node->last_child->next_sibling=tmp; 0896 } 0897 else { 0898 position.node->first_child=tmp; 0899 } 0900 tmp->prev_sibling=position.node->last_child; 0901 position.node->last_child=tmp; 0902 tmp->next_sibling=nullptr; 0903 return tmp; 0904 } 0905 0906 template <class T, class tree_node_allocator> 0907 template <class iter> 0908 iter tree<T, tree_node_allocator>::prepend_child(iter position, const T& x) 0909 { 0910 assert(position.node!=head); 0911 assert(position.node); 0912 0913 tree_node* tmp = alloc_.allocate(1,0); 0914 kp::constructor(&tmp->data, x); 0915 tmp->first_child=0; 0916 tmp->last_child=0; 0917 0918 tmp->parent=position.node; 0919 if(position.node->first_child!=0) { 0920 position.node->first_child->prev_sibling=tmp; 0921 } 0922 else { 0923 position.node->last_child=tmp; 0924 } 0925 tmp->next_sibling=position.node->first_child; 0926 position.node->first_child=tmp; 0927 tmp->prev_sibling=0; 0928 return tmp; 0929 } 0930 0931 template <class T, class tree_node_allocator> 0932 template <class iter> 0933 iter tree<T, tree_node_allocator>::append_child(iter position, iter other) 0934 { 0935 assert(position.node!=head); 0936 assert(position.node); 0937 0938 sibling_iterator aargh=append_child(position, value_type()); 0939 return replace(aargh, other); 0940 } 0941 0942 template <class T, class tree_node_allocator> 0943 template <class iter> 0944 iter tree<T, tree_node_allocator>::prepend_child(iter position, iter other) 0945 { 0946 assert(position.node!=head); 0947 assert(position.node); 0948 0949 sibling_iterator aargh=prepend_child(position, value_type()); 0950 return replace(aargh, other); 0951 } 0952 0953 template <class T, class tree_node_allocator> 0954 template <class iter> 0955 iter tree<T, tree_node_allocator>::append_children(iter position, sibling_iterator from, sibling_iterator to) 0956 { 0957 assert(position.node!=head); 0958 assert(position.node); 0959 0960 iter ret=from; 0961 0962 while(from!=to) { 0963 insert_subtree(position.end(), from); 0964 ++from; 0965 } 0966 return ret; 0967 } 0968 0969 template <class T, class tree_node_allocator> 0970 template <class iter> 0971 iter tree<T, tree_node_allocator>::prepend_children(iter position, sibling_iterator from, sibling_iterator to) 0972 { 0973 assert(position.node!=head); 0974 assert(position.node); 0975 0976 iter ret=from; 0977 0978 while(from!=to) { 0979 insert_subtree(position.begin(), from); 0980 ++from; 0981 } 0982 return ret; 0983 } 0984 0985 template <class T, class tree_node_allocator> 0986 typename tree<T, tree_node_allocator>::pre_order_iterator tree<T, tree_node_allocator>::set_head(const T& x) 0987 { 0988 assert(head->next_sibling==feet); 0989 return insert(iterator(feet), x); 0990 } 0991 0992 template <class T, class tree_node_allocator> 0993 template <class iter> 0994 iter tree<T, tree_node_allocator>::insert(iter position, const T& x) 0995 { 0996 if(position.node==0) { 0997 position.node=feet; // Backward compatibility: when calling insert on a null node, 0998 // insert before the feet. 0999 } 1000 tree_node* tmp = alloc_.allocate(1,0); 1001 kp::constructor(&tmp->data, x); 1002 tmp->first_child=0; 1003 tmp->last_child=0; 1004 1005 tmp->parent=position.node->parent; 1006 tmp->next_sibling=position.node; 1007 tmp->prev_sibling=position.node->prev_sibling; 1008 position.node->prev_sibling=tmp; 1009 1010 if(tmp->prev_sibling==0) { 1011 if(tmp->parent) // when inserting nodes at the head, there is no parent 1012 tmp->parent->first_child=tmp; 1013 } 1014 else 1015 tmp->prev_sibling->next_sibling=tmp; 1016 return tmp; 1017 } 1018 1019 template <class T, class tree_node_allocator> 1020 typename tree<T, tree_node_allocator>::sibling_iterator tree<T, tree_node_allocator>::insert(sibling_iterator position, const T& x) 1021 { 1022 tree_node* tmp = alloc_.allocate(1,0); 1023 kp::constructor(&tmp->data, x); 1024 tmp->first_child=0; 1025 tmp->last_child=0; 1026 1027 tmp->next_sibling=position.node; 1028 if(position.node==0) { // iterator points to end of a subtree 1029 tmp->parent=position.parent_; 1030 tmp->prev_sibling=position.range_last(); 1031 tmp->parent->last_child=tmp; 1032 } 1033 else { 1034 tmp->parent=position.node->parent; 1035 tmp->prev_sibling=position.node->prev_sibling; 1036 position.node->prev_sibling=tmp; 1037 } 1038 1039 if(tmp->prev_sibling==0) { 1040 if(tmp->parent) // when inserting nodes at the head, there is no parent 1041 tmp->parent->first_child=tmp; 1042 } 1043 else 1044 tmp->prev_sibling->next_sibling=tmp; 1045 return tmp; 1046 } 1047 1048 template <class T, class tree_node_allocator> 1049 template <class iter> 1050 iter tree<T, tree_node_allocator>::insert_after(iter position, const T& x) 1051 { 1052 tree_node* tmp = alloc_.allocate(1,0); 1053 kp::constructor(&tmp->data, x); 1054 tmp->first_child=0; 1055 tmp->last_child=0; 1056 1057 tmp->parent=position.node->parent; 1058 tmp->prev_sibling=position.node; 1059 tmp->next_sibling=position.node->next_sibling; 1060 position.node->next_sibling=tmp; 1061 1062 if(tmp->next_sibling==0) { 1063 if(tmp->parent) // when inserting nodes at the head, there is no parent 1064 tmp->parent->last_child=tmp; 1065 } 1066 else { 1067 tmp->next_sibling->prev_sibling=tmp; 1068 } 1069 return tmp; 1070 } 1071 1072 template <class T, class tree_node_allocator> 1073 template <class iter> 1074 iter tree<T, tree_node_allocator>::insert_subtree(iter position, const iterator_base& subtree) 1075 { 1076 // insert dummy 1077 iter it=insert(position, value_type()); 1078 // replace dummy with subtree 1079 return replace(it, subtree); 1080 } 1081 1082 template <class T, class tree_node_allocator> 1083 template <class iter> 1084 iter tree<T, tree_node_allocator>::insert_subtree_after(iter position, const iterator_base& subtree) 1085 { 1086 // insert dummy 1087 iter it=insert_after(position, value_type()); 1088 // replace dummy with subtree 1089 return replace(it, subtree); 1090 } 1091 1092 // template <class T, class tree_node_allocator> 1093 // template <class iter> 1094 // iter tree<T, tree_node_allocator>::insert_subtree(sibling_iterator position, iter subtree) 1095 // { 1096 // // insert dummy 1097 // iter it(insert(position, value_type())); 1098 // // replace dummy with subtree 1099 // return replace(it, subtree); 1100 // } 1101 1102 template <class T, class tree_node_allocator> 1103 template <class iter> 1104 iter tree<T, tree_node_allocator>::replace(iter position, const T& x) 1105 { 1106 kp::destructor(&position.node->data); 1107 kp::constructor(&position.node->data, x); 1108 return position; 1109 } 1110 1111 template <class T, class tree_node_allocator> 1112 template <class iter> 1113 iter tree<T, tree_node_allocator>::replace(iter position, const iterator_base& from) 1114 { 1115 assert(position.node!=head); 1116 tree_node *current_from=from.node; 1117 tree_node *start_from=from.node; 1118 tree_node *current_to =position.node; 1119 1120 // replace the node at position with head of the replacement tree at from 1121 // std::cout << "warning!" << position.node << std::endl; 1122 erase_children(position); 1123 // std::cout << "no warning!" << std::endl; 1124 tree_node* tmp = alloc_.allocate(1,0); 1125 kp::constructor(&tmp->data, (*from)); 1126 tmp->first_child=0; 1127 tmp->last_child=0; 1128 if(current_to->prev_sibling==0) { 1129 if(current_to->parent!=0) 1130 current_to->parent->first_child=tmp; 1131 } 1132 else { 1133 current_to->prev_sibling->next_sibling=tmp; 1134 } 1135 tmp->prev_sibling=current_to->prev_sibling; 1136 if(current_to->next_sibling==0) { 1137 if(current_to->parent!=0) 1138 current_to->parent->last_child=tmp; 1139 } 1140 else { 1141 current_to->next_sibling->prev_sibling=tmp; 1142 } 1143 tmp->next_sibling=current_to->next_sibling; 1144 tmp->parent=current_to->parent; 1145 kp::destructor(¤t_to->data); 1146 alloc_.deallocate(current_to,1); 1147 current_to=tmp; 1148 1149 // only at this stage can we fix 'last' 1150 tree_node *last=from.node->next_sibling; 1151 1152 pre_order_iterator toit=tmp; 1153 // copy all children 1154 do { 1155 assert(current_from!=0); 1156 if(current_from->first_child != 0) { 1157 current_from=current_from->first_child; 1158 toit=append_child(toit, current_from->data); 1159 } 1160 else { 1161 while(current_from->next_sibling==0 && current_from!=start_from) { 1162 current_from=current_from->parent; 1163 toit=parent(toit); 1164 assert(current_from!=0); 1165 } 1166 current_from=current_from->next_sibling; 1167 if(current_from!=last) { 1168 toit=append_child(parent(toit), current_from->data); 1169 } 1170 } 1171 } while(current_from!=last); 1172 1173 return current_to; 1174 } 1175 1176 template <class T, class tree_node_allocator> 1177 typename tree<T, tree_node_allocator>::sibling_iterator tree<T, tree_node_allocator>::replace( 1178 sibling_iterator orig_begin, 1179 sibling_iterator orig_end, 1180 sibling_iterator new_begin, 1181 sibling_iterator new_end) 1182 { 1183 tree_node *orig_first=orig_begin.node; 1184 tree_node *new_first=new_begin.node; 1185 tree_node *orig_last=orig_first; 1186 while((++orig_begin)!=orig_end) 1187 orig_last=orig_last->next_sibling; 1188 tree_node *new_last=new_first; 1189 while((++new_begin)!=new_end) 1190 new_last=new_last->next_sibling; 1191 1192 // insert all siblings in new_first..new_last before orig_first 1193 bool first=true; 1194 pre_order_iterator ret; 1195 while(1==1) { 1196 pre_order_iterator tt=insert_subtree(pre_order_iterator(orig_first), pre_order_iterator(new_first)); 1197 if(first) { 1198 ret=tt; 1199 first=false; 1200 } 1201 if(new_first==new_last) 1202 break; 1203 new_first=new_first->next_sibling; 1204 } 1205 1206 // erase old range of siblings 1207 bool last=false; 1208 tree_node *next=orig_first; 1209 while(1==1) { 1210 if(next==orig_last) 1211 last=true; 1212 next=next->next_sibling; 1213 erase((pre_order_iterator)orig_first); 1214 if(last) 1215 break; 1216 orig_first=next; 1217 } 1218 return ret; 1219 } 1220 1221 template <class T, class tree_node_allocator> 1222 template <typename iter> 1223 iter tree<T, tree_node_allocator>::flatten(iter position) 1224 { 1225 if(position.node->first_child==0) 1226 return position; 1227 1228 tree_node *tmp=position.node->first_child; 1229 while(tmp) { 1230 tmp->parent=position.node->parent; 1231 tmp=tmp->next_sibling; 1232 } 1233 if(position.node->next_sibling) { 1234 position.node->last_child->next_sibling=position.node->next_sibling; 1235 position.node->next_sibling->prev_sibling=position.node->last_child; 1236 } 1237 else { 1238 position.node->parent->last_child=position.node->last_child; 1239 } 1240 position.node->next_sibling=position.node->first_child; 1241 position.node->next_sibling->prev_sibling=position.node; 1242 position.node->first_child=0; 1243 position.node->last_child=0; 1244 1245 return position; 1246 } 1247 1248 1249 template <class T, class tree_node_allocator> 1250 template <typename iter> 1251 iter tree<T, tree_node_allocator>::reparent(iter position, sibling_iterator begin, sibling_iterator end) 1252 { 1253 tree_node *first=begin.node; 1254 tree_node *last=first; 1255 1256 assert(first!=position.node); 1257 1258 if(begin==end) return begin; 1259 // determine last node 1260 while((++begin)!=end) { 1261 last=last->next_sibling; 1262 } 1263 // move subtree 1264 if(first->prev_sibling==0) { 1265 first->parent->first_child=last->next_sibling; 1266 } 1267 else { 1268 first->prev_sibling->next_sibling=last->next_sibling; 1269 } 1270 if(last->next_sibling==0) { 1271 last->parent->last_child=first->prev_sibling; 1272 } 1273 else { 1274 last->next_sibling->prev_sibling=first->prev_sibling; 1275 } 1276 if(position.node->first_child==0) { 1277 position.node->first_child=first; 1278 position.node->last_child=last; 1279 first->prev_sibling=0; 1280 } 1281 else { 1282 position.node->last_child->next_sibling=first; 1283 first->prev_sibling=position.node->last_child; 1284 position.node->last_child=last; 1285 } 1286 last->next_sibling=0; 1287 1288 tree_node *pos=first; 1289 while(1==1) { 1290 pos->parent=position.node; 1291 if(pos==last) break; 1292 pos=pos->next_sibling; 1293 } 1294 1295 return first; 1296 } 1297 1298 template <class T, class tree_node_allocator> 1299 template <typename iter> iter tree<T, tree_node_allocator>::reparent(iter position, iter from) 1300 { 1301 if(from.node->first_child==0) return position; 1302 return reparent(position, from.node->first_child, end(from)); 1303 } 1304 1305 template <class T, class tree_node_allocator> 1306 template <typename iter> iter tree<T, tree_node_allocator>::wrap(iter position, const T& x) 1307 { 1308 assert(position.node!=0); 1309 sibling_iterator fr=position, to=position; 1310 ++to; 1311 iter ret = insert(position, x); 1312 reparent(ret, fr, to); 1313 return ret; 1314 } 1315 1316 template <class T, class tree_node_allocator> 1317 template <typename iter> iter tree<T, tree_node_allocator>::move_after(iter target, iter source) 1318 { 1319 tree_node *dst=target.node; 1320 tree_node *src=source.node; 1321 assert(dst); 1322 assert(src); 1323 1324 if(dst==src) return source; 1325 if(dst->next_sibling) 1326 if(dst->next_sibling==src) // already in the right spot 1327 return source; 1328 1329 // take src out of the tree 1330 if(src->prev_sibling!=0) src->prev_sibling->next_sibling=src->next_sibling; 1331 else src->parent->first_child=src->next_sibling; 1332 if(src->next_sibling!=0) src->next_sibling->prev_sibling=src->prev_sibling; 1333 else src->parent->last_child=src->prev_sibling; 1334 1335 // connect it to the new point 1336 if(dst->next_sibling!=0) dst->next_sibling->prev_sibling=src; 1337 else dst->parent->last_child=src; 1338 src->next_sibling=dst->next_sibling; 1339 dst->next_sibling=src; 1340 src->prev_sibling=dst; 1341 src->parent=dst->parent; 1342 return src; 1343 } 1344 1345 template <class T, class tree_node_allocator> 1346 template <typename iter> iter tree<T, tree_node_allocator>::move_before(iter target, iter source) 1347 { 1348 tree_node *dst=target.node; 1349 tree_node *src=source.node; 1350 assert(dst); 1351 assert(src); 1352 1353 if(dst==src) return source; 1354 if(dst->prev_sibling) 1355 if(dst->prev_sibling==src) // already in the right spot 1356 return source; 1357 1358 // take src out of the tree 1359 if(src->prev_sibling!=0) src->prev_sibling->next_sibling=src->next_sibling; 1360 else src->parent->first_child=src->next_sibling; 1361 if(src->next_sibling!=0) src->next_sibling->prev_sibling=src->prev_sibling; 1362 else src->parent->last_child=src->prev_sibling; 1363 1364 // connect it to the new point 1365 if(dst->prev_sibling!=0) dst->prev_sibling->next_sibling=src; 1366 else dst->parent->first_child=src; 1367 src->prev_sibling=dst->prev_sibling; 1368 dst->prev_sibling=src; 1369 src->next_sibling=dst; 1370 src->parent=dst->parent; 1371 return src; 1372 } 1373 1374 // specialisation for sibling_iterators 1375 template <class T, class tree_node_allocator> 1376 typename tree<T, tree_node_allocator>::sibling_iterator tree<T, tree_node_allocator>::move_before(sibling_iterator target, 1377 sibling_iterator source) 1378 { 1379 tree_node *dst=target.node; 1380 tree_node *src=source.node; 1381 tree_node *dst_prev_sibling; 1382 if(dst==0) { // must then be an end iterator 1383 dst_prev_sibling=target.parent_->last_child; 1384 assert(dst_prev_sibling); 1385 } 1386 else dst_prev_sibling=dst->prev_sibling; 1387 assert(src); 1388 1389 if(dst==src) return source; 1390 if(dst_prev_sibling) 1391 if(dst_prev_sibling==src) // already in the right spot 1392 return source; 1393 1394 // take src out of the tree 1395 if(src->prev_sibling!=0) src->prev_sibling->next_sibling=src->next_sibling; 1396 else src->parent->first_child=src->next_sibling; 1397 if(src->next_sibling!=0) src->next_sibling->prev_sibling=src->prev_sibling; 1398 else src->parent->last_child=src->prev_sibling; 1399 1400 // connect it to the new point 1401 if(dst_prev_sibling!=0) dst_prev_sibling->next_sibling=src; 1402 else target.parent_->first_child=src; 1403 src->prev_sibling=dst_prev_sibling; 1404 if(dst) { 1405 dst->prev_sibling=src; 1406 src->parent=dst->parent; 1407 } 1408 src->next_sibling=dst; 1409 return src; 1410 } 1411 1412 template <class T, class tree_node_allocator> 1413 template <typename iter> iter tree<T, tree_node_allocator>::move_ontop(iter target, iter source) 1414 { 1415 tree_node *dst=target.node; 1416 tree_node *src=source.node; 1417 assert(dst); 1418 assert(src); 1419 1420 if(dst==src) return source; 1421 1422 // remember connection points 1423 tree_node *b_prev_sibling=dst->prev_sibling; 1424 tree_node *b_next_sibling=dst->next_sibling; 1425 tree_node *b_parent=dst->parent; 1426 1427 // remove target 1428 erase(target); 1429 1430 // take src out of the tree 1431 if(src->prev_sibling!=0) src->prev_sibling->next_sibling=src->next_sibling; 1432 else src->parent->first_child=src->next_sibling; 1433 if(src->next_sibling!=0) src->next_sibling->prev_sibling=src->prev_sibling; 1434 else src->parent->last_child=src->prev_sibling; 1435 1436 // connect it to the new point 1437 if(b_prev_sibling!=0) b_prev_sibling->next_sibling=src; 1438 else b_parent->first_child=src; 1439 if(b_next_sibling!=0) b_next_sibling->prev_sibling=src; 1440 else b_parent->last_child=src; 1441 src->prev_sibling=b_prev_sibling; 1442 src->next_sibling=b_next_sibling; 1443 src->parent=b_parent; 1444 return src; 1445 } 1446 1447 template <class T, class tree_node_allocator> 1448 void tree<T, tree_node_allocator>::merge(sibling_iterator to1, sibling_iterator to2, 1449 sibling_iterator from1, sibling_iterator from2, 1450 bool duplicate_leaves) 1451 { 1452 sibling_iterator fnd; 1453 while(from1!=from2) { 1454 if((fnd=std::find(to1, to2, (*from1))) != to2) { // element found 1455 if(from1.begin()==from1.end()) { // full depth reached 1456 if(duplicate_leaves) 1457 append_child(parent(to1), (*from1)); 1458 } 1459 else { // descend further 1460 merge(fnd.begin(), fnd.end(), from1.begin(), from1.end(), duplicate_leaves); 1461 } 1462 } 1463 else { // element missing 1464 insert_subtree(to2, from1); 1465 } 1466 ++from1; 1467 } 1468 } 1469 1470 1471 template <class T, class tree_node_allocator> 1472 void tree<T, tree_node_allocator>::sort(sibling_iterator from, sibling_iterator to, bool deep) 1473 { 1474 std::less<T> comp; 1475 sort(from, to, comp, deep); 1476 } 1477 1478 template <class T, class tree_node_allocator> 1479 template <class StrictWeakOrdering> 1480 void tree<T, tree_node_allocator>::sort(sibling_iterator from, sibling_iterator to, 1481 StrictWeakOrdering comp, bool deep) 1482 { 1483 if(from==to) return; 1484 // make list of sorted nodes 1485 // CHECK: if multiset stores equivalent nodes in the order in which they 1486 // are inserted, then this routine should be called 'stable_sort'. 1487 std::multiset<tree_node *, compare_nodes<StrictWeakOrdering> > nodes(comp); 1488 sibling_iterator it=from, it2=to; 1489 while(it != to) { 1490 nodes.insert(it.node); 1491 ++it; 1492 } 1493 // reassemble 1494 --it2; 1495 1496 // prev and next are the nodes before and after the sorted range 1497 tree_node *prev=from.node->prev_sibling; 1498 tree_node *next=it2.node->next_sibling; 1499 typename std::multiset<tree_node *, compare_nodes<StrictWeakOrdering> >::iterator nit=nodes.begin(), eit=nodes.end(); 1500 if(prev==0) { 1501 if((*nit)->parent!=0) // to catch "sorting the head" situations, when there is no parent 1502 (*nit)->parent->first_child=(*nit); 1503 } 1504 else prev->next_sibling=(*nit); 1505 1506 --eit; 1507 while(nit!=eit) { 1508 (*nit)->prev_sibling=prev; 1509 if(prev) 1510 prev->next_sibling=(*nit); 1511 prev=(*nit); 1512 ++nit; 1513 } 1514 // prev now points to the last-but-one node in the sorted range 1515 if(prev) 1516 prev->next_sibling=(*eit); 1517 1518 // eit points to the last node in the sorted range. 1519 (*eit)->next_sibling=next; 1520 (*eit)->prev_sibling=prev; // missed in the loop above 1521 if(next==0) { 1522 if((*eit)->parent!=0) // to catch "sorting the head" situations, when there is no parent 1523 (*eit)->parent->last_child=(*eit); 1524 } 1525 else next->prev_sibling=(*eit); 1526 1527 if(deep) { // sort the children of each node too 1528 sibling_iterator bcs(*nodes.begin()); 1529 sibling_iterator ecs(*eit); 1530 ++ecs; 1531 while(bcs!=ecs) { 1532 sort(begin(bcs), end(bcs), comp, deep); 1533 ++bcs; 1534 } 1535 } 1536 } 1537 1538 template <class T, class tree_node_allocator> 1539 template <typename iter> 1540 bool tree<T, tree_node_allocator>::equal(const iter& one_, const iter& two, const iter& three_) const 1541 { 1542 std::equal_to<T> comp; 1543 return equal(one_, two, three_, comp); 1544 } 1545 1546 template <class T, class tree_node_allocator> 1547 template <typename iter> 1548 bool tree<T, tree_node_allocator>::equal_subtree(const iter& one_, const iter& two_) const 1549 { 1550 std::equal_to<T> comp; 1551 return equal_subtree(one_, two_, comp); 1552 } 1553 1554 template <class T, class tree_node_allocator> 1555 template <typename iter, class BinaryPredicate> 1556 bool tree<T, tree_node_allocator>::equal(const iter& one_, const iter& two, const iter& three_, BinaryPredicate fun) const 1557 { 1558 pre_order_iterator one(one_), three(three_); 1559 1560 // if(one==two && is_valid(three) && three.number_of_children()!=0) 1561 // return false; 1562 while(one!=two && is_valid(three)) { 1563 if(!fun(*one,*three)) 1564 return false; 1565 if(one.number_of_children()!=three.number_of_children()) 1566 return false; 1567 ++one; 1568 ++three; 1569 } 1570 return true; 1571 } 1572 1573 template <class T, class tree_node_allocator> 1574 template <typename iter, class BinaryPredicate> 1575 bool tree<T, tree_node_allocator>::equal_subtree(const iter& one_, const iter& two_, BinaryPredicate fun) const 1576 { 1577 pre_order_iterator one(one_), two(two_); 1578 1579 if(!fun(*one,*two)) return false; 1580 if(number_of_children(one)!=number_of_children(two)) return false; 1581 return equal(begin(one),end(one),begin(two),fun); 1582 } 1583 1584 template <class T, class tree_node_allocator> 1585 tree<T, tree_node_allocator> tree<T, tree_node_allocator>::subtree(sibling_iterator from, sibling_iterator to) const 1586 { 1587 tree tmp; 1588 tmp.set_head(value_type()); 1589 tmp.replace(tmp.begin(), tmp.end(), from, to); 1590 return tmp; 1591 } 1592 1593 template <class T, class tree_node_allocator> 1594 void tree<T, tree_node_allocator>::subtree(tree& tmp, sibling_iterator from, sibling_iterator to) const 1595 { 1596 tmp.set_head(value_type()); 1597 tmp.replace(tmp.begin(), tmp.end(), from, to); 1598 } 1599 1600 template <class T, class tree_node_allocator> 1601 int tree<T, tree_node_allocator>::size() const 1602 { 1603 int i=0; 1604 pre_order_iterator it=begin(), eit=end(); 1605 while(it!=eit) { 1606 ++i; 1607 ++it; 1608 } 1609 return i; 1610 } 1611 1612 template <class T, class tree_node_allocator> 1613 int tree<T, tree_node_allocator>::size(const iterator_base& top) const 1614 { 1615 int i=0; 1616 pre_order_iterator it=top, eit=top; 1617 eit.skip_children(); 1618 ++eit; 1619 while(it!=eit) { 1620 ++i; 1621 ++it; 1622 } 1623 return i; 1624 } 1625 1626 template <class T, class tree_node_allocator> 1627 bool tree<T, tree_node_allocator>::empty() const 1628 { 1629 pre_order_iterator it=begin(), eit=end(); 1630 return (it==eit); 1631 } 1632 1633 template <class T, class tree_node_allocator> 1634 int tree<T, tree_node_allocator>::depth(const iterator_base& it) const 1635 { 1636 tree_node* pos=it.node; 1637 assert(pos!=nullptr); 1638 int ret=0; 1639 while(pos->parent!=nullptr) { 1640 pos=pos->parent; 1641 ++ret; 1642 } 1643 return ret; 1644 } 1645 1646 template <class T, class tree_node_allocator> 1647 int tree<T, tree_node_allocator>::max_depth() const 1648 { 1649 return max_depth(begin()); 1650 } 1651 1652 1653 template <class T, class tree_node_allocator> 1654 int tree<T, tree_node_allocator>::max_depth(const iterator_base& pos) const 1655 { 1656 tree_node *tmp=pos.node; 1657 int curdepth=0, maxdepth=0; 1658 while(true) { // try to walk the bottom of the tree 1659 while(tmp->first_child==0) { 1660 if(tmp==pos.node) return maxdepth; 1661 if(tmp->next_sibling==0) { 1662 // try to walk up and then right again 1663 do { 1664 tmp=tmp->parent; 1665 if(tmp==0) return maxdepth; 1666 --curdepth; 1667 } while(tmp->next_sibling==0); 1668 } 1669 if(tmp==pos.node) return maxdepth; 1670 tmp=tmp->next_sibling; 1671 } 1672 tmp=tmp->first_child; 1673 ++curdepth; 1674 maxdepth=std::max(curdepth, maxdepth); 1675 } 1676 } 1677 1678 template <class T, class tree_node_allocator> 1679 unsigned int tree<T, tree_node_allocator>::number_of_children(const iterator_base& it) 1680 { 1681 tree_node *pos=it.node->first_child; 1682 if(pos==0) return 0; 1683 1684 unsigned int ret=1; 1685 // while(pos!=it.node->last_child) { 1686 // ++ret; 1687 // pos=pos->next_sibling; 1688 // } 1689 while((pos=pos->next_sibling)) 1690 ++ret; 1691 return ret; 1692 } 1693 1694 template <class T, class tree_node_allocator> 1695 unsigned int tree<T, tree_node_allocator>::number_of_siblings(const iterator_base& it) const 1696 { 1697 tree_node *pos=it.node; 1698 unsigned int ret=0; 1699 // count forward 1700 while(pos->next_sibling && 1701 pos->next_sibling!=head && 1702 pos->next_sibling!=feet) { 1703 ++ret; 1704 pos=pos->next_sibling; 1705 } 1706 // count backward 1707 pos=it.node; 1708 while(pos->prev_sibling && 1709 pos->prev_sibling!=head && 1710 pos->prev_sibling!=feet) { 1711 ++ret; 1712 pos=pos->prev_sibling; 1713 } 1714 1715 return ret; 1716 } 1717 1718 template <class T, class tree_node_allocator> 1719 void tree<T, tree_node_allocator>::swap(sibling_iterator it) 1720 { 1721 tree_node *nxt=it.node->next_sibling; 1722 if(nxt) { 1723 if(it.node->prev_sibling) 1724 it.node->prev_sibling->next_sibling=nxt; 1725 else 1726 it.node->parent->first_child=nxt; 1727 nxt->prev_sibling=it.node->prev_sibling; 1728 tree_node *nxtnxt=nxt->next_sibling; 1729 if(nxtnxt) 1730 nxtnxt->prev_sibling=it.node; 1731 else 1732 it.node->parent->last_child=it.node; 1733 nxt->next_sibling=it.node; 1734 it.node->prev_sibling=nxt; 1735 it.node->next_sibling=nxtnxt; 1736 } 1737 } 1738 1739 template <class T, class tree_node_allocator> 1740 void tree<T, tree_node_allocator>::swap(iterator one, iterator two) 1741 { 1742 // if one and two are adjacent siblings, use the sibling swap 1743 if(one.node->next_sibling==two.node) swap(one); 1744 else if(two.node->next_sibling==one.node) swap(two); 1745 else { 1746 tree_node *nxt1=one.node->next_sibling; 1747 tree_node *nxt2=two.node->next_sibling; 1748 tree_node *pre1=one.node->prev_sibling; 1749 tree_node *pre2=two.node->prev_sibling; 1750 tree_node *par1=one.node->parent; 1751 tree_node *par2=two.node->parent; 1752 1753 // reconnect 1754 one.node->parent=par2; 1755 one.node->next_sibling=nxt2; 1756 if(nxt2) nxt2->prev_sibling=one.node; 1757 else par2->last_child=one.node; 1758 one.node->prev_sibling=pre2; 1759 if(pre2) pre2->next_sibling=one.node; 1760 else par2->first_child=one.node; 1761 1762 two.node->parent=par1; 1763 two.node->next_sibling=nxt1; 1764 if(nxt1) nxt1->prev_sibling=two.node; 1765 else par1->last_child=two.node; 1766 two.node->prev_sibling=pre1; 1767 if(pre1) pre1->next_sibling=two.node; 1768 else par1->first_child=two.node; 1769 } 1770 } 1771 1772 // template <class BinaryPredicate> 1773 // tree<T, tree_node_allocator>::iterator tree<T, tree_node_allocator>::find_subtree( 1774 // sibling_iterator subfrom, sibling_iterator subto, iterator from, iterator to, 1775 // BinaryPredicate fun) const 1776 // { 1777 // assert(1==0); // this routine is not finished yet. 1778 // while(from!=to) { 1779 // if(fun(*subfrom, *from)) { 1780 // 1781 // } 1782 // } 1783 // return to; 1784 // } 1785 1786 template <class T, class tree_node_allocator> 1787 bool tree<T, tree_node_allocator>::is_in_subtree(const iterator_base& it, const iterator_base& begin, 1788 const iterator_base& end) const 1789 { 1790 // FIXME: this should be optimised. 1791 pre_order_iterator tmp=begin; 1792 while(tmp!=end) { 1793 if(tmp==it) return true; 1794 ++tmp; 1795 } 1796 return false; 1797 } 1798 1799 template <class T, class tree_node_allocator> 1800 bool tree<T, tree_node_allocator>::is_valid(const iterator_base& it) const 1801 { 1802 if(it.node==0 || it.node==feet || it.node==head) return false; 1803 else return true; 1804 } 1805 1806 template <class T, class tree_node_allocator> 1807 unsigned int tree<T, tree_node_allocator>::index(sibling_iterator it) const 1808 { 1809 unsigned int ind=0; 1810 if(it.node->parent==0) { 1811 while(it.node->prev_sibling!=head) { 1812 it.node=it.node->prev_sibling; 1813 ++ind; 1814 } 1815 } 1816 else { 1817 while(it.node->prev_sibling!=0) { 1818 it.node=it.node->prev_sibling; 1819 ++ind; 1820 } 1821 } 1822 return ind; 1823 } 1824 1825 1826 template <class T, class tree_node_allocator> 1827 typename tree<T, tree_node_allocator>::sibling_iterator tree<T, tree_node_allocator>::child(const iterator_base& it, unsigned int num) const 1828 { 1829 tree_node *tmp=it.node->first_child; 1830 while(num--) { 1831 assert(tmp!=0); 1832 tmp=tmp->next_sibling; 1833 } 1834 return tmp; 1835 } 1836 1837 1838 1839 1840 // Iterator base 1841 1842 template <class T, class tree_node_allocator> 1843 tree<T, tree_node_allocator>::iterator_base::iterator_base() 1844 : node(0), skip_current_children_(false) 1845 { 1846 } 1847 1848 template <class T, class tree_node_allocator> 1849 tree<T, tree_node_allocator>::iterator_base::iterator_base(tree_node *tn) 1850 : node(tn), skip_current_children_(false) 1851 { 1852 } 1853 1854 template <class T, class tree_node_allocator> 1855 T& tree<T, tree_node_allocator>::iterator_base::operator*() const 1856 { 1857 return node->data; 1858 } 1859 1860 template <class T, class tree_node_allocator> 1861 T* tree<T, tree_node_allocator>::iterator_base::operator->() const 1862 { 1863 return &(node->data); 1864 } 1865 1866 template <class T, class tree_node_allocator> 1867 bool tree<T, tree_node_allocator>::post_order_iterator::operator!=(const post_order_iterator& other) const 1868 { 1869 if(other.node!=this->node) return true; 1870 else return false; 1871 } 1872 1873 template <class T, class tree_node_allocator> 1874 bool tree<T, tree_node_allocator>::post_order_iterator::operator==(const post_order_iterator& other) const 1875 { 1876 if(other.node==this->node) return true; 1877 else return false; 1878 } 1879 1880 template <class T, class tree_node_allocator> 1881 bool tree<T, tree_node_allocator>::pre_order_iterator::operator!=(const pre_order_iterator& other) const 1882 { 1883 if(other.node!=this->node) return true; 1884 else return false; 1885 } 1886 1887 template <class T, class tree_node_allocator> 1888 bool tree<T, tree_node_allocator>::pre_order_iterator::operator==(const pre_order_iterator& other) const 1889 { 1890 if(other.node==this->node) return true; 1891 else return false; 1892 } 1893 1894 template <class T, class tree_node_allocator> 1895 bool tree<T, tree_node_allocator>::sibling_iterator::operator!=(const sibling_iterator& other) const 1896 { 1897 if(other.node!=this->node) return true; 1898 else return false; 1899 } 1900 1901 template <class T, class tree_node_allocator> 1902 bool tree<T, tree_node_allocator>::sibling_iterator::operator==(const sibling_iterator& other) const 1903 { 1904 if(other.node==this->node) return true; 1905 else return false; 1906 } 1907 1908 template <class T, class tree_node_allocator> 1909 bool tree<T, tree_node_allocator>::leaf_iterator::operator!=(const leaf_iterator& other) const 1910 { 1911 if(other.node!=this->node) return true; 1912 else return false; 1913 } 1914 1915 template <class T, class tree_node_allocator> 1916 bool tree<T, tree_node_allocator>::leaf_iterator::operator==(const leaf_iterator& other) const 1917 { 1918 if(other.node==this->node) return true; 1919 else return false; 1920 } 1921 1922 template <class T, class tree_node_allocator> 1923 typename tree<T, tree_node_allocator>::sibling_iterator tree<T, tree_node_allocator>::iterator_base::begin() const 1924 { 1925 if(node->first_child==0) 1926 return end(); 1927 1928 sibling_iterator ret(node->first_child); 1929 ret.parent_=this->node; 1930 return ret; 1931 } 1932 1933 template <class T, class tree_node_allocator> 1934 typename tree<T, tree_node_allocator>::sibling_iterator tree<T, tree_node_allocator>::iterator_base::end() const 1935 { 1936 sibling_iterator ret(0); 1937 ret.parent_=node; 1938 return ret; 1939 } 1940 1941 template <class T, class tree_node_allocator> 1942 void tree<T, tree_node_allocator>::iterator_base::skip_children() 1943 { 1944 skip_current_children_=true; 1945 } 1946 1947 template <class T, class tree_node_allocator> 1948 unsigned int tree<T, tree_node_allocator>::iterator_base::number_of_children() const 1949 { 1950 tree_node *pos=node->first_child; 1951 if(pos==0) return 0; 1952 1953 unsigned int ret=1; 1954 while(pos!=node->last_child) { 1955 ++ret; 1956 pos=pos->next_sibling; 1957 } 1958 return ret; 1959 } 1960 1961 1962 1963 // Pre-order iterator 1964 1965 template <class T, class tree_node_allocator> 1966 tree<T, tree_node_allocator>::pre_order_iterator::pre_order_iterator() 1967 : iterator_base(nullptr) 1968 { 1969 } 1970 1971 template <class T, class tree_node_allocator> 1972 tree<T, tree_node_allocator>::pre_order_iterator::pre_order_iterator(tree_node *tn) 1973 : iterator_base(tn) 1974 { 1975 } 1976 1977 template <class T, class tree_node_allocator> 1978 tree<T, tree_node_allocator>::pre_order_iterator::pre_order_iterator(const iterator_base &other) 1979 : iterator_base(other.node) 1980 { 1981 } 1982 1983 template <class T, class tree_node_allocator> 1984 tree<T, tree_node_allocator>::pre_order_iterator::pre_order_iterator(const sibling_iterator& other) 1985 : iterator_base(other.node) 1986 { 1987 if(this->node==nullptr) { 1988 if(other.range_last()!=nullptr) 1989 this->node=other.range_last(); 1990 else 1991 this->node=other.parent_; 1992 this->skip_children(); 1993 ++(*this); 1994 } 1995 } 1996 1997 template <class T, class tree_node_allocator> 1998 typename tree<T, tree_node_allocator>::pre_order_iterator& tree<T, tree_node_allocator>::pre_order_iterator::operator++() 1999 { 2000 assert(this->node!=nullptr); 2001 if(!this->skip_current_children_ && this->node->first_child != nullptr) { 2002 this->node=this->node->first_child; 2003 } 2004 else { 2005 this->skip_current_children_=false; 2006 while(this->node->next_sibling==nullptr) { 2007 this->node=this->node->parent; 2008 if(this->node==nullptr) 2009 return *this; 2010 } 2011 this->node=this->node->next_sibling; 2012 } 2013 return *this; 2014 } 2015 2016 template <class T, class tree_node_allocator> 2017 typename tree<T, tree_node_allocator>::pre_order_iterator& tree<T, tree_node_allocator>::pre_order_iterator::operator--() 2018 { 2019 assert(this->node!=0); 2020 if(this->node->prev_sibling) { 2021 this->node=this->node->prev_sibling; 2022 while(this->node->last_child) 2023 this->node=this->node->last_child; 2024 } 2025 else { 2026 this->node=this->node->parent; 2027 if(this->node==0) 2028 return *this; 2029 } 2030 return *this; 2031 } 2032 2033 template <class T, class tree_node_allocator> 2034 typename tree<T, tree_node_allocator>::pre_order_iterator tree<T, tree_node_allocator>::pre_order_iterator::operator++(int n) 2035 { 2036 n = 0; 2037 pre_order_iterator copy = *this; 2038 ++(*this); 2039 return copy; 2040 } 2041 2042 template <class T, class tree_node_allocator> 2043 typename tree<T, tree_node_allocator>::pre_order_iterator tree<T, tree_node_allocator>::pre_order_iterator::operator--(int n) 2044 { 2045 n = 0; 2046 pre_order_iterator copy = *this; 2047 --(*this); 2048 return copy; 2049 } 2050 2051 template <class T, class tree_node_allocator> 2052 typename tree<T, tree_node_allocator>::pre_order_iterator& tree<T, tree_node_allocator>::pre_order_iterator::operator+=(unsigned int num) 2053 { 2054 while(num>0) { 2055 ++(*this); 2056 --num; 2057 } 2058 return (*this); 2059 } 2060 2061 template <class T, class tree_node_allocator> 2062 typename tree<T, tree_node_allocator>::pre_order_iterator& tree<T, tree_node_allocator>::pre_order_iterator::operator-=(unsigned int num) 2063 { 2064 while(num>0) { 2065 --(*this); 2066 --num; 2067 } 2068 return (*this); 2069 } 2070 2071 2072 2073 // Post-order iterator 2074 2075 template <class T, class tree_node_allocator> 2076 tree<T, tree_node_allocator>::post_order_iterator::post_order_iterator() 2077 : iterator_base(0) 2078 { 2079 } 2080 2081 template <class T, class tree_node_allocator> 2082 tree<T, tree_node_allocator>::post_order_iterator::post_order_iterator(tree_node *tn) 2083 : iterator_base(tn) 2084 { 2085 } 2086 2087 template <class T, class tree_node_allocator> 2088 tree<T, tree_node_allocator>::post_order_iterator::post_order_iterator(const iterator_base &other) 2089 : iterator_base(other.node) 2090 { 2091 } 2092 2093 template <class T, class tree_node_allocator> 2094 tree<T, tree_node_allocator>::post_order_iterator::post_order_iterator(const sibling_iterator& other) 2095 : iterator_base(other.node) 2096 { 2097 if(this->node==0) { 2098 if(other.range_last()!=0) 2099 this->node=other.range_last(); 2100 else 2101 this->node=other.parent_; 2102 this->skip_children(); 2103 ++(*this); 2104 } 2105 } 2106 2107 template <class T, class tree_node_allocator> 2108 typename tree<T, tree_node_allocator>::post_order_iterator& tree<T, tree_node_allocator>::post_order_iterator::operator++() 2109 { 2110 assert(this->node!=0); 2111 if(this->node->next_sibling==0) { 2112 this->node=this->node->parent; 2113 this->skip_current_children_=false; 2114 } 2115 else { 2116 this->node=this->node->next_sibling; 2117 if(this->skip_current_children_) { 2118 this->skip_current_children_=false; 2119 } 2120 else { 2121 while(this->node->first_child) 2122 this->node=this->node->first_child; 2123 } 2124 } 2125 return *this; 2126 } 2127 2128 template <class T, class tree_node_allocator> 2129 typename tree<T, tree_node_allocator>::post_order_iterator& tree<T, tree_node_allocator>::post_order_iterator::operator--() 2130 { 2131 assert(this->node!=0); 2132 if(this->skip_current_children_ || this->node->last_child==0) { 2133 this->skip_current_children_=false; 2134 while(this->node->prev_sibling==0) 2135 this->node=this->node->parent; 2136 this->node=this->node->prev_sibling; 2137 } 2138 else { 2139 this->node=this->node->last_child; 2140 } 2141 return *this; 2142 } 2143 2144 template <class T, class tree_node_allocator> 2145 typename tree<T, tree_node_allocator>::post_order_iterator tree<T, tree_node_allocator>::post_order_iterator::operator++(int) 2146 { 2147 post_order_iterator copy = *this; 2148 ++(*this); 2149 return copy; 2150 } 2151 2152 template <class T, class tree_node_allocator> 2153 typename tree<T, tree_node_allocator>::post_order_iterator tree<T, tree_node_allocator>::post_order_iterator::operator--(int) 2154 { 2155 post_order_iterator copy = *this; 2156 --(*this); 2157 return copy; 2158 } 2159 2160 2161 template <class T, class tree_node_allocator> 2162 typename tree<T, tree_node_allocator>::post_order_iterator& tree<T, tree_node_allocator>::post_order_iterator::operator+=(unsigned int num) 2163 { 2164 while(num>0) { 2165 ++(*this); 2166 --num; 2167 } 2168 return (*this); 2169 } 2170 2171 template <class T, class tree_node_allocator> 2172 typename tree<T, tree_node_allocator>::post_order_iterator& tree<T, tree_node_allocator>::post_order_iterator::operator-=(unsigned int num) 2173 { 2174 while(num>0) { 2175 --(*this); 2176 --num; 2177 } 2178 return (*this); 2179 } 2180 2181 template <class T, class tree_node_allocator> 2182 void tree<T, tree_node_allocator>::post_order_iterator::descend_all() 2183 { 2184 assert(this->node!=0); 2185 while(this->node->first_child) 2186 this->node=this->node->first_child; 2187 } 2188 2189 2190 // Breadth-first iterator 2191 2192 template <class T, class tree_node_allocator> 2193 tree<T, tree_node_allocator>::breadth_first_queued_iterator::breadth_first_queued_iterator() 2194 : iterator_base() 2195 { 2196 } 2197 2198 template <class T, class tree_node_allocator> 2199 tree<T, tree_node_allocator>::breadth_first_queued_iterator::breadth_first_queued_iterator(tree_node *tn) 2200 : iterator_base(tn) 2201 { 2202 traversal_queue.push(tn); 2203 } 2204 2205 template <class T, class tree_node_allocator> 2206 tree<T, tree_node_allocator>::breadth_first_queued_iterator::breadth_first_queued_iterator(const iterator_base& other) 2207 : iterator_base(other.node) 2208 { 2209 traversal_queue.push(other.node); 2210 } 2211 2212 template <class T, class tree_node_allocator> 2213 bool tree<T, tree_node_allocator>::breadth_first_queued_iterator::operator!=(const breadth_first_queued_iterator& other) const 2214 { 2215 if(other.node!=this->node) return true; 2216 else return false; 2217 } 2218 2219 template <class T, class tree_node_allocator> 2220 bool tree<T, tree_node_allocator>::breadth_first_queued_iterator::operator==(const breadth_first_queued_iterator& other) const 2221 { 2222 if(other.node==this->node) return true; 2223 else return false; 2224 } 2225 2226 template <class T, class tree_node_allocator> 2227 typename tree<T, tree_node_allocator>::breadth_first_queued_iterator& tree<T, tree_node_allocator>::breadth_first_queued_iterator::operator++() 2228 { 2229 assert(this->node!=0); 2230 2231 // Add child nodes and pop current node 2232 sibling_iterator sib=this->begin(); 2233 while(sib!=this->end()) { 2234 traversal_queue.push(sib.node); 2235 ++sib; 2236 } 2237 traversal_queue.pop(); 2238 if(traversal_queue.size()>0) 2239 this->node=traversal_queue.front(); 2240 else 2241 this->node=0; 2242 return (*this); 2243 } 2244 2245 template <class T, class tree_node_allocator> 2246 typename tree<T, tree_node_allocator>::breadth_first_queued_iterator tree<T, tree_node_allocator>::breadth_first_queued_iterator::operator++(int n) 2247 { 2248 n = 0; 2249 breadth_first_queued_iterator copy = *this; 2250 ++(*this); 2251 return copy; 2252 } 2253 2254 template <class T, class tree_node_allocator> 2255 typename tree<T, tree_node_allocator>::breadth_first_queued_iterator& tree<T, tree_node_allocator>::breadth_first_queued_iterator::operator+=(unsigned int num) 2256 { 2257 while(num>0) { 2258 ++(*this); 2259 --num; 2260 } 2261 return (*this); 2262 } 2263 2264 2265 2266 // Fixed depth iterator 2267 2268 template <class T, class tree_node_allocator> 2269 tree<T, tree_node_allocator>::fixed_depth_iterator::fixed_depth_iterator() 2270 : iterator_base() 2271 { 2272 set_first_parent_(); 2273 } 2274 2275 template <class T, class tree_node_allocator> 2276 tree<T, tree_node_allocator>::fixed_depth_iterator::fixed_depth_iterator(tree_node *tn) 2277 : iterator_base(tn) 2278 { 2279 set_first_parent_(); 2280 } 2281 2282 template <class T, class tree_node_allocator> 2283 tree<T, tree_node_allocator>::fixed_depth_iterator::fixed_depth_iterator(const iterator_base& other) 2284 : iterator_base(other.node) 2285 { 2286 set_first_parent_(); 2287 } 2288 2289 template <class T, class tree_node_allocator> 2290 tree<T, tree_node_allocator>::fixed_depth_iterator::fixed_depth_iterator(const sibling_iterator& other) 2291 : iterator_base(other.node), first_parent_(other.parent_) 2292 { 2293 find_leftmost_parent_(); 2294 } 2295 2296 template <class T, class tree_node_allocator> 2297 tree<T, tree_node_allocator>::fixed_depth_iterator::fixed_depth_iterator(const fixed_depth_iterator& other) 2298 : iterator_base(other.node), first_parent_(other.first_parent_) 2299 { 2300 } 2301 2302 template <class T, class tree_node_allocator> 2303 bool tree<T, tree_node_allocator>::fixed_depth_iterator::operator==(const fixed_depth_iterator& other) const 2304 { 2305 if(other.node==this->node && other.first_parent_==first_parent_) return true; 2306 else return false; 2307 } 2308 2309 template <class T, class tree_node_allocator> 2310 bool tree<T, tree_node_allocator>::fixed_depth_iterator::operator!=(const fixed_depth_iterator& other) const 2311 { 2312 if(other.node!=this->node || other.first_parent_!=first_parent_) return true; 2313 else return false; 2314 } 2315 2316 template <class T, class tree_node_allocator> 2317 void tree<T, tree_node_allocator>::fixed_depth_iterator::set_first_parent_() 2318 { 2319 return; // FIXME: we do not use first_parent_ yet, and it actually needs some serious reworking if 2320 // it is ever to work at the 'head' level. 2321 first_parent_=0; 2322 if(this->node==0) return; 2323 if(this->node->parent!=0) 2324 first_parent_=this->node->parent; 2325 if(first_parent_) 2326 find_leftmost_parent_(); 2327 } 2328 2329 template <class T, class tree_node_allocator> 2330 void tree<T, tree_node_allocator>::fixed_depth_iterator::find_leftmost_parent_() 2331 { 2332 return; // FIXME: see 'set_first_parent()' 2333 tree_node *tmppar=first_parent_; 2334 while(tmppar->prev_sibling) { 2335 tmppar=tmppar->prev_sibling; 2336 if(tmppar->first_child) 2337 first_parent_=tmppar; 2338 } 2339 } 2340 2341 template <class T, class tree_node_allocator> 2342 typename tree<T, tree_node_allocator>::fixed_depth_iterator& tree<T, tree_node_allocator>::fixed_depth_iterator::operator++() 2343 { 2344 assert(this->node!=0); 2345 2346 if(this->node->next_sibling) { 2347 this->node=this->node->next_sibling; 2348 } 2349 else { 2350 int relative_depth=0; 2351 upper: 2352 do { 2353 this->node=this->node->parent; 2354 if(this->node==0) return *this; 2355 --relative_depth; 2356 } while(this->node->next_sibling==0); 2357 lower: 2358 this->node=this->node->next_sibling; 2359 while(this->node->first_child==0) { 2360 if(this->node->next_sibling==0) 2361 goto upper; 2362 this->node=this->node->next_sibling; 2363 if(this->node==0) return *this; 2364 } 2365 while(relative_depth<0 && this->node->first_child!=0) { 2366 this->node=this->node->first_child; 2367 ++relative_depth; 2368 } 2369 if(relative_depth<0) { 2370 if(this->node->next_sibling==0) goto upper; 2371 else goto lower; 2372 } 2373 } 2374 return *this; 2375 2376 // if(this->node->next_sibling!=0) { 2377 // this->node=this->node->next_sibling; 2378 // assert(this->node!=0); 2379 // if(this->node->parent==0 && this->node->next_sibling==0) // feet element 2380 // this->node=0; 2381 // } 2382 // else { 2383 // tree_node *par=this->node->parent; 2384 // do { 2385 // par=par->next_sibling; 2386 // if(par==0) { // FIXME: need to keep track of this! 2387 // this->node=0; 2388 // return *this; 2389 // } 2390 // } while(par->first_child==0); 2391 // this->node=par->first_child; 2392 // } 2393 return *this; 2394 } 2395 2396 template <class T, class tree_node_allocator> 2397 typename tree<T, tree_node_allocator>::fixed_depth_iterator& tree<T, tree_node_allocator>::fixed_depth_iterator::operator--() 2398 { 2399 assert(this->node!=0); 2400 if(this->node->prev_sibling!=0) { 2401 this->node=this->node->prev_sibling; 2402 assert(this->node!=0); 2403 if(this->node->parent==0 && this->node->prev_sibling==0) // head element 2404 this->node=0; 2405 } 2406 else { 2407 tree_node *par=this->node->parent; 2408 do { 2409 par=par->prev_sibling; 2410 if(par==0) { // FIXME: need to keep track of this! 2411 this->node=0; 2412 return *this; 2413 } 2414 } while(par->last_child==0); 2415 this->node=par->last_child; 2416 } 2417 return *this; 2418 } 2419 2420 template <class T, class tree_node_allocator> 2421 typename tree<T, tree_node_allocator>::fixed_depth_iterator tree<T, tree_node_allocator>::fixed_depth_iterator::operator++(int) 2422 { 2423 fixed_depth_iterator copy = *this; 2424 ++(*this); 2425 return copy; 2426 } 2427 2428 template <class T, class tree_node_allocator> 2429 typename tree<T, tree_node_allocator>::fixed_depth_iterator tree<T, tree_node_allocator>::fixed_depth_iterator::operator--(int) 2430 { 2431 fixed_depth_iterator copy = *this; 2432 --(*this); 2433 return copy; 2434 } 2435 2436 template <class T, class tree_node_allocator> 2437 typename tree<T, tree_node_allocator>::fixed_depth_iterator& tree<T, tree_node_allocator>::fixed_depth_iterator::operator-=(unsigned int num) 2438 { 2439 while(num>0) { 2440 --(*this); 2441 --(num); 2442 } 2443 return (*this); 2444 } 2445 2446 template <class T, class tree_node_allocator> 2447 typename tree<T, tree_node_allocator>::fixed_depth_iterator& tree<T, tree_node_allocator>::fixed_depth_iterator::operator+=(unsigned int num) 2448 { 2449 while(num>0) { 2450 ++(*this); 2451 --(num); 2452 } 2453 return *this; 2454 } 2455 2456 // FIXME: add the other members of fixed_depth_iterator. 2457 2458 2459 // Sibling iterator 2460 2461 template <class T, class tree_node_allocator> 2462 tree<T, tree_node_allocator>::sibling_iterator::sibling_iterator() 2463 : iterator_base() 2464 { 2465 set_parent_(); 2466 } 2467 2468 template <class T, class tree_node_allocator> 2469 tree<T, tree_node_allocator>::sibling_iterator::sibling_iterator(tree_node *tn) 2470 : iterator_base(tn) 2471 { 2472 set_parent_(); 2473 } 2474 2475 template <class T, class tree_node_allocator> 2476 tree<T, tree_node_allocator>::sibling_iterator::sibling_iterator(const iterator_base& other) 2477 : iterator_base(other.node) 2478 { 2479 set_parent_(); 2480 } 2481 2482 template <class T, class tree_node_allocator> 2483 tree<T, tree_node_allocator>::sibling_iterator::sibling_iterator(const sibling_iterator&) 2484 = default; 2485 2486 template <class T, class tree_node_allocator> 2487 void tree<T, tree_node_allocator>::sibling_iterator::set_parent_() 2488 { 2489 parent_=nullptr; 2490 if(this->node==nullptr) return; 2491 if(this->node->parent!=nullptr) 2492 parent_=this->node->parent; 2493 } 2494 2495 template <class T, class tree_node_allocator> 2496 typename tree<T, tree_node_allocator>::sibling_iterator& tree<T, tree_node_allocator>::sibling_iterator::operator++() 2497 { 2498 if(this->node) 2499 this->node=this->node->next_sibling; 2500 return *this; 2501 } 2502 2503 template <class T, class tree_node_allocator> 2504 typename tree<T, tree_node_allocator>::sibling_iterator& tree<T, tree_node_allocator>::sibling_iterator::operator--() 2505 { 2506 if(this->node) this->node=this->node->prev_sibling; 2507 else { 2508 assert(parent_); 2509 this->node=parent_->last_child; 2510 } 2511 return *this; 2512 } 2513 2514 template <class T, class tree_node_allocator> 2515 typename tree<T, tree_node_allocator>::sibling_iterator tree<T, tree_node_allocator>::sibling_iterator::operator++(int) 2516 { 2517 sibling_iterator copy = *this; 2518 ++(*this); 2519 return copy; 2520 } 2521 2522 template <class T, class tree_node_allocator> 2523 typename tree<T, tree_node_allocator>::sibling_iterator tree<T, tree_node_allocator>::sibling_iterator::operator--(int) 2524 { 2525 sibling_iterator copy = *this; 2526 --(*this); 2527 return copy; 2528 } 2529 2530 template <class T, class tree_node_allocator> 2531 typename tree<T, tree_node_allocator>::sibling_iterator& tree<T, tree_node_allocator>::sibling_iterator::operator+=(unsigned int num) 2532 { 2533 while(num>0) { 2534 ++(*this); 2535 --num; 2536 } 2537 return (*this); 2538 } 2539 2540 template <class T, class tree_node_allocator> 2541 typename tree<T, tree_node_allocator>::sibling_iterator& tree<T, tree_node_allocator>::sibling_iterator::operator-=(unsigned int num) 2542 { 2543 while(num>0) { 2544 --(*this); 2545 --num; 2546 } 2547 return (*this); 2548 } 2549 2550 template <class T, class tree_node_allocator> 2551 typename tree<T, tree_node_allocator>::tree_node *tree<T, tree_node_allocator>::sibling_iterator::range_first() const 2552 { 2553 tree_node *tmp=parent_->first_child; 2554 return tmp; 2555 } 2556 2557 template <class T, class tree_node_allocator> 2558 typename tree<T, tree_node_allocator>::tree_node *tree<T, tree_node_allocator>::sibling_iterator::range_last() const 2559 { 2560 return parent_->last_child; 2561 } 2562 2563 // Leaf iterator 2564 2565 template <class T, class tree_node_allocator> 2566 tree<T, tree_node_allocator>::leaf_iterator::leaf_iterator() 2567 : iterator_base(0) 2568 { 2569 } 2570 2571 template <class T, class tree_node_allocator> 2572 tree<T, tree_node_allocator>::leaf_iterator::leaf_iterator(tree_node *tn) 2573 : iterator_base(tn) 2574 { 2575 } 2576 2577 template <class T, class tree_node_allocator> 2578 tree<T, tree_node_allocator>::leaf_iterator::leaf_iterator(const iterator_base &other) 2579 : iterator_base(other.node) 2580 { 2581 } 2582 2583 template <class T, class tree_node_allocator> 2584 tree<T, tree_node_allocator>::leaf_iterator::leaf_iterator(const sibling_iterator& other) 2585 : iterator_base(other.node) 2586 { 2587 if(this->node==0) { 2588 if(other.range_last()!=0) 2589 this->node=other.range_last(); 2590 else 2591 this->node=other.parent_; 2592 ++(*this); 2593 } 2594 } 2595 2596 template <class T, class tree_node_allocator> 2597 typename tree<T, tree_node_allocator>::leaf_iterator& tree<T, tree_node_allocator>::leaf_iterator::operator++() 2598 { 2599 assert(this->node!=0); 2600 while(this->node->next_sibling==0) { 2601 if (this->node->parent==0) return *this; 2602 this->node=this->node->parent; 2603 } 2604 this->node=this->node->next_sibling; 2605 while(this->node->first_child) 2606 this->node=this->node->first_child; 2607 return *this; 2608 } 2609 2610 template <class T, class tree_node_allocator> 2611 typename tree<T, tree_node_allocator>::leaf_iterator& tree<T, tree_node_allocator>::leaf_iterator::operator--() 2612 { 2613 assert(this->node!=0); 2614 while (this->node->prev_sibling==0) { 2615 if (this->node->parent==0) return *this; 2616 this->node=this->node->parent; 2617 } 2618 this->node=this->node->prev_sibling; 2619 while(this->node->last_child) 2620 this->node=this->node->last_child; 2621 return *this; 2622 } 2623 2624 template <class T, class tree_node_allocator> 2625 typename tree<T, tree_node_allocator>::leaf_iterator tree<T, tree_node_allocator>::leaf_iterator::operator++(int) 2626 { 2627 leaf_iterator copy = *this; 2628 ++(*this); 2629 return copy; 2630 } 2631 2632 template <class T, class tree_node_allocator> 2633 typename tree<T, tree_node_allocator>::leaf_iterator tree<T, tree_node_allocator>::leaf_iterator::operator--(int) 2634 { 2635 leaf_iterator copy = *this; 2636 --(*this); 2637 return copy; 2638 } 2639 2640 2641 template <class T, class tree_node_allocator> 2642 typename tree<T, tree_node_allocator>::leaf_iterator& tree<T, tree_node_allocator>::leaf_iterator::operator+=(unsigned int num) 2643 { 2644 while(num>0) { 2645 ++(*this); 2646 --num; 2647 } 2648 return (*this); 2649 } 2650 2651 template <class T, class tree_node_allocator> 2652 typename tree<T, tree_node_allocator>::leaf_iterator& tree<T, tree_node_allocator>::leaf_iterator::operator-=(unsigned int num) 2653 { 2654 while(num>0) { 2655 --(*this); 2656 --num; 2657 } 2658 return (*this); 2659 } 2660 2661 #endif 2662 2663 // Local variables: 2664 // default-tab-width: 3 2665 // End: