00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00031 #ifndef __VGTL_TREE_H_
00032 #define __VGTL_TREE_H_
00033
00034 #include <vector>
00035 #include <list>
00036 #include <multimap>
00037 #include <multiset>
00038 #include <algorithm>
00039 #include <iterator>
00040 #include <string>
00041 #include <utility>
00042 #include <g_data.h>
00043
00045 #define _C_W_preorder 1
00046
00047 #define _C_W_postorder 2
00048
00050 typedef enum {cw_pre = _C_W_preorder, cw_post = _C_W_postorder,
00051 cw_pre_post = _C_W_preorder|_C_W_postorder} walker_type;
00052
00053 #include <vgtl_helpers.h>
00054 #include <vgtl_intadapt.h>
00055
00056 __VGTL_BEGIN_NAMESPACE
00057
00059
00063 template <class _Tp, class _Ctr, class _Iterator>
00064 class _Tree_node
00065 {
00066 private:
00067 typedef void* _Void_pointer;
00068 typedef _Iterator _Ctr_iterator;
00069 typedef _Tree_node<_Tp,_Ctr,_Iterator> _Self;
00070
00071 public:
00073 _Tp _C_data;
00075 _Void_pointer _C_parent;
00077 _Ctr _C_children;
00078
00080 _Tree_node() { _C_data();
00081 __VGTL_TRY {
00082 initialize();
00083 }
00084 __VGTL_UNWIND( );
00085 }
00086
00088 void initialize() {
00089 std::_Construct(&_C_children);
00090 _C_parent = NULL;
00091 }
00092
00094 void get_rid_of() {
00095 std::_Destroy(&_C_children);
00096 }
00097
00099 void clear_tree();
00101 void clear_children()
00102 { _C_children.erase(_C_children.begin(), _C_children.end()); }
00103
00105 _Ctr_iterator get_childentry_iterator(_Void_pointer __p)
00106 { _Ctr_iterator __tmp;
00107 for(__tmp = _C_children.begin(); __tmp != _C_children.end(); ++__tmp)
00108 if(*__tmp == __p) break;
00109 return __tmp;
00110 }
00111
00112 #ifdef __VGTL_MEMBER_TEMPLATES
00113
00117 template <class _Output_Iterator>
00118 void add_all_children(_Output_Iterator fi, _Self* _parent);
00119
00121 template <class Compare>
00122 void sort_children(_Ctr_iterator first, _Ctr_iterator last, Compare comp)
00123 {
00124 sort(first, last, _G_compare_adaptor<Compare, _Self>(comp));
00125 }
00126
00128 template <class Compare>
00129 void sort_parents(_Ctr_iterator first, _Ctr_iterator last, Compare comp) {}
00130 #endif
00131 };
00132
00134
00138 template <class _Tp, class _Ctr, class _Iterator>
00139 class _ITree_node : public _Tree_node<_Tp,_Ctr,_Iterator>
00140 {
00141 private:
00142 typedef void* _Void_pointer;
00143 typedef _Iterator _Ctr_iterator;
00144 typedef _ITree_node<_Tp,_Ctr,_Iterator> _Self;
00145
00146 public:
00148 ctree_data_hook _C_data_hook;
00149
00151 _ITree_node() { _C_data();
00152 __VGTL_TRY {
00153 initialize();
00154 }
00155 __VGTL_UNWIND( );
00156 }
00157
00159 void initialize() {
00160 std::_Construct(&_C_children);
00161 _C_parent = NULL;
00162 _C_data_hook.f = 0;
00163 }
00164
00166 void get_rid_of() {
00167 std::_Destroy(&_C_children);
00168 std::_Destroy(&_C_data_hook);
00169 }
00170
00172 ctree_data_hook& data_hook() { return _C_data_hook; }
00173
00174 };
00175
00176 #ifdef __VGTL_MEMBER_TEMPLATES
00177 template <class _Tp, class _Ctr, class _Iterator>
00178 template <class _Output_Iterator>
00179 void
00180 _Tree_node<_Tp,_Ctr,_Iterator>::
00181 add_all_children(_Output_Iterator fi, _Self* _parent)
00182 { _Ctr_iterator it;
00183 for(it = _C_children.begin();
00184 it != _C_children.end();
00185 ++it)
00186 {
00187 *fi++ = *it;
00188 (*((_Self*)*it))._C_parent = _parent;
00189 }
00190 };
00191 #endif
00192
00193
00194 template <class _Tp, class _Ctr, class _Iterator>
00195 void
00196 _Tree_node<_Tp,_Ctr,_Iterator>::clear_tree()
00197 {
00198 _Ctr* chld = &this->_C_children;
00199 _Iterator it;
00200
00201 for(it = chld->begin(); it != chld->end(); ++it)
00202 ((_Tree_node<_Tp,_Ctr,_Iterator>*)(*it))->clear_tree();
00203 this->_C_children.erase(this->_C_children.begin(), this->_C_children.end());
00204 this->_C_parent = NULL;
00205
00206 std::_Destroy(&this->_C_data);
00207 get_rid_of();
00208 }
00209
00210
00211 template <class _Tp, class _Ref, class _Ptr, class _Ctr, class _Iterator,
00212 class _Node>
00213 class _Tree_iterator;
00214
00216
00220 template <class _Tp, class _Ref, class _Ptr, class _Ctr, class _Iterator,
00221 class _Node>
00222 class _Tree_walker_base
00223 {
00224 public:
00225 typedef _Tree_walker_base<_Tp,_Tp&,_Tp*,_Ctr,_Iterator,_Node> walker;
00226 typedef _Tree_walker_base<_Tp,const _Tp&,const _Tp*,_Ctr,_Iterator,_Node> const_walker;
00227 typedef _Tree_walker_base<_Tp,_Ref,_Ptr,_Ctr,_Iterator,_Node> _Self;
00228 typedef _Tree_iterator<_Tp,_Ref,_Ptr,_Ctr,_Iterator,_Node> _Itr;
00229
00231
00232 typedef _Tp value_type;
00233 typedef _Ptr pointer;
00234 typedef _Ref reference;
00236 private:
00237 typedef _Iterator _Ctr_iterator;
00238
00239 public:
00241
00242 typedef __one_iterator<void *> parents_iterator;
00243 typedef _Ctr_iterator children_iterator;
00244 typedef _Node node_type;
00245
00246 typedef size_t size_type;
00247 typedef ptrdiff_t difference_type;
00249
00250 public:
00252 _Node* _C_w_cur;
00253
00254 public:
00256 _Tree_walker_base() {}
00257
00259 _Tree_walker_base(_Node* __x) : _C_w_cur(__x) { }
00260
00262 _Tree_walker_base(const walker& __x) : _C_w_cur(__x._C_w_cur) {}
00263
00265 reference operator*() const { return (*_C_w_cur)._C_data; }
00266
00267 #ifndef __SGI_STL_NO_ARROW_OPERATOR
00268
00269 pointer operator->() const { return &(operator*()); }
00270 #endif
00271
00272 public:
00274 _Self& operator=(const _Itr& __x) {
00275 _C_w_cur = __x._C_i_cur;
00276 return *this;
00277 }
00278
00280 ctree_data_hook& data_hook() { return (*_C_w_cur)._C_data_hook; }
00282 ctree_data_hook& parent_data_hook()
00283 { return ((_Node*)(*_C_w_cur)._C_parent)->_C_data_hook; }
00284
00286 const _Node* parent() { return (_Node*)(*_C_w_cur)._C_parent; }
00288 const _Node* node() { return _C_w_cur; }
00289
00291 size_type n_children() { return _C_w_cur->_C_children.size(); }
00293 size_type n_parents() { return _C_w_cur->_C_parent ? 1 : 0; }
00294
00296 bool is_leaf() { return _C_w_cur->_C_children.empty(); }
00298 bool is_root()
00299 { return parent() != NULL && (*parent())._C_parent == NULL; }
00300
00302 bool is_ground() { return parent() == NULL; }
00304 bool is_sky() { return false; }
00305
00307 children_iterator child_begin() { return _C_w_cur->_C_children.begin(); }
00309 children_iterator child_end() { return _C_w_cur->_C_children.end(); }
00310
00312 parents_iterator parent_begin()
00313 { return parents_iterator(&_C_w_cur->_C_parent); }
00315 parents_iterator parent_end()
00316 { return parents_iterator(&_C_w_cur->_C_parent, false); }
00317
00319 template <class _Function>
00320 _Function for_each_child(_Function __f)
00321 {
00322 return for_each(child_begin(), child_end(), __f);
00323 }
00325 template <class _Function>
00326 _Function for_each_parent(_Function __f)
00327 {
00328 return for_each(parent_begin(), parent_end(), __f);
00329 }
00330
00332 template <class Compare>
00333 void sort_children(children_iterator first, children_iterator last,
00334 Compare comp)
00335 { (*_C_w_cur).sort_children(first, last, comp); }
00336
00338 template <class Compare>
00339 void sort_parents(parents_iterator first, parents_iterator last,
00340 Compare comp) {}
00341
00343 template <class Compare>
00344 void sort_children(Compare comp)
00345 { (*_C_w_cur).sort_children(child_begin(), child_end(), comp); }
00346
00348 template <class Compare>
00349 void sort_parents(Compare comp) {}
00350 };
00351
00353
00358 template <class _Tp, class _Ref, class _Ptr, class _Ctr, class _Iterator,
00359 class _Node>
00360 class _Tree_walker : public _Tree_walker_base<_Tp,_Ref,_Ptr,_Ctr,_Iterator,_Node>
00361 {
00362 public:
00363 typedef _Tree_walker<_Tp,_Tp&,_Tp*,_Ctr,_Iterator,_Node> walker;
00364 typedef _Tree_walker<_Tp,const _Tp&,const _Tp*,_Ctr,_Iterator,_Node> const_walker;
00365 typedef _Tree_walker<_Tp,_Ref,_Ptr,_Ctr,_Iterator,_Node> _Self;
00366
00367 public:
00369 struct {
00370 unsigned int order:2;
00371 unsigned int front_to_back:1;
00372 unsigned int depth_first:1;
00373 } _C_w_t;
00375 bool _C_w_in_preorder;
00377 std::vector<_Iterator> _C_w_cur_it;
00378
00379 public:
00381 _Tree_walker() : _C_w_cur_it() {_C_w_cur_it.reserve(32); }
00382
00383 private:
00385 void _find_start_node();
00387 void _find_end_node();
00389 void _find_next_preorder();
00391 void _find_next_postorder();
00393 void _find_next_any();
00395 void _find_prev_preorder();
00397 void _find_prev_postorder();
00399 void _find_prev_any();
00400
00401 public:
00406 _Tree_walker(_Node* __x, int order = (_C_W_preorder|_C_W_postorder),
00407 bool front_to_back = true, bool depth_first = true,
00408 bool find_start = true)
00409 : _C_w_cur_it() {
00410 _C_w_cur = __x;
00411 _C_w_cur_it.reserve(32);
00412 _C_w_t.order = order;
00413 _C_w_t.front_to_back = front_to_back;
00414 _C_w_t.depth_first = depth_first;
00415 if(__x->_C_parent == NULL)
00416 _C_w_in_preorder = find_start;
00417 else if(find_start)
00418 _find_start_node();
00419 else
00420 _find_end_node(); }
00421
00423 _Tree_walker(const walker& __x) : _C_w_in_preorder(__x._C_w_in_preorder),
00424 _C_w_cur_it(__x._C_w_cur_it) {
00425 _C_w_cur = __x._C_w_cur;
00426 _C_w_t = __x._C_w_t;
00427 }
00428
00430
00431 bool operator==(const _Self& __x) const
00432 { return (_C_w_t.order == 0 && __x._C_w_t.order == 0) ||
00433 (_C_w_cur == __x._C_w_cur &&
00434 _C_w_in_preorder == __x._C_w_in_preorder &&
00435 _C_w_t.order == __x._C_w_t.order &&
00436 _C_w_t.front_to_back == __x._C_w_t.front_to_back &&
00437 _C_w_t.depth_first == __x._C_w_t.depth_first &&
00438 _C_w_cur_it == __x._C_w_cur_it); }
00439 bool operator!=(const _Self& __x) const
00440 { return (_C_w_t.order != 0 || __x._C_w_t.order != 0) &&
00441 (_C_w_cur != __x._C_w_cur ||
00442 _C_w_in_preorder != __x._C_w_in_preorder ||
00443 _C_w_t.order != __x._C_w_t.order ||
00444 _C_w_t.front_to_back != __x._C_w_t.front_to_back ||
00445 _C_w_t.depth_first != __x._C_w_t.depth_first ||
00446 _C_w_cur_it != __x._C_w_cur_it); }
00448
00449 public:
00451
00452 _Self& operator++() {
00453 if(_C_w_t.depth_first)
00454 {
00455 if(_C_w_t.order == (_C_W_preorder | _C_W_postorder))
00456 {
00457 _find_next_any();
00458 }
00459 else if(_C_w_t.order & _C_W_preorder)
00460 {
00461 _find_next_preorder();
00462 }
00463 else
00464 {
00465 _find_next_postorder();
00466 }
00467 }
00468 else
00469 {
00470 ;
00471 }
00472 return *this;
00473 }
00474 _Self operator++(int) {
00475 _Self __tmp = *this;
00476 ++*this;
00477 return __tmp;
00478 }
00479
00480 _Self& operator--() {
00481 if(_C_w_t.depth_first)
00482 {
00483 if(_C_w_t.order == (_C_W_preorder | _C_W_postorder))
00484 {
00485 _find_prev_any();
00486 }
00487 else if(_C_w_t.order & _C_W_preorder)
00488 {
00489 _find_prev_preorder();
00490 }
00491 else
00492 {
00493 _find_prev_postorder();
00494 }
00495 }
00496 else
00497 {
00498 ;
00499 }
00500 return *this;
00501 }
00502 _Self operator--(int) {
00503 _Self __tmp = *this;
00504 --*this;
00505 return __tmp;
00506 }
00508
00510
00511 _Self operator<<(const parents_iterator& __dummy) {
00512 _Self __tmp = *this;
00513 _Node *help = __tmp._C_w_cur._C_parent;
00514 if(help == NULL)
00515 {
00516 __tmp._C_w_t.order = 0;
00517 }
00518 else
00519 {
00520 __tmp._C_w_cur = help;
00521 if(__tmp._C_w_t.order & _C_W_postorder)
00522 __tmp._C_w_in_preorder = false;
00523 if(!__tmp._C_w_cur_it.empty())
00524 pop_back(__tmp._C_w_cur_it);
00525 }
00526 return __tmp;
00527 }
00528
00530
00531 _Self operator>>(const children_iterator& __i) {
00532 _Self __tmp = *this;
00533 _Node *help = (_Node*) *__i;
00534 if(__tmp._C_w_t.order & _C_W_preorder)
00535 __tmp._C_w_in_preorder = true;
00536 __tmp._C_w_cur_it.push_back(__i);
00537 __tmp._C_w_cur = help;
00538 return __tmp;
00539 }
00540
00542 _Self& operator<<=(const parents_iterator& __dummy) {
00543 _Node *help = _C_w_cur._C_parent;
00544 if(help == NULL)
00545 {
00546 _C_w_t.order = 0;
00547 }
00548 else
00549 {
00550 _C_w_cur = help;
00551 if(_C_w_t.order & _C_W_postorder)
00552 _C_w_in_preorder = false;
00553 if(!_C_w_cur_it.empty())
00554 pop_back(_C_w_cur_it);
00555 }
00556 return *this;
00557 }
00558
00560 _Self& operator>>=(const children_iterator& __i) {
00561 _Node *help = (_Node*) *__i;
00562 if(_C_w_t.order & _C_W_preorder)
00563 _C_w_in_preorder = true;
00564 push_back(_C_w_cur_it, __i);
00565 _C_w_cur = help;
00566 return *this;
00567 }
00568
00570 _Self& operator~() {
00571 if(_C_w_t.order == (_C_W_preorder | _C_W_postorder))
00572 _C_w_in_preorder = !_C_w_in_preorder;
00573 return *this;
00574 }
00575
00577 _Self& operator=(const _Itr& __x) {
00578 _C_w_cur = __x._C_i_cur;
00579 _C_w_cur_it = __x._C_i_cur_it;
00580 _C_w_t.order = _C_W_postorder;
00581 _C_w_t.depth_first = 1;
00582 _C_w_t.front_to_back = 1;
00583 return *this;
00584 }
00585
00587 bool in_preorder() { return _C_w_in_preorder; }
00588 };
00589
00590 template <class _Tp, class _Ref, class _Ptr, class _Ctr, class _Iterator,
00591 class _Node>
00592 void _Tree_walker<_Tp,_Ref,_Ptr,_Ctr,_Iterator,_Node>::_find_start_node()
00593 {
00594 if(_C_w_t.depth_first)
00595 {
00596 if(_C_w_t.order & _C_W_preorder)
00597 {
00598 _C_w_in_preorder = true;
00599 }
00600 else if(_C_w_t.order & _C_W_postorder)
00601 {
00602 _C_w_in_preorder = false;
00603 if(_C_w_t.front_to_back)
00604 {
00605 while(!_C_w_cur->_C_children.empty())
00606 {
00607 _Ctr_iterator help;
00608
00609 help = _C_w_cur->_C_children.begin();
00610 _C_w_cur = (_Node*)*help;
00611 _C_w_cur_it.push_back(help);
00612 }
00613 }
00614 else
00615 {
00616 while(!_C_w_cur->_C_children.empty())
00617 {
00618 _Ctr_iterator help;
00619
00620 help = _C_w_cur->_C_children.end();
00621 --help;
00622 _C_w_cur = (_Node*)*help;
00623 _C_w_cur_it.push_back(help);
00624 }
00625 }
00626 }
00627 }
00628 else
00629 {
00630 ;
00631 }
00632 }
00633
00634 template <class _Tp, class _Ref, class _Ptr, class _Ctr, class _Iterator,
00635 class _Node>
00636 void _Tree_walker<_Tp,_Ref,_Ptr,_Ctr,_Iterator,_Node>::_find_end_node()
00637 {
00638 if(_C_w_t.depth_first)
00639 {
00640 if(_C_w_t.order & _C_W_postorder)
00641 {
00642 _C_w_in_preorder = false;
00643 (&_C_w_cur_it)->~std::vector();
00644 _C_w_cur_it.reserve(32);
00645 }
00646 else if(_C_w_t.order & _C_W_postorder)
00647 {
00648 _C_w_in_preorder = true;
00649 if(_C_w_t.front_to_back)
00650 {
00651 while(!_C_w_cur->_C_children.empty())
00652 {
00653 _Ctr_iterator help;
00654
00655 help = _C_w_cur->_C_children.begin();
00656 _C_w_cur = (_Node*)*help;
00657 _C_w_cur_it.push_back(help);
00658 }
00659 }
00660 else
00661 {
00662 while(!_C_w_cur->_C_children.empty())
00663 {
00664 _Ctr_iterator help;
00665
00666 help = _C_w_cur->_C_children.end();
00667 --help;
00668 _C_w_cur = (_Node*)*help;
00669 _C_w_cur_it.push_back(help);
00670 }
00671 }
00672 }
00673 }
00674 else
00675 {
00676 ;
00677 }
00678 }
00679
00680 template <class _Tp, class _Ref, class _Ptr, class _Ctr, class _Iterator,
00681 class _Node>
00682 void _Tree_walker<_Tp,_Ref,_Ptr,_Ctr,_Iterator,_Node>::_find_next_preorder()
00683 {
00684 _Ctr_iterator help;
00685
00686 if(_C_w_cur->_C_children.empty())
00687 {
00688 while(1)
00689 {
00690 if(_C_w_cur_it.empty())
00691 {
00692 if(_C_w_cur->_C_parent == NULL && _C_w_in_preorder)
00693 _C_w_in_preorder = false;
00694 else
00695 _C_w_t.order = 0;
00696 break;
00697 }
00698 help = _C_w_cur_it.back();
00699 _C_w_cur_it.pop_back();
00700 _C_w_cur = (_Node*)_C_w_cur->_C_parent;
00701 if(_C_w_t.front_to_back)
00702 {
00703 ++help;
00704 if(help != _C_w_cur->_C_children.end())
00705
00706 {
00707 _C_w_cur_it.push_back(help);
00708 _C_w_cur = (_Node*)*help;
00709 break;
00710 }
00711 }
00712 else
00713 {
00714 if(help != _C_w_cur->_C_children.begin())
00715
00716 {
00717 --help;
00718 _C_w_cur_it.push_back(help);
00719 _C_w_cur = (_Node*)*help;
00720 break;
00721 }
00722 }
00723 }
00724 }
00725 else if(!_C_w_in_preorder)
00726 _C_w_t.order = 0;
00727 else
00728 {
00729 if(_C_w_t.front_to_back)
00730 help = _C_w_cur->_C_children.begin();
00731 else
00732 {
00733 help = _C_w_cur->_C_children.end();
00734 help--;
00735 }
00736 _C_w_cur_it.push_back(help);
00737 _C_w_cur = (_Node*)*help;
00738 }
00739 }
00740
00741 template <class _Tp, class _Ref, class _Ptr, class _Ctr, class _Iterator,
00742 class _Node>
00743 void _Tree_walker<_Tp,_Ref,_Ptr,_Ctr,_Iterator,_Node>::_find_next_postorder()
00744 {
00745 if(_C_w_in_preorder)
00746 {
00747 _C_w_in_preorder = false;
00748 _find_start_node();
00749 }
00750 else if(_C_w_cur_it.empty())
00751 {
00752 _C_w_t.order = 0;
00753 }
00754 else
00755 {
00756 _Ctr_iterator help = _C_w_cur_it.back();
00757 _C_w_cur_it.pop_back();
00758 if(_C_w_t.front_to_back)
00759 {
00760 ++help;
00761 if(help != ((_Node*)_C_w_cur->_C_parent)->_C_children.end())
00762
00763 {
00764 do
00765 {
00766 _C_w_cur_it.push_back(help);
00767 _C_w_cur = (_Node*)*help;
00768 help = _C_w_cur->_C_children.begin();
00769 } while(!_C_w_cur->_C_children.empty());
00770 }
00771 else
00772 {
00773 _C_w_cur = (_Node*)_C_w_cur->_C_parent;
00774 }
00775 }
00776 else
00777 {
00778 if(help != ((_Node*)_C_w_cur->_C_parent)->_C_children.begin())
00779
00780 {
00781 do
00782 {
00783 --help;
00784 _C_w_cur_it.push_back(help);
00785 _C_w_cur = (_Node*)*help;
00786 help = _C_w_cur->_C_children.end();
00787 } while(!_C_w_cur->_C_children.empty());
00788 }
00789 else
00790 {
00791 _C_w_cur = (_Node*)_C_w_cur->_C_parent;
00792 }
00793 }
00794 }
00795 }
00796
00797 template <class _Tp, class _Ref, class _Ptr, class _Ctr, class _Iterator,
00798 class _Node>
00799 void _Tree_walker<_Tp,_Ref,_Ptr,_Ctr,_Iterator,_Node>::_find_next_any() {
00800 if(_C_w_in_preorder)
00801 {
00802 if(_C_w_cur->_C_children.empty())
00803 _C_w_in_preorder = false;
00804 else
00805 {
00806 _Ctr_iterator help;
00807 if(_C_w_t.front_to_back)
00808 help = _C_w_cur->_C_children.begin();
00809 else
00810 {
00811 help = _C_w_cur->_C_children.end();
00812 --help;
00813 }
00814 _C_w_cur_it.push_back(help);
00815 _C_w_cur = (_Node*)*help;
00816 }
00817 }
00818 else
00819 {
00820 _Ctr_iterator help;
00821
00822 if(!_C_w_cur_it.empty())
00823 {
00824 help = _C_w_cur_it.back();
00825 _C_w_cur_it.pop_back();
00826 if(_C_w_t.front_to_back)
00827 {
00828 ++help;
00829 if(help != ((_Node*)_C_w_cur->_C_parent)->_C_children.end())
00830
00831 {
00832 _C_w_cur_it.push_back(help);
00833 _C_w_cur = (_Node*)*help;
00834 _C_w_in_preorder = true;
00835 }
00836 else
00837 {
00838 _C_w_cur = (_Node*)_C_w_cur->_C_parent;
00839 }
00840 }
00841 else
00842 {
00843 if(help != ((_Node*)_C_w_cur->_C_parent)->_C_children.begin())
00844
00845 {
00846 --help;
00847 _C_w_cur_it.push_back(help);
00848 _C_w_cur = (_Node*)*help;
00849 _C_w_in_preorder = true;
00850 }
00851 else
00852 {
00853 _C_w_cur = (_Node*)_C_w_cur->_C_parent;
00854 }
00855 }
00856 }
00857 else
00858 {
00859 _C_w_t.order = 0;
00860 }
00861 }
00862 }
00863
00864 template <class _Tp, class _Ref, class _Ptr, class _Ctr, class _Iterator,
00865 class _Node>
00866 void _Tree_walker<_Tp,_Ref,_Ptr,_Ctr,_Iterator,_Node>::_find_prev_preorder()
00867 {
00868 if(!_C_w_in_preorder)
00869 {
00870 _C_w_t.order = _C_W_postorder;
00871 _C_w_t.front_to_back = !_C_w_t.front_to_back;
00872 _find_start_node();
00873 _C_w_in_preorder = true;
00874 _C_w_t.order = _C_W_preorder;
00875 _C_w_t.front_to_back = !_C_w_t.front_to_back;
00876 }
00877 else if(_C_w_cur_it.empty())
00878 {
00879 _C_w_t.order = 0;
00880 }
00881 else
00882 {
00883 _Ctr_iterator help = _C_w_cur_it.back();
00884 _C_w_cur_it.pop_back();
00885 if(!_C_w_t.front_to_back)
00886 {
00887 ++help;
00888 if(help != ((_Node*)_C_w_cur->_C_parent)->_C_children.end())
00889
00890 {
00891 do
00892 {
00893 _C_w_cur_it.push_back(help);
00894 _C_w_cur = (_Node*)*help;
00895 help = _C_w_cur->_C_children.begin();
00896 } while(!_C_w_cur->_C_children.empty());
00897 }
00898 else
00899 {
00900 _C_w_cur = (_Node*)_C_w_cur->_C_parent;
00901 }
00902 }
00903 else
00904 {
00905 if(help != ((_Node*)_C_w_cur->_C_parent)->_C_children.begin())
00906
00907 {
00908 do
00909 {
00910 --help;
00911 _C_w_cur_it.push_back(help);
00912 _C_w_cur = (_Node*)*help;
00913 help = _C_w_cur->_C_children.end();
00914 } while(!_C_w_cur->_C_children.empty());
00915 }
00916 else
00917 {
00918 _C_w_cur = (_Node*)_C_w_cur->_C_parent;
00919 }
00920 }
00921 }
00922 }
00923
00924 template <class _Tp, class _Ref, class _Ptr, class _Ctr, class _Iterator,
00925 class _Node>
00926 void _Tree_walker<_Tp,_Ref,_Ptr,_Ctr,_Iterator,_Node>::_find_prev_postorder()
00927 {
00928 _Ctr_iterator help;
00929
00930 if(_C_w_cur->_C_children.empty())
00931 {
00932 while(1)
00933 {
00934 if(_C_w_cur_it.empty())
00935 {
00936 if(_C_w_cur->_C_parent == NULL && !_C_w_in_preorder)
00937 _C_w_in_preorder = true;
00938 else
00939 _C_w_t.order = 0;
00940 break;
00941 }
00942 help = _C_w_cur_it.back();
00943 _C_w_cur_it.pop_back();
00944 _C_w_cur = (_Node*)_C_w_cur->_C_parent;
00945 if(!_C_w_t.front_to_back)
00946 {
00947 ++help;
00948 if(help != _C_w_cur->_C_children.end())
00949
00950 {
00951 _C_w_cur_it.push_back(help);
00952 _C_w_cur = (_Node*)*help;
00953 break;
00954 }
00955 }
00956 else
00957 {
00958 if(help != _C_w_cur->_C_children.begin())
00959
00960 {
00961 --help;
00962 _C_w_cur_it.push_back(help);
00963 _C_w_cur = (_Node*)*help;
00964 break;
00965 }
00966 }
00967 }
00968 }
00969 else if(_C_w_in_preorder)
00970 _C_w_t.order = 0;
00971 else
00972 {
00973 if(!_C_w_t.front_to_back)
00974 help = _C_w_cur->_C_children.begin();
00975 else
00976 {
00977 help = _C_w_cur->_C_children.end();
00978 help--;
00979 }
00980 _C_w_cur_it.push_back(help);
00981 _C_w_cur = (_Node*)*help;
00982 }
00983 }
00984
00985 template <class _Tp, class _Ref, class _Ptr, class _Ctr, class _Iterator,
00986 class _Node>
00987 void _Tree_walker<_Tp,_Ref,_Ptr,_Ctr,_Iterator,_Node>::_find_prev_any()
00988 {
00989 if(!_C_w_in_preorder)
00990 {
00991 if(_C_w_cur->_C_children.empty())
00992 _C_w_in_preorder = true;
00993 else
00994 {
00995 _Ctr_iterator help;
00996 if(!_C_w_t.front_to_back)
00997 help = _C_w_cur->_C_children.begin();
00998 else
00999 {
01000 help = _C_w_cur->_C_children.end();
01001 --help;
01002 }
01003 _C_w_cur_it.push_back(help);
01004 _C_w_cur = (_Node*)*help;
01005 }
01006 }
01007 else
01008 {
01009 _Ctr_iterator help;
01010
01011 if(!_C_w_cur_it.empty())
01012 {
01013 help = _C_w_cur_it.back();
01014 _C_w_cur_it.pop_back();
01015 if(!_C_w_t.front_to_back)
01016 {
01017 ++help;
01018 if(help != ((_Node*)_C_w_cur->_C_parent)->_C_children.end())
01019
01020 {
01021 _C_w_cur_it.push_back(help);
01022 _C_w_cur = (_Node*)*help;
01023 _C_w_in_preorder = false;
01024 }
01025 else
01026 {
01027 _C_w_cur = (_Node*)_C_w_cur->_C_parent;
01028 }
01029 }
01030 else
01031 {
01032 if(help != ((_Node*)_C_w_cur->_C_parent)->_C_children.begin())
01033
01034 {
01035 --help;
01036 _C_w_cur_it.push_back(help);
01037 _C_w_cur = (_Node*)*help;
01038 _C_w_in_preorder = false;
01039 }
01040 else
01041 {
01042 _C_w_cur = (_Node*)_C_w_cur->_C_parent;
01043 }
01044 }
01045 }
01046 else
01047 {
01048 _C_w_t.order = 0;
01049 }
01050 }
01051 }
01052
01054
01059 template <class _Tp, class _Ref, class _Ptr, class _Ctr, class _Iterator,
01060 class _Node>
01061 class _RTree_walker : public _Tree_walker_base<_Tp,_Ref,_Ptr,_Ctr,_Iterator,_Node>
01062 {
01063 public:
01064 typedef _RTree_walker<_Tp,_Tp&,_Tp*,_Ctr,_Iterator,_Node> walker;
01065 typedef _RTree_walker<_Tp,const _Tp&,const _Tp*,_Ctr,_Iterator,_Node> const_walker;
01066 typedef _RTree_walker<_Tp,_Ref,_Ptr,_Ctr,_Iterator,_Node> _Self;
01067
01068 public:
01070 _RTree_walker() {}
01071
01073 _RTree_walker(_Node* __x) { _C_w_cur = __x; }
01074
01076 _RTree_walker(const walker& __x) { _C_w_cur = __x._C_w_cur; }
01077
01078 public:
01080
01081 bool operator==(const _Self& __x) const
01082 { return _C_w_cur == __x._C_w_cur; }
01083 bool operator!=(const _Self& __x) const
01084 { return _C_w_cur != __x._C_w_cur; }
01086
01088
01089 _Self operator<<(const parents_iterator& __dummy) {
01090 _Self __tmp = *this;
01091 _Node *help = __tmp._C_w_cur._C_parent;
01092 if(help != NULL)
01093 __tmp._C_w_cur = help;
01094 return __tmp;
01095 }
01096
01098
01099 _Self operator>>(const children_iterator& __i) {
01100 _Self __tmp = *this;
01101 __tmp._C_w_cur = (_Node*) *__i;
01102 return __tmp;
01103 }
01104
01106 _Self& operator<<=(const parents_iterator& __dummy) {
01107 _Node *help = _C_w_cur._C_parent;
01108 if(help != NULL)
01109 _C_w_cur = help;
01110 return *this;
01111 }
01112
01114 _Self& operator>>=(const children_iterator& __i) {
01115 _C_w_cur = (_Node*) *__i;
01116 return *this;
01117 }
01118
01120 _Self& operator=(const _Itr& __x) {
01121 _C_w_cur = __x._C_i_cur;
01122 return *this;
01123 }
01124
01126 _Self& operator=(const _Tree_walker<_Tp,_Ref,_Ptr,_Ctr,_Iterator,_Node>& __x)
01127 {
01128 _C_w_cur = __x._C_w_cur;
01129 return *this;
01130 }
01131 };
01132
01134
01139 template <class _Tp, class _Ref, class _Ptr, class _Ctr, class _Iterator,
01140 class _Node>
01141 class _Tree_iterator
01142 {
01143 public:
01144 typedef _Tree_iterator<_Tp,_Tp&,_Tp*,_Ctr,_Iterator,_Node> iterator;
01145 typedef _Tree_iterator<_Tp,const _Tp&,const _Tp*,_Ctr,_Iterator,_Node> const_iterator;
01146 typedef _Tree_iterator<_Tp,_Ref,_Ptr,_Ctr,_Iterator,_Node> _Self;
01147 typedef _Tree_walker<_Tp,_Ref,_Ptr,_Ctr,_Iterator,_Node> _Walk;
01148
01150
01151 typedef std::bidirectional_iterator_tag iterator_category;
01152 typedef _Tp value_type;
01153 typedef _Ptr pointer;
01154 typedef _Ref reference;
01155 typedef size_t size_type;
01156 typedef ptrdiff_t difference_type;
01158 typedef _Iterator _Ctr_iterator;
01159
01160 protected:
01162 _Node* _C_i_cur;
01164 std::vector<_Ctr_iterator> _C_i_cur_it;
01165
01166 public:
01168 _Tree_iterator() : _C_i_cur_it() {_C_i_cur_it.reserve(32);}
01170 _Tree_iterator(const iterator& __x) : _C_i_cur(__x._C_i_cur),
01171 _C_i_cur_it(__x._C_i_cur_it) {}
01173 _Tree_iterator(const _Node* __n, bool st = false) : _C_i_cur_it()
01174 { _C_i_cur = __n; if(st) find_start_node(); }
01175
01177
01178 bool operator==(const _Self& __x) const
01179 { return (_C_i_cur->_C_parent == NULL &&
01180 __x._C_i_cur->_C_parent == NULL) ||
01181 ( _C_i_cur == __x._C_i_cur &&
01182 _C_i_cur_it == __x._C_i_cur_it);
01183 }
01184 bool operator!=(const _Self& __x) const
01185 { return (_C_i_cur->_C_parent != NULL ||
01186 __x._C_i_cur->_C_parent != NULL) &&
01187 ( _C_i_cur != __x._C_i_cur ||
01188 _C_i_cur_it != __x._C_i_cur_it);
01189 }
01191
01192 reference operator*() const { return (*_C_i_cur)._C_data; }
01193
01194 #ifndef __SGI_STL_NO_ARROW_OPERATOR
01195
01196 pointer operator->() const { return &(operator*()); }
01197 #endif
01198
01199 ctree_data_hook& data_hook() { return (*_C_i_cur)._C_data_hook; }
01200
01201 private:
01203 void find_start_node();
01205 void find_next_node();
01207 void find_prev_node();
01208
01209 public:
01211 _Self& operator=(const _Walk& __x) {
01212 _C_i_cur = __x._C_w_cur;
01213 _C_i_cur_it.erase(_C_i_cur_it.begin(), _C_i_cur_it.end());
01214
01215
01216 find_start_node();
01217 return *this;
01218 }
01219
01221
01222 _Self& operator++() {
01223 find_next_node();
01224 return *this;
01225 }
01226 _Self operator++(int) {
01227 _Self __tmp = *this;
01228 ++*this;
01229 return __tmp;
01230 }
01231
01232 _Self& operator--() {
01233 find_prev_node();
01234 return *this;
01235 }
01236 _Self operator--(int) {
01237 _Self __tmp = *this;
01238 --*this;
01239 return __tmp;
01240 }
01242 };
01243
01244 #ifndef __VGTL_CLASS_PARTIAL_SPECIALIZATION
01245 template <class _Tp, class _Ref, class _Ptr, class _Ctr, class _TI, class _TN>
01246 inline std::bidirectional_iterator_tag
01247 iterator_category(const _Tree_iterator<_Tp,_Ref,_Ptr,_Ctr,_TI,_TN>&)
01248 {
01249 return std::bidirectional_iterator_tag();
01250 }
01251
01252 template <class _Tp, class _Ref, class _Ptr, class _Ctr, class _TI, class _TN>
01253 inline _Tp*
01254 value_type(const _Tree_iterator<_Tp,_Ref,_Ptr,_Ctr,_TI,_TN>&)
01255 {
01256 return 0;
01257 }
01258
01259 template <class _Tp, class _Ref, class _Ptr, class _Ctr, class _TI, class _TN>
01260 inline ptrdiff_t*
01261 distance_type(const _Tree_iterator<_Tp,_Ref,_Ptr,_Ctr,_TI,_TN>&)
01262 {
01263 return 0;
01264 }
01265
01266 #endif
01267
01268 template <class _Tp, class _Ref, class _Ptr, class _Ctr, class _Iterator,
01269 class _Node>
01270 void
01271 _Tree_iterator<_Tp,_Ref,_Ptr,_Ctr,_Iterator,_Node>::find_start_node()
01272 {
01273 _Node* __s = _C_i_cur;
01274 _Ctr_iterator __help;
01275
01276 while(__s->_C_parent != NULL)
01277 {
01278 __help = ((_Node*)__s->_C_parent)->get_childentry_iterator((void*)__s);
01279 _C_i_cur_it.insert(_C_i_cur_it.begin(), __help);
01280 __s = (_Node*)__s->_C_parent;
01281 }
01282 while(!_C_i_cur->_C_children.empty())
01283 {
01284 __help = _C_i_cur->_C_children.begin();
01285 _C_i_cur = (_Node*)*__help;
01286 _C_i_cur_it.push_back(__help);
01287 }
01288 }
01289
01290 template <class _Tp, class _Ref, class _Ptr, class _Ctr, class _Iterator,
01291 class _Node>
01292 void
01293 _Tree_iterator<_Tp,_Ref,_Ptr,_Ctr,_Iterator,_Node>::find_next_node()
01294 {
01295 if(_C_i_cur->_C_parent != NULL)
01296 {
01297 _Ctr_iterator __help = _C_i_cur_it.back();
01298 _C_i_cur_it.pop_back();
01299 ++__help;
01300 if(__help != ((_Node*)_C_i_cur->_C_parent)->_C_children.end())
01301 {
01302 do
01303 {
01304 _C_i_cur_it.push_back(__help);
01305 _C_i_cur = (_Node*)*__help;
01306 __help = _C_i_cur->_C_children.begin();
01307 } while(!_C_i_cur->_C_children.empty());
01308 }
01309 else
01310 {
01311 _C_i_cur = (_Node*)_C_i_cur->_C_parent;
01312 }
01313 }
01314 }
01315
01316 template <class _Tp, class _Ref, class _Ptr, class _Ctr, class _Iterator,
01317 class _Node>
01318 void
01319 _Tree_iterator<_Tp,_Ref,_Ptr,_Ctr,_Iterator,_Node>::find_prev_node()
01320 {
01321 _Ctr_iterator help;
01322
01323 if(_C_i_cur->_C_children.empty())
01324 {
01325 while(1)
01326 {
01327 if(_C_i_cur->_C_parent == NULL)
01328 break;
01329 help = _C_i_cur_it.back();
01330 _C_i_cur_it.pop_back();
01331 _C_i_cur = (_Node*)_C_i_cur->_C_parent;
01332 if(help != _C_i_cur->_C_children.begin())
01333 {
01334 --help;
01335 _C_i_cur_it.push_back(help);
01336 _C_i_cur = (_Node*)*help;
01337 break;
01338 }
01339 }
01340 }
01341 else
01342 {
01343 help = _C_i_cur->_C_children.end();
01344 --help;
01345 _C_i_cur_it.push_back(help);
01346 _C_i_cur = (_Node*)*help;
01347 }
01348 }
01349
01350 #ifdef __VGTL_USE_STD_ALLOCATORS
01351
01353
01363 template <class _Tp, class _Ctr, class _TI, class _Node, class _Allocator,
01364 bool _IsStatic>
01365 class _Tree_alloc_base {
01366 public:
01367 typedef typename std::_Alloc_traits<_Tp, _Allocator>::allocator_type
01368 allocator_type;
01369 allocator_type get_allocator() const { return _Node_allocator; }
01370
01371 _Tree_alloc_base(const allocator_type& __a) : _Node_allocator(__a) {}
01372
01373 protected:
01375 _Node* _C_get_node()
01376 { return _Node_allocator.allocate(1); }
01378 void _C_put_node(_Node* __p)
01379 { _Node_allocator.deallocate(__p, 1); }
01380
01381 protected:
01382 typename std::_Alloc_traits<_Node, _Allocator>::allocator_type
01383 _Node_allocator;
01384
01385 protected:
01387 _Node* _C_node;
01388 };
01389
01391
01401 template <class _Tp, class _Ctr, class _TI, class _Node, class _Allocator>
01402 class _Tree_alloc_base<_Tp, _Ctr, _TI, _Node, _Allocator, true> {
01403 public:
01404 typedef typename std::_Alloc_traits<_Tp, _Allocator>::allocator_type
01405 allocator_type;
01406 allocator_type get_allocator() const { return allocator_type(); }
01407
01408 _Tree_alloc_base(const allocator_type&) {}
01409
01410 protected:
01411 typedef typename std::_Alloc_traits<_Node, _Allocator>::_Alloc_type
01412 _Alloc_type;
01414 _Node* _C_get_node()
01415 { return _Alloc_type::allocate(1); }
01417 void _C_put_node(_Node* __p)
01418 { _Alloc_type::deallocate(__p, 1); }
01419
01420 protected:
01422 _Node* _C_node;
01423 };
01424
01426
01430 template <class _Tp, class _Ctr, class _TI, class _Node, class _Alloc>
01431 class _Tree_base
01432 : public _Tree_alloc_base<_Tp, _Ctr, _TI, _Node, _Alloc,
01433 std::_Alloc_traits<_Tp, _Alloc>::_S_instanceless>
01434 {
01435 public:
01436 typedef _Tree_alloc_base<_Tp, _Ctr, _TI, _Node, _Alloc,
01437 std::_Alloc_traits<_Tp, _Alloc>::_S_instanceless>
01438 _Base;
01440 typedef typename _Base::allocator_type allocator_type;
01441
01443 typedef _Ctr container_type;
01445 typedef _TI children_iterator;
01447 typedef __one_iterator<void *> parents_iterator;
01448
01450 _Tree_base(const allocator_type& __a) : _Base(__a) {
01451 _C_node = _C_get_node();
01452 __VGTL_TRY {
01453 _C_node->initialize();
01454 }
01455 __VGTL_UNWIND(_C_put_node(_C_node));
01456 }
01458 virtual ~_Tree_base() {
01459 clear();
01460 _C_put_node(_C_node);
01461 }
01462
01464 void clear();
01466 void clear_children()
01467 { _C_node->clear_children(); }
01468
01471 template <class _Output_Iterator>
01472 void add_all_children(_Output_Iterator fi, _Node* _parent);
01473 };
01474 #else
01475
01477
01481 template <class _Tp, class _Ctr, class _Iterator, class _Node, class _Alloc>
01482 class _Tree_base
01483 {
01484 public:
01486 typedef _Alloc allocator_type;
01488 allocator_type get_allocator() const { return allocator_type(); }
01489
01491 typedef _Ctr container_type;
01493 typedef _Iterator children_iterator;
01495 typedef __one_iterator<void *> parents_iterator;
01496
01498 _Tree_base(const allocator_type&) {
01499 _C_node = _C_get_node();
01500 _C_node->initialize();
01501 }
01503 virtual ~_Tree_base() {
01504 clear();
01505 _C_put_node(_C_node);
01506 }
01507
01509 void clear();
01510
01511 protected:
01512 typedef simple_alloc<Node, _Alloc> _Alloc_type;
01514 _Node* _C_get_node()
01515 { return _Alloc_type::allocate(1); }
01517 void _C_put_node(_Node* __p)
01518 { _Alloc_type::deallocate(__p, 1); }
01519
01520 protected:
01522 _Node* _C_node;
01523
01525 void clear_children()
01526 { _C_node->clear_children(); }
01527
01530 template <class _Output_Iterator>
01531 void add_all_children(_Output_Iterator fi, _Node* _parent);
01532 };
01533
01534 #endif
01535
01536 template <class _Tp, class _Ctr, class _Iterator, class _Node, class _Alloc>
01537 template <class _Output_Iterator>
01538 inline void
01539 _Tree_base<_Tp,_Ctr,_Iterator,_Node,_Alloc>::add_all_children(
01540 _Output_Iterator fi, _Node* _parent)
01541 { _C_node->add_all_children(fi, _parent); };
01542
01543 template <class _Tp, class _Ctr, class _Iterator, class _Node, class _Alloc>
01544 void
01545 _Tree_base<_Tp,_Ctr,_Iterator,_Node,_Alloc>::clear()
01546 {
01547 this->_C_node->clear_tree();
01548 this->_C_node->clear_children();
01549 }
01550
01552
01557 template <class _Tp, class _Ctr, class _Iterator, class _Inserter, class _Node,
01558 class _Alloc>
01559 class __Tree_t : public _Tree_base<_Tp, _Ctr, _Iterator, _Node, _Alloc>
01560 {
01561 public:
01562 typedef _Ctr container_type;
01563 typedef _Iterator children_iterator;
01564 typedef __one_iterator<void *> parents_iterator;
01565 typedef _Inserter container_insert_arg;
01566
01567 protected:
01568 typedef void* _Void_pointer;
01569 typedef _Tree_base<_Tp, container_type, children_iterator, _Node, _Alloc> _Base;
01570 typedef __Tree_t<_Tp,_Ctr,_Iterator,_Inserter,_Node,_Alloc> _Self;
01571
01572 public:
01574
01575 typedef _Tp value_type;
01576 typedef _Node node_type;
01577 typedef value_type* pointer;
01578 typedef const value_type* const_pointer;
01579 typedef value_type& reference;
01580 typedef const value_type& const_reference;
01581 typedef size_t size_type;
01582 typedef ptrdiff_t difference_type;
01584
01585 typedef typename _Base::allocator_type allocator_type;
01587 allocator_type get_allocator() const { return _Base::get_allocator(); }
01588
01589 public:
01591 typedef _Tree_iterator<_Tp,_Tp&,_Tp*,container_type,children_iterator,node_type> iterator;
01593 typedef _Tree_iterator<_Tp,const _Tp&,const _Tp*,container_type,children_iterator,node_type> const_iterator;
01594
01595 #ifdef __VGTL_CLASS_PARTIAL_SPECIALIZATION
01596
01597 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
01599 typedef std::reverse_iterator<iterator> reverse_iterator;
01600 #else
01601
01602 typedef std::reverse_bidirectional_iterator<const_iterator,value_type,
01603 const_reference,difference_type>
01604 const_reverse_iterator;
01606 typedef std::reverse_bidirectional_iterator<iterator,value_type,reference,
01607 difference_type>
01608 reverse_iterator;
01609 #endif
01610
01612 typedef _RTree_walker<_Tp,_Tp&,_Tp*,container_type,children_iterator,node_type> walker;
01614 typedef _RTree_walker<_Tp,const _Tp&,const _Tp*,container_type,children_iterator,node_type> const_walker;
01615
01616 protected:
01617 typedef _Tree_walker_base<_Tp,_Tp&,_Tp*,container_type,children_iterator,node_type> __walker_base;
01618 typedef _Tree_walker_base<_Tp,const _Tp&,const _Tp*,container_type,children_iterator,node_type> __const_walker_base;
01619
01620 protected:
01621 #ifdef __VGTL_HAS_NAMESPACES
01622 using _Base::_C_node;
01623 using _Base::_C_put_node;
01624 using _Base::_C_get_node;
01625 #endif
01626
01627 protected:
01629 _Node* _C_create_node(const _Tp& __x)
01630 {
01631 _Node* __p = _C_get_node();
01632 __VGTL_TRY {
01633 __p->initialize();
01634 std::_Construct(&__p->_C_data, __x);
01635 }
01636 __VGTL_UNWIND(_C_put_node(__p));
01637 return __p;
01638 }
01639
01641 _Node* _C_create_node()
01642 {
01643 _Node* __p = _C_get_node();
01644 __VGTL_TRY {
01645 __p->initialize();
01646 std::_Construct(&__p->_C_data);
01647 }
01648 __VGTL_UNWIND(_C_put_node(__p));
01649 return __p;
01650 }
01651
01652 public:
01654 explicit __Tree_t(const allocator_type& __a = allocator_type()) : _Base(__a) {}
01655
01657 bool empty() const { return _C_node->_C_children.empty(); }
01658
01660 size_type max_size() const { return size_type(-1); }
01661
01663 void swap(_Self& __x)
01664 { __STD::swap(_C_node, __x._C_node); }
01665
01668 void insert_child(const __walker_base& __position, const _Tp& __x,
01669 const container_insert_arg& __It)
01670 { _Node* __tmp = _C_create_node(__x);
01671 __position._C_w_cur->_C_children.insert(__It,__tmp);
01672 __tmp->_C_parent = __position._C_w_cur;
01673 }
01676 void insert_child(const __walker_base& __position,
01677 const container_insert_arg& __It)
01678 { insert_child(__position, _Tp(), __It); }
01679
01682 void insert_children(const __walker_base& __position, size_type __n,
01683 const _Tp& __x, const children_iterator& __It)
01684 { _Node* __tmp;
01685 std::vector<_Node *> __Ctr;
01686 __Ctr.reserve(__n);
01687
01688 for(int i=n; i>0; i--)
01689 {
01690 __tmp = _C_create_node(__x);
01691 __Ctr.insert(__Ctr.end(), __tmp);
01692 __tmp->_C_parent = __position._C_w_cur;
01693 }
01694 __position._C_w_cur->_C_children.insert(__It,
01695 __Ctr.begin(), __Ctr.end());
01696 }
01697
01702 void insert_subtree(const __walker_base& __position,
01703 _Self& __subtree, const children_iterator& __It)
01704 {
01705 __subtree.add_all_children(inserter(__position._C_w_cur->_C_children,
01706 __It), __position._C_w_cur);
01707 __subtree.clear_children();
01708 }
01709
01713 void erase(const __walker_base& __position)
01714 { _Node* __tmp = __position._C_w_cur;
01715 _Node* __parent = (_Node*)__tmp->_C_parent;
01716
01717 if(__parent != NULL)
01718 {
01719 children_iterator __ip = __parent->get_childentry_iterator(__tmp);
01720
01721 if(!__tmp->_C_children.empty())
01722 {
01723 children_iterator __it = __tmp->_C_children.end();
01724
01725 *__ip = *--__it;
01726 ((_Node*)*__it)->_C_parent = __parent;
01727 __tmp->_C_children.erase(__it);
01728 if(!__tmp->_C_children.empty())
01729 {
01730 __tmp->add_all_children(inserter(__parent->_C_children, __ip),
01731 __parent);
01732 __tmp->clear_children();
01733 }
01734 }
01735 _C_put_node(__tmp);
01736 }
01737 }
01738
01743 _Node* erase_tree(const __walker_base& __position)
01744 { _Node* __tmp = __position._C_w_cur;
01745 _Node* __parent = (_Node*)__tmp->_C_parent;
01746
01747 if(__parent == NULL)
01748 {
01749 this->_C_node = _C_create_node();
01750 return __tmp;
01751 }
01752 else
01753 {
01754 children_iterator __ip = __parent->get_childentry_iterator(__tmp);
01755
01756 __parent->_C_children.erase(__ip);
01757 __parent = _C_create_node();
01758 __parent->_C_data = 0;
01759 __parent->_C_children.insert(__parent->_C_children.end(), __tmp);
01760 __tmp->_C_parent = __parent;
01761 return __parent;
01762 }
01763 }
01764
01765
01770 bool erase_child(const __walker_base& __position,
01771 const children_iterator& __It)
01772 {
01773 _Node* __chld = (_Node*)*__It;
01774
01775 if(!__chld->_C_children.empty())
01776 { return false; }
01777 else
01778 {
01779 __position->_C_w_cur->_C_children.erase(__It);
01780 _C_put_node(__chld);
01781 return true;
01782 }
01783 }
01784
01790 _Node* erase_subtree(const __walker_base& __position,
01791 const children_iterator& __It)
01792 { _Node* __chld = (_Node*)*__It;
01793 _Node* __tmp = _C_create_node();
01794
01795 __position->_C_w_cur->_C_children.erase(__It);
01796 __chld->_C_parent = __tmp;
01797 __tmp->_C_data = 0;
01798 __tmp->_C_children.insert(__tmp->_C_children.end(), __chld);
01799 return __tmp;
01800 }
01801
01805 size_type depth(const walker& __position)
01806 { size_type __d=0;
01807 _Node* __x = __position._C_w_cur;
01808 while(__x != NULL)
01809 {
01810 __x = (_Node*)__x._C_parent;
01811 __d++;
01812 }
01813 return __d;
01814 }
01815
01817 void clear() { _Base::clear(); }
01818
01823 __Tree_t(size_type __n, const _Tp& __value,
01824 const allocator_type& __a = allocator_type()) : _Base(__a)
01825 { insert(root(), __n, __value); }
01830 explicit __Tree_t(size_type __n) : _Base(allocator_type())
01831 { insert(root(), __n, _Tp()); }
01832
01833 private:
01835 _Node* copy_subtree(_Node* __x, _Node* __parent)
01836 { children_iterator cit;
01837
01838 _Node* __h = _C_create_node();
01839 std::_Construct(&__h->_C_data, __x->_C_data);
01840 __h->_C_parent = (void*)__parent;
01841 for(cit = __x->_C_children.begin(); cit != __x->_C_children.end(); ++cit)
01842 __h->_C_children.insert(__h->_C_children.end(),
01843 copy_subtree((_Node*)*cit, __h));
01844 return __h;
01845 }
01846
01847 public:
01849 __Tree_t(const _Self& __x) : _Base(__x.get_allocator())
01850 { children_iterator cit;
01851 for(cit = __x._C_node->_C_children.begin();
01852 cit != __x._C_node->_C_children.end(); ++cit)
01853 this->_C_node->_C_children.insert(this->_C_node->_C_children.end(),
01854 copy_subtree((_Node*)*cit, this->_C_node));
01855 }
01856
01858 virtual ~__Tree_t() {}
01859
01861 _Self& operator=(const _Self& __x);
01862
01867 _Self& operator=(_Node* __x)
01868 {
01869 this->_C_node = __x;
01870 return *this;
01871 }
01872
01873 #if 0
01874 friend bool operator== __VGTL_NULL_TMPL_ARGS (
01875 const __Tree_t& __x, const __Tree_t& __y);
01876 #endif
01877 };
01878
01880
01884 template <class _Tp, class _Ctr, class _Iterator, class _Inserter, class _Alloc>
01885 class __Tree : public __Tree_t<_Tp, _Ctr, _Iterator, _Inserter,
01886 _Tree_node<_Tp,_Ctr,_Iterator>, _Alloc>
01887 {
01888 protected:
01889 typedef void* _Void_pointer;
01890 typedef _Tree_node<_Tp, container_type, children_iterator> _Node;
01891 typedef __Tree_t<_Tp, container_type, children_iterator, _Inserter, _Node, _Alloc> _Base;
01892 typedef __Tree<_Tp,_Ctr,_Iterator,_Inserter,_Alloc> _Self;
01893
01894 public:
01895 typedef _Node node_type;
01896
01897 public:
01899 typedef _Tree_iterator<_Tp,_Tp&,_Tp*,container_type,children_iterator,node_type> iterator;
01901 typedef _Tree_iterator<_Tp,const _Tp&,const _Tp*,container_type,children_iterator,node_type> const_iterator;
01902
01903 #ifdef __VGTL_CLASS_PARTIAL_SPECIALIZATION
01904
01905 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
01907 typedef std::reverse_iterator<iterator> reverse_iterator;
01908 #else
01909
01910 typedef std::reverse_bidirectional_iterator<const_iterator,value_type,
01911 const_reference,difference_type>
01912 const_reverse_iterator;
01914 typedef std::reverse_bidirectional_iterator<iterator,value_type,reference,
01915 difference_type>
01916 reverse_iterator;
01917 #endif
01918
01919 protected:
01920 typedef _Tree_walker_base<_Tp,_Tp&,_Tp*,container_type,children_iterator,node_type> __walker_base;
01921 typedef _Tree_walker_base<_Tp,const _Tp&,const _Tp*,container_type,children_iterator,node_type> __const_walker_base;
01922
01923 protected:
01924 #ifdef __VGTL_HAS_NAMESPACES
01925 using _Base::_C_node;
01926 using _Base::_C_put_node;
01927 using _Base::_C_get_node;
01928 #endif
01929
01930 public:
01932 explicit __Tree(const allocator_type& __a = allocator_type()) : _Base(__a) {}
01933
01934
01935
01936
01937
01939 walker ground()
01940 { walker __help(_C_node); return __help; }
01941
01943 const_walker ground() const
01944 { const_walker __help(_C_node); return __help; }
01945
01947 walker root(children_iterator __it)
01948 { walker __help = root();
01949 __help >>= __it;
01950 return __help; }
01952 const_walker root(children_iterator __it) const
01953 { const_walker __help = root();
01954 __help >>= __it;
01955 return __help; }
01957 walker root()
01958 { return root(_C_node->child_begin()); }
01960 const_walker root() const
01961 { return root(_C_node->child_begin()); }
01962
01964 iterator begin()
01965 { iterator __help(_C_node, true);
01966 return __help; }
01968 iterator end()
01969 { iterator __help(_C_node);
01970 return __help; }
01971
01973 const_iterator begin() const
01974 { const_iterator __help(_C_node, true);
01975 return __help; }
01977 const_iterator end() const
01978 { const_iterator __help(_C_node);
01979 return __help; }
01980
01982 reverse_iterator rbegin()
01983 { return reverse_iterator(end()); }
01985 reverse_iterator rend()
01986 { return reverse_iterator(begin()); }
01987
01989 const_reverse_iterator rbegin() const
01990 { return const_reverse_iterator(end()); }
01992 const_reverse_iterator rend() const
01993 { return const_reverse_iterator(begin()); }
01994
01996 reference getroot() { return *ground(); }
01998 const_reference getroot() const { return *ground(); }
01999
02004 __Tree(size_type __n, const _Tp& __value,
02005 const allocator_type& __a = allocator_type()) : _Base(__a)
02006 { insert(ground(), __n, __value); }
02011 explicit __Tree(size_type __n) : _Base(allocator_type())
02012 { insert(ground(), __n, _Tp()); }
02013
02014 public:
02016 __Tree(const _Self& __x) : _Base(__x) {}
02017
02019 virtual ~__Tree() {}
02020
02022 _Self& operator=(const _Self& __x);
02023
02028 _Self& operator=(_Node* __x)
02029 {
02030 this->_C_node = __x;
02031 return *this;
02032 }
02033
02035 friend bool operator== __VGTL_NULL_TMPL_ARGS (
02036 const __Tree& __x, const __Tree& __y);
02037 };
02038
02040
02044 template <class _Tp, class _Ctr, class _Iterator, class _Inserter, class _Alloc>
02045 class __ITree : public __Tree_t<_Tp, _Ctr, _Iterator, _Inserter,
02046 _ITree_node<_Tp,_Ctr,_Iterator>, _Alloc>
02047 {
02048 protected:
02049 typedef void* _Void_pointer;
02050 typedef _ITree_node<_Tp, container_type, children_iterator> _Node;
02051 typedef __Tree_t<_Tp,container_type,children_iterator,_Inserter,_Node,_Alloc> _Base;
02052 typedef __ITree<_Tp,_Ctr,_Iterator,_Inserter,_Alloc> _Self;
02053
02054 public:
02055 typedef _Node node_type;
02056
02058 typedef _Tree_iterator<_Tp,_Tp&,_Tp*,container_type,children_iterator,node_type> iterator;
02060 typedef _Tree_iterator<_Tp,const _Tp&,const _Tp*,container_type,children_iterator,node_type> const_iterator;
02061
02063 typedef _Tree_walker<_Tp,_Tp&,_Tp*,container_type,children_iterator,_Node> iterative_walker;
02065 typedef _Tree_walker<_Tp,const _Tp&,const _Tp*,container_type,children_iterator,_Node> const_iterative_walker;
02066
02067 #ifdef __VGTL_CLASS_PARTIAL_SPECIALIZATION
02068
02069 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
02071 typedef std::reverse_iterator<iterator> reverse_iterator;
02072 #else
02073
02074 typedef std::reverse_bidirectional_iterator<const_iterator,value_type,
02075 const_reference,difference_type>
02076 const_reverse_iterator;
02078 typedef std::reverse_bidirectional_iterator<iterator,value_type,reference,
02079 difference_type>
02080 reverse_iterator;
02081 #endif
02082
02083 protected:
02084 #ifdef __VGTL_HAS_NAMESPACES
02085 using _Base::_C_node;
02086 using _Base::_C_put_node;
02087 using _Base::_C_get_node;
02088 #endif
02089
02090 public:
02092 explicit __ITree(const allocator_type& __a = allocator_type()) : _Base(__a) {}
02093
02094
02095
02096
02097
02099 iterative_walker root(walker_type wt = cw_pre_post,
02100 bool front_to_back = true,
02101 bool depth_first = true)
02102 { iterative_walker __help(_C_node, (int)wt, front_to_back, depth_first);
02103 return __help; }
02104
02106 const_iterative_walker root(walker_type wt = cw_pre_post,
02107 bool front_to_back = true,
02108 bool depth_first = true) const
02109 { const_iterative_walker __help(_C_node, (int)wt, front_to_back, depth_first);
02110 return __help; }
02111
02113 iterative_walker through()
02114 { iterative_walker __help(_C_node, 0, 0, 0);
02115 return __help; }
02117 const_iterative_walker through() const
02118 { const_iterative_walker __help(_C_node, 0, 0, 0);
02119 return __help; }
02120
02122 iterative_walker begin(walker_type wt = cw_pre_post,
02123 bool front_to_back = true,
02124 bool depth_first = true)
02125 { iterative_walker __help = root(wt, front_to_back, depth_first);
02126 ++__help;
02127 return __help; }
02129 const_iterative_walker begin(walker_type wt = cw_pre_post,
02130 bool front_to_back = true,
02131 bool depth_first = true) const
02132 { const_iterative_walker __help = root(wt, front_to_back, depth_first);
02133 ++__help;
02134 return __help; }
02135
02137 iterative_walker end(walker_type wt = cw_pre_post,
02138 bool front_to_back = true,
02139 bool depth_first = true)
02140 { iterative_walker __help(_C_node, (int)wt, front_to_back, depth_first, false);
02141 return __help; }
02143 const_iterative_walker end(walker_type wt = cw_pre_post,
02144 bool front_to_back = true,
02145 bool depth_first = true) const
02146 { const_iterative_walker __help(_C_node, (int)wt, front_to_back,
02147 depth_first, false);
02148 return __help; }
02149
02151 reverse_iterator rbegin()
02152 { return reverse_iterator(end()); }
02154 reverse_iterator rend()
02155 { return reverse_iterator(begin()); }
02156
02158 const_reverse_iterator rbegin() const
02159 { return const_reverse_iterator(end()); }
02161 const_reverse_iterator rend() const
02162 { return const_reverse_iterator(begin()); }
02163
02165 size_type size() const {
02166 size_type __result = 0;
02167 distance(begin(cw_pre), end(cw_pre), __result);
02168 return __result;
02169 }
02170
02172 reference getroot() { return *root(cw_pre,true,true); }
02174 const_reference getroot() const { return *root(cw_pre,true,true); }
02175
02177 size_type depth(const iterative_walker& __position)
02178 { return __position._C_w_cur_it.size(); }
02179
02184 __ITree(size_type __n, const _Tp& __value,
02185 const allocator_type& __a = allocator_type()) : _Base(__a)
02186 { insert(root(), __n, __value); }
02191 explicit __ITree(size_type __n) : _Base(allocator_type())
02192 { insert(root(), __n, _Tp()); }
02193
02194 public:
02196 __ITree(const _Self& __x) : _Base(__x) {}
02197
02199 virtual ~__ITree() {}
02200
02202 _Self& operator=(const _Self& __x);
02203
02208 _Self& operator=(_Node* __x)
02209 {
02210 this->_C_node = __x;
02211 return *this;
02212 }
02213
02215 friend bool operator== __VGTL_NULL_TMPL_ARGS (
02216 const __ITree& __x, const __ITree& __y);
02217 };
02218
02219
02220 template <class _Tp, class _Ctr, class _Iterator, class _Inserter, class _Alloc>
02221 inline bool operator==(const __Tree<_Tp,_Ctr,_Iterator,_Inserter,_Alloc>& __x,
02222 const __Tree<_Tp,_Ctr,_Iterator,_Inserter,_Alloc>& __y)
02223 {
02224 typedef typename __Tree<_Tp,_Ctr,_Iterator,_Inserter,_Alloc>::const_walker const_walker;
02225 typedef typename __Tree<_Tp,_Ctr,_Iterator,_Inserter,_Alloc>::children_iterator children_iterator;
02226 const_walker __w1 = __x.ground();
02227 const_walker __w2 = __y.ground();
02228 children_iterator __it1, __it2;
02229
02230 if(__w1->n_children() != __w2->n_children())
02231 return false;
02232 for(__it1 = __w1->child_begin(), __it2 = __w2->child_begin();
02233 __it1 != __w1->child_end(); ++__it1, ++__it2)
02234 {
02235 if(!compare_trees(__w1 >> __it1, __w2 >> __it2))
02236 return false;
02237 }
02238 return true;
02239 }
02240
02241 template <class _Tp, class _Ctr, class _Iterator, class _Inserter, class _Alloc>
02242 inline bool operator==(const __ITree<_Tp,_Ctr,_Iterator,_Inserter,_Alloc>& __x,
02243 const __ITree<_Tp,_Ctr,_Iterator,_Inserter,_Alloc>& __y)
02244 {
02245 typedef typename __ITree<_Tp,_Ctr,_Iterator,_Inserter,_Alloc>::const_iterative_walker const_walker;
02246 const_walker __w1 = __x.begin(cw_pre);
02247 const_walker __w2 = __y.begin(cw_pre);
02248 const_walker __e1 = __x.end(cw_pre);
02249 const_walker __e2 = __y.end(cw_pre);
02250
02251
02252 for(; __w1 != __e1 && __w2 != __e2 ; ++__w1, ++__w2)
02253 if(__w1.in_preorder() != __w2.in_preorder() ||
02254 (__w1.in_preorder() && *__w1 != *__w2))
02255 return false;
02256 return __w1 == __e1 && __w2 == __e2;
02257 }
02258
02259 template <class _Tp, class _Ctr, class _Iterator, class _Inserter, class _Alloc>
02260 inline bool operator<(const __Tree<_Tp,_Ctr,_Iterator,_Inserter,_Alloc>& __x,
02261 const __Tree<_Tp,_Ctr,_Iterator,_Inserter,_Alloc>& __y)
02262 {
02263 return lexicographical_compare(__x.begin(), __x.end(),
02264 __y.begin(), __y.end());
02265 }
02266
02267 template <class _Tp, class _Ctr, class _Iterator, class _Inserter, class _Alloc>
02268 inline bool operator<(const __ITree<_Tp,_Ctr,_Iterator,_Inserter,_Alloc>& __x,
02269 const __ITree<_Tp,_Ctr,_Iterator,_Inserter,_Alloc>& __y)
02270 {
02271 return lexicographical_compare(__x.begin(cw_pre), __x.end(cw_pre),
02272 __y.begin(cw_pre), __y.end(cw_pre));
02273 }
02274
02275 template <class _Tp, class _Ctr, class _Iterator, class _Inserter, class _Alloc>
02276 __Tree<_Tp, _Ctr, _Iterator, _Inserter, _Alloc>&
02277 __Tree<_Tp, _Ctr, _Iterator, _Inserter, _Alloc>::operator=(const __Tree<_Tp, _Ctr, _Iterator, _Inserter, _Alloc>& __x)
02278 {
02279 children_iterator cit;
02280
02281 if (this != &__x) {
02282 this->_C_node->clear_tree();
02283 this->_C_node->_C_children.erase(this->_C_node->_C_children.begin(),
02284 this->_C_node->_C_children.end());
02285 for(cit = __x._C_node->_C_children.begin();
02286 cit != __x._C_node->_C_children.end(); ++cit)
02287 this->_C_node->_C_children.insert(this->_C_node->_C_children.end(),
02288 copy_subtree((_Node*)*cit, this->_C_node));
02289 }
02290 return *this;
02291 }
02292
02293 template <class _Tp, class _Ctr, class _Iterator, class _Inserter, class _Alloc>
02294 __ITree<_Tp, _Ctr, _Iterator, _Inserter, _Alloc>&
02295 __ITree<_Tp, _Ctr, _Iterator, _Inserter, _Alloc>::operator=(const __ITree<_Tp, _Ctr, _Iterator, _Inserter, _Alloc>& __x)
02296 {
02297 children_iterator cit;
02298
02299 if (this != &__x) {
02300 this->_C_node->clear_tree();
02301 this->_C_node->_C_children.erase(this->_C_node->_C_children.begin(),
02302 this->_C_node->_C_children.end());
02303 for(cit = __x._C_node->_C_children.begin();
02304 cit != __x._C_node->_C_children.end(); ++cit)
02305 this->_C_node->_C_children.insert(this->_C_node->_C_children.end(),
02306 copy_subtree((_Node*)*cit, this->_C_node));
02307 }
02308 return *this;
02309 }
02310
02312
02318 template <class _Tp,
02319 template <class __Ty, class __AllocT> class _SequenceCtr = std::vector,
02320 class _PtrAlloc = __VGTL_DEFAULT_ALLOCATOR(void *),
02321 class _Alloc = __VGTL_DEFAULT_ALLOCATOR(_Tp) >
02322 class ntree : public __ITree<_Tp, _SequenceCtr<void*, _PtrAlloc>,
02323 typename _SequenceCtr<void*, _PtrAlloc>::iterator,
02324 typename _SequenceCtr<void*, _PtrAlloc>::iterator,
02325 _Alloc>
02326 {
02327 private:
02328 typedef typename _SequenceCtr<void*, _PtrAlloc>::iterator ___cit;
02329 protected:
02330 typedef ntree<_Tp,_SequenceCtr,_PtrAlloc,_Alloc> _Self;
02331
02332 public:
02336 void insert(const __walker_base& __position, const _Tp& __x)
02337 { _Node* __tmp = _C_create_node(__x);
02338 _Node* __parent = (_Node*)__position._C_w_cur->_C_parent;
02339 children_iterator __it;
02340
02341 if(__parent == NULL)
02342 {
02343 __position._C_w_cur->add_all_children(
02344 back_inserter(__tmp->_C_children), __tmp);
02345 __tmp->_C_parent = __position._C_w_cur;
02346 __position._C_w_cur->clear_children();
02347 __position._C_w_cur->_C_children.insert(
02348 __position._C_w_cur->_C_children.end(), __tmp);
02349 }
02350 else
02351 {
02352 __it = __parent->get_childentry_iterator(__position._C_w_cur);
02353 *__it = __tmp;
02354 __tmp->clear_children();
02355 __tmp->_C_children.insert(__tmp->_C_children.end(),
02356 __position._C_w_cur);
02357 __position._C_w_cur->_C_parent = __tmp;
02358 __tmp->_C_parent = __parent;
02359 }
02360 }
02364 void insert(const __walker_base& __position)
02365 { insert(__position, _Tp()); }
02366
02369 void push_child(const __walker_base& __position, const _Tp& __x)
02370 { insert_child(__position, __x,
02371 __position._C_w_cur->_C_children.end()); }
02374 void push_child(const __walker_base& __position)
02375 { push_child(__position, _Tp()); }
02376
02379 void push_children(const __walker_base& __position, size_type __n,
02380 const _Tp& __x)
02381 { insert_children(__position, __n, __x,
02382 __position._C_w_cur->_C_children.end()); }
02385 void push_children(const __walker_base& __position, size_type __n)
02386 { push_children(__position, n, _Tp()); }
02387
02390 void unshift_child(const __walker_base& __position, const _Tp& __x)
02391 { insert_child(__position, __x,
02392 __position._C_w_cur->_C_children.begin()); }
02395 void unshift_child(const __walker_base& __position)
02396 { unshift_child(__position, _Tp()); }
02397
02400 void unshift_children(const __walker_base& __position, size_type __n,
02401 const _Tp& __x)
02402 { insert_children(__position, __n, __x,
02403 __position._C_w_cur->_C_children.begin()); }
02406 void unshift_children(const __walker_base& __position, size_type __n)
02407 { unshift_children(__position, n, _Tp()); }
02408
02413 void push_subtree(const __walker_base& __position, _Self& __subtree)
02414 {
02415 __subtree.add_all_children(back_inserter(__position._C_w_cur->_C_children), __position._C_w_cur);
02416 __subtree.clear_children();
02417 }
02418
02423 void unshift_subtree(const __walker_base& __position, _Self& __subtree)
02424 {
02425 __subtree.add_all_children(front_inserter(__position._C_w_cur->_C_children), __position._C_w_cur);
02426 __subtree.clear_children();
02427 }
02428
02433 bool pop_child(const __walker_base& __position)
02434 { if(__position._C_w_cur->_C_children.empty())
02435 { return false; }
02436 else
02437 {
02438 children_iterator __it = __position._C_w_cur->_C_children.end();
02439 return erase_child(__position, --__it);
02440 }
02441 }
02442
02447 bool shift_child(const __walker_base& __position)
02448 { if(__position._C_w_cur->_C_children.empty())
02449 { return false; }
02450 else
02451 {
02452 children_iterator __it = __position._C_w_cur->_C_children.begin();
02453 return erase_child(__position, __it);
02454 }
02455 }
02456
02461 _Node* pop_subtree(const __walker_base& __position)
02462 {
02463 if(__position._C_w_cur->_C_children.empty())
02464 { return NULL; }
02465 else
02466 {
02467 children_iterator __it = __position._C_w_cur->_C_children.end();
02468 return erase_subtree(__position, --__it);
02469 }
02470 }
02471
02476 _Node* shift_subtree(const __walker_base& __position)
02477 {
02478 if(__position._C_w_cur->_C_children.empty())
02479 { return NULL; }
02480 else
02481 {
02482 children_iterator __it = __position._C_w_cur->_C_children.begin();
02483 return erase_subtree(__position, __it);
02484 }
02485 }
02486
02491 _Self& operator=(_Node* __x)
02492 {
02493 this->_C_node = __x;
02494 return *this;
02495 }
02496 };
02497
02499
02505 template <class _Tp,
02506 template <class __Ty, class __AllocT> class _SequenceCtr = std::vector,
02507 class _PtrAlloc = __VGTL_DEFAULT_ALLOCATOR(void *),
02508 class _Alloc = __VGTL_DEFAULT_ALLOCATOR(_Tp) >
02509 class rntree : public __Tree<_Tp, _SequenceCtr<void*, _PtrAlloc>,
02510 typename _SequenceCtr<void*, _PtrAlloc>::iterator,
02511 typename _SequenceCtr<void*, _PtrAlloc>::iterator,
02512 _Alloc>
02513 {
02514 private:
02515 typedef typename _SequenceCtr<void*, _PtrAlloc>::iterator ___cit;
02516 protected:
02517 typedef rntree<_Tp,_SequenceCtr,_PtrAlloc,_Alloc> _Self;
02518
02519 public:
02523 void insert(const __walker_base& __position, const _Tp& __x)
02524 { _Node* __tmp = _C_create_node(__x);
02525 _Node* __parent = (_Node*)__position._C_w_cur->_C_parent;
02526 children_iterator __it;
02527
02528 if(__parent == NULL)
02529 {
02530 __position._C_w_cur->add_all_children(
02531 back_inserter(__tmp->_C_children), __tmp);
02532 __tmp->_C_parent = __position._C_w_cur;
02533 __position._C_w_cur->clear_children();
02534 __position._C_w_cur->_C_children.insert(
02535 __position._C_w_cur->_C_children.end(), __tmp);
02536 }
02537 else
02538 {
02539 __it = __parent->get_childentry_iterator(__position._C_w_cur);
02540 *__it = __tmp;
02541 __tmp->clear_children();
02542 __tmp->_C_children.insert(__tmp->_C_children.end(),
02543 __position._C_w_cur);
02544 __position._C_w_cur->_C_parent = __tmp;
02545 __tmp->_C_parent = __parent;
02546 }
02547 }
02551 void insert(const __walker_base& __position)
02552 { insert(__position, _Tp()); }
02553
02556 void push_child(const __walker_base& __position, const _Tp& __x)
02557 { insert_child(__position, __x,
02558 __position._C_w_cur->_C_children.end()); }
02561 void push_child(const __walker_base& __position)
02562 { push_child(__position, _Tp()); }
02563
02566 void push_children(const __walker_base& __position, size_type __n,
02567 const _Tp& __x)
02568 { insert_children(__position, __n, __x,
02569 __position._C_w_cur->_C_children.end()); }
02572 void push_children(const __walker_base& __position, size_type __n)
02573 { push_children(__position, n, _Tp()); }
02574
02577 void unshift_child(const __walker_base& __position, const _Tp& __x)
02578 { insert_child(__position, __x,
02579 __position._C_w_cur->_C_children.begin()); }
02582 void unshift_child(const __walker_base& __position)
02583 { unshift_child(__position, _Tp()); }
02584
02587 void unshift_children(const __walker_base& __position, size_type __n,
02588 const _Tp& __x)
02589 { insert_children(__position, __n, __x,
02590 __position._C_w_cur->_C_children.begin()); }
02593 void unshift_children(const __walker_base& __position, size_type __n)
02594 { unshift_children(__position, n, _Tp()); }
02595
02600 void push_subtree(const __walker_base& __position, _Self& __subtree)
02601 {
02602 __subtree.add_all_children(back_inserter(__position._C_w_cur->_C_children), __position._C_w_cur);
02603 __subtree.clear_children();
02604 }
02605
02610 void unshift_subtree(const __walker_base& __position, _Self& __subtree)
02611 {
02612 __subtree.add_all_children(front_inserter(__position._C_w_cur->_C_children), __position._C_w_cur);
02613 __subtree.clear_children();
02614 }
02615
02620 bool pop_child(const __walker_base& __position)
02621 { if(__position._C_w_cur->_C_children.empty())
02622 { return false; }
02623 else
02624 {
02625 children_iterator __it = __position._C_w_cur->_C_children.end();
02626 return erase_child(__position, --__it);
02627 }
02628 }
02629
02634 bool shift_child(const __walker_base& __position)
02635 { if(__position._C_w_cur->_C_children.empty())
02636 { return false; }
02637 else
02638 {
02639 children_iterator __it = __position._C_w_cur->_C_children.begin();
02640 return erase_child(__position, __it);
02641 }
02642 }
02643
02648 _Node* pop_subtree(const __walker_base& __position)
02649 {
02650 if(__position._C_w_cur->_C_children.empty())
02651 { return NULL; }
02652 else
02653 {
02654 children_iterator __it = __position._C_w_cur->_C_children.end();
02655 return erase_subtree(__position, --__it);
02656 }
02657 }
02658
02663 _Node* shift_subtree(const __walker_base& __position)
02664 {
02665 if(__position._C_w_cur->_C_children.empty())
02666 { return NULL; }
02667 else
02668 {
02669 children_iterator __it = __position._C_w_cur->_C_children.begin();
02670 return erase_subtree(__position, __it);
02671 }
02672 }
02673
02678 _Self& operator=(_Node* __x)
02679 {
02680 this->_C_node = __x;
02681 return *this;
02682 }
02683 };
02684
02685
02687
02694 template <class _Tp,
02695 template <class __Key, class __Ty, class __Compare, class __AllocT> class _AssocCtr = std::multimap,
02696 class _Key = string,
02697 class _Compare = less<_Key>,
02698 class _PtrAlloc = __VGTL_DEFAULT_ALLOCATOR(void *),
02699 class _Alloc = __VGTL_DEFAULT_ALLOCATOR(_Tp) >
02700 class atree : public __ITree<_Tp, _AssocCtr<_Key, void*, _Compare, _PtrAlloc>,
02701 pair_adaptor<typename _AssocCtr<_Key, void*,
02702 _Compare, _PtrAlloc>::iterator>,
02703 _Key, _Alloc>
02704 {
02705 protected:
02706 typedef atree<_Tp,_AssocCtr,_Key,_Compare,_PtrAlloc,_Alloc> _Self;
02707
02708 public:
02713 _Self& operator=(_Node* __x)
02714 {
02715 this->_C_node = __x;
02716 return *this;
02717 }
02718
02722 void insert(const __walker_base& __position, const _Tp& __x, const _Key& __k)
02723 { _Node* __tmp = _C_create_node(__x);
02724 _Node* __parent = (_Node*)__position._C_w_cur->_C_parent;
02725 children_iterator __it;
02726
02727 if(__parent == NULL)
02728 {
02729 __position._C_w_cur->add_all_children(
02730 inserter(__tmp->_C_children.end()), __tmp);
02731 __tmp->_C_parent = __position._C_w_cur;
02732 __position._C_w_cur->clear_children();
02733 __position._C_w_cur->_C_children.insert(__k, __tmp);
02734 }
02735 else
02736 {
02737 __it = __parent->get_childentry_iterator(__position._C_w_cur);
02738 *__it = __tmp;
02739 __tmp->clear_children();
02740 __tmp->_C_children.insert(__k, __position._C_w_cur);
02741 __position._C_w_cur->_C_parent = __tmp;
02742 __tmp->_C_parent = __parent;
02743 }
02744 }
02748 void insert(const __walker_base& __position, const _Key& __k)
02749 { insert(__position, _Tp(), __k); }
02750
02751
02752 };
02753
02755
02762 template <class _Key, class _Compare = less<_Key>,
02763 template <class __Key, class __Compare, class __AllocT> class _AssocCtr = std::multiset,
02764 class _PtrAlloc = __VGTL_DEFAULT_ALLOCATOR(void *),
02765 class _Alloc = __VGTL_DEFAULT_ALLOCATOR(_Key&) >
02766 class stree : public __ITree<_Key, _AssocCtr<_Key&, pointer_adaptor<_Compare>,
02767 _PtrAlloc>,
02768 typename _AssocCtr<_Key&, pointer_adaptor<_Compare>,
02769 _PtrAlloc>::iterator,
02770 _Key&, _Alloc>
02771 {
02772 protected:
02773 typedef stree<_Key,_Compare,_AssocCtr,_PtrAlloc,_Alloc> _Self;
02774
02775 public:
02780 _Self& operator=(_Node* __x)
02781 {
02782 this->_C_node = __x;
02783 return *this;
02784 }
02785 };
02786
02788
02795 template <class _Tp,
02796 template <class __Key, class __Ty, class __Compare, class __AllocT> class _AssocCtr = std::multimap,
02797 class _Key = string,
02798 class _Compare = less<_Key>,
02799 class _PtrAlloc = __VGTL_DEFAULT_ALLOCATOR(void *),
02800 class _Alloc = __VGTL_DEFAULT_ALLOCATOR(_Tp) >
02801 class ratree : public __Tree<_Tp, _AssocCtr<_Key, void*, _Compare, _PtrAlloc>,
02802 pair_adaptor<typename _AssocCtr<_Key, void*,
02803 _Compare, _PtrAlloc>::iterator>,
02804 _Key, _Alloc>
02805 {
02806 protected:
02807 typedef ratree<_Tp,_AssocCtr,_Key,_Compare,_PtrAlloc,_Alloc> _Self;
02808
02809 public:
02814 _Self& operator=(_Node* __x)
02815 {
02816 this->_C_node = __x;
02817 return *this;
02818 }
02819
02823 void insert(const __walker_base& __position, const _Tp& __x, const _Key& __k)
02824 { _Node* __tmp = _C_create_node(__x);
02825 _Node* __parent = (_Node*)__position._C_w_cur->_C_parent;
02826 children_iterator __it;
02827
02828 if(__parent == NULL)
02829 {
02830 __position._C_w_cur->add_all_children(
02831 inserter(__tmp->_C_children.end()), __tmp);
02832 __tmp->_C_parent = __position._C_w_cur;
02833 __position._C_w_cur->clear_children();
02834 __position._C_w_cur->_C_children.insert(__k, __tmp);
02835 }
02836 else
02837 {
02838 __it = __parent->get_childentry_iterator(__position._C_w_cur);
02839 *__it = __tmp;
02840 __tmp->clear_children();
02841 __tmp->_C_children.insert(__k, __position._C_w_cur);
02842 __position._C_w_cur->_C_parent = __tmp;
02843 __tmp->_C_parent = __parent;
02844 }
02845 }
02849 void insert(const __walker_base& __position, const _Key& __k)
02850 { insert(__position, _Tp(), __k); }
02851
02852
02853 };
02854
02856
02863 template <class _Key, class _Compare = less<_Key>,
02864 template <class __Key, class __Compare, class __AllocT> class _AssocCtr = std::multiset,
02865 class _PtrAlloc = __VGTL_DEFAULT_ALLOCATOR(void *),
02866 class _Alloc = __VGTL_DEFAULT_ALLOCATOR(_Key&) >
02867 class rstree : public __Tree<_Key, _AssocCtr<_Key&, pointer_adaptor<_Compare>,
02868 _PtrAlloc>,
02869 typename _AssocCtr<_Key&, pointer_adaptor<_Compare>,
02870 _PtrAlloc>::iterator,
02871 _Key&, _Alloc>
02872 {
02873 protected:
02874 typedef rstree<_Key,_Compare,_AssocCtr,_PtrAlloc,_Alloc> _Self;
02875
02876 public:
02881 _Self& operator=(_Node* __x)
02882 {
02883 this->_C_node = __x;
02884 return *this;
02885 }
02886 };
02887
02888 __VGTL_END_NAMESPACE
02889
02890 #endif