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_DAGBASE_H_
00032 #define __VGTL_DAGBASE_H_
00033
00034 #include <vgtl_helpers.h>
00035 #include <vgtl_intadapt.h>
00036
00037 __VGTL_BEGIN_NAMESPACE
00038
00040
00044 template <class _Tp, class _Ctr, class _Iterator>
00045 class _DG_node
00046 {
00047 private:
00048 typedef void* _Void_pointer;
00049 typedef _Iterator _Ctr_iterator;
00050 typedef _DG_node<_Tp,_Ctr,_Iterator> _Self;
00051
00052 public:
00054 _Tp _C_data;
00056 _Ctr _C_parents;
00058 _Ctr _C_children;
00060 int _C_visited;
00061
00063 _DG_node() {
00064 _C_visited=0;
00065 __VGTL_TRY {
00066 _VGTL_Construct_1(&_C_data);
00067 _VGTL_Construct_1(&_C_children);
00068 _VGTL_Construct_1(&_C_parents);
00069 }
00070 __VGTL_UNWIND( ;);
00071 }
00072
00074 ~_DG_node() {
00075 _VGTL_Destroy(&_C_data);
00076 _VGTL_Destroy(&_C_children);
00077 _VGTL_Destroy(&_C_parents);
00078 }
00079
00081 void clear_children()
00082 { _C_children.erase(_C_children.begin(), _C_children.end()); }
00084 void clear_parents()
00085 { _C_parents.erase(_C_parents.begin(), _C_parents.end()); }
00086
00088 _Ctr_iterator get_childentry_iterator(const _Void_pointer __p)
00089 { _Ctr_iterator __tmp;
00090 _Ctr_iterator __end = _C_children.end();
00091 for(__tmp = _C_children.begin(); __tmp != __end; ++__tmp)
00092 if(*__tmp == __p) break;
00093 return __tmp;
00094 }
00095
00097 _Ctr_iterator get_parententry_iterator(const _Void_pointer __p)
00098 { _Ctr_iterator __tmp;
00099 _Ctr_iterator __end = _C_parents.end();
00100 for(__tmp = _C_parents.begin(); __tmp != __end; ++__tmp)
00101 if(*__tmp == __p) break;
00102 return __tmp;
00103 }
00104
00105
00106 #ifdef __VGTL_MEMBER_TEMPLATES
00107
00111 template <class _Output_Iterator>
00112 void add_all_children(_Output_Iterator fi, _Self* _parent);
00113
00114
00119 template <class _Output_Iterator>
00120 void add_all_parents(_Output_Iterator fi, _Self* _child);
00121
00123 template <class Compare>
00124 void sort_child_edges(_Ctr_iterator first, _Ctr_iterator last, Compare comp)
00125 {
00126 sort(first, last, _G_compare_adaptor<Compare, _Self>(comp));
00127 }
00128
00130 template <class Compare>
00131 void sort_parent_edges(_Ctr_iterator first, _Ctr_iterator last, Compare comp)
00132 {
00133 sort(first, last, _G_compare_adaptor<Compare, _Self>(comp));
00134 }
00135 #endif
00136 };
00137
00138 #ifdef __VGTL_MEMBER_TEMPLATES
00139 template <class _Tp, class _Ctr, class _Iterator>
00140 template <class _Output_Iterator>
00141 inline void
00142 _DG_node<_Tp,_Ctr,_Iterator>::
00143 add_all_children(_Output_Iterator fi, _Self* _parent)
00144 { _Ctr_iterator it;
00145 for(it = _C_children.begin();
00146 it != _C_children.end();
00147 ++it)
00148 {
00149 *fi++ = *it;
00150 ((_Self*)*it)->_C_parents.insert(((_Self*)*it)->_C_parents.end(), _parent);
00151 }
00152 };
00153
00154 template <class _Tp, class _Ctr, class _Iterator>
00155 template <class _Output_Iterator>
00156 inline void
00157 _DG_node<_Tp,_Ctr,_Iterator>::
00158 add_all_parents(_Output_Iterator fi, _Self* _child)
00159 { _Ctr_iterator it;
00160 for(it = _C_parents.begin();
00161 it != _C_parents.end();
00162 ++it)
00163 {
00164 *fi++ = *it;
00165 ((_Self*)*it)->_C_children.insert(((_Self*)*it)->_C_children.end(), _child);
00166 }
00167 };
00168 #endif
00169
00170 #if __VGTL_USE_STD_ALLOCATORS
00171
00173
00184 template <class _Tp, class _Ctr, class _TI, class _Allocator, bool _IsStatic>
00185 class _DG_alloc_base {
00186 public:
00187 typedef typename std::_Alloc_traits<_Tp, _Allocator>::allocator_type
00188 allocator_type;
00189 allocator_type get_allocator() const { return _Node_allocator; }
00190
00191 _DG_alloc_base(const allocator_type& __a) : _Node_allocator(__a) {}
00192
00193 protected:
00195 _DG_node<_Tp,_Ctr,_TI>* _C_get_node()
00196 { return _Node_allocator.allocate(1); }
00198 void _C_put_node(_DG_node<_Tp,_Ctr,_TI>* __p)
00199 { _Node_allocator.deallocate(__p, 1); }
00200
00201 protected:
00202 typename std::_Alloc_traits<_DG_node<_Tp,_Ctr,_TI>, _Allocator>::allocator_type
00203 _Node_allocator;
00204
00205 protected:
00207 _DG_node<_Tp,_Ctr,_TI>* _C_ground;
00209 _DG_node<_Tp,_Ctr,_TI>* _C_sky;
00211 int _C_mark;
00212 };
00213
00215
00226 template <class _Tp, class _Ctr, class _TI, class _Allocator>
00227 class _DG_alloc_base<_Tp, _Ctr, _TI, _Allocator, true> {
00228 public:
00229 typedef typename std::_Alloc_traits<_Tp, _Allocator>::allocator_type
00230 allocator_type;
00231 allocator_type get_allocator() const { return allocator_type(); }
00232
00233 _DG_alloc_base(const allocator_type&) {}
00234
00235 protected:
00236 typedef typename std::_Alloc_traits<_DG_node<_Tp,_Ctr,_TI>, _Allocator>::_Alloc_type
00237 _Alloc_type;
00239 _DG_node<_Tp,_Ctr,_TI>* _C_get_node()
00240 { return _Alloc_type::allocate(1); }
00242 void _C_put_node(_DG_node<_Tp,_Ctr,_TI>* __p)
00243 { _Alloc_type::deallocate(__p, 1); }
00244
00245 protected:
00247 _DG_node<_Tp,_Ctr,_TI>* _C_ground;
00249 _DG_node<_Tp,_Ctr,_TI>* _C_sky;
00251 int _C_mark;
00252 };
00253
00254
00256
00260 template <class _Tp, class _Ctr, class _Iterator, class _CIterator,
00261 class _Alloc>
00262 class _DG_base
00263 : public _DG_alloc_base<_Tp, _Ctr, _Iterator, _Alloc,
00264 std::_Alloc_traits<_Tp, _Alloc>::_S_instanceless>
00265 {
00266 public:
00267 typedef _DG_alloc_base<_Tp, _Ctr, _Iterator, _Alloc,
00268 std::_Alloc_traits<_Tp, _Alloc>::_S_instanceless>
00269 _Base;
00271 typedef typename _Base::allocator_type allocator_type;
00272
00274 typedef _Ctr container_type;
00276 typedef _Iterator children_iterator;
00277 typedef _CIterator children_const_iterator;
00279 typedef _Iterator parents_iterator;
00280 typedef _CIterator parents_const_iterator;
00281
00282
00284 _DG_base(const allocator_type& __a) : _Base(__a) {
00285 this->_C_ground = _C_get_node();
00286 this->_C_sky = _C_get_node();
00287 memset(this->_C_ground, 0, sizeof(*this->_C_ground));
00288 memset(this->_C_sky, 0, sizeof(*this->_C_sky));
00289 this->_C_mark = 0;
00290 __VGTL_TRY {
00291 _VGTL_Construct_1(&this->_C_ground->_C_children);
00292 _VGTL_Construct_1(&this->_C_ground->_C_parents);
00293 _VGTL_Construct_1(&this->_C_ground->_C_data);
00294 _VGTL_Construct_1(&this->_C_sky->_C_children);
00295 _VGTL_Construct_1(&this->_C_sky->_C_parents);
00296 _VGTL_Construct_1(&this->_C_sky->_C_data);
00297 }
00298 __VGTL_UNWIND(_C_put_node(this->_C_ground); _C_put_node(this->_C_sky));
00299 this->_C_ground->_C_children.push_back((void*)this->_C_sky);
00300 this->_C_sky->_C_parents.push_back((void*)this->_C_ground);
00301 this->_C_ground->_C_visited=0;
00302 this->_C_sky->_C_visited=0;
00303 }
00304
00306 ~_DG_base() {
00307 clear();
00308
00309
00310 }
00311
00312 protected:
00314 void clear_graph(_DG_node<_Tp,_Ctr,_Iterator>* _node);
00315
00316 public:
00318 void clear();
00320 void clear_children()
00321 { this->_C_ground->clear_children(); }
00323 void clear_parents()
00324 { this->_C_sky->clear_parents(); }
00325
00328 template <class _Output_Iterator>
00329 void add_all_children(_Output_Iterator fi,
00330 _DG_node<_Tp,_Ctr,_Iterator>* _parent);
00333 template <class _Output_Iterator>
00334 void add_all_parents(_Output_Iterator fi,
00335 _DG_node<_Tp,_Ctr,_Iterator>* _child);
00336 };
00337 #else
00338
00339
00341
00347 template <class _Tp, class _Ctr, class _Iterator, class _CIterator,
00348 class _Alloc>
00349 class _DG_base
00350 {
00351 public:
00353 typedef _Alloc allocator_type;
00355 allocator_type get_allocator() const { return allocator_type(); }
00356
00358 typedef _Ctr container_type;
00360 typedef _Iterator children_iterator;
00361 typedef _CIterator children_const_iterator;
00363 typedef _Iterator parents_iterator;
00364 typedef _CIterator parents_const_iterator;
00365
00367 _DG_base(const allocator_type&) {
00368 this->_C_ground = _C_get_node();
00369 this->_C_sky = _C_get_node();
00370 memset(this->_C_ground, 0, sizeof(*this->_C_ground));
00371 memset(this->_C_sky, 0, sizeof(*this->_C_sky));
00372 this->_C_mark = 0;
00373 __VGTL_TRY {
00374 _VGTL_Construct_1(&this->_C_ground->_C_children);
00375 _VGTL_Construct_1(&this->_C_ground->_C_parents);
00376 _VGTL_Construct_1(&this->_C_ground->_C_data);
00377 _VGTL_Construct_1(&this->_C_sky->_C_children);
00378 _VGTL_Construct_1(&this->_C_sky->_C_parents);
00379 _VGTL_Construct_1(&this->_C_sky->_C_data);
00380 }
00381 __VGTL_UNWIND(_C_put_node(this->_C_ground); _C_put_node(this->_C_sky));
00382 this->_C_ground->_C_children.push_back((void*)this->_C_sky);
00383 this->_C_sky->_C_parents.push_back((void*)this->_C_ground);
00384 this->_C_ground->_C_visited=0;
00385 this->_C_sky->_C_visited=0;
00386 }
00388 ~_DG_base() {
00389 clear();
00390
00391
00392 }
00393
00394 protected:
00396 void clear_graph(_DG_node<_Tp,_Ctr,_Iterator>* _node);
00397
00398 public:
00400 void clear();
00401
00402 protected:
00403 typedef std::allocator<_DG_node<_Tp,_Ctr,_Iterator> > _Alloc_type;
00405 _DG_node<_Tp,_Ctr,_Iterator>* _C_get_node()
00406 { return _Alloc_type().allocate(1); }
00408 void _C_put_node(_DG_node<_Tp,_Ctr,_Iterator>* __p)
00409 { _Alloc_type().deallocate(__p, 1); }
00410
00411 protected:
00413 _DG_node<_Tp,_Ctr,_Iterator>* _C_ground;
00415 _DG_node<_Tp,_Ctr,_Iterator>* _C_sky;
00417 int _C_mark;
00418
00420 void clear_children()
00421 { this->_C_ground->clear_children(); }
00423 void clear_parents()
00424 { this->_C_sky->clear_parents(); }
00425
00428 template <class _Output_Iterator>
00429 void add_all_children(_Output_Iterator fi,
00430 _DG_node<_Tp,_Ctr,_Iterator>* _parent);
00433 template <class _Output_Iterator>
00434 void add_all_parents(_Output_Iterator fi,
00435 _DG_node<_Tp,_Ctr,_Iterator>* _child);
00436 };
00437
00438 #endif
00439
00440
00441 template <class _Tp, class _Ctr, class _Iterator, class _CIterator,
00442 class _Alloc>
00443 void
00444 _DG_base<_Tp,_Ctr,_Iterator,_CIterator,_Alloc>::clear_graph(
00445 _DG_node<_Tp,_Ctr,_Iterator>* _node)
00446 {
00447 _Ctr* chld = &_node->_C_children;
00448 _Iterator it;
00449
00450 if(_node->_C_parents.size() <= 1)
00451 {
00452
00453 for(it = chld->begin(); it != chld->end(); ++it)
00454 clear_graph((_DG_node<_Tp,_Ctr,_Iterator>*)(*it));
00455 _node->_C_children.erase(_node->_C_children.begin(),
00456 _node->_C_children.end());
00457
00458 _VGTL_Destroy(&_node->_C_children);
00459 _VGTL_Destroy(&_node->_C_data);
00460 }
00461 if(_node->_C_parents.size() >= 1)
00462 _node->_C_parents.erase(_node->_C_parents.begin());
00463 if(_node->_C_parents.size() == 0)
00464 {
00465 _VGTL_Destroy(&_node->_C_parents);
00466 _C_put_node(_node);
00467 }
00468 }
00469
00470 template <class _Tp, class _Ctr, class _Iterator, class _CIterator,
00471 class _Alloc>
00472 template <class _Output_Iterator>
00473 inline void
00474 _DG_base<_Tp,_Ctr,_Iterator,_CIterator,_Alloc>::
00475 add_all_children(_Output_Iterator fi,
00476 _DG_node<_Tp,_Ctr,_Iterator>* _parent)
00477 { this->_C_ground->add_all_children(fi, _parent); };
00478
00479 template <class _Tp, class _Ctr, class _Iterator, class _CIterator,
00480 class _Alloc>
00481 template <class _Output_Iterator>
00482 inline void
00483 _DG_base<_Tp,_Ctr,_Iterator,_CIterator,_Alloc>::
00484 add_all_parents(_Output_Iterator fi,
00485 _DG_node<_Tp,_Ctr,_Iterator>* _child)
00486 { this->_C_sky->add_all_parents(fi, _child); };
00487
00488 template <class _Tp, class _Ctr, class _Iterator, class _CIterator,
00489 class _Alloc>
00490 void
00491 _DG_base<_Tp,_Ctr,_Iterator,_CIterator,_Alloc>::clear()
00492 {
00493 clear_graph(this->_C_ground);
00494
00495
00496
00497
00498 }
00499
00500 #if 0
00501
00502
00503
00504 template <class _Tp>
00505 class dag_node : public _DG_node<_Tp, vector<void*>, vector<void*>::iterator>
00506 {
00507 }
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546 template <class _Tp>
00547 class dag_base
00548 {
00549 protected:
00550 dag_node<_Tp>* _C_ground;
00551 dag_node<_Tp>* _C_sky;
00552 int _C_mark;
00553
00554 public:
00555 dag_base();
00556 ~dag_base();
00557
00558 private:
00559
00560 void clear_graph(dag_node<_Tp>* _node);
00561
00562 public:
00563 void clear();
00564
00565 dag_node<_Tp>* _C_get_node();
00566 void _C_put_node(dag_node<_Tp>* __p);
00567
00568 protected:
00569 void clear_children();
00570 void clear_parents();
00571
00572
00573 template <class _Output_Iterator>
00574 void add_all_children(_Output_Iterator fi, dag_node<_Tp>* _parent);
00575 void add_all_parents(_Output_Iterator fi, dag_node<_Tp>* _child);
00576 }
00577
00578 #endif
00579
00580 __VGTL_END_NAMESPACE
00581
00582
00583 #endif // __VGTL_DAGBASE_H_