-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_gr_sit1.h
1696 lines (1518 loc) · 54.4 KB
/
_gr_sit1.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef __GR_SITR
#define __GR_SITR
// Copyright David Lawrence Bien 1997 - 2021.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// https://www.boost.org/LICENSE_1_0.txt).
// _gr_sitr.h
#error this must have been an experient.
// This module implements safe iterators for the graph.
// REVIEW: TODO: Why have an extra memory object for the connection link when there is a one-to-one
// correspondence between links and iteration objects <and> the iteration object owns the object
// lifetime of the connection link? In other words they could share the same memory object with ease.
// It makes exception handling much easier as well.
// Safe node iterator - a connection is maintained between the node and the iterator
// such that if the node is destroyed then the iterator's pointer is nullified,
// Could allow user data and callback function
template < class t_TyGraphNodeSafe, class t_TyGraphLinkSafe, class t_TyConnLinkAlloc >
class _graph_node_iterator_base_safe
: public _alloc_base< _TyGraphConnectionLink, t_TyConnLinkAlloc >,
public _graph_node_iterator_base_notsafe< typename t_TyGraphNodeSafe::__TyGraphNodeBase,
typename t_TyGraphLinkSafe::__TyGraphLinkBase >
{
protected:
// Get the base classes from the safe classes:
typedef typename t_TyGraphNodeSafe::__TyGraphNodeBase _TyGraphNodeBase;
typedef typename t_TyGraphLinkSafe::__TyGraphLinkBase _TyGraphLinkBase;
private:
typedef _alloc_base< _TyGraphConnectionLink, t_TyConnLinkAlloc > _TyAllocConnLinkBase;
typedef _graph_node_iterator_base_notsafe< _TyGraphNodeBase, _TyGraphLinkBase > _TyIterBase;
typedef _graph_node_iterator_base_safe< t_TyGraphNodeSafe, t_TyGraphLinkSafe, t_TyConnLinkAlloc > _TyThis;
protected:
// Get the real allocator from the base:
typedef _TyAllocConnLinkBase::_TyAllocatorType _TyConnLinkAlloc;
_TyGraphConnectionLink * m_pgclNode; // pointer to link from graph-object to this object.
void _Reference( _TyGraphNodeBase * _pgnbCur )
{
// m_pgclNode may contain garbage - only valid when !!PGNSCur().
Assert( !PGNSCur() ); // In case of throw - should always have valid or NULL value.
_TyAllocConnLinkBase::allocate_type( m_pgclNode );
m_pgnbCur = _pgnbCur; // No throw after this.
m_pgclNode->m_pvConnection = (void*)this;
m_pgclNode->m_egclType = s_egclGraphNodeIterator;
PGNSCur()->PushConnection( m_pgclNode ); // Crash here ? Perhaps you mixed non-safe and safe graphs.
}
void _Dereference() noexcept(true)
{
if ( PGNBCur() )
{
m_pgclNode->remove_link();
_TyAllocConnLinkBase::deallocate_type( m_pgclNode );
__DEBUG_STMT( m_pgclNode = (_TyGraphConnectionLink*)0xbaadf00d );
}
}
// The graph node to which this iterator is connected is being deinitialized.
// We don't need to unlink the connection link.
void _node_deinit() noexcept(true)
{
Assert( PGNBCur() );
Assert( m_pgclNode );
_TyIterBase::Clear();
_TyAllocConnLinkBase::deallocate_type( m_pgclNode );
__DEBUG_STMT( m_pgclNode = (_TyGraphConnectionLink*)0xbaadf0ad );
}
public:
explicit _graph_node_iterator_base_safe( t_TyGraphNodeSafe * _pgnbCur,
const t_TyConnLinkAlloc & _rAlloc )
: _TyAllocConnLinkBase( _rAlloc ),
_TyIterBase( 0 ) // In case of throw.
// m_pgclNode( 0 ) - no need for this - only valid when !!PGNBCur().
{
__DEBUG_STMT( m_pgclNode = (_TyGraphConnectionLink*)0xbaadfaad );
if ( _pgnbCur )
{
_Reference( _pgnbCur );
}
}
template < class t_TySafeNodeIter >
explicit _graph_node_iterator_base_safe( const t_TySafeNodeIter & _r )
: _TyAllocConnLinkBase( _r._GetCLAllocatorAsPassed() ),
_TyIterBase( 0 ) // In case of throw.
// m_pgclNode( 0 ) - no need for this - only valid when !!PGNBCur().
{
__DEBUG_STMT( m_pgclNode = (_TyGraphConnectionLink*)0xbaadfaab );
if ( _r.PGNBCur() )
{
_Reference( _r.PGNBCur() );
}
}
template < class t_TySafeNodeIter >
explicit _graph_node_iterator_base_safe( const t_TySafeNodeIter & _r, std::false_type )
: _TyAllocConnLinkBase( _r._GetCLAllocatorAsPassed() ),
_TyIterBase( 0 ) // In case of throw.
// m_pgclNode( 0 ) - no need for this - only valid when !!PGNBCur().
{
__DEBUG_STMT( m_pgclNode = (_TyGraphConnectionLink*)0xbaadfeeb );
// just copy allocator.
}
// Initialize with a reference and a different allocator:
// This is not as safe as it might be - but misuse would involve assigning from the
// iterator of another ( non-safe ) graph - which is a bug in itself - perhaps we
// should record graph identities in debug to ensure iterators are used on the
// appropriate graph. The way around this ( not the actual bug ) is to templatize the
// non-safe graph node iterator base class by the safe graph node and link structs - this
// way no mingling can occur. The problem with that is that if ( for some reason ) we
// want multiple graph types in the implementation ( some safe, some not ) then the base
// graph code will not be shared between the two ( unless the compiler is smart ) - this
// is very easy to test for a given compiler - just change the template above and then
// see if the code gets larger ( or slower ). Of course it is very safe to initialize
// with a non-safe iterator that iterates safe graph nodes.
template < class t_TyNodeIter >
explicit _graph_node_iterator_base_safe( const t_TyNodeIter & _r,
const t_TyConnLinkAlloc & _rAlloc )
: _TyAllocConnLinkBase( _rAlloc ),
_TyIterBase( 0 ) // throwsafe.
// m_pgclNode( 0 ) - no need for this - only valid when !!PGNBCur().
{
__DEBUG_STMT( m_pgclNode = (_TyGraphConnectionLink*)0xbaadfeab );
if ( _r.PGNBCur() )
{
_Reference( _r.PGNBCur() );
}
}
~_graph_node_iterator_base_safe()
{
_Dereference();
}
void Clear()
{
_Dereference();
_TyIterBase::Clear();
}
t_TyGraphNodeSafe * PGNSCur() const noexcept(true)
{
return static_cast< t_TyGraphNodeSafe * >( m_pgnbCur );
}
// Once again this can be misused when multiple graph types ( safe and non-safe ) are mixed.
template < class t_TyNodeIter >
_TyThis & operator = ( const t_TyNodeIter & _r )
{
SetPGNBCur( _r.PGNBCur() );
return *this;
}
void SetPGNBCur( _TyGraphNodeBase * _pgnbCur )
{
// No need to allocate if we already have a conn link:
if ( PGNBCur() )
{
m_pgclNode->remove_link();
if ( m_pgnbCur = _pgnbCur )
{
PGNSCur()->PushConnection( m_pgclNode );
}
else
{
_TyAllocConnLinkBase::deallocate_type( m_pgclNode );
}
}
else
if ( _pgnbCur )
{
_Reference( _pgnbCur );
}
}
// Set to value of passed non-NULL node - assume currently populated link - won't throw.
void SetPGNBCur_Populated( t_TyGraphNodeBase * _pgnb ) noexcept(true)
{
Assert( m_pgclNode );
Assert( _pgnb );
m_pgclNode->remove_link();
(m_pgnbCur = _pgnb)->PushConnection( m_pgclNode );
}
// Set to value of passed iterator - assume currently populated link - won't throw.
// Typesafe problem when multiple graph types ( safe and non-safe ) are mixed.
template < class t_TyNodeIter >
void Set_Populated( const t_TyNodeIter & _r ) noexcept(true)
{
SetPGNBCur_Populated( _r.PGNBCur() );
}
void GoParent( _TyGNIndex _u ) noexcept(true)
{
Assert( m_pgclNode );
Assert( m_pgnbCur );
// Remove the reference to the current node and re-use it to reference the
// new node:
m_pgclNode->remove_link();
_TyIterBase::GoParent( _u );
PGNSCur()->PushConnection( m_pgclNode );
}
void GoChild( _TyGNIndex _u ) noexcept(true)
{
Assert( m_pgclNode );
Assert( m_pgnbCur );
// Remove the reference to the current node and re-use it to reference the
// new node:
m_pgclNode->remove_link();
_TyIterBase::GoChild( _u );
PGNSCur()->PushConnection( m_pgclNode );
}
// relation operators:
// Re-declare so that gets overridden methods:
void GoRelation( bool _fChild, _TyGNIndex _u ) noexcept(true)
{
if ( _fChild )
GoChild( _u );
else
GoParent( _u );
}
};
template < class t_TyGraphNodeBase, class t_TyGraphLinkBase >
class _graph_link_ident_iterator_base_notsafe;
// This iterator maintains a constant position within either the child or the parent list.
// It also has an end() position that is a valid insert before position.
template < class t_TyGraphNodeSafe, class t_TyGraphLinkSafe, class t_TyConnLinkAlloc >
class _graph_link_pos_iterator_base_safe
: public _alloc_base< _TyGraphConnectionLink, t_TyConnLinkAlloc >,
public _graph_link_pos_iterator_base_notsafe< typename t_TyGraphNodeSafe::__TyGraphNodeBase,
typename t_TyGraphLinkSafe::__TyGraphLinkBase >
{
protected:
// Get the base classes from the safe classes:
typedef typename t_TyGraphNodeSafe::__TyGraphNodeBase _TyGraphNodeBase;
typedef typename t_TyGraphLinkSafe::__TyGraphLinkBase _TyGraphLinkBase;
private:
typedef _alloc_base< _TyGraphConnectionLink, t_TyConnLinkAlloc > _TyAllocConnLinkBase;
typedef _graph_link_pos_iterator_base_notsafe< _TyGraphNodeBase, _TyGraphLinkBase > _TyIterBase;
typedef _graph_link_pos_iterator_base_safe< _TyGraphNodeBase, _TyGraphLinkBase, t_TyConnLinkAlloc > _TyThis;
protected:
// Get the real allocator from the base:
typedef _TyAllocConnLinkBase::_TyAllocatorType _TyConnLinkAlloc;
_TyGraphConnectionLink * m_pgclObj; // pointer to link from graph-object to this object.
void _InitRef( )
{
_TyAllocConnLinkBase::allocate_type( m_pgclObj );
m_pgclObj->m_ppvConnection = (void*)this;
m_pgclObj->m_egclType = s_egclLinkPositionIterator;
}
void _PushConnection( _TyGraphLinkBase ** _ppglbCur )
{
t_TyGraphLinkSafe * _pgls;
t_TyGraphNodeSafe * _pgns;
t_TyGraphLinkSafe::PGrObjFromPPGLB( _ppglbCur, _pgls, _pgns );
if ( _pgls )
{
_pgls->PushConnection( m_pgclObj ); // Crash here ? Perhaps you mixed non-safe and safe graphs.
}
else
{
_pgns->PushConnection( m_pgclObj ); // Crash here ? Perhaps you mixed non-safe and safe graphs.
}
}
void _Reference( _TyGraphLinkBase ** _ppglbCur )
{
Assert( !PPGLBCur() );
_InitRef();
_PushConnection( _ppglbCur );
_TyIterBase::SetPPGLBCur( _ppglbCur );
}
void _Dereference() noexcept(true)
{
if ( PPGLBCur() )
{
m_pgclObj->remove_link();
_TyAllocConnLinkBase::deallocate_type( m_pgclObj );
__DEBUG_STMT( m_pgclObj = (_TyGraphConnectionLink*)0xbaadbeed );
}
}
// The graph node to which this iterator is connected is being deinitialized.
// We don't need to unlink the connection link.
void _node_deinit() noexcept(true)
{
Assert( PPGLBCur() );
Assert( m_pgclObj );
#ifndef NDEBUG
t_TyGraphLinkSafe * _pgls;
t_TyGraphNodeSafe * _pgns;
t_TyGraphLinkSafe::PGrObjFromPPGLB( PPGLBCur(), _pgls, _pgns );
Assert( !_pgls && _pgns ); // We should currently be at the head of a relation list.
#endif //!NDEBUG
_TyIterBase::Clear();
_TyAllocConnLinkBase::deallocate_type( m_pgclObj );
__DEBUG_STMT( m_pgclObj = 0xbaadf0ed );
}
// The graph link to which this iterator is connected is being deinitialized.
// We don't need to unlink the connection link.
void _link_deinit() noexcept(true)
{
Assert( PPGLBCur() );
Assert( m_pgclObj );
#ifndef NDEBUG
t_TyGraphLinkSafe * _pgls;
t_TyGraphNodeSafe * _pgns;
t_TyGraphLinkSafe::PGrObjFromPPGLB( PPGLBCur(), _pgls, _pgns );
Assert( _pgls ); // We should currently not be at the head of a relation list.
#endif //!NDEBUG
_TyIterBase::Clear();
_TyAllocConnLinkBase::deallocate_type( m_pgclObj );
__DEBUG_STMT( m_pgclObj = 0xbaadf0dd );
}
public:
typedef _graph_link_ident_iterator_base_notsafe< _TyGraphNodeBase, _TyGraphLinkBase > _TyLinkIdentIter;
protected:
// Initialize with child/parent position - if this iterator is located at the head
// of either list then the node located at that head must be supplied ( else crash ).
explicit _graph_link_pos_iterator_base_safe( const _TyGraphLinkBase ** _ppglbCur,
t_TyConnLinkAlloc const & _alloc )
: _TyAllocConnLinkBase( _alloc ),
_TyIterBase( 0 ) // In case of throw.
{
__DEBUG_STMT( m_pgclObj = (_TyGraphConnectionLink*)0xbaadbead );
if ( _ppglbCur )
{
_Reference( _ppglbCur );
}
}
// Init with an iterator that has an allocator:
template < class t_TyLinkPosIter >
explicit _graph_link_pos_iterator_base_safe( const t_TyLinkPosIter & _r )
: _TyAllocConnLinkBase( _r ),
_TyIterBase( )
{
__DEBUG_STMT( m_pgclObj = (_TyGraphConnectionLink*)0xbaadbaad );
if ( _r.PPGLBCur() )
{
_Reference( _r.PPGLBCur() );
}
}
// Init with an iterator that has an allocator:
template < class t_TyLinkPosIter >
explicit _graph_link_pos_iterator_base_safe( const t_TyLinkPosIter & _r, std::false_type )
: _TyAllocConnLinkBase( _r ),
_TyIterBase( )
{
// just get the allocator.
__DEBUG_STMT( m_pgclObj = (_TyGraphConnectionLink*)0xbaadbaed );
}
// Init with a reference to an iterator and a different allocator:
template < class t_TyLinkPosIter >
explicit _graph_link_pos_iterator_base_safe( const t_TyLinkPosIter & _r,
t_TyConnLinkAlloc const & _alloc )
: _TyAllocConnLinkBase( _alloc ),
_TyIterBase( )
{
__DEBUG_STMT( m_pgclObj = (_TyGraphConnectionLink*)0xbaadbeef );
if ( _r.PPGLBCur() )
{
_Reference( _r.PPGLBCur() );
}
}
public:
void Clear() noexcept(true)
{
_Dereference();
_TyIterBase::Clear();
}
// Accessors:
void SetPPGLBCur( _TyGraphLinkBase ** _ppglbCur )
{
if ( PPGLBCur() )
{
m_pgclObj->remove_link();
if ( m_ppglbCur = _ppglbCur )
{
_PushConnection( m_ppglbCur );
}
else
{
_TyAllocConnLinkBase::deallocate_type( m_pgclObj );
}
}
else
if ( _ppglbCur )
{
_Reference( _ppglbCur );
}
}
// Set a non-NULL <_pglbCur> into an iterator:
void SetPGLBCur_Populated( _TyGraphLinkBase ** _ppglbCur )
{
Assert( _ppglbCur );
if ( PPGLBCur() )
{
m_pgclObj->remove_link();
m_ppglbCur = _ppglbCur;
_PushConnection( m_ppglbCur );
}
else
{
_Reference( _ppglbCur );
}
}
// Set a non-NULL <_pglbCur> into a populated iterator:
void SetPGLBCur_Populated( const _TyGraphLinkBase ** _ppglbCur )
{
Assert( PPGLBCur() );
Assert( _ppglbCur );
m_pgclObj->remove_link();
m_ppglbCur = _ppglbCur;
_PushConnection( m_ppglbCur );
}
// Node operations - these are the same as the node iterator:
// We will make go parent work correctly even if we are at the end of the list:
void GoParent( _TyGNIndex _u ) noexcept(true)
{
if ( *m_ppglbCur )
{
m_pgclObj->remove_link();
if ( _u )
{
_TyIterBase::GoParent( _u );
t_TyGraphLinkSafe * _pgls = static_cast< t_TyGraphLinkSafe * >( _TyGraphLinkBase::PGLBGetThisFromPPGLBNextParent( m_ppglbCur ) );
_pgls->PushConnection( m_pgclObj );
}
else
{
// Then we are referencing the head:
(*m_ppglbCur)->m_pgnbNodeParent->PushConnection( m_pgclObj );
m_ppglbCur = (*m_ppglbCur)->m_pgnbNodeParent->PPGLParentHead();
}
}
else
{
Assert( 0 ); // This is not supported - my feeling is that this is not
}
}
void GoChild( _TyGNIndex _u ) noexcept(true)
{
if ( *m_ppglbCur )
{
m_pgclObj->remove_link();
if ( _u )
{
_TyIterBase::GoChild( _u );
t_TyGraphLinkSafe * _pgls = static_cast< t_TyGraphLinkSafe * >( _TyGraphLinkBase::PGLBGetThisFromPPGLBNextChild( m_ppglbCur ) );
_pgls->PushConnection( m_pgclObj );
}
else
{
// Then we are referencing the head:
static_cast< t_TyGraphNodeSafe * >( (*m_ppglbCur)->m_pgnbNodeChild )->PushConnection( m_pgclObj );
m_ppglbCur = (*m_ppglbCur)->m_pgnbNodeChild->PPGLChildHead();
}
}
else
{
Assert( 0 ); // This is not supported - my feeling is that this is not
}
}
void GoRelation( bool _fChild, _TyGNIndex _u ) noexcept(true)
{
if ( _fChild )
GoChild( _u );
else
GoParent( _u );
}
// Link operations:
// The safe iterator gives enough info to compute the number of parents
// before the tail position appropriately:
_TyGNIndex UParentsBefore() const noexcept(true)
{
t_TyGraphLinkSafe * _pgls;
t_TyGraphNodeSafe * _pgns;
t_TyGraphLinkSafe::PGrObjFromPPGLB( _ppglbCur, _pgls, _pgns );
if ( _pgls )
{
return _pgls->UParentsBefore() + 1;
}
else
{
return 0;
}
}
void NextParent() noexcept(true)
{
m_pgclObj->remove_link();
_TyIterBase::NextParent();
// This would have to be a link:
t_TyGraphLinkSafe * _pgls = static_cast< t_TyGraphLinkSafe * >(
_TyGraphLinkBase::PGLBGetThisFromPPGLBNextChild( m_ppglbCur ) );
_pgls->PushConnection( m_pgclObj );
}
void PrevParent() noexcept(true)
{
m_pgclObj->remove_link();
_TyIterBase::PrevParent();
// This may be a graph node:
_PushConnection( m_ppglbCur );
}
_TyGNIndex UChildrenBefore() const noexcept(true)
{
t_TyGraphLinkSafe * _pgls;
t_TyGraphNodeSafe * _pgns;
t_TyGraphLinkSafe::PGrObjFromPPGLB( _ppglbCur, _pgls, _pgns );
if ( _pgls )
{
return _pgls->UChildrenBefore() + 1;
}
else
{
return 0;
}
}
void NextChild() noexcept(true)
{
m_pgclObj->remove_link();
_TyIterBase::NextChild();
// This would have to be a link:
t_TyGraphLinkSafe * _pgls = static_cast< t_TyGraphLinkSafe * >(
_TyGraphLinkBase::PGLBGetThisFromPPGLBNextChild( m_ppglbCur ) );
_pgls->PushConnection( m_pgclObj );
}
void PrevChild() noexcept(true)
{
m_pgclObj->remove_link();
_TyIterBase::PrevChild();
// This may be a graph node:
_PushConnection( m_ppglbCur );
}
void NextRelation( bool _fChild ) noexcept(true)
{
if ( _fChild )
NextChild();
else
NextParent();
}
void PrevRelation( bool _fChild ) noexcept(true)
{
if ( _fChild )
PrevChild();
else
PrevParent();
}
// Operations between link iterators:
#warning Think this code needs updating.
template < class t_TyLinkIterator >
void ExchangeSiblingParents( t_TyLinkIterator const & _rSiblingParent ) const noexcept(true)
{
Assert( PGLBCur() != _rSiblingParent.PGLBCur() );
Assert( PGLBCur()->m_pgnbNodeChild == _rSiblingParent.PGLBCur()->m_pgnbNodeChild );
_TyGraphLinkBase::ExchangeParents( PGLBCur(), _rSiblingParent.PGLBCur() );
}
template < class t_TyLinkIterator >
void ExchangeSiblingChildren( t_TyLinkIterator const & _rSiblingChild ) const noexcept(true)
{
Assert( _rSiblingChild.PGLBCur() != PGLBCur() );
Assert( PGLBCur()->m_pgnbNodeParent == _rSiblingChild.PGLBCur()->m_pgnbNodeParent );
_TyGraphLinkBase::ExchangeChildren( PGLBCur(), _rSiblingChild.PGLBCur() );
}
template < class t_TyLinkPosIterator >
_TyThis & operator = ( t_TyLinkPosIterator const & _r ) noexcept(true)
{
SetPPGLBCur( _r.PPGLBCur() );
return *this;
}
template < class t_TyLinkPosIterator >
void SetToParent( t_TyLinkPosIterator const & _r ) noexcept(true)
{
SetPGLBCur_Populated( _r.PPGLBCur_Parent() );
}
template < class t_TyLinkPosIterator >
void SetToChild( t_TyLinkPosIterator const & _r ) noexcept(true)
{
SetPGLBCur_Populated( _r.PPGLBCur_Child() );
}
};
// This iterator maintains a link identity within either the child or the parent list.
// It also has an end() position that is a valid insert before position.
template < class t_TyGraphNodeSafe, class t_TyGraphLinkSafe, class t_TyConnLinkAlloc >
class _graph_link_ident_iterator_base_safe
: public _alloc_base< _TyGraphConnectionLink, t_TyConnLinkAlloc >,
public _graph_link_ident_iterator_base_notsafe< typename t_TyGraphNodeSafe::__TyGraphNodeBase,
typename t_TyGraphLinkSafe::__TyGraphLinkBase >
{
protected:
// Get the base classes from the safe classes:
typedef typename t_TyGraphNodeSafe::__TyGraphNodeBase _TyGraphNodeBase;
typedef typename t_TyGraphLinkSafe::__TyGraphLinkBase _TyGraphLinkBase;
private:
typedef _alloc_base< _TyGraphConnectionLink, t_TyConnLinkAlloc > _TyAllocConnLinkBase;
typedef _graph_link_ident_iterator_base_notsafe< _TyGraphNodeBase, _TyGraphLinkBase > _TyIterBase;
typedef _graph_link_ident_iterator_base_safe< _TyGraphNodeBase, _TyGraphLinkBase, t_TyConnLinkAlloc > _TyThis;
protected:
// Get the real allocator from the base:
typedef _TyAllocConnLinkBase::_TyAllocatorType _TyConnLinkAlloc;
_TyGraphConnectionLink * m_pgclLink; // pointer to link from graph-object to this object.
void _Reference( _TyGraphLinkBase * _pglbCur )
{
// m_pgclLink may contain garbage - only valid when !!PGNSCur().
Assert( !PGLSCur() ); // In case of throw - should always have valid or NULL value.
_TyAllocConnLinkBase::allocate_type( m_pgclLink );
m_pglbCur = _pglbCur; // No throw after this.
m_pgclLink->m_ppvConnection = (void*)this;
m_pgclLink->m_egclType = s_egclGraphLinkIdentIterator;
PGLSCur()->PushConnection( m_pgclLink ); // Crash here ? Perhaps you mixed non-safe and safe graphs.
}
void _Dereference() noexcept(true)
{
if ( PGLSCur() )
{
m_pgclLink->remove_link();
_TyAllocConnLinkBase::deallocate_type( m_pgclLink );
}
}
// The graph link to which this iterator is connected is being deinitialized.
// We don't need to unlink the connection link.
void _link_deinit() noexcept(true)
{
Assert( PGLBCur() );
Assert( m_pgclLink );
_TyIterBase::Clear();
_TyAllocConnLinkBase::deallocate_type( m_pgclLink );
__DEBUG_STMT( m_pgclLink = 0xbaadf000 );
}
public:
typedef _graph_link_ident_iterator_base_notsafe< _TyGraphNodeBase, _TyGraphLinkBase > _TyIdentPosIter;
explicit _graph_link_ident_iterator_base_safe( const _TyGraphLinkBase * _pglbCur,
t_TyConnLinkAlloc const & _alloc )
: _TyAllocConnLinkBase( _alloc ),
_TyIterBase( 0 )
{
if ( _pglbCur )
{
_Reference( _pglbCur );
}
}
// Init with iterator that has an allocator:
template < class _TyLinkIdentIter >
explicit _graph_link_ident_iterator_base_safe( const _TyLinkIdentIter & _r )
: _TyAllocConnLinkBase( _r ),
_TyIterBase( 0 )
{
if ( _r.PGLBCur() )
{
_Reference( _r.PGLBCur() );
}
}
template < class _TyLinkIdentIter >
explicit _graph_link_ident_iterator_base_safe( const _TyLinkIdentIter & _r, std::false_type )
: _TyAllocConnLinkBase( _r ),
_TyIterBase( 0 )
{
// Just get the allocator.
}
// Init with iterator and allocator:
template < class _TyLinkIdentIter >
explicit _graph_link_ident_iterator_base_safe( const _TyLinkIdentIter & _r,
t_TyConnLinkAlloc const & _alloc )
: _TyAllocConnLinkBase( _alloc ),
_TyIterBase( 0 )
{
if ( _r.PGLBCur() )
{
_Reference( _r.PGLBCur() );
}
}
void Clear() noexcept(true)
{
_Dereference();
_TyIterBase::Clear();
}
t_TyGraphLinkSafe * PGLSCur() const noexcept(true)
{
return static_cast< t_TyGraphLinkSafe * >( m_pglbCur );
}
void SetPGLBCur( _TyGraphLinkBase * _pglbCur )
{
if ( PGLSCur() )
{
m_pgclLink->remove_link();
if ( m_pglbCur = _pglbCur )
{
PGLSCur()->PushConnection( m_pgclLink );
}
else
{
_TyAllocConnLinkBase::deallocate_type( m_pgclLink );
}
}
else
if ( _pglbCur )
{
_Reference( _pglbCur );
}
}
// Set a non-NULL <_pglbCur> into a populated iterator:
void SetPGLBCur_Populated( _TyGraphLinkBase * _pglbCur ) noexcept(true)
{
Assert( PGLSCur() );
Assert( _pglbCur );
m_pgclLink->remove_link();
m_pglbCur = _pglbCur;
PGLSCur()->PushConnection( m_pgclLink );
}
// Node operations - these are the same as the node iterator:
void GoParent( _TyGNIndex _u ) noexcept(true)
{
m_pgclLink->remove_link();
_TyIterBase::GoParent( _u );
PGLSCur()->PushConnection( m_pgclLink );
}
void GoChild( _TyGNIndex _u ) noexcept(true)
{
m_pgclLink->remove_link();
_TyIterBase::GoChild( _u );
PGLSCur()->PushConnection( m_pgclLink );
}
void GoRelation( bool _fChild, _TyGNIndex _u ) noexcept(true)
{
if ( _fChild )
GoChild( _u );
else
GoParent( _u );
}
// Link operations:
void NextParent() noexcept(true)
{
m_pgclLink->remove_link();
_TyIterBase::NextParent( );
PGLSCur()->PushConnection( m_pgclLink );
}
void PrevParent() noexcept(true)
{
m_pgclLink->remove_link();
_TyIterBase::PrevParent( );
PGLSCur()->PushConnection( m_pgclLink );
}
void NextChild() noexcept(true)
{
m_pgclLink->remove_link();
_TyIterBase::NextChild( );
PGLSCur()->PushConnection( m_pgclLink );
}
void PrevChild() noexcept(true)
{
m_pgclLink->remove_link();
_TyIterBase::PrevChild( );
PGLSCur()->PushConnection( m_pgclLink );
}
void NextRelation( bool _fChild ) noexcept(true)
{
if ( _fChild )
NextChild();
else
NextParent();
}
void PrevRelation( bool _fChild ) noexcept(true)
{
if ( _fChild )
PrevChild();
else
PrevParent();
}
template < class t_TyLinkIter >
bool operator == ( t_TyLinkIter const & _r ) const noexcept(true)
{
return PGLBCur() == _r.PGLBCur();
}
template < class t_TyLinkIterator >
_TyThis & operator = ( t_TyLinkIterator const & _r )
{
SetPGLBCur( _r.PGLBCur() );
return *this;
}
};
// Path iterator - maintains path.
template < class t_TyGraphNodeSafe, class t_TyGraphLinkSafe,
class t_TyPathNodeSafe, class t_TyPathNodeSafeAllocator,
class t_TyConnLinkAlloc >
class _graph_path_iterator_base_safe
: public _alloc_base< t_TyPathNodeSafe, t_TyPathNodeSafeAllocator >, // This allocates safe path nodes for us.
public _alloc_base< _TyGraphConnectionLink, t_TyConnLinkAlloc >, // This allocates connection links for us.
// NOTE: there is a significant design decision here - we could base on the safe types - but that would cause
// multiple implementations of the same code - one for non-safe and one for safe. However, this way, we have
// multiple allocators - the base classes allocator will not be used in this class. When instanced allocators
// are used we will have an extra member in the class that will not be used - this is not horrible but perhaps
// it can be avoided somehow in the future.
public _graph_path_iterator_base_notsafe<
typename t_TyGraphNodeSafe::__TyGraphNodeBase,
typename t_TyGraphLinkSafe::__TyGraphLinkBase,
typename t_TyPathNodeSafe::__TyPathNodeBase,
typename _Alloc_traits<
typename t_TyPathNodeSafe::__TyPathNodeBase, t_TyPathNodeSafeAllocator >::allocator_type >
{
protected:
// Get the base classes from the safe classes:
typedef typename t_TyGraphNodeSafe::__TyGraphNodeBase _TyGraphNodeBase;
typedef typename t_TyGraphLinkSafe::__TyGraphLinkBase _TyGraphLinkBase;
typedef typename t_TyPathNodeSafe::__TyPathNodeBase _TyPathNodeBase;
// Get the allocator for the base from that of the safe - the base class's
// allocator
typedef typename _Alloc_traits< _TyPathNodeBase, t_TyPathNodeSafeAllocator >::allocator_type _TyPathNodeBaseAllocator;
private:
typedef _alloc_base< t_TyGraphNodeSafe, t_TyPathNodeSafeAllocator > _TyBaseAllocPathNode;
typedef _alloc_base< _TyGraphConnectionLink, t_TyConnLinkAlloc > _TyBaseAllocConnLink;
typedef _graph_path_iterator_base_safe< t_TyGraphNodeSafe, t_TyGraphLinkSafe, t_TyPathNodeSafe, t_TyPathNodeSafeAllocator, t_TyConnLinkAlloc > _TyThis;
typedef _graph_path_iterator_base_notsafe< _TyGraphNodeBase, _TyGraphLinkBase, _TyPathNodeBase, _TyPathNodeBaseAllocator > _TyBase;
// Get the actual allocators from the base:
typedef typename _TyBaseAllocPathNode::_TyAllocatorType _TyPathNodeSafeAllocator;
typedef typename _TyBaseAllocConnLink::_TyAllocatorType _TyConnLinkAlloc;
protected:
void _CreateConnLink( _TyGraphConnectionLink *& _pgcl,
t_TyPathNodeSafe * _ppns )
{
_TyBaseAllocConnLink::allocate_type( _pgcl );
_pgcl->m_pvConnection = (void*)_ppns;
_pgcl->m_pvConnectionContainer = (void*)this;
}
void _DestroyConnLink( _TyGraphConnectionLink * _pgcl ) noexcept(true)
{
_pgcl->remove_link();
_TyBaseAllocConnLink::deallocate_type( _pgcl );
}
void _UpdateConnLink( t_TyPathNodeSafe & _rpns ) noexcept(true)
{
_rpns.m_pgclLink->remove_link();
_rpns.m_pgclNode->remove_link();
_rpns.m_pgnbNode->PushConnection( _rpns.m_pgclNode );
_rpns.m_pgnbLink->PushConnection( _rpns.m_pgclLink );
}
// Note: This method does nothing with the count - that must be kept up-to-date
// by the caller.
void _AppendPath( const _TyPathNodeBase * _ppnbAppend )
{
if ( _ppnbAppend )
{
// Save the identity of the old tail - we will remove after successful appendage.
_TyPathNodeBase * _ppnbTailOld = *m_pppnbTail;
// Save the position of the old tail - we will revert if we throw:
_TyPathNodeBase ** _pppnbTailOld = m_pppnbTail;
_BIEN_TRY
{
// Special case the last element - we won't be allocating a link.
__SDP( t_TyPathNodeSafe, _TyPathNodeSafeAllocator, GetPNSAllocator(), _ppnsNew );
__SDP( _TyGraphConnectionLink, _TyConnLinkAlloc, GetCLAllocator(), _pgclNode );
__SDP( _TyGraphConnectionLink, _TyConnLinkAlloc, GetCLAllocator(), _pgclLink );
for ( ;
_ppnbAppend->m_ppnbNext;
_ppnbAppend = _ppnbAppend->m_ppnbNext )
{
_TyBaseAllocPathNode::allocate_type( _ppnsNew );
new ( _ppnsNew ) t_TyPathNodeSafe( *_ppnbAppend );
_CreateConnLink( _pgclNode, _ppnsNew );
_CreateConnLink( _pgclLink, _ppnsNew );
_ppnsNew->m_pgclNode = _pgclNode;
_ppnsNew->m_pgclLink = _pgclLink;
_ppnsNew->push_back_node( m_pppnbTail );
_ppnsNew->m_pgnbNode->PushConnection( __SDP_TRANSFER(_pgclNode) );
__SDP_TRANSFER(_ppnsNew)->m_pglbLink->PushConnection( __SDP_TRANSFER(_pgclLink) );
}
_TyBaseAllocPathNode::allocate_type( _ppnsNew );
new ( _ppnsNew ) t_TyPathNodeSafe( *_ppnbAppend );
Assert( !_ppnsNew->m_pglbLink );
_CreateConnLink( _pgclNode, _ppnsNew );
#ifndef _BIEN_USE_EXCEPTIONS
if ( !_pgclNode )
{
_TyBaseAllocPathNode::deallocate_type( _ppnsNew );
_DestroyPath( _pppnbTailOld );
return;
}
#endif //!_BIEN_USE_EXCEPTIONS
_ppnsNew->m_pgclNode = _pgclNode;
_ppnsNew->push_back_node( m_pppnbTail );
__SDP_TRANSFER(_ppnsNew)->m_pgnbNode->PushConnection( __SDP_TRANSFER(_pgclNode) );
}
_BIEN_UNWIND( _DestroyPath( _pppnbTailOld ) )
// If we had an old tail then update by removing now - for throw-safety:
if ( _ppnbTailOld )
{
Assert( !_ppnbTailOld->m_pglbLink );
Assert( _ppnbTailOld->m_pgnbNode == _ppnbTailOld->m_ppnbNext->m_pgnbNode ); // Paths must connect.
_ppnbTailOld->remove_node_in_middle();
_DestroyConnLink( _ppnbTailOld->m_pgclNode );
_TyBaseAllocPathNode::deallocate_type( _ppnbTailOld );
}
}
}
void _AppendReversePath( const _TyPathNodeBase * _ppnbAppend )
{
if ( _ppnbAppend )
{
// Save the identity of the old tail - we will remove after successful appendage.
_TyPathNodeBase * _ppnbTailOld = *m_pppnbTail;
// Save the old tail - we will revert if we throw:
_TyPathNodeBase ** _pppnbTailOld = m_pppnbTail;
_BIEN_TRY
{
__SDP( t_TyPathNodeSafe, _TyPathNodeSafeAllocator, GetPNSAllocator(), _ppnsNew );
_TyBaseAllocPathNode::allocate_type( _ppnsNew );
__SDP_CHECK_VOID( _ppnsNew );
new ( _ppnsNew ) t_TyPathNodeSafe( _ppnbAppend->m_pgnbNode );
_ppnsNew->m_pglbLink = 0; // This is the new tail.