-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_gr_def.h
1063 lines (953 loc) · 35.5 KB
/
_gr_def.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_DEF_H
#define __GR_DEF_H
// 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_def.h
// This module defines the default graph objects.
#define __GR_THROWGRAPHNAVERRORS
#include <_assert.h>
#ifdef __GR_THROWGRAPHNAVERRORS
// We use the SGI exception stuff:
#include <stdexcept>
#endif //__GR_THROWGRAPHNAVERRORS
#include "_gr_alst.h"
__DGRAPH_BEGIN_NAMESPACE
#ifdef __GR_THROWGRAPHNAVERRORS
class _graph_nav_except : public _t__Named_exception< __DGRAPH_DEFAULT_ALLOCATOR >
{
typedef _graph_nav_except _TyThis;
typedef _t__Named_exception< __DGRAPH_DEFAULT_ALLOCATOR > _TyBase;
public:
_graph_nav_except( const char * _pc )
: _TyBase( _pc )
{
}
_graph_nav_except( const string_type & __s )
: _TyBase( __s )
{
}
};
#else //__GR_THROWGRAPHNAVERRORS
#error This not currently supported. Need error propagation - not too tough, but...
#endif //__GR_THROWGRAPHNAVERRORS
// Predeclare:
class _graph_node_base;
template < class t_TyGraphNodeBase > struct _graph_link_base;
// Graph link stuff.
// Graph links link graph nodes to graph nodes.
template < class t_TyGraphLinkBase >
_TyGNIndex
GLB_UCountParents( const t_TyGraphLinkBase * _pglb ) _BIEN_NOTHROW
{
_TyGNIndex u = 0;
for ( ;
_pglb;
_pglb = _pglb->m_pglbNextParent, ++u )
;
return u;
}
template < class t_TyGraphLinkBase >
_TyGNIndex
GLB_UCountParentsBefore( const t_TyGraphLinkBase * _pglb,
const t_TyGraphLinkBase ** _ppglbHead ) _BIEN_NOTHROW
{
// Walk the head to the element - this let's the end element work correctly ( _pglb == 0 ).
_TyGNIndex u = 0;
for ( ;
_pglb != *_ppglbHead;
_ppglbHead = &( (*_ppglbHead)->m_pglbNextParent ), ++u )
;
return u;
}
template < class t_TyGraphLinkBase >
t_TyGraphLinkBase * const *
GLB_PPGLBGetNthParent( t_TyGraphLinkBase * const * _ppglbAfter, _TyGNIndex _u ) _BIEN_NOTHROW
{
for ( ; _u-- && *_ppglbAfter; _ppglbAfter = &( (*_ppglbAfter)->m_pglbNextParent ) )
;
return _ppglbAfter;
}
template < class t_TyGraphLinkBase >
_TyGNIndex
GLB_UCountChildren( const t_TyGraphLinkBase * _pglb ) _BIEN_NOTHROW
{
_TyGNIndex u = 0;
for ( ;
_pglb;
_pglb = _pglb->m_pglbNextChild, ++u )
;
return u;
}
template < class t_TyGraphLinkBase >
_TyGNIndex
GLB_UCountChildrenBefore( const t_TyGraphLinkBase * _pglb,
const t_TyGraphLinkBase ** _ppglbHead ) _BIEN_NOTHROW
{
// Walk the head to the element - this let's the end element work correctly ( _pglb == 0 ).
_TyGNIndex u = 0;
for ( ;
_pglb != *_ppglbHead;
_ppglbHead = &( *_ppglbHead->m_pglbNextChild ), ++u )
;
return u;
}
template < class t_TyGraphLinkBase >
t_TyGraphLinkBase * const *
GLB_PPGLBGetNthChild( t_TyGraphLinkBase * const * _ppglbAfter, _TyGNIndex _u ) _BIEN_NOTHROW
{
for ( ; _u-- && *_ppglbAfter; _ppglbAfter = &( (*_ppglbAfter)->m_pglbNextChild ) )
;
return _ppglbAfter;
}
// POD
template < class t_TyGraphNodeBase >
struct _graph_link_base
{
typedef _graph_link_base< t_TyGraphNodeBase > _TyThis;
_TyThis ** m_ppglbPrevNextParent;
_TyThis ** m_ppglbPrevNextChild;
t_TyGraphNodeBase * m_pgnbNodeParent;
t_TyGraphNodeBase * m_pgnbNodeChild;
_TyThis * m_pglbNextParent; // Keep these last - as we will maintain the same relative positions in
_TyThis * m_pglbNextChild; // both the graph link->safe graph link and the graph node->safe graph node
// this let's us place information about the position of the link with
// minimal costs ( and still keeps the non-safe and safe versions of the
// data structures the same - sharing the same base code ).
// PUT NO MEMBERS HERE.
// No need for construction as all fields are populated by use.
public:
typedef t_TyGraphNodeBase _TyGraphNodeBase;
typedef _TyThis _TyGraphLinkBase;
void Init() _BIEN_NOTHROW
{
// Nothing to be done - all should be initialized by user.
}
bool FIsConstructed() const _BIEN_NOTHROW
{
// Currently a link is constructed ( i.e. the construction of the link is owned
// by the graph ) when it has both a parent and a child node connected to it.
// If constructed links that have one or no nodes connected to it then a boolean
// would need to be added ( m_fIsConstructed ).
return m_pgnbNodeParent && m_pgnbNodeChild;
}
void SetParentNode( t_TyGraphNodeBase * _pgnbNodeParent ) _BIEN_NOTHROW
{
m_pgnbNodeParent = _pgnbNodeParent;
}
void SetChildNode( t_TyGraphNodeBase * _pgnbNodeChild ) _BIEN_NOTHROW
{
m_pgnbNodeChild = _pgnbNodeChild;
}
void SetRelationNode( bool _fChild, t_TyGraphNodeBase * _pgnbNode ) _BIEN_NOTHROW
{
if ( _fChild )
SetChildNode( _pgnbNode );
else
SetParentNode( _pgnbNode );
}
t_TyGraphNodeBase * PGNBParent() const _BIEN_NOTHROW
{
return m_pgnbNodeParent;
}
t_TyGraphNodeBase * PGNBChild() const _BIEN_NOTHROW
{
return m_pgnbNodeChild;
}
t_TyGraphNodeBase * PGNBRelation( bool _fChild ) const _BIEN_NOTHROW
{
return _fChild ? m_pgnbNodeChild : m_pgnbNodeParent;
}
_TyGNIndex UCountParents() const _BIEN_NOTHROW
{
return GLB_UCountParents( this );
}
// Operations:
_TyGNIndex UParentsBefore() const _BIEN_NOTHROW
{
return GLB_UCountParentsBefore( this, m_pgnbNodeChild->PPGLBParentHead() );
}
static _TyThis * const *
PPGLBGetNthParent( _TyThis * const * _ppglbAfter, _TyGNIndex _u ) _BIEN_NOTHROW
{
return GLB_PPGLBGetNthParent( _ppglbAfter, _u );
}
static _TyThis **
PPGLBGetNthParent( _TyThis ** _ppglbAfter, _TyGNIndex _u ) _BIEN_NOTHROW
{
return const_cast< _TyThis ** >( GLB_PPGLBGetNthParent( _ppglbAfter, _u ) );
}
static _TyThis * PGLBGetThisFromPPGLBNextParent( _TyThis ** _ppglbNextParent )
{
return reinterpret_cast< _TyThis * >(
reinterpret_cast< char* >( _ppglbNextParent ) -
offsetof( _TyThis, m_pglbNextParent ) );
}
// Return the address of the next parent in the list.
_TyThis * const * PPGLBGetNextParent() const _BIEN_NOTHROW
{
return &m_pglbNextParent;
}
_TyThis ** PPGLBGetNextParent() _BIEN_NOTHROW
{
return &m_pglbNextParent;
}
_TyThis * PGLBGetNextParent() const _BIEN_NOTHROW
{
return m_pglbNextParent;
}
// Return the address of the pointer to this graph link in the parent list.
_TyThis ** PPGLBGetThisParent() const _BIEN_NOTHROW
{
return m_ppglbPrevNextParent;
}
// Return the previous graph link in the parent list.
_TyThis * PGLBGetPrevParent() const _BIEN_NOTHROW
{
return reinterpret_cast< const _TyThis * >(
reinterpret_cast< char* >(m_ppglbPrevNextParent) -
offsetof(_TyThis,m_pglbNextParent) );
}
// Return the pointer to the address of the previous element in the parent list.
_TyThis ** PPGLBGetPrevParent() const _BIEN_NOTHROW
{
return PGLBGetPrevParent()->m_ppglbPrevNextParent;
}
void InsertParent( _TyThis ** _ppglbBefore ) _BIEN_NOTHROW
{
m_ppglbPrevNextParent = _ppglbBefore;
m_pglbNextParent = *_ppglbBefore;
if ( *_ppglbBefore )
{
(*_ppglbBefore)->m_ppglbPrevNextParent = &m_pglbNextParent;
}
*_ppglbBefore = this;
}
void InsertParentAssume( _TyThis ** _ppglbBefore ) _BIEN_NOTHROW
{
m_ppglbPrevNextParent = _ppglbBefore;
m_pglbNextParent = *_ppglbBefore;
(*_ppglbBefore)->m_ppglbPrevNextParent = &m_pglbNextParent;
*_ppglbBefore = this;
}
// Append the parent list to the given tail of some parent list - used to move parent
// lists to nodes enmass:
void AppendParentListToTail( _TyThis ** _ppglbTail ) _BIEN_NOTHROW
{
Assert( !*_ppglbTail ); // Must be tail.
m_ppglbPrevNextParent = _ppglbTail;
*_ppglbTail = this;
}
void RemoveParent( ) const _BIEN_NOTHROW
{
if ( m_pglbNextParent )
{
m_pglbNextParent->m_ppglbPrevNextParent = m_ppglbPrevNextParent;
}
*m_ppglbPrevNextParent = m_pglbNextParent;
}
void RemoveParentAssume( ) const _BIEN_NOTHROW
{
m_pglbNextParent->m_ppglbPrevNextParent = m_ppglbPrevNextParent;
*m_ppglbPrevNextParent = m_pglbNextParent;
}
// Exchange the two parent links - <_ppglbParentBefore> must be before <_ppglbParentAfter>
// in the parent list - this allows optimization.
// Actually <_ppglbParentBefore> can be after <_ppglbParentAfter> as long as <_ppglbParentBefore>
// is not the tail of the list.
static void ExchangeParentsOrdered( _TyThis * _pglbParentBefore,
_TyThis * _pglbParentAfter )
{
if ( !_pglbParentBefore || !_pglbParentAfter )
throw _graph_nav_except("_graph_link_base::ExchangeParentsOrdered(): bad parameters.");
Assert( _pglbParentBefore != _pglbParentAfter ); // not a nop.
_TyThis ** ppglbPrevNextAfter = _pglbParentAfter->m_ppglbPrevNextParent;
_pglbParentAfter->m_ppglbPrevNextParent = _pglbParentBefore->m_ppglbPrevNextParent;
_pglbParentBefore->m_ppglbPrevNextParent = ppglbPrevNextAfter;
_TyThis * pglbNextAfter = _pglbParentAfter->m_pglbNextParent;
if ( pglbNextAfter )
{
pglbNextAfter->m_ppglbPrevNextParent = &( _pglbParentBefore->m_pglbNextParent );
}
_pglbParentBefore->m_pglbNextParent->m_ppglbPrevNextParent = &( _pglbParentAfter->m_pglbNextParent );
_pglbParentAfter->m_pglbNextParent = _pglbParentBefore->m_pglbNextParent;
_pglbParentBefore->m_pglbNextParent = pglbNextAfter;
}
static void ExchangeParents( _TyThis * _pglbParentBefore,
_TyThis * _pglbParentAfter )
{
if ( !_pglbParentBefore || !_pglbParentAfter )
throw _graph_nav_except("_graph_link_base::ExchangeParentsOrdered(): bad parameters.");
Assert( _pglbParentBefore != _pglbParentAfter ); // not a nop.
_TyThis ** ppglbPrevNextAfter = _pglbParentAfter->m_ppglbPrevNextParent;
_pglbParentAfter->m_ppglbPrevNextParent = _pglbParentBefore->m_ppglbPrevNextParent;
_pglbParentBefore->m_ppglbPrevNextParent = ppglbPrevNextAfter;
_TyThis * pglbNextAfter = _pglbParentAfter->m_pglbNextParent;
if ( pglbNextAfter )
{
pglbNextAfter->m_ppglbPrevNextParent = &( _pglbParentBefore->m_pglbNextParent );
}
if ( _pglbParentBefore->m_pglbNextParent )
{
_pglbParentBefore->m_pglbNextParent->m_ppglbPrevNextParent = &( _pglbParentAfter->m_pglbNextParent );
}
_pglbParentAfter->m_pglbNextParent = _pglbParentBefore->m_pglbNextParent;
_pglbParentBefore->m_pglbNextParent = pglbNextAfter;
}
_TyGNIndex UCountChildren() const _BIEN_NOTHROW
{
return GLB_UCountChildren( this );
}
_TyGNIndex UChildrenBefore() const _BIEN_NOTHROW
{
return GLB_UCountChildrenBefore( this, m_pgnbNodeParent->PPGLBChildHead() );
}
static _TyThis * const *
PPGLBGetNthChild( _TyThis * const * _ppglbAfter, _TyGNIndex _u ) _BIEN_NOTHROW
{
return GLB_PPGLBGetNthChild( _ppglbAfter, _u );
}
static _TyThis **
PPGLBGetNthChild( _TyThis ** _ppglbAfter, _TyGNIndex _u ) _BIEN_NOTHROW
{
return const_cast< _TyThis ** >( GLB_PPGLBGetNthChild( _ppglbAfter, _u ) );
}
static _TyThis * PGLBGetThisFromPPGLBNextChild( _TyThis ** _ppglbNextChild )
{
return reinterpret_cast< _TyThis * >(
reinterpret_cast< char* >( _ppglbNextChild ) -
offsetof( _TyThis, m_pglbNextChild ) );
}
// Return the address of the next child in the list.
_TyThis ** PPGLBGetNextChild() _BIEN_NOTHROW
{
return &m_pglbNextChild;
}
_TyThis * const * PPGLBGetNextChild() const _BIEN_NOTHROW
{
return &m_pglbNextChild;
}
_TyThis * PGLBGetNextChild() const _BIEN_NOTHROW
{
return m_pglbNextChild;
}
// Return the address of the pointer to this graph link in the child list.
_TyThis ** PPGLBGetThisChild() const _BIEN_NOTHROW
{
return m_ppglbPrevNextChild;
}
// Return the previous graph link in the child list.
_TyThis * PGLBGetPrevChild() const _BIEN_NOTHROW
{
return reinterpret_cast< _TyThis * >(
reinterpret_cast< char* >( m_ppglbPrevNextChild ) -
offsetof( _TyThis,m_pglbNextChild ) );
}
// Return the pointer to the address of the previous element in the child list.
_TyThis ** PPGLBGetPrevChild() const _BIEN_NOTHROW
{
return PGLBGetPrevChild()->m_ppglbPrevNextChild;
}
void InsertChild( _TyThis ** _ppglbBefore ) _BIEN_NOTHROW
{
m_ppglbPrevNextChild = _ppglbBefore;
m_pglbNextChild = *_ppglbBefore;
if ( *_ppglbBefore )
{
(*_ppglbBefore)->m_ppglbPrevNextChild = &m_pglbNextChild;
}
*_ppglbBefore = this;
}
void InsertChildAssume( _TyThis ** _ppglbBefore ) _BIEN_NOTHROW
{
m_ppglbPrevNextChild = _ppglbBefore;
m_pglbNextChild = *_ppglbBefore;
(*_ppglbBefore)->m_ppglbPrevNextChild = &m_pglbNextChild;
*_ppglbBefore = this;
}
// Append the child list to the given tail of some child list - used to move child
// lists to nodes enmass:
void AppendChildListToTail( _TyThis ** _ppglbTail ) _BIEN_NOTHROW
{
Assert( !*_ppglbTail ); // Must be tail.
m_ppglbPrevNextChild = _ppglbTail;
*_ppglbTail = this;
}
void RemoveChild( ) const _BIEN_NOTHROW
{
if ( m_pglbNextChild )
{
m_pglbNextChild->m_ppglbPrevNextChild = m_ppglbPrevNextChild;
}
*m_ppglbPrevNextChild = m_pglbNextChild;
}
void RemoveChildAssume( ) const _BIEN_NOTHROW
{
m_pglbNextChild->m_ppglbPrevNextChild = m_ppglbPrevNextChild;
*m_ppglbPrevNextChild = m_pglbNextChild;
}
// Relation methods:
_TyGNIndex UCountRelations( bool _fChildren ) const _BIEN_NOTHROW
{
return _fChildren ? GLB_UCountChildren( this ) : GLB_UCountParents( this );
}
_TyThis * PGLBGetNextRelation( bool _fChild ) const _BIEN_NOTHROW
{
return _fChild ? m_pglbNextChild : m_pglbNextParent;
}
_TyThis ** PPGLBGetNextRelation( bool _fChild ) _BIEN_NOTHROW
{
return _fChild ? &m_pglbNextChild : &m_pglbNextParent;
}
_TyThis * const * PPGLBGetNextRelation( bool _fChild ) const _BIEN_NOTHROW
{
return _fChild ? &m_pglbNextChild : &m_pglbNextParent;
}
_TyThis ** PPGLBGetThisRelation( bool _fChild ) const _BIEN_NOTHROW
{
return _fChild ? PPGLBGetThisChild() : PPGLBGetThisParent();
}
void InsertRelation( bool _fChild, _TyThis ** _ppglbBefore ) _BIEN_NOTHROW
{
_fChild ? InsertChild( _ppglbBefore ) : InsertParent( _ppglbBefore );
}
void RemoveRelation( bool _fChild ) const _BIEN_NOTHROW
{
_fChild ? RemoveChild() : RemoveParent();
}
// Exchange the two child links - <_ppglbChildBefore> must be before <_ppglbChildAfter>
// in the child list - this allows optimization.
// Actually <_ppglbChildBefore> can be after <_ppglbChildAfter> as long as <_ppglbChildBefore>
// is not the tail of the list.
static void ExchangeChildrenOrdered( _TyThis * _pglbChildBefore,
_TyThis * _pglbChildAfter ) _BIEN_NOTHROW
{
Assert( _pglbChildBefore != _pglbChildAfter ); // not a nop.
_TyThis ** ppglbPrevNextAfter = _pglbChildAfter->m_ppglbPrevNextChild;
_pglbChildAfter->m_ppglbPrevNextChild = _pglbChildBefore->m_ppglbPrevNextChild;
_pglbChildBefore->m_ppglbPrevNextChild = ppglbPrevNextAfter;
_TyThis * pglbNextAfter = _pglbChildAfter->m_pglbNextChild;
if ( pglbNextAfter )
{
pglbNextAfter->m_ppglbPrevNextChild = &( _pglbChildBefore->m_pglbNextChild );
}
_pglbChildBefore->m_pglbNextChild->m_ppglbPrevNextChild = &( _pglbChildAfter->m_pglbNextChild );
_pglbChildAfter->m_pglbNextChild = _pglbChildBefore->m_pglbNextChild;
_pglbChildBefore->m_pglbNextChild = pglbNextAfter;
}
static void ExchangeChildren( _TyThis * _pglbChildBefore,
_TyThis * _pglbChildAfter ) _BIEN_NOTHROW
{
Assert( _pglbChildBefore != _pglbChildAfter ); // not a nop.
_TyThis ** ppglbPrevNextAfter = _pglbChildAfter->m_ppglbPrevNextChild;
_pglbChildAfter->m_ppglbPrevNextChild = _pglbChildBefore->m_ppglbPrevNextChild;
_pglbChildBefore->m_ppglbPrevNextChild = ppglbPrevNextAfter;
_TyThis * pglbNextAfter = _pglbChildAfter->m_pglbNextChild;
if ( pglbNextAfter )
{
pglbNextAfter->m_ppglbPrevNextChild = &( _pglbChildBefore->m_pglbNextChild );
}
if ( _pglbChildBefore->m_pglbNextChild )
{
_pglbChildBefore->m_pglbNextChild->m_ppglbPrevNextChild = &( _pglbChildAfter->m_pglbNextChild );
}
_pglbChildAfter->m_pglbNextChild = _pglbChildBefore->m_pglbNextChild;
_pglbChildBefore->m_pglbNextChild = pglbNextAfter;
}
};
// Graph node:
// POD
class _graph_node_base
{
typedef _graph_node_base _TyThis;
public:
typedef _graph_node_base _TyGraphNodeBase;
typedef _graph_link_base< _TyThis > _TyGraphLinkBase;
protected:
_TyGraphLinkBase * m_pglbParents; // Parent list.
_TyGraphLinkBase * m_pglbChildren; // Child list.
public:
__INLINE void Init()
{
m_pglbParents = 0;
m_pglbChildren = 0;
}
_TyGNIndex UParents() const _BIEN_NOTHROW
{
return m_pglbParents ? m_pglbParents->UCountParents() : 0; // tho' m_pglbParents->UCountParents() should work.
}
bool FParents() const _BIEN_NOTHROW
{
return !!m_pglbParents;
}
_TyGraphLinkBase ** PPGLBParentHead() _BIEN_NOTHROW
{
return &m_pglbParents;
}
_TyGraphLinkBase * const * PPGLBParentHead() const _BIEN_NOTHROW
{
return &m_pglbParents;
}
_TyThis * PGNBGetNthParent( const _TyGNIndex & _u ) const
{
_TyGraphLinkBase * const * ppglb = _TyGraphLinkBase::PPGLBGetNthParent( &m_pglbParents, _u );
if ( !*ppglb )
{
throw _graph_nav_except("_graph_node_base::PGNBGetNthParent(): index beyond end.");
}
return (*ppglb)->m_pgnbNodeParent;
}
static _TyThis * PGNBGetThisFromPPGLBParentList( _TyGraphLinkBase ** _ppglbParents )
{
return reinterpret_cast< _TyThis * >( reinterpret_cast< char * >( _ppglbParents ) -
offsetof(_TyThis,m_pglbParents) );
}
void AddParent( _TyThis & _rgnbAdd, _TyGraphLinkBase & _rgl,
_TyGraphLinkBase *& _rpglInsertBeforeParent,
_TyGraphLinkBase *& _rpglInsertBeforeAddChild ) _BIEN_NOTHROW
{
_rgl.m_pgnbNodeChild = this;
_rgl.m_pgnbNodeParent = &_rgnbAdd;
_rgl.InsertChild( &_rpglInsertBeforeAddChild );
_rgl.InsertParent( &_rpglInsertBeforeParent );
}
void ExchangeParentsOrdered( _TyGNIndex _uMin, _TyGNIndex _uMax ) const _BIEN_NOTHROW
{
Assert( _uMin < _uMax );
Assert( _uMin < UParents() );
Assert( _uMax < UParents() );
_TyGraphLinkBase * const * _ppglbMin = _TyGraphLinkBase::PPGLBGetNthParent( &m_pglbParents, _uMin );
_TyGraphLinkBase * const * _ppglbMax = _TyGraphLinkBase::PPGLBGetNthParent( _ppglbMin, _uMax - _uMin );
_TyGraphLinkBase::ExchangeParentsOrdered( *_ppglbMin, *_ppglbMax );
}
void MoveParentUp( _TyGNIndex _uRemove, _TyGNIndex _uInsert )
{
Assert( _uRemove < _uInsert );
Assert( _uRemove < UParents()-1 ); // no-op otherwise.
_TyGraphLinkBase ** _ppglbRemove =
_TyGraphLinkBase::PPGLBGetNthParent( &m_pglbParents, _uRemove );
_TyGraphLinkBase ** _ppglbInsert =
_TyGraphLinkBase::PPGLBGetNthParent( _ppglbRemove, _uInsert - _uRemove );
if ( *_ppglbRemove )
{
(*_ppglbRemove)->RemoveParentAssume();
(*_ppglbRemove)->InsertParent( _ppglbInsert );
}
else
{
throw _graph_nav_except("_graph_node_base::MoveParentUp(): index beyond end.");
}
}
void MoveParentDown( _TyGNIndex _uRemove, _TyGNIndex _uInsert )
{
Assert( _uInsert < _uRemove );
_TyGraphLinkBase ** _ppglbInsert = _TyGraphLinkBase::PPGLBGetNthParent( &m_pglbParents, _uRemove );
_TyGraphLinkBase ** _ppglbRemove = _TyGraphLinkBase::PPGLBGetNthParent( _ppglbInsert, _uRemove - _uInsert );
if ( *_ppglbRemove )
{
(*_ppglbRemove)->RemoveParent();
(*_ppglbRemove)->InsertParentAssume( _ppglbInsert );
}
else
{
throw _graph_nav_except("_graph_node_base::MoveParentDown(): index beyond end.");
}
}
void MoveParent( const _TyGNIndex & _uRemove, const _TyGNIndex & _uInsert ) _BIEN_NOTHROW
{
if ( _uRemove < _uInsert )
{
MoveParentUp( _uRemove, _uInsert );
}
else
{
MoveParentDown( _uRemove, _uInsert );
}
}
void RemoveParent( const _TyGNIndex & _uRemove,
_TyThis ** _ppgnbRemoved,
_TyGraphLinkBase ** _ppglRemoved ) const
{
Assert( _uRemove < UParents() );
*_ppglRemoved = *_TyGraphLinkBase::PPGLBGetNthParent( &m_pglbParents, _uRemove );
if ( *_ppglRemoved )
{
(*_ppglRemoved)->RemoveParent();
*_ppgnbRemoved = (*_ppglRemoved)->m_pgnbNodeParent;
}
else
{
throw _graph_nav_except("_graph_node_base::RemoveParent(): index beyond end.");
}
}
_TyGNIndex UChildren() const _BIEN_NOTHROW
{
return m_pglbChildren ? m_pglbChildren->UCountChildren() : 0;
}
bool FChildren() const _BIEN_NOTHROW
{
return !!m_pglbChildren;
}
_TyGraphLinkBase ** PPGLBChildHead() _BIEN_NOTHROW
{
return &m_pglbChildren;
}
_TyGraphLinkBase * const * PPGLBChildHead() const _BIEN_NOTHROW
{
return &m_pglbChildren;
}
_TyThis * PGNBGetNthChild( const _TyGNIndex & _u ) const
{
_TyGraphLinkBase * const * ppglb = _TyGraphLinkBase::PPGLBGetNthChild( &m_pglbChildren, _u );
if ( !*ppglb )
{
throw _graph_nav_except("_graph_node_base::PGNBGetNthChild(): index beyond end.");
}
return (*ppglb)->m_pgnbNodeChild;
}
static _TyThis * PGNBGetThisFromPPGLBChildList( _TyGraphLinkBase ** _ppglbChildren )
{
return reinterpret_cast< _TyThis * >( reinterpret_cast< char * >( _ppglbChildren ) -
offsetof(_TyThis,m_pglbChildren) );
}
void AddChild( _TyThis & _rgnbAdd, _TyGraphLinkBase & _rgl,
_TyGraphLinkBase *& _rpglInsertBeforeChild,
_TyGraphLinkBase *& _rpglInsertBeforeAddParent ) _BIEN_NOTHROW
{
_rgl.m_pgnbNodeParent = this;
_rgl.m_pgnbNodeChild = &_rgnbAdd;
_rgl.InsertParent( &_rpglInsertBeforeAddParent );
_rgl.InsertChild( &_rpglInsertBeforeChild );
}
void ExchangeChildren( _TyGNIndex _uMin, _TyGNIndex _uMax ) _BIEN_NOTHROW
{
Assert( _uMin < _uMax );
Assert( _uMin < UChildren() );
Assert( _uMax < UChildren() );
_TyGraphLinkBase ** _ppglbMin = _TyGraphLinkBase::PPGLBGetNthChild( &m_pglbChildren, _uMin );
_TyGraphLinkBase ** _ppglbMax = _TyGraphLinkBase::PPGLBGetNthChild( _ppglbMin, _uMax - _uMin );
_TyGraphLinkBase::ExchangeChildrenOrdered( *_ppglbMin, *_ppglbMax );
}
void MoveChildUp( _TyGNIndex _uRemove, _TyGNIndex _uInsert )
{
Assert( _uRemove < _uInsert );
Assert( _uRemove < UChildren()-1 ); // no-op otherwise.
_TyGraphLinkBase ** _ppglbRemove = _TyGraphLinkBase::PPGLBGetNthChild( &m_pglbChildren, _uRemove );
_TyGraphLinkBase ** _ppglbInsert = _TyGraphLinkBase::PPGLBGetNthChild( _ppglbRemove, _uInsert - _uRemove );
if ( *_ppglbRemove )
{
(*_ppglbRemove)->RemoveChildAssume();
(*_ppglbRemove)->InsertChild( _ppglbInsert );
}
else
{
throw _graph_nav_except("_graph_node_base::MoveChildUp(): index beyond end.");
}
}
void MoveChildDown( _TyGNIndex _uRemove, _TyGNIndex _uInsert )
{
Assert( _uInsert < _uRemove );
_TyGraphLinkBase ** _ppglbInsert = _TyGraphLinkBase::PPGLBGetNthChild( &m_pglbChildren, _uRemove );
_TyGraphLinkBase ** _ppglbRemove = _TyGraphLinkBase::PPGLBGetNthChild( _ppglbInsert, _uRemove - _uInsert );
if ( *_ppglbRemove )
{
(*_ppglbRemove)->RemoveChild();
(*_ppglbRemove)->InsertChildAssume( _ppglbInsert );
}
else
{
throw _graph_nav_except("_graph_node_base::MoveChildDown(): index beyond end.");
}
}
void MoveChild( const _TyGNIndex & _uRemove, const _TyGNIndex & _uInsert ) _BIEN_NOTHROW
{
if ( _uRemove < _uInsert )
{
MoveChildUp( _uRemove, _uInsert );
}
else
{
MoveChildDown( _uRemove, _uInsert );
}
}
void RemoveChild( const _TyGNIndex & _uRemove,
_TyThis ** _ppgnbRemoved,
_TyGraphLinkBase ** _ppglRemoved )
{
Assert( _uRemove < UChildren() );
*_ppglRemoved = *_TyGraphLinkBase::PPGLBGetNthChild( &m_pglbChildren, _uRemove );
if ( *_ppglRemoved )
{
(*_ppglRemoved)->RemoveChild();
*_ppgnbRemoved = (*_ppglRemoved)->m_pgnbNodeChild;
}
else
{
throw _graph_nav_except("_graph_node_base::RemoveChild(): index beyond end.");
}
}
// Relation methods:
_TyGNIndex URelations( bool _fChildren ) const _BIEN_NOTHROW
{
return _fChildren ? UChildren() : UParents();
}
_TyGNIndex FRelations( bool _fChildren ) const _BIEN_NOTHROW
{
return _fChildren ? FChildren() : FParents();
}
_TyGraphLinkBase ** PPGLBRelationHead( bool _fChild ) _BIEN_NOTHROW
{
return _fChild ? PPGLBChildHead() : PPGLBParentHead();
}
_TyGraphLinkBase * const * PPGLBRelationHead( bool _fChild ) const _BIEN_NOTHROW
{
return _fChild ? PPGLBChildHead() : PPGLBParentHead();
}
};
// Template functions for graph path node:
template < class t_TyPathNodeBase >
t_TyPathNodeBase **
GPNB_PPPNBGetNth( const t_TyPathNodeBase * const * _pppnbAfter, _TyGNIndex _u ) _BIEN_NOTHROW
{
for ( ; _u--; _pppnbAfter = &( (*_pppnbAfter)->m_ppnbNext ) )
;
return const_cast< t_TyPathNodeBase ** >( _pppnbAfter );
}
template < class t_TyPathNodeBase >
t_TyPathNodeBase **
GPNB_PPPNBGetNthPrev( const t_TyPathNodeBase * const * _pppnbBefore, _TyGNIndex _u ) _BIEN_NOTHROW
{
for ( ; _u--; _pppnbBefore = t_TyPathNodeBase::PPPNBGetPrevPrevNext( _pppnbBefore ) )
;
return const_cast< t_TyPathNodeBase ** >( _pppnbBefore );
}
template < class t_TyGraphNodeBase, class t_TyGraphLinkBase >
class _graph_path_node_base
{
typedef _graph_path_node_base< t_TyGraphNodeBase, t_TyGraphLinkBase > _TyThis;
public:
typedef t_TyGraphLinkBase _TyGraphLinkBase;
typedef t_TyGraphNodeBase _TyGraphNodeBase;
typedef _TyThis _TyPathNodeBase;
t_TyGraphLinkBase * m_pglbLink; // link - may be null - if no previous graph path node.
t_TyGraphNodeBase * m_pgnbNode; // node.
_TyThis ** m_pppnbPrevNext;// pointer to address of the previous element's next pointer.
_TyThis * m_ppnbNext; // next in path.
// unsafe constructor:
_graph_path_node_base() _BIEN_NOTHROW
{
}
explicit _graph_path_node_base( _TyThis const & _r ) _BIEN_NOTHROW
: m_pgnbNode( _r.m_pgnbNode ),
m_pglbLink( _r.m_pglbLink )
{
}
#if 0 // Not a safe constructor - use first ctor - just as unsafe - but advertises as such.
explicit _graph_path_node_base( const t_TyGraphNodeBase * _pgnbNode ) _BIEN_NOTHROW
: m_pgnbNode( _pgnbNode )
{
}
#endif //0
_graph_path_node_base( t_TyGraphNodeBase * _pgnbNode,
t_TyGraphLinkBase * _pglbLink ) _BIEN_NOTHROW
: m_pgnbNode( _pgnbNode ),
m_pglbLink( _pglbLink )
{
}
~_graph_path_node_base() _BIEN_NOTHROW
{
}
// These methods are not const safe - they are meant to be used
// in the implementation only.
static _TyThis * PGNBGetThisFromPrevNext( const _TyThis * const * _pppnbBefore ) _BIEN_NOTHROW
{
return (_TyThis*)( ((char*)_pppnbBefore) - offsetof( _TyThis, m_ppnbNext ) );
}
static _TyThis ** PPPNBGetPrevPrevNext( const _TyThis * const * _pppnbBefore ) _BIEN_NOTHROW
{
return PGNBGetThisFromPrevNext( _pppnbBefore )->m_pppnbPrevNext;
}
static _TyThis ** PPGPNBGetNth( const _TyThis * const * _pppnbAfter,
_TyGNIndex _u ) _BIEN_NOTHROW
{
return GPNB_PPPNBGetNth( _pppnbAfter, _u );
}
static _TyThis ** PPGPNBGetNthPrev( const _TyThis * const * _pppnbBefore,
_TyGNIndex _u ) _BIEN_NOTHROW
{
return GPNB_PPPNBGetNthPrev( _pppnbBefore, _u );
}
static _TyThis ** PPGPNBGetNth( const _TyThis * const * _pppnbHead,
const _TyThis * const * _pppnbTail,
_TyGNIndex _uCount, _TyGNIndex _u ) _BIEN_NOTHROW
{
// Choose the fastest way to get the desired node:
if ( _u > ( _uCount / 2 ) )
{
return GPNB_PPPNBGetNthPrev( _pppnbTail, _uCount - _u );
}
else
{
return GPNB_PPPNBGetNth( _pppnbHead, _u );
}
}
void insert_node( _TyThis *& _rpgnBefore,
_TyThis **& _rpppnbTail ) _BIEN_NOTHROW
{
m_pppnbPrevNext = &_rpgnBefore;
m_ppnbNext = _rpgnBefore;
if ( _rpgnBefore )
{
_rpgnBefore->m_pppnbPrevNext = &m_ppnbNext;
}
else
{
Assert( m_pppnbPrevNext == _rpppnbTail );
_rpppnbTail = &m_ppnbNext;
}
_rpgnBefore = this;
}
// Insert into the middle of the list - assumes
void insert_node_in_middle( _TyThis *& _rpgnBefore ) _BIEN_NOTHROW
{
Assert( _rpgnBefore );
m_pppnbPrevNext = &_rpgnBefore;
m_ppnbNext = _rpgnBefore;
_rpgnBefore->m_pppnbPrevNext = &m_ppnbNext;
_rpgnBefore = this;
}
// Push this node onto the tail:
void push_back_node( _TyThis **& _rpppnbTail ) _BIEN_NOTHROW
{
m_pppnbPrevNext = _rpppnbTail;
*_rpppnbTail = this;
m_ppnbNext = 0;
_rpppnbTail = &m_ppnbNext;
}
void remove_node( _TyThis **& _rpppnbTail ) _BIEN_NOTHROW
{
if ( m_ppnbNext )
{
m_ppnbNext->m_pppnbPrevNext = m_pppnbPrevNext;
}
*m_pppnbPrevNext = m_ppnbNext;
if ( _rpppnbTail == &m_ppnbNext )
{
_rpppnbTail = m_pppnbPrevNext;
}
}
void remove_node_in_middle( ) _BIEN_NOTHROW
{
m_ppnbNext->m_pppnbPrevNext = m_pppnbPrevNext;
*m_pppnbPrevNext = m_ppnbNext;
}
void operator = ( const _TyThis & _r ) _BIEN_NOTHROW
{
m_pglbLink = _r.m_pglbLink;
m_pgnbNode = _r.m_pgnbNode;
}
};
// Predeclare iterator types:
template < class t_TyGraphNodeBase, class t_TyGraphLinkBase > class _graph_node_iterator_base_notsafe;
template < class t_TyGraphNodeBase, class t_TyGraphLinkBase > class _graph_link_pos_iterator_base_notsafe;
template < class t_TyGraphNodeBase, class t_TyGraphLinkBase > class _graph_link_ident_iterator_base_notsafe;
template < class t_TyPathNodeBase, class t_TyPathNodeAllocator > class _graph_path_iterator_base_notsafe;
// Passing allocators:
template < class _TyGraphNodeSafe, class _TyGraphLinkSafe,
class t_TyFIsConstIter, class _TyGraphNodeIterBaseSafe > class _graph_iterator_pass;
template < class t_TyPathNodeBase, class t_TyPathNodeAllocator,
class t_TyFIsConstIter, class t_TyBaseClass > class _graph_path_iterator_pass;
template < class t_TyPathNodeBase, class t_TyPathNodeAllocator >
struct _graph_base
// Need this to store non-static allocators - no path nodes are allocated by this object:
: public _alloc_base< t_TyPathNodeBase, t_TyPathNodeAllocator >
{
typedef _graph_base< t_TyPathNodeBase, t_TyPathNodeAllocator > _TyThis;
protected:
typedef _alloc_base< t_TyPathNodeBase, t_TyPathNodeAllocator > _TyBaseAllocPathNodeBase;
public:
typedef typename t_TyPathNodeBase::_TyGraphNodeBase _TyGraphNodeBaseBase;
typedef typename t_TyPathNodeBase::_TyGraphLinkBase _TyGraphLinkBaseBase;
typedef t_TyPathNodeBase _TyPathNodeBaseBase;
typedef _TyGraphNodeBaseBase _TyGraphNodeBase;
typedef _TyGraphLinkBaseBase _TyGraphLinkBase;
typedef t_TyPathNodeBase _TyPathNodeBase;
typedef _TyThis _TyGraphBaseBase;
typedef t_TyPathNodeAllocator _TyPathNodeBaseAllocatorAsPassed;
typedef typename _TyBaseAllocPathNodeBase::_TyAllocatorType _TyPathNodeBaseAllocator;
typedef _allocator_set_notsafe_base< _TyPathNodeBaseAllocatorAsPassed > _TyAllocatorSet;
// Define the various types of base iterators:
typedef _graph_node_iterator_base_notsafe< _TyGraphNodeBase, _TyGraphLinkBase > _TyGraphNodeIterBaseNotSafe;
typedef _graph_link_pos_iterator_base_notsafe< _TyGraphNodeBase, _TyGraphLinkBase > _TyGraphLinkPosIterBaseNotSafe;
typedef _graph_link_ident_iterator_base_notsafe< _TyGraphNodeBase, _TyGraphLinkBase > _TyGraphLinkIdentIterBaseNotSafe;
typedef _graph_path_iterator_base_notsafe< t_TyPathNodeBase, _TyPathNodeBaseAllocatorAsPassed > _TyGraphPathIterBaseNotSafe;
// Passing non-safe allocator types:
typedef _graph_iterator_pass< _TyGraphNodeBase, _TyGraphLinkBase,
std::true_type, _TyGraphNodeIterBaseNotSafe > _TyGraphNodeIterNotSafePassConst;
typedef _graph_iterator_pass< _TyGraphNodeBase, _TyGraphLinkBase,
std::true_type, _TyGraphLinkPosIterBaseNotSafe > _TyGraphLinkPosIterNotSafePassConst;
typedef _graph_iterator_pass< _TyGraphNodeBase, _TyGraphLinkBase,
std::true_type, _TyGraphLinkIdentIterBaseNotSafe > _TyGraphLinkIdentIterNotSafePassConst;
typedef _graph_path_iterator_pass< t_TyPathNodeBase, _TyPathNodeBaseAllocatorAsPassed,
std::true_type, _TyGraphPathIterBaseNotSafe > _TyGraphPathIterNotSafePassConst;
typedef _graph_iterator_pass< _TyGraphNodeBase, _TyGraphLinkBase,
std::false_type, _TyGraphNodeIterBaseNotSafe > _TyGraphNodeIterNotSafePassNonConst;
typedef _graph_iterator_pass< _TyGraphNodeBase, _TyGraphLinkBase,
std::false_type, _TyGraphLinkPosIterBaseNotSafe > _TyGraphLinkPosIterNotSafePassNonConst;
typedef _graph_iterator_pass< _TyGraphNodeBase, _TyGraphLinkBase,
std::false_type, _TyGraphLinkIdentIterBaseNotSafe > _TyGraphLinkIdentIterNotSafePassNonConst;
typedef _graph_path_iterator_pass< t_TyPathNodeBase, _TyPathNodeBaseAllocatorAsPassed,
std::false_type, _TyGraphPathIterBaseNotSafe > _TyGraphPathIterNotSafePassNonConst;
private: