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_GRAPH_H_
00032 #define __VGTL_GRAPH_H_
00033
00034 #include <vector.h>
00035 #include <list.h>
00036 #include <multimap.h>
00037 #include <multiset.h>
00038 #include <algo.h>
00039 #include <iterator.h>
00040 #include <string>
00041 #include <g_data.h>
00042
00043 #include <vgtl_helpers.h>
00044
00045 __VGTL_BEGIN_NAMESPACE
00046
00047 #define _C_W_preorder 1
00048 #define _C_W_postorder 2
00049
00050 WARNING: DO NOT USE THIS FILE -> NOT YET FINISHED!!!!!
00051
00052
00053 template <class _Tp, class _Ctr, class _Iterator>
00054 class _Graph_node
00055 {
00056 private:
00057 typedef void* _Void_pointer;
00058 typedef _Iterator _Ctr_iterator;
00059 typedef _Graph_node<_Tp,_Ctr,_Iterator> _Self;
00060
00061 public:
00062 _Tp _C_data;
00063 _Ctr _C_neighbors;
00064
00065 _Graph_node() { _C_data();
00066 __VGTL_TRY {
00067 construct(&_C_neighbors);
00068 }
00069 __VGTL_UNWIND( );
00070 }
00071 void clear_graph();
00072 void clear_neighbors()
00073 { _C_neighbors.erase(_C_neighbors.begin(), _C_neighbors.end()); }
00074
00075 _Ctr_iterator get_entry_iterator(_Void_pointer __p)
00076 { _Ctr_iterator __tmp;
00077 for(__tmp = _C_neighbors.begin(); __tmp != _C_neighbors.end();
00078 ++__tmp)
00079 if(*__tmp == __p) break;
00080 return __tmp;
00081 }
00082
00083 #ifdef __VGTL_MEMBER_TEMPLATES
00084 template <class _Output_Iterator>
00085 void add_all_neighbors(_Output_Iterator fi, _Self* _nb);
00086 #endif
00087 };
00088
00089 #ifdef __VGTL_MEMBER_TEMPLATES
00090 template <class _Tp, class _Ctr, class _Iterator>
00091 template <class _Output_Iterator>
00092 void
00093 _Graph_node<_Tp,_Ctr,_Iterator>::
00094 add_all_neighbors(_Output_Iterator fi, _Self* _nb)
00095 { _Ctr_iterator it;
00096 for(it = _C_neighbors.begin();
00097 it != _C_neighbors.end();
00098 ++it)
00099 {
00100 *fi++ = *it;
00101 ((_Self*)*it)->_C_neighbors.insert(((_Self*)*it)->_C_neighbors.end(),_nb);
00102 }
00103 };
00104 #endif
00105
00106
00107 template <class _Tp, class _Ctr, class _Iterator>
00108 void
00109 _Graph_node<_Tp,_Ctr,_Iterator>::clear_graph()
00110 {
00111
00112 destroy(&this->_C_data);
00113 }
00114
00115
00116 template <class _Tp, class _Ref, class _Ptr, class _Ctr, class _Iterator>
00117 class _Graph_iterator;
00118
00119 template <class _Tp, class _Ref, class _Ptr, class _Ctr, class _Iterator>
00120 class _Graph_walker_base
00121 {
00122 public:
00123 typedef _Graph_walker_base<_Tp,_Tp&,_Tp*,_Ctr,_Iterator> walker;
00124 typedef _Graph_walker_base<_Tp,const _Tp&,const _Tp*,_Ctr,_Iterator> const_walker;
00125 typedef _Graph_walker_base<_Tp,_Ref,_Ptr,_Ctr,_Iterator> _Self;
00126 typedef _Graph_iterator<_Tp,_Ref,_Ptr,_Ctr,_Iterator> _Itr;
00127
00128 typedef _Tp value_type;
00129 typedef _Ptr pointer;
00130 typedef _Ref reference;
00131 private:
00132 typedef _Graph_node<_Tp,_Ctr,_Iterator> _Node;
00133 typedef _Iterator _Ctr_iterator;
00134
00135 public:
00136 typedef _Ctr_iterator container_iterator;
00137 typedef _Node node_type;
00138
00139 typedef size_t size_type;
00140 typedef ptrdiff_t difference_type;
00141
00142 public:
00143 _Node* _C_w_cur;
00144
00145 public:
00146 _Graph_walker_base() {}
00147
00148 public:
00149 _Graph_walker_base(_Node* __x) : _C_w_cur(__x) { }
00150
00151 _Graph_walker_base(const walker& __x) : _C_w_cur(__x._C_w_cur) {}
00152
00153 reference operator*() const { return (*_C_w_cur)._C_data; }
00154
00155 #ifndef __SGI_STL_NO_ARROW_OPERATOR
00156 pointer operator->() const { return &(operator*()); }
00157 #endif
00158
00159 public:
00160 _Self& operator=(_Itr __x) {
00161 _C_w_cur = __x._C_i_cur;
00162 return *this;
00163 }
00164
00165 const _Node* node() { return _C_w_cur; }
00166 size_type n_neighbors() { return _C_w_cur->_C_neighbors.size(); }
00167
00168 _Ctr_iterator neighbor_begin() { return _C_w_cur->_C_neighbors.begin(); }
00169 _Ctr_iterator neighbor_end() { return _C_w_cur->_C_neighbors.end(); }
00170
00171 template <class _Function>
00172 _Function for_each_neighbor(_Function __f)
00173 {
00174 return for_each(neighbor_begin(), neighbor_end(), __f);
00175 }
00176 };
00177
00178
00179 template <class _Tp, class _Ref, class _Ptr, class _Ctr, class _Iterator>
00180 class _Graph_walker : public _Graph_walker_base<_Tp,_Ref,_Ptr,_Ctr,_Iterator>
00181 {
00182 public:
00183 typedef _Graph_walker<_Tp,_Tp&,_Tp*,_Ctr,_Iterator> walker;
00184 typedef _Graph_walker<_Tp,const _Tp&,const _Tp*,_Ctr,_Iterator> const_walker;
00185 typedef _Graph_walker<_Tp,_Ref,_Ptr,_Ctr,_Iterator> _Self;
00186
00187 public:
00188 struct {
00189 unsigned int order:2;
00190 unsigned int front_to_back:1;
00191 unsigned int depth_first:1;
00192 } _C_w_t;
00193 bool _C_w_in_preorder;
00194 vector<_Iterator> _C_w_cur_it;
00195
00196 public:
00197 _Tree_walker() : _C_w_cur_it() {_C_w_cur_it.reserve(32); }
00198
00199 private:
00200 void _find_start_node();
00201 void _find_end_node();
00202 void _find_next_preorder();
00203 void _find_next_postorder();
00204 void _find_next_any();
00205 void _find_prev_preorder();
00206 void _find_prev_postorder();
00207 void _find_prev_any();
00208
00209 public:
00210 _Tree_walker(_Node* __x, int order = (_C_W_preorder|_C_W_postorder),
00211 bool front_to_back = true, bool depth_first = true,
00212 bool find_start = true)
00213 : _C_w_cur_it() {
00214 _C_w_cur = __x;
00215 _C_w_cur_it.reserve(32);
00216 _C_w_t.order = order;
00217 _C_w_t.front_to_back = front_to_back;
00218 _C_w_t.depth_first = depth_first;
00219 if(__x->_C_parent == NULL)
00220 _C_w_in_preorder = find_start;
00221 else if(find_start)
00222 _find_start_node();
00223 else
00224 _find_end_node(); }
00225
00226 _Tree_walker(const walker& __x) : _C_w_in_preorder(__x._C_w_in_preorder),
00227 _C_w_cur_it(__x._C_w_cur_it) {
00228 _C_w_cur = __x._C_w_cur;
00229 _C_w_t = __x._C_w_t;
00230 }
00231
00232 bool operator==(const _Self& __x) const
00233 { return (_C_w_t.order == 0 && __x._C_w_t.order == 0) ||
00234 (_C_w_cur == __x._C_w_cur &&
00235 _C_w_in_preorder == __x._C_w_in_preorder &&
00236 _C_w_t.order == __x._C_w_t.order &&
00237 _C_w_t.front_to_back == __x._C_w_t.front_to_back &&
00238 _C_w_t.depth_first == __x._C_w_t.depth_first &&
00239 _C_w_cur_it == __x._C_w_cur_it); }
00240 bool operator!=(const _Self& __x) const
00241 { return (_C_w_t.order != 0 || __x._C_w_t.order != 0) &&
00242 (_C_w_cur != __x._C_w_cur ||
00243 _C_w_in_preorder != __x._C_w_in_preorder ||
00244 _C_w_t.order != __x._C_w_t.order ||
00245 _C_w_t.front_to_back != __x._C_w_t.front_to_back ||
00246 _C_w_t.depth_first != __x._C_w_t.depth_first ||
00247 _C_w_cur_it != __x._C_w_cur_it); }
00248
00249 public:
00250 _Self& operator++() {
00251 if(_C_w_t.depth_first)
00252 {
00253 if(_C_w_t.order == (_C_W_preorder | _C_W_postorder))
00254 {
00255 _find_next_any();
00256 }
00257 else if(_C_w_t.order & _C_W_preorder)
00258 {
00259 _find_next_preorder();
00260 }
00261 else
00262 {
00263 _find_next_postorder();
00264 }
00265 }
00266 else
00267 {
00268 ;
00269 }
00270 return *this;
00271 }
00272 _Self operator++(int) {
00273 _Self __tmp = *this;
00274 ++*this;
00275 return __tmp;
00276 }
00277
00278 _Self& operator--() {
00279 if(_C_w_t.depth_first)
00280 {
00281 if(_C_w_t.order == (_C_W_preorder | _C_W_postorder))
00282 {
00283 _find_prev_any();
00284 }
00285 else if(_C_w_t.order & _C_W_preorder)
00286 {
00287 _find_prev_preorder();
00288 }
00289 else
00290 {
00291 _find_prev_postorder();
00292 }
00293 }
00294 else
00295 {
00296 ;
00297 }
00298 return *this;
00299 }
00300 _Self operator--(int) {
00301 _Self __tmp = *this;
00302 --*this;
00303 return __tmp;
00304 }
00305
00306 _Self operator<<(bool search_start) {
00307 _Self __tmp = *this;
00308 _Node *help = __tmp._C_w_cur._C_parent;
00309 if(help == NULL)
00310 {
00311 __tmp._C_w_t.order = 0;
00312 }
00313 else
00314 {
00315 __tmp._C_w_cur = help;
00316 if(__tmp._C_w_t.order & _C_W_postorder)
00317 __tmp._C_w_in_preorder = false;
00318 if(!__tmp._C_w_cur_it.empty())
00319 pop_back(__tmp._C_w_cur_it);
00320 if(search_start)
00321 __tmp._find_start_node();
00322 }
00323 return __tmp;
00324 }
00325
00326 _Self operator>>(_Ctr_iterator i) {
00327 _Self __tmp = *this;
00328 _Node *help = (_Node*) *i;
00329 if(__tmp._C_w_t.order & _C_W_preorder)
00330 __tmp._C_w_in_preorder = true;
00331 __tmp._C_w_cur_it.push_back(i);
00332 __tmp._C_w_cur = help;
00333 return __tmp;
00334 }
00335
00336 _Self& operator<<=(bool search_start) {
00337 _Node *help = _C_w_cur._C_parent;
00338 if(help == NULL)
00339 {
00340 _C_w_t.order = 0;
00341 }
00342 else
00343 {
00344 _C_w_cur = help;
00345 if(_C_w_t.order & _C_W_postorder)
00346 _C_w_in_preorder = false;
00347 if(!_C_w_cur_it.empty())
00348 pop_back(_C_w_cur_it);
00349 if(search_start)
00350 _find_start_node();
00351 }
00352 return *this;
00353 }
00354
00355 _Self& operator>>=(_Ctr_iterator i) {
00356 _Node *help = (_Node*) *i;
00357 if(_C_w_t.order & _C_W_preorder)
00358 _C_w_in_preorder = true;
00359 push_back(_C_w_cur_it, i);
00360 _C_w_cur = help;
00361 return *this;
00362 }
00363
00364 _Self& operator~() {
00365 if(_C_w_t.order == (_C_W_preorder | _C_W_postorder))
00366 _C_w_in_preorder = !_C_w_in_preorder;
00367 return *this;
00368 }
00369
00370 _Self& operator=(_Itr __x) {
00371 _C_w_cur = __x._C_i_cur;
00372 _C_w_cur_it = __x._C_i_cur_it;
00373 _C_w_t.order = _C_W_postorder;
00374 _C_w_t.depth_first = 1;
00375 _C_w_t.front_to_back = 1;
00376 return *this;
00377 }
00378
00379 bool in_preorder() { return _C_w_in_preorder; }
00380 };
00381
00382 template <class _Tp, class _Ref, class _Ptr, class _Ctr, class _Iterator>
00383 void _Tree_walker<_Tp,_Ref,_Ptr,_Ctr,_Iterator>::_find_start_node()
00384 {
00385 if(_C_w_t.depth_first)
00386 {
00387 if(_C_w_t.order & _C_W_preorder)
00388 {
00389 _C_w_in_preorder = true;
00390 }
00391 else if(_C_w_t.order & _C_W_postorder)
00392 {
00393 _C_w_in_preorder = false;
00394 if(_C_w_t.front_to_back)
00395 {
00396 while(!_C_w_cur->_C_children.empty())
00397 {
00398 _Ctr_iterator help;
00399
00400 help = _C_w_cur->_C_children.begin();
00401 _C_w_cur = (_Node*)*help;
00402 _C_w_cur_it.push_back(help);
00403 }
00404 }
00405 else
00406 {
00407 while(!_C_w_cur->_C_children.empty())
00408 {
00409 _Ctr_iterator help;
00410
00411 help = _C_w_cur->_C_children.end();
00412 --help;
00413 _C_w_cur = (_Node*)*help;
00414 _C_w_cur_it.push_back(help);
00415 }
00416 }
00417 }
00418 }
00419 else
00420 {
00421 ;
00422 }
00423 }
00424
00425 template <class _Tp, class _Ref, class _Ptr, class _Ctr, class _Iterator>
00426 void _Tree_walker<_Tp,_Ref,_Ptr,_Ctr,_Iterator>::_find_end_node()
00427 {
00428 if(_C_w_t.depth_first)
00429 {
00430 if(_C_w_t.order & _C_W_postorder)
00431 {
00432 _C_w_in_preorder = false;
00433 (&_C_w_cur_it)->~vector();
00434 _C_w_cur_it.reserve(32);
00435 }
00436 else if(_C_w_t.order & _C_W_postorder)
00437 {
00438 _C_w_in_preorder = true;
00439 if(_C_w_t.front_to_back)
00440 {
00441 while(!_C_w_cur->_C_children.empty())
00442 {
00443 _Ctr_iterator help;
00444
00445 help = _C_w_cur->_C_children.begin();
00446 _C_w_cur = (_Node*)*help;
00447 _C_w_cur_it.push_back(help);
00448 }
00449 }
00450 else
00451 {
00452 while(!_C_w_cur->_C_children.empty())
00453 {
00454 _Ctr_iterator help;
00455
00456 help = _C_w_cur->_C_children.end();
00457 --help;
00458 _C_w_cur = (_Node*)*help;
00459 _C_w_cur_it.push_back(help);
00460 }
00461 }
00462 }
00463 }
00464 else
00465 {
00466 ;
00467 }
00468 }
00469
00470 template <class _Tp, class _Ref, class _Ptr, class _Ctr, class _Iterator>
00471 void _Tree_walker<_Tp,_Ref,_Ptr,_Ctr,_Iterator>::_find_next_preorder()
00472 {
00473 _Ctr_iterator help;
00474
00475 if(_C_w_cur->_C_children.empty())
00476 {
00477 while(1)
00478 {
00479 if(_C_w_cur_it.empty())
00480 {
00481 if(_C_w_cur->_C_parent == NULL && _C_w_in_preorder)
00482 _C_w_in_preorder = false;
00483 else
00484 _C_w_t.order = 0;
00485 break;
00486 }
00487 help = _C_w_cur_it.back();
00488 _C_w_cur_it.pop_back();
00489 _C_w_cur = (_Node*)_C_w_cur->_C_parent;
00490 if(_C_w_t.front_to_back)
00491 {
00492 ++help;
00493 if(help != _C_w_cur->_C_children.end())
00494
00495 {
00496 _C_w_cur_it.push_back(help);
00497 _C_w_cur = (_Node*)*help;
00498 break;
00499 }
00500 }
00501 else
00502 {
00503 if(help != _C_w_cur->_C_children.begin())
00504
00505 {
00506 --help;
00507 _C_w_cur_it.push_back(help);
00508 _C_w_cur = (_Node*)*help;
00509 break;
00510 }
00511 }
00512 }
00513 }
00514 else if(!_C_w_in_preorder)
00515 _C_w_t.order = 0;
00516 else
00517 {
00518 if(_C_w_t.front_to_back)
00519 help = _C_w_cur->_C_children.begin();
00520 else
00521 {
00522 help = _C_w_cur->_C_children.end();
00523 help--;
00524 }
00525 _C_w_cur_it.push_back(help);
00526 _C_w_cur = (_Node*)*help;
00527 }
00528 }
00529
00530 template <class _Tp, class _Ref, class _Ptr, class _Ctr, class _Iterator>
00531 void _Tree_walker<_Tp,_Ref,_Ptr,_Ctr,_Iterator>::_find_next_postorder()
00532 {
00533 if(_C_w_in_preorder)
00534 {
00535 _C_w_in_preorder = false;
00536 _find_start_node();
00537 }
00538 else if(_C_w_cur_it.empty())
00539 {
00540 _C_w_t.order = 0;
00541 }
00542 else
00543 {
00544 _Ctr_iterator help = _C_w_cur_it.back();
00545 _C_w_cur_it.pop_back();
00546 if(_C_w_t.front_to_back)
00547 {
00548 ++help;
00549 if(help != ((_Node*)_C_w_cur->_C_parent)->_C_children.end())
00550
00551 {
00552 do
00553 {
00554 _C_w_cur_it.push_back(help);
00555 _C_w_cur = (_Node*)*help;
00556 help = _C_w_cur->_C_children.begin();
00557 } while(!_C_w_cur->_C_children.empty());
00558 }
00559 else
00560 {
00561 _C_w_cur = (_Node*)_C_w_cur->_C_parent;
00562 }
00563 }
00564 else
00565 {
00566 if(help != ((_Node*)_C_w_cur->_C_parent)->_C_children.begin())
00567
00568 {
00569 do
00570 {
00571 --help;
00572 _C_w_cur_it.push_back(help);
00573 _C_w_cur = (_Node*)*help;
00574 help = _C_w_cur->_C_children.end();
00575 } while(!_C_w_cur->_C_children.empty());
00576 }
00577 else
00578 {
00579 _C_w_cur = (_Node*)_C_w_cur->_C_parent;
00580 }
00581 }
00582 }
00583 }
00584
00585 template <class _Tp, class _Ref, class _Ptr, class _Ctr, class _Iterator>
00586 void _Tree_walker<_Tp,_Ref,_Ptr,_Ctr,_Iterator>::_find_next_any() {
00587 if(_C_w_in_preorder)
00588 {
00589 if(_C_w_cur->_C_children.empty())
00590 _C_w_in_preorder = false;
00591 else
00592 {
00593 _Ctr_iterator help;
00594 if(_C_w_t.front_to_back)
00595 help = _C_w_cur->_C_children.begin();
00596 else
00597 {
00598 help = _C_w_cur->_C_children.end();
00599 --help;
00600 }
00601 _C_w_cur_it.push_back(help);
00602 _C_w_cur = (_Node*)*help;
00603 }
00604 }
00605 else
00606 {
00607 _Ctr_iterator help;
00608
00609 if(!_C_w_cur_it.empty())
00610 {
00611 help = _C_w_cur_it.back();
00612 _C_w_cur_it.pop_back();
00613 if(_C_w_t.front_to_back)
00614 {
00615 ++help;
00616 if(help != ((_Node*)_C_w_cur->_C_parent)->_C_children.end())
00617
00618 {
00619 _C_w_cur_it.push_back(help);
00620 _C_w_cur = (_Node*)*help;
00621 _C_w_in_preorder = true;
00622 }
00623 else
00624 {
00625 _C_w_cur = (_Node*)_C_w_cur->_C_parent;
00626 }
00627 }
00628 else
00629 {
00630 if(help != ((_Node*)_C_w_cur->_C_parent)->_C_children.begin())
00631
00632 {
00633 --help;
00634 _C_w_cur_it.push_back(help);
00635 _C_w_cur = (_Node*)*help;
00636 _C_w_in_preorder = true;
00637 }
00638 else
00639 {
00640 _C_w_cur = (_Node*)_C_w_cur->_C_parent;
00641 }
00642 }
00643 }
00644 else
00645 {
00646 _C_w_t.order = 0;
00647 }
00648 }
00649 }
00650
00651 template <class _Tp, class _Ref, class _Ptr, class _Ctr, class _Iterator>
00652 void _Tree_walker<_Tp,_Ref,_Ptr,_Ctr,_Iterator>::_find_prev_preorder()
00653 {
00654 if(!_C_w_in_preorder)
00655 {
00656 _C_w_t.order = _C_W_postorder;
00657 _C_w_t.front_to_back = !_C_w_t.front_to_back;
00658 _find_start_node();
00659 _C_w_in_preorder = true;
00660 _C_w_t.order = _C_W_preorder;
00661 _C_w_t.front_to_back = !_C_w_t.front_to_back;
00662 }
00663 else if(_C_w_cur_it.empty())
00664 {
00665 _C_w_t.order = 0;
00666 }
00667 else
00668 {
00669 _Ctr_iterator help = _C_w_cur_it.back();
00670 _C_w_cur_it.pop_back();
00671 if(!_C_w_t.front_to_back)
00672 {
00673 ++help;
00674 if(help != ((_Node*)_C_w_cur->_C_parent)->_C_children.end())
00675
00676 {
00677 do
00678 {
00679 _C_w_cur_it.push_back(help);
00680 _C_w_cur = (_Node*)*help;
00681 help = _C_w_cur->_C_children.begin();
00682 } while(!_C_w_cur->_C_children.empty());
00683 }
00684 else
00685 {
00686 _C_w_cur = (_Node*)_C_w_cur->_C_parent;
00687 }
00688 }
00689 else
00690 {
00691 if(help != ((_Node*)_C_w_cur->_C_parent)->_C_children.begin())
00692
00693 {
00694 do
00695 {
00696 --help;
00697 _C_w_cur_it.push_back(help);
00698 _C_w_cur = (_Node*)*help;
00699 help = _C_w_cur->_C_children.end();
00700 } while(!_C_w_cur->_C_children.empty());
00701 }
00702 else
00703 {
00704 _C_w_cur = (_Node*)_C_w_cur->_C_parent;
00705 }
00706 }
00707 }
00708 }
00709
00710 template <class _Tp, class _Ref, class _Ptr, class _Ctr, class _Iterator>
00711 void _Tree_walker<_Tp,_Ref,_Ptr,_Ctr,_Iterator>::_find_prev_postorder()
00712 {
00713 _Ctr_iterator help;
00714
00715 if(_C_w_cur->_C_children.empty())
00716 {
00717 while(1)
00718 {
00719 if(_C_w_cur_it.empty())
00720 {
00721 if(_C_w_cur->_C_parent == NULL && !_C_w_in_preorder)
00722 _C_w_in_preorder = true;
00723 else
00724 _C_w_t.order = 0;
00725 break;
00726 }
00727 help = _C_w_cur_it.back();
00728 _C_w_cur_it.pop_back();
00729 _C_w_cur = (_Node*)_C_w_cur->_C_parent;
00730 if(!_C_w_t.front_to_back)
00731 {
00732 ++help;
00733 if(help != _C_w_cur->_C_children.end())
00734
00735 {
00736 _C_w_cur_it.push_back(help);
00737 _C_w_cur = (_Node*)*help;
00738 break;
00739 }
00740 }
00741 else
00742 {
00743 if(help != _C_w_cur->_C_children.begin())
00744
00745 {
00746 --help;
00747 _C_w_cur_it.push_back(help);
00748 _C_w_cur = (_Node*)*help;
00749 break;
00750 }
00751 }
00752 }
00753 }
00754 else if(_C_w_in_preorder)
00755 _C_w_t.order = 0;
00756 else
00757 {
00758 if(!_C_w_t.front_to_back)
00759 help = _C_w_cur->_C_children.begin();
00760 else
00761 {
00762 help = _C_w_cur->_C_children.end();
00763 help--;
00764 }
00765 _C_w_cur_it.push_back(help);
00766 _C_w_cur = (_Node*)*help;
00767 }
00768 }
00769
00770 template <class _Tp, class _Ref, class _Ptr, class _Ctr, class _Iterator>
00771 void _Tree_walker<_Tp,_Ref,_Ptr,_Ctr,_Iterator>::_find_prev_any()
00772 {
00773 if(!_C_w_in_preorder)
00774 {
00775 if(_C_w_cur->_C_children.empty())
00776 _C_w_in_preorder = true;
00777 else
00778 {
00779 _Ctr_iterator help;
00780 if(!_C_w_t.front_to_back)
00781 help = _C_w_cur->_C_children.begin();
00782 else
00783 {
00784 help = _C_w_cur->_C_children.end();
00785 --help;
00786 }
00787 _C_w_cur_it.push_back(help);
00788 _C_w_cur = (_Node*)*help;
00789 }
00790 }
00791 else
00792 {
00793 _Ctr_iterator help;
00794
00795 if(!_C_w_cur_it.empty())
00796 {
00797 help = _C_w_cur_it.back();
00798 _C_w_cur_it.pop_back();
00799 if(!_C_w_t.front_to_back)
00800 {
00801 ++help;
00802 if(help != ((_Node*)_C_w_cur->_C_parent)->_C_children.end())
00803
00804 {
00805 _C_w_cur_it.push_back(help);
00806 _C_w_cur = (_Node*)*help;
00807 _C_w_in_preorder = false;
00808 }
00809 else
00810 {
00811 _C_w_cur = (_Node*)_C_w_cur->_C_parent;
00812 }
00813 }
00814 else
00815 {
00816 if(help != ((_Node*)_C_w_cur->_C_parent)->_C_children.begin())
00817
00818 {
00819 --help;
00820 _C_w_cur_it.push_back(help);
00821 _C_w_cur = (_Node*)*help;
00822 _C_w_in_preorder = false;
00823 }
00824 else
00825 {
00826 _C_w_cur = (_Node*)_C_w_cur->_C_parent;
00827 }
00828 }
00829 }
00830 else
00831 {
00832 _C_w_t.order = 0;
00833 }
00834 }
00835 }
00836
00837 template <class _Tp, class _Ref, class _Ptr, class _Ctr, class _Iterator>
00838 class _RTree_walker : public _Tree_walker_base<_Tp,_Ref,_Ptr,_Ctr,_Iterator>
00839 {
00840 public:
00841 typedef _RTree_walker<_Tp,_Tp&,_Tp*,_Ctr,_Iterator> walker;
00842 typedef _RTree_walker<_Tp,const _Tp&,const _Tp*,_Ctr,_Iterator> const_walker;
00843 typedef _RTree_walker<_Tp,_Ref,_Ptr,_Ctr,_Iterator> _Self;
00844
00845 public:
00846 _RTree_walker() {}
00847
00848 public:
00849 _RTree_walker(_Node* __x) { _C_w_cur = __x; }
00850
00851 _RTree_walker(const walker& __x) { _C_w_cur = __x._C_w_cur; }
00852
00853 public:
00854 bool operator==(const _Self& __x) const
00855 { return _C_w_cur == __x._C_w_cur; }
00856 bool operator!=(const _Self& __x) const
00857 { return _C_w_cur != __x._C_w_cur; }
00858
00859 _Self operator<<(bool search_start) {
00860 _Self __tmp = *this;
00861 _Node *help = __tmp._C_w_cur._C_parent;
00862 if(help != NULL)
00863 __tmp._C_w_cur = help;
00864 return __tmp;
00865 }
00866
00867 _Self operator>>(_Ctr_iterator i) {
00868 _Self __tmp = *this;
00869 __tmp._C_w_cur = (_Node*) *i;
00870 return __tmp;
00871 }
00872
00873 _Self& operator<<=(bool search_start) {
00874 _Node *help = _C_w_cur._C_parent;
00875 if(help != NULL)
00876 _C_w_cur = help;
00877 return *this;
00878 }
00879
00880 _Self& operator>>=(_Ctr_iterator i) {
00881 _C_w_cur = (_Node*) *i;
00882 return *this;
00883 }
00884
00885 _Self& operator=(_Itr __x) {
00886 _C_w_cur = __x._C_i_cur;
00887 return *this;
00888 }
00889
00890 _Self& operator=(_Tree_walker<_Tp,_Ref,_Ptr,_Ctr,_Iterator> __x) {
00891 _C_w_cur = __x._C_w_cur;
00892 return *this;
00893 }
00894 };
00895
00896 template <class _Tp, class _Ref, class _Ptr, class _Ctr, class _Iterator>
00897 class _Tree_iterator
00898 {
00899 public:
00900 typedef _Tree_iterator<_Tp,_Tp&,_Tp*,_Ctr,_Iterator> iterator;
00901 typedef _Tree_iterator<_Tp,const _Tp&,const _Tp*,_Ctr,_Iterator> const_iterator;
00902 typedef _Tree_iterator<_Tp,_Ref,_Ptr,_Ctr,_Iterator> _Self;
00903 typedef _Tree_walker<_Tp,_Ref,_Ptr,_Ctr,_Iterator> _Walk;
00904
00905 typedef bidirectional_iterator_tag iterator_category;
00906 typedef _Tp value_type;
00907 typedef _Ptr pointer;
00908 typedef _Ref reference;
00909 typedef _Tree_node<_Tp,_Ctr,_Iterator> _Node;
00910 typedef size_t size_type;
00911 typedef ptrdiff_t difference_type;
00912 typedef _Iterator _Ctr_iterator;
00913
00914 protected:
00915 _Node* _C_i_cur;
00916 vector<_Ctr_iterator> _C_i_cur_it;
00917
00918 public:
00919 _Tree_iterator() : _C_i_cur_it() {_C_i_cur_it.reserve(32);}
00920 _Tree_iterator(const iterator& __x) : _C_i_cur(__x._C_i_cur),
00921 _C_i_cur_it(__x._C_i_cur_it) {}
00922
00923 bool operator==(const _Self& __x) const
00924 { return (_C_i_cur->_C_parent == NULL &&
00925 __x._C_i_cur->_C_parent == NULL) ||
00926 ( _C_i_cur == __x._C_i_cur &&
00927 _C_i_cur_it == __x._C_i_cur_it);
00928 }
00929 bool operator!=(const _Self& __x) const
00930 { return (_C_i_cur->_C_parent != NULL ||
00931 __x._C_i_cur->_C_parent != NULL) &&
00932 ( _C_i_cur != __x._C_i_cur ||
00933 _C_i_cur_it != __x._C_i_cur_it);
00934 }
00935 reference operator*() const { return (*_C_i_cur)._C_data; }
00936
00937 #ifndef __SGI_STL_NO_ARROW_OPERATOR
00938 pointer operator->() const { return &(operator*()); }
00939 #endif
00940 ctree_data_hook& data_hook() { return (*_C_i_cur)._C_data_hook; }
00941
00942 private:
00943 void find_start_node();
00944 void find_next_node();
00945 void find_prev_node();
00946
00947 public:
00948 _Self& operator=(_Walk __x) {
00949 _C_i_cur = __x._C_w_cur;
00950 _C_i_cur_it.erase(_C_i_cur_it.begin(), _C_i_cur_it.end());
00951
00952
00953 find_start_node();
00954 return *this;
00955 }
00956
00957 _Self& operator++() {
00958 find_next_node();
00959 return *this;
00960 }
00961 _Self operator++(int) {
00962 _Self __tmp = *this;
00963 ++*this;
00964 return __tmp;
00965 }
00966
00967 _Self& operator--() {
00968 find_prev_node();
00969 return *this;
00970 }
00971 _Self operator--(int) {
00972 _Self __tmp = *this;
00973 --*this;
00974 return __tmp;
00975 }
00976 };
00977
00978 #ifndef __VGTL_CLASS_PARTIAL_SPECIALIZATION
00979 template <class _Tp, class _Ref, class _Ptr, class _Ctr>
00980 inline bidirectional_iterator_tag
00981 iterator_category(const _Tree_iterator<_Tp,_Ref,_Ptr,_Ctr>&)
00982 {
00983 return bidirectional_iterator_tag();
00984 }
00985
00986 template <class _Tp, class _Ref, class _Ptr, class _Ctr>
00987 inline _Tp*
00988 value_type(const _Tree_iterator<_Tp,_Ref,_Ptr,_Ctr>&)
00989 {
00990 return 0;
00991 }
00992
00993 template <class _Tp, class _Ref, class _Ptr, class _Ctr>
00994 inline ptrdiff_t*
00995 distance_type(const _Tree_iterator<_Tp,_Ref,_Ptr,_Ctr>&)
00996 {
00997 return 0;
00998 }
00999
01000 #endif
01001
01002 template <class _Tp, class _Ref, class _Ptr, class _Ctr, class _Iterator>
01003 void
01004 _Tree_iterator<_Tp,_Ref,_Ptr,_Ctr,_Iterator>::find_start_node()
01005 {
01006 _Node* __s = _C_i_cur;
01007 _Ctr_iterator __help;
01008
01009 while(__s->_C_parent != NULL)
01010 {
01011 __help = ((_Node*)__s->_C_parent)->get_entry_iterator((void*)__s);
01012 _C_i_cur_it.push_front(__help);
01013 __s = (_Node*)__s->_C_parent;
01014 }
01015 while(!_C_i_cur->_C_children.empty())
01016 {
01017 __help = _C_i_cur->_C_children.begin();
01018 _C_i_cur = (_Node*)*__help;
01019 _C_i_cur_it.push_back(__help);
01020 }
01021 }
01022
01023 template <class _Tp, class _Ref, class _Ptr, class _Ctr, class _Iterator>
01024 void
01025 _Tree_iterator<_Tp,_Ref,_Ptr,_Ctr,_Iterator>::find_next_node()
01026 {
01027 if(_C_i_cur->_C_parent != NULL)
01028 {
01029 _Ctr_iterator help = _C_i_cur_it.back();
01030 _C_i_cur_it.pop_back();
01031 ++help;
01032 if(help != ((_Node*)_C_i_cur->_C_parent)->_C_children.end())
01033 {
01034 do
01035 {
01036 _C_i_cur_it.push_back(help);
01037 _C_i_cur = (_Node*)*help;
01038 help = _C_i_cur->_C_children.begin();
01039 } while(!_C_i_cur->_C_children.empty());
01040 }
01041 else
01042 {
01043 _C_i_cur = (_Node*)_C_i_cur->_C_parent;
01044 }
01045 }
01046 }
01047
01048 template <class _Tp, class _Ref, class _Ptr, class _Ctr, class _Iterator>
01049 void
01050 _Tree_iterator<_Tp,_Ref,_Ptr,_Ctr,_Iterator>::find_prev_node()
01051 {
01052 _Ctr_iterator help;
01053
01054 if(_C_i_cur->_C_children.empty())
01055 {
01056 while(1)
01057 {
01058 if(_C_i_cur->_C_parent == NULL)
01059 break;
01060 help = _C_i_cur_it.back();
01061 _C_i_cur_it.pop_back();
01062 _C_i_cur = (_Node*)_C_i_cur->_C_parent;
01063 if(help != _C_i_cur->_C_children.begin())
01064 {
01065 --help;
01066 _C_i_cur_it.push_back(help);
01067 _C_i_cur = (_Node*)*help;
01068 break;
01069 }
01070 }
01071 }
01072 else
01073 {
01074 help = _C_i_cur->_C_children.end();
01075 --help;
01076 _C_i_cur_it.push_back(help);
01077 _C_i_cur = (_Node*)*help;
01078 }
01079 }
01080
01081
01082
01083
01084
01085
01086
01087
01088 #ifdef __VGTL_USE_STD_ALLOCATORS
01089
01090
01091
01092 template <class _Tp, class _Ctr, class _TI, class _Allocator, bool _IsStatic>
01093 class _Tree_alloc_base {
01094 public:
01095 typedef typename _Alloc_traits<_Tp, _Allocator>::allocator_type
01096 allocator_type;
01097 allocator_type get_allocator() const { return _Node_allocator; }
01098
01099 _Tree_alloc_base(const allocator_type& __a) : _Node_allocator(__a) {}
01100
01101 protected:
01102 _Tree_node<_Tp,_Ctr,_TI>* _C_get_node()
01103 { return _Node_allocator.allocate(1); }
01104 void _C_put_node(_Tree_node<_Tp,_Ctr,_TI>* __p)
01105 { _Node_allocator.deallocate(__p, 1); }
01106
01107 protected:
01108 typename _Alloc_traits<_Tree_node<_Tp,_Ctr,_TI>, _Allocator>::allocator_type
01109 _Node_allocator;
01110
01111 protected:
01112 _Tree_node<_Tp,_Ctr,_TI>* _C_node;
01113 };
01114
01115
01116
01117 template <class _Tp, class _Ctr, class _TI, class _Allocator>
01118 class _Tree_alloc_base<_Tp, _Ctr, _TI, _Allocator, true> {
01119 public:
01120 typedef typename _Alloc_traits<_Tp, _Allocator>::allocator_type
01121 allocator_type;
01122 allocator_type get_allocator() const { return allocator_type(); }
01123
01124 _Tree_alloc_base(const allocator_type&) {}
01125
01126 protected:
01127 typedef typename _Alloc_traits<_Tree_node<_Tp,_Ctr,_TI>, _Allocator>::_Alloc_type
01128 _Alloc_type;
01129 _Tree_node<_Tp,_Ctr,_TI>* _C_get_node()
01130 { return _Alloc_type::allocate(1); }
01131 void _C_put_node(_Tree_node<_Tp,_Ctr,_TI>* __p)
01132 { _Alloc_type::deallocate(__p, 1); }
01133
01134 protected:
01135 _Tree_node<_Tp,_Ctr,_TI>* _C_node;
01136 };
01137
01138 template <class _Tp, class _Ctr, class _TI, class _Alloc>
01139 class _Tree_base
01140 : public _Tree_alloc_base<_Tp, _Ctr, _TI, _Alloc,
01141 _Alloc_traits<_Tp, _Alloc>::_S_instanceless>
01142 {
01143 public:
01144 typedef _Tree_alloc_base<_Tp, _Ctr, _TI, _Alloc,
01145 _Alloc_traits<_Tp, _Alloc>::_S_instanceless>
01146 _Base;
01147 typedef typename _Base::allocator_type allocator_type;
01148
01149 typedef _Ctr container_type;
01150 typedef _TI container_iterator;
01151
01152 _Tree_base(const allocator_type& __a) : _Base(__a) {
01153 _C_node = _C_get_node();
01154 __VGTL_TRY {
01155 construct(&_C_node->_C_children);
01156 }
01157 __VGTL_UNWIND(_C_put_node(_C_node));
01158 _C_node->_C_parent = NULL;
01159 _C_node->_C_data_hook.f = 0;
01160 }
01161 ~_Tree_base() {
01162 clear();
01163 _C_put_node(_C_node);
01164 }
01165
01166 void clear();
01167 void clear_children()
01168 { _C_node->clear_children(); }
01169
01170 template <class _Output_Iterator>
01171 void add_all_children(_Output_Iterator fi,
01172 _Tree_node<_Tp,_Ctr,_TI>* _parent);
01173 };
01174 #else
01175
01176 template <class _Tp, class _Ctr, class _Iterator, class _Alloc>
01177 class _Tree_base
01178 {
01179 public:
01180 typedef _Alloc allocator_type;
01181 allocator_type get_allocator() const { return allocator_type(); }
01182
01183 typedef _Ctr container_type;
01184 typedef _Iterator container_iterator;
01185
01186 _Tree_base(const allocator_type&) {
01187 _C_node = _C_get_node();
01188 _C_node->_C_children();
01189 _C_node->_C_parent = NULL;
01190 }
01191 ~_Tree_base() {
01192 clear();
01193 _C_put_node(_C_node);
01194 }
01195
01196 void clear();
01197
01198 protected:
01199 typedef simple_alloc<_Tree_node<_Tp,_Ctr,_Iterator>, _Alloc> _Alloc_type;
01200 _Tree_node<_Tp,_Ctr,_Iterator>* _C_get_node()
01201 { return _Alloc_type::allocate(1); }
01202 void _C_put_node(_Tree_node<_Tp,_Ctr,_Iterator>* __p)
01203 { _Alloc_type::deallocate(__p, 1); }
01204
01205 protected:
01206 _Tree_node<_Tp,_Ctr,_Iterator>* _C_node;
01207
01208 void clear_children()
01209 { _C_node->clear_children(); }
01210
01211 template <class _Output_Iterator>
01212 void add_all_children(_Output_Iterator fi,
01213 _Tree_node<_Tp,_Ctr,_Iterator>* _parent);
01214 };
01215
01216 #endif
01217
01218 template <class _Tp, class _Ctr, class _Iterator, class _Alloc>
01219 template <class _Output_Iterator>
01220 inline void
01221 _Tree_base<_Tp,_Ctr,_Iterator,_Alloc>::add_all_children(_Output_Iterator fi,
01222 _Tree_node<_Tp,_Ctr,_Iterator>* _parent)
01223 { _C_node->add_all_children(fi, _parent); };
01224
01225 template <class _Tp, class _Ctr, class _Iterator, class _Alloc>
01226 void
01227 _Tree_base<_Tp,_Ctr,_Iterator,_Alloc>::clear()
01228 {
01229 this->_C_node->clear_tree();
01230 this->_C_node->clear_children();
01231 }
01232
01233 template <class _Tp, class _Ctr, class _Iterator, class _Inserter, class _Alloc>
01234 class __Tree : public _Tree_base<_Tp, _Ctr, _Iterator, _Alloc>
01235 {
01236 public:
01237 typedef _Ctr container_type;
01238 typedef _Iterator container_iterator;
01239 typedef _Inserter container_insert_arg;
01240
01241 protected:
01242 typedef void* _Void_pointer;
01243 typedef _Tree_base<_Tp, container_type, container_iterator, _Alloc> _Base;
01244 typedef _Tree_node<_Tp, container_type, container_iterator> _Node;
01245 typedef __Tree<_Tp,_Ctr,_Iterator,_Inserter,_Alloc> _Self;
01246
01247 public:
01248 typedef _Tp value_type;
01249 typedef _Node node_type;
01250 typedef value_type* pointer;
01251 typedef const value_type* const_pointer;
01252 typedef value_type& reference;
01253 typedef const value_type& const_reference;
01254 typedef size_t size_type;
01255 typedef ptrdiff_t difference_type;
01256
01257
01258 typedef typename _Base::allocator_type allocator_type;
01259 allocator_type get_allocator() const { return _Base::get_allocator(); }
01260
01261 public:
01262 typedef _Tree_iterator<_Tp,_Tp&,_Tp*,container_type,container_iterator> iterator;
01263 typedef _Tree_iterator<_Tp,const _Tp&,const _Tp*,container_type,container_iterator> const_iterator;
01264
01265 #ifdef __VGTL_CLASS_PARTIAL_SPECIALIZATION
01266 typedef reverse_iterator<const_iterator> const_reverse_iterator;
01267 typedef reverse_iterator<iterator> reverse_iterator;
01268 #else
01269 typedef reverse_bidirectional_iterator<const_iterator,value_type,
01270 const_reference,difference_type>
01271 const_reverse_iterator;
01272 typedef reverse_bidirectional_iterator<iterator,value_type,reference,
01273 difference_type>
01274 reverse_iterator;
01275 #endif
01276
01277 typedef _Tree_walker<_Tp,_Tp&,_Tp*,container_type,container_iterator> walker;
01278 typedef _Tree_walker<_Tp,const _Tp&,const _Tp*,container_type,container_iterator> const_walker;
01279
01280 typedef _RTree_walker<_Tp,_Tp&,_Tp*,container_type,container_iterator> recursive_walker;
01281 typedef _RTree_walker<_Tp,const _Tp&,const _Tp*,container_type,container_iterator> const_recursive_walker;
01282
01283 protected:
01284 typedef _Tree_walker_base<_Tp,_Tp&,_Tp*,container_type,container_iterator> __walker_base;
01285 typedef _Tree_walker_base<_Tp,const _Tp&,const _Tp*,container_type,container_iterator> __const_walker_base;
01286
01287 protected:
01288 #ifdef __VGTL_HAS_NAMESPACES
01289 using _Base::_C_node;
01290 using _Base::_C_put_node;
01291 using _Base::_C_get_node;
01292 #endif
01293
01294 protected:
01295 _Node* _C_create_node(const _Tp& __x)
01296 {
01297 _Node* __p = _C_get_node();
01298 __VGTL_TRY {
01299 construct(&__p->_C_data, __x);
01300 construct(&__p->_C_children);
01301 }
01302 __VGTL_UNWIND(_C_put_node(__p));
01303 __p->_C_data_hook.f = 0;
01304 __p->_C_parent = NULL;
01305 return __p;
01306 }
01307
01308 _Node* _C_create_node()
01309 {
01310 _Node* __p = _C_get_node();
01311 __VGTL_TRY {
01312 construct(&__p->_C_data);
01313 construct(&__p->_C_children);
01314 }
01315 __VGTL_UNWIND(_C_put_node(__p));
01316 __p->_C_data_hook.f = 0;
01317 __p->_C_parent = NULL;
01318 return __p;
01319 }
01320
01321 public:
01322 explicit __Tree(const allocator_type& __a = allocator_type()) : _Base(__a) {}
01323
01324
01325
01326
01327
01328 walker root(walker_type wt = cw_pre_post,
01329 bool front_to_back = true,
01330 bool depth_first = true)
01331 { walker __help(_C_node, (int)wt, front_to_back, depth_first);
01332 return __help; }
01333
01334 const_walker root(walker_type wt = cw_pre_post,
01335 bool front_to_back = true,
01336 bool depth_first = true) const
01337 { const_walker __help(_C_node, (int)wt, front_to_back, depth_first);
01338 return __help; }
01339
01340 walker through()
01341 { walker __help(_C_node, 0, 0, 0);
01342 return __help; }
01343 const_walker through() const
01344 { const_walker __help(_C_node, 0, 0, 0);
01345 return __help; }
01346
01347 walker begin(walker_type wt = cw_pre_post,
01348 bool front_to_back = true,
01349 bool depth_first = true)
01350 { walker __help = root(wt, front_to_back, depth_first);
01351 ++__help;
01352 return __help; }
01353 const_walker begin(walker_type wt = cw_pre_post,
01354 bool front_to_back = true,
01355 bool depth_first = true) const
01356 { const_walker __help = root(wt, front_to_back, depth_first);
01357 ++__help;
01358 return __help; }
01359
01360 walker end(walker_type wt = cw_pre_post,
01361 bool front_to_back = true,
01362 bool depth_first = true)
01363 { walker __help(_C_node, (int)wt, front_to_back, depth_first, false);
01364 return __help; }
01365 const_walker end(walker_type wt = cw_pre_post,
01366 bool front_to_back = true,
01367 bool depth_first = true) const
01368 { const_walker __help(_C_node, (int)wt, front_to_back,
01369 depth_first, false);
01370 return __help; }
01371
01372
01373
01374
01375
01376
01377
01378
01379
01380
01381
01382 reverse_iterator rbegin()
01383 { return reverse_iterator(end()); }
01384 reverse_iterator rend()
01385 { return reverse_iterator(begin()); }
01386
01387 const_reverse_iterator rbegin() const
01388 { return const_reverse_iterator(end()); }
01389 const_reverse_iterator rend() const
01390 { return const_reverse_iterator(begin()); }
01391
01392 bool empty() const { return _C_node->_C_children.empty(); }
01393
01394 size_type size() const {
01395 size_type __result = 0;
01396 distance(begin(cw_pre), end(cw_pre), __result);
01397 return __result;
01398 }
01399
01400 size_type max_size() const { return size_type(-1); }
01401
01402 reference getroot() { return *root(cw_pre,true,true); }
01403 const_reference getroot() const { return *root(cw_pre,true,true); }
01404
01405 void swap(_Self& __x)
01406 { __STD::swap(_C_node, __x._C_node); }
01407
01408
01409 void insert_child(const __walker_base& __position, const _Tp& __x,
01410 const container_insert_arg& __It)
01411 { _Node* __tmp = _C_create_node(__x);
01412 __position._C_w_cur->_C_children.insert(__It,__tmp);
01413 __tmp->_C_parent = __position._C_w_cur;
01414 }
01415 void insert_child(const __walker_base& __position,
01416 const container_insert_arg& __It)
01417 { insert_child(__position, _Tp(), __It); }
01418
01419 void insert_children(const __walker_base& __position, size_type __n,
01420 const _Tp& __x, const container_iterator& __It)
01421 { _Node* __tmp;
01422 vector<_Node *> __Ctr;
01423 __Ctr.reserve(__n);
01424
01425 for(int i=n; i>0; i--)
01426 {
01427 __tmp = _C_create_node(__x);
01428 __Ctr.insert(__Ctr.end(), __tmp);
01429 __tmp->_C_parent = __position._C_w_cur;
01430 }
01431 __position._C_w_cur->_C_children.insert(__It,
01432 __Ctr.begin(), __Ctr.end());
01433 }
01434
01435
01436 void insert_subtree(const __walker_base& __position,
01437 _Self& __subtree, const container_iterator& __It)
01438 {
01439 __subtree.add_all_children(inserter(__position._C_w_cur->_C_children, __It), __position._C_w_cur);
01440 __subtree.clear_children();
01441 }
01442
01443
01444 void erase(const __walker_base& __position)
01445 { _Node* __tmp = __position._C_w_cur;
01446 _Node* __parent = (_Node*)__tmp->_C_parent;
01447
01448 if(__parent != NULL)
01449 {
01450 container_iterator __ip = __parent->get_entry_iterator(__tmp);
01451
01452 if(!__tmp->_C_children.empty())
01453 {
01454 container_iterator __it = __tmp->_C_children.end();
01455
01456 *__ip = *--__it;
01457 ((_Node*)*__it)->_C_parent = __parent;
01458 __tmp->_C_children.erase(__it);
01459 if(!__tmp->_C_children.empty())
01460 {
01461 __tmp->add_all_children(inserter(__parent->_C_children, __ip),
01462 __parent);
01463 __tmp->clear_children();
01464 }
01465 }
01466 _C_put_node(__tmp);
01467 }
01468 }
01469
01470
01471 _Node* erase_tree(const __walker_base& __position)
01472 { _Node* __tmp = __position._C_w_cur;
01473 _Node* __parent = (_Node*)__tmp->_C_parent;
01474
01475 if(__parent == NULL)
01476 {
01477 this->_C_node = _C_create_node();
01478 return __tmp;
01479 }
01480 else
01481 {
01482 container_iterator __ip = __parent->get_entry_iterator(__tmp);
01483
01484 __parent->_C_children.erase(__ip);
01485 __parent = _C_create_node();
01486 __parent->_C_data = 0;
01487 __parent->_C_children.insert(__parent->_C_children.end(), __tmp);
01488 __tmp->_C_parent = __parent;
01489 return __parent;
01490 }
01491 }
01492
01493
01494
01495 bool erase_child(const __walker_base& __position,
01496 const container_iterator& __It)
01497 {
01498 _Node* __chld = (_Node*)*__It;
01499
01500 if(!__chld->_C_children.empty())
01501 { return false; }
01502 else
01503 {
01504 __position->_C_w_cur->_C_children.erase(__It);
01505 _C_put_node(__chld);
01506 return true;
01507 }
01508 }
01509
01510
01511 _Node* erase_subtree(const __walker_base& __position,
01512 const container_iterator& __It)
01513 { _Node* __chld = (_Node*)*__It;
01514 _Node* __tmp = _C_create_node();
01515
01516 __position->_C_w_cur->_C_children.erase(__It);
01517 __chld->_C_parent = __tmp;
01518 __tmp->_C_data = 0;
01519 __tmp->_C_children.insert(__tmp->_C_children.end(), __chld);
01520 return __tmp;
01521 }
01522
01523
01524 bool is_root() { return _C_node->_C_parent == NULL; }
01525
01526 size_type depth(const walker& __position)
01527 { return __position._C_w_cur_it.size(); }
01528
01529 size_type depth(const recursive_walker& __position)
01530 { size_type __d=0;
01531 _Node* __x = __position._C_w_cur;
01532 while(__x != NULL)
01533 {
01534 __x = (_Node*)__x._C_parent;
01535 __d++;
01536 }
01537 return __d;
01538 }
01539
01540 void clear() { _Base::clear(); }
01541
01542 __Tree(size_type __n, const _Tp& __value,
01543 const allocator_type& __a = allocator_type()) : _Base(__a)
01544 { insert(root(), __n, __value); }
01545 explicit __Tree(size_type __n) : _Base(allocator_type())
01546 { insert(root(), __n, _Tp()); }
01547
01548 private:
01549 _Node* copy_subtree(_Node* __x, _Node* __parent)
01550 { container_iterator cit;
01551
01552 _Node* __h = _C_create_node();
01553 construct(&__h->_C_data, __x->_C_data);
01554 __h->_C_parent = (void*)__parent;
01555 for(cit = __x->_C_children.begin(); cit != __x->_C_children.end(); ++cit)
01556 __h->_C_children.insert(__h->_C_children.end(),
01557 copy_subtree((_Node*)*cit, __h));
01558 return __h;
01559 }
01560
01561 public:
01562 __Tree(const _Self& __x) :
01563 _Base(__x.get_allocator())
01564 { container_iterator cit;
01565 for(cit = __x._C_node->_C_children.begin();
01566 cit != __x._C_node->_C_children.end(); ++cit)
01567 this->_C_node->_C_children.insert(this->_C_node->_C_children.end(),
01568 copy_subtree((_Node*)*cit, this->_C_node));
01569 }
01570
01571 ~__Tree() {}
01572
01573 _Self& operator=(const _Self& __x);
01574
01575 _Self& operator=(_Node* __x)
01576 {
01577 this->_C_node = __x;
01578 return *this;
01579 }
01580
01581 friend bool operator== __VGTL_NULL_TMPL_ARGS (
01582 const __Tree& __x, const __Tree& __y);
01583 };
01584
01585 template <class _Tp, class _Ctr, class _Iterator, class _Inserter, class _Alloc>
01586 inline bool operator==(const __Tree<_Tp,_Ctr,_Iterator,_Inserter,_Alloc>& __x,
01587 const __Tree<_Tp,_Ctr,_Iterator,_Inserter,_Alloc>& __y)
01588 {
01589 typedef typename __Tree<_Tp,_Ctr,_Iterator,_Inserter,_Alloc>::const_walker const_walker;
01590 const_walker __w1 = __x.begin(cw_pre);
01591 const_walker __w2 = __y.begin(cw_pre);
01592 const_walker __e1 = __x.end(cw_pre);
01593 const_walker __e2 = __y.end(cw_pre);
01594
01595
01596 for(; __w1 != __e1 && __w2 != __e2 ; ++__w1, ++__w2)
01597 if(__w1.in_preorder() != __w2.in_preorder() ||
01598 (__w1.in_preorder() && *__w1 != *__w2))
01599 return false;
01600 return __w1 == __e1 && __w2 == __e2;
01601 }
01602
01603 template <class _Tp, class _Ctr, class _Iterator, class _Inserter, class _Alloc>
01604 inline bool operator<(const __Tree<_Tp,_Ctr,_Iterator,_Inserter,_Alloc>& __x,
01605 const __Tree<_Tp,_Ctr,_Iterator,_Inserter,_Alloc>& __y)
01606 {
01607 return lexicographical_compare(__x.begin(cw_pre), __x.end(cw_pre),
01608 __y.begin(cw_pre), __y.end(cw_pre));
01609 }
01610
01611 template <class _Tp, class _Ctr, class _Iterator, class _Inserter, class _Alloc>
01612 __Tree<_Tp, _Ctr, _Iterator, _Inserter, _Alloc>&
01613 __Tree<_Tp, _Ctr, _Iterator, _Inserter, _Alloc>::operator=(const __Tree<_Tp, _Ctr, _Iterator, _Inserter, _Alloc>& __x)
01614 {
01615 container_iterator cit;
01616
01617 if (this != &__x) {
01618 this->_C_node->clear_tree();
01619 this->_C_node->_C_children.erase(this->_C_node->_C_children.begin(),
01620 this->_C_node->_C_children.end());
01621 for(cit = __x._C_node->_C_children.begin();
01622 cit != __x._C_node->_C_children.end(); ++cit)
01623 this->_C_node->_C_children.insert(this->_C_node->_C_children.end(),
01624 copy_subtree((_Node*)*cit, this->_C_node));
01625 }
01626 return *this;
01627 }
01628
01629 template <class _Tp,
01630 template <class __Ty, class __AllocT> class _SequenceCtr = vector,
01631 class _PtrAlloc = __VGTL_DEFAULT_ALLOCATOR(void *),
01632 class _Alloc = __VGTL_DEFAULT_ALLOCATOR(_Tp) >
01633 class ntree : public __Tree<_Tp, _SequenceCtr<void*, _PtrAlloc>,
01634 typename _SequenceCtr<void*, _PtrAlloc>::iterator,
01635 typename _SequenceCtr<void*, _PtrAlloc>::iterator,
01636 _Alloc>
01637 {
01638 private:
01639 typedef typename _SequenceCtr<void*, _PtrAlloc>::iterator ___cit;
01640 protected:
01641 typedef ntree<_Tp,_SequenceCtr,_PtrAlloc,_Alloc> _Self;
01642
01643 public:
01644 void insert(const __walker_base& __position, const _Tp& __x)
01645 { _Node* __tmp = _C_create_node(__x);
01646 _Node* __parent = (_Node*)__position._C_w_cur->_C_parent;
01647 container_iterator __it;
01648
01649 if(__parent == NULL)
01650 {
01651 __position._C_w_cur->add_all_children(
01652 back_inserter(__tmp->_C_children), __tmp);
01653 __tmp->_C_parent = __position._C_w_cur;
01654 __position._C_w_cur->clear_children();
01655 __position._C_w_cur->_C_children.insert(
01656 __position._C_w_cur->_C_children.end(), __tmp);
01657 }
01658 else
01659 {
01660 __it = __parent->get_entry_iterator(__position._C_w_cur);
01661 *__it = __tmp;
01662 __tmp->clear_children();
01663 __tmp->_C_children.insert(__tmp->_C_children.end(),
01664 __position._C_w_cur);
01665 __position._C_w_cur->_C_parent = __tmp;
01666 __tmp->_C_parent = __parent;
01667 }
01668 }
01669 void insert(const __walker_base& __position)
01670 { insert(__position, _Tp()); }
01671
01672
01673 void push_child(const __walker_base& __position, const _Tp& __x)
01674 { insert_child(__position, __x,
01675 __position._C_w_cur->_C_children.end()); }
01676 void push_child(const __walker_base& __position)
01677 { push_child(__position, _Tp()); }
01678
01679 void push_children(const __walker_base& __position, size_type __n,
01680 const _Tp& __x)
01681 { insert_children(__position, __n, __x,
01682 __position._C_w_cur->_C_children.end()); }
01683 void push_children(const __walker_base& __position, size_type __n)
01684 { push_children(__position, n, _Tp()); }
01685
01686
01687 void unshift_child(const __walker_base& __position, const _Tp& __x)
01688 { insert_child(__position, __x,
01689 __position._C_w_cur->_C_children.begin()); }
01690 void unshift_child(const __walker_base& __position)
01691 { unshift_child(__position, _Tp()); }
01692
01693 void unshift_children(const __walker_base& __position, size_type __n,
01694 const _Tp& __x)
01695 { insert_children(__position, __n, __x,
01696 __position._C_w_cur->_C_children.begin()); }
01697 void unshift_children(const __walker_base& __position, size_type __n)
01698 { unshift_children(__position, n, _Tp()); }
01699
01700
01701 void push_subtree(const __walker_base& __position, _Self& __subtree)
01702 {
01703 __subtree.add_all_children(back_inserter(__position._C_w_cur->_C_children), __position._C_w_cur);
01704 __subtree.clear_children();
01705 }
01706
01707 void unshift_subtree(const __walker_base& __position, _Self& __subtree)
01708 {
01709 __subtree.add_all_children(front_inserter(__position._C_w_cur->_C_children), __position._C_w_cur);
01710 __subtree.clear_children();
01711 }
01712
01713
01714 bool pop_child(const __walker_base& __position)
01715 { if(__position._C_w_cur->_C_children.empty())
01716 { return false; }
01717 else
01718 {
01719 container_iterator __it = __position._C_w_cur->_C_children.end();
01720 return erase_child(__position, --__it);
01721 }
01722 }
01723
01724 bool shift_child(const __walker_base& __position)
01725 { if(__position._C_w_cur->_C_children.empty())
01726 { return false; }
01727 else
01728 {
01729 container_iterator __it = __position._C_w_cur->_C_children.begin();
01730 return erase_child(__position, __it);
01731 }
01732 }
01733
01734
01735 _Node* pop_subtree(const __walker_base& __position)
01736 {
01737 if(__position._C_w_cur->_C_children.empty())
01738 { return NULL; }
01739 else
01740 {
01741 container_iterator __it = __position._C_w_cur->_C_children.end();
01742 return erase_subtree(__position, --__it);
01743 }
01744 }
01745
01746 _Node* shift_subtree(const __walker_base& __position)
01747 {
01748 if(__position._C_w_cur->_C_children.empty())
01749 { return NULL; }
01750 else
01751 {
01752 container_iterator __it = __position._C_w_cur->_C_children.begin();
01753 return erase_subtree(__position, __it);
01754 }
01755 }
01756
01757 _Self& operator=(_Node* __x)
01758 {
01759 this->_C_node = __x;
01760 return *this;
01761 }
01762 };
01763
01764 template <class _Tp,
01765 template <class __Key, class __Ty, class __Compare, class __AllocT> class _AssocCtr = multimap,
01766 class _Key = string,
01767 class _Compare = less<_Key>,
01768 class _PtrAlloc = __VGTL_DEFAULT_ALLOCATOR(void *),
01769 class _Alloc = __VGTL_DEFAULT_ALLOCATOR(_Tp) >
01770 class atree : public __Tree<_Tp, _AssocCtr<_Key, void*, _Compare, _PtrAlloc>,
01771 pair_adaptor<typename _AssocCtr<_Key, void*,
01772 _Compare, _PtrAlloc>::iterator>,
01773 _Key, _Alloc>
01774 {
01775 protected:
01776 typedef atree<_Tp,_AssocCtr,_Key,_Compare,_PtrAlloc,_Alloc> _Self;
01777
01778 public:
01779 _Self& operator=(_Node* __x)
01780 {
01781 this->_C_node = __x;
01782 return *this;
01783 }
01784
01785 void insert(const __walker_base& __position, const _Tp& __x, const _Key& __k)
01786 { _Node* __tmp = _C_create_node(__x);
01787 _Node* __parent = (_Node*)__position._C_w_cur->_C_parent;
01788 container_iterator __it;
01789
01790 if(__parent == NULL)
01791 {
01792 __position._C_w_cur->add_all_children(
01793 inserter(__tmp->_C_children.end()), __tmp);
01794 __tmp->_C_parent = __position._C_w_cur;
01795 __position._C_w_cur->clear_children();
01796 __position._C_w_cur->_C_children.insert(__k, __tmp);
01797 }
01798 else
01799 {
01800 __it = __parent->get_entry_iterator(__position._C_w_cur);
01801 *__it = __tmp;
01802 __tmp->clear_children();
01803 __tmp->_C_children.insert(__k, __position._C_w_cur);
01804 __position._C_w_cur->_C_parent = __tmp;
01805 __tmp->_C_parent = __parent;
01806 }
01807 }
01808 void insert(const __walker_base& __position, const _Key& __k)
01809 { insert(__position, _Tp(), __k); }
01810
01811
01812 };
01813
01814 template <class _Key, class _Compare = less<_Key>,
01815 template <class __Key, class __Compare, class __AllocT> class _AssocCtr = multiset,
01816 class _PtrAlloc = __VGTL_DEFAULT_ALLOCATOR(void *),
01817 class _Alloc = __VGTL_DEFAULT_ALLOCATOR(_Key&) >
01818 class stree : public __Tree<_Key, _AssocCtr<_Key&, pointer_adaptor<_Compare>,
01819 _PtrAlloc>,
01820 typename _AssocCtr<_Key&, pointer_adaptor<_Compare>,
01821 _PtrAlloc>::iterator,
01822 _Key&, _Alloc>
01823 {
01824 protected:
01825 typedef stree<_Key,_Compare,_AssocCtr,_PtrAlloc,_Alloc> _Self;
01826
01827 public:
01828 _Self& operator=(_Node* __x)
01829 {
01830 this->_C_node = __x;
01831 return *this;
01832 }
01833 };
01834
01835 __VGTL_END_NAMESPACE
01836
01837 #endif // __VGTL_GRAPH_H_