-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_gr_dump.h
More file actions
402 lines (361 loc) · 11.5 KB
/
_gr_dump.h
File metadata and controls
402 lines (361 loc) · 11.5 KB
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
#ifndef __GR_DUMP_H
#define __GR_DUMP_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_dump.h
// Human-readable(barely:-) dumping of graph to a stream.
// Dumped in depth first order - need to maintain a context stack
// if you are trying to reconstruct by hand.
#include <map>
__DGRAPH_BEGIN_NAMESPACE
enum EDumpOptions
{
e_doNone,
e_doMapPointers
// We map the node/link pointers to unique ids as we dump.
// This allows comparison of graphs that aren't at the same
// place in memory.
};
template < class t_TyGraphNodeBase,
class t_TyGraphLinkBase,
class t_TyStreamObject,
class t_TyAllocator >
struct _dump_object_base
{
private:
typedef _dump_object_base< t_TyGraphNodeBase, t_TyGraphLinkBase,
t_TyStreamObject, t_TyAllocator > _TyThis;
public:
typedef t_TyAllocator _TyAllocator;
typedef t_TyGraphLinkBase _TyGraphLinkBase;
typedef t_TyGraphNodeBase _TyGraphNodeBase;
typedef typename t_TyStreamObject::_TyInitArg _TyInitArg;
typedef typename t_TyStreamObject::_TyStreamPos _TyStreamPos;
typedef typename t_TyStreamObject::_TyIONodeEl _TyIONodeEl;
typedef typename t_TyStreamObject::_TyIOLinkEl _TyIOLinkEl;
t_TyStreamObject m_ros; // The stream into which we are dumping.
bool m_fDirectionDown; // The current direction of the iteration.
bool m_fWaitWriteStateNumber;
int m_iStateNumber;
bool m_fOutputOn; // Allow caller to turn off output.
EDumpOptions m_doOptions; // Dump options.
// Graph object -> id mapping stuff:
typedef size_t _TyObjectId;
_TyObjectId m_idNodeCur; // Current node id.
_TyObjectId m_idLinkCur; // Current link id.
typedef typename _Alloc_traits< typename map< void *, _TyObjectId, less< void * > >::value_type, _TyAllocator >::allocator_type _TyAllocatorMapPv;
typedef map< void *, _TyObjectId, less< void * >, _TyAllocatorMapPv > _TyMapToIds;
_TyMapToIds m_mapNodeIds;
_TyMapToIds m_mapLinkIds;
AssertStatement( bool m_fSetDirection )
_dump_object_base( _TyInitArg _ros, bool _fDirectionDown,
_TyIONodeEl const & _rione,
_TyIOLinkEl const & _riole,
t_TyAllocator const & _rA )
: m_ros( _ros, _rione, _riole ),
m_fDirectionDown( _fDirectionDown ),
m_fWaitWriteStateNumber( false ),
m_iStateNumber( 0 ),
m_fOutputOn( true ),
m_doOptions( e_doNone ),
m_idNodeCur( 0 ),
m_idLinkCur( 0 ),
m_mapNodeIds( typename _TyMapToIds::key_compare(), _rA ),
m_mapLinkIds( typename _TyMapToIds::key_compare(), _rA )
#if ASSERTSENABLED
,m_fSetDirection( 0 )
#endif //ASSERTSENABLED
{
}
template < class t_TyOptions >
void SetOutputOptions( t_TyOptions _o )
{
m_doOptions = _o;
}
void _SetDirection( bool _fDirectionDown )
{
AssertStatement( m_fSetDirection = 0 )
m_fDirectionDown = _fDirectionDown;
}
_TyStreamPos _Tell() { return m_ros.TellP(); }
void _Seek( _TyStreamPos _sp ) { (void)m_ros.SeekP( _sp ); }
void _WriteStateNumber()
{
if ( m_fWaitWriteStateNumber )
{
m_fWaitWriteStateNumber = false;
}
else
{
if ( m_fOutputOn )
{
__THROWPT( e_ttFileOutput | e_ttMemory );
m_ros << "{" << m_iStateNumber << "}:";
}
m_iStateNumber++;
}
}
void _WriteContext( bool _fPush )
{
_WriteStateNumber();
m_fWaitWriteStateNumber = true;
if ( m_fOutputOn )
{
__THROWPT( e_ttFileOutput | e_ttMemory );
m_ros << ( _fPush ? "<Push" : "<Pop" ) << " context>\n";
}
}
void _WriteDirectionChange( bool _fDirectionDown )
{
// We should always be up-to-date:
Assert( !m_fSetDirection || ( m_fDirectionDown == !_fDirectionDown ) );
_WriteStateNumber();
m_fWaitWriteStateNumber = true;
if ( m_fOutputOn )
{
__THROWPT( e_ttFileOutput | e_ttMemory );
m_ros << "<Change direction to " <<
( _fDirectionDown ? "down>\n" : "up>\n" );
}
AssertStatement( m_fSetDirection = 1 )
m_fDirectionDown = _fDirectionDown;
}
void _WriteNodePtr( t_TyGraphNodeBase * _pgnb )
{
m_ros << "(";
if ( e_doMapPointers == m_doOptions )
{
typedef pair< typename _TyMapToIds::iterator, bool > _TyPib;
_TyPib pib = m_mapNodeIds.
insert( typename _TyMapToIds::value_type( _pgnb, m_idNodeCur ) );
m_ros << pib.first->second;
if ( pib.second )
{
++m_idNodeCur;
}
}
else
{
m_ros << _pgnb;
}
m_ros << ")";
}
void _WriteLinkPtr( t_TyGraphLinkBase * _pglb )
{
m_ros << "<";
if ( e_doMapPointers == m_doOptions )
{
typedef pair< typename _TyMapToIds::iterator, bool > _TyPib;
_TyPib pib = m_mapLinkIds.
insert( typename _TyMapToIds::value_type( _pglb, m_idLinkCur ) );
m_ros << pib.first->second;
if ( pib.second )
{
++m_idLinkCur;
}
}
else
{
m_ros << _pglb;
}
m_ros << ">";
}
// Return the number of relatives in the opposite direction of the current iteration.
void _WriteNewUnfinishedNodeHeader( t_TyGraphNodeBase * _pgnb,
t_TyGraphLinkBase * _pglb )
{
_WriteStateNumber();
if ( m_fOutputOn )
{
__THROWPT( e_ttFileOutput | e_ttMemory );
#ifdef __GR_DUMP_DONTWRITENAMES
_pgnb = 0;
_pglb = 0;
#endif //__GR_DUMP_DONTWRITENAMES
m_ros << "New unfinished node: ";
_WriteNodePtr( _pgnb );
m_ros << " entered from link ";
_WriteLinkPtr( _pglb );
m_ros << ".\n";
}
}
int _WriteUnfinishedNodeFooter( t_TyGraphNodeBase * _pgnb,
t_TyGraphLinkBase * _pglb )
{
if ( m_fOutputOn )
{
__THROWPT( e_ttFileOutput | e_ttMemory );
#ifdef __GR_DUMP_DONTWRITENAMES
t_TyGraphNodeBase * _pgnbWrite = 0;
t_TyGraphLinkBase * _pglbWrite = 0;
#else //__GR_DUMP_DONTWRITENAMES
t_TyGraphNodeBase *& _pgnbWrite = _pgnb;
t_TyGraphLinkBase *& _pglbWrite = _pglb;
#endif //__GR_DUMP_DONTWRITENAMES
m_ros << "Unfinished node footer: ";
_WriteNodePtr( _pgnbWrite );
m_ros << " entered from link ";
_WriteLinkPtr( _pglbWrite );
m_ros << ".\n";
m_ros << ( m_fDirectionDown ? "Parent" : "Child" ) << " relations [";
int iRelations = 0;
t_TyGraphLinkBase ** _ppglb;
for ( _ppglb = _pgnb->PPGLBRelationHead( !m_fDirectionDown );
(*_ppglb)->PGLBGetNextRelation( !m_fDirectionDown );
_ppglb = (*_ppglb)->PPGLBGetNextRelation( !m_fDirectionDown ),
iRelations++ )
{
#ifdef __GR_DUMP_DONTWRITENAMES
_WriteLinkPtr( 0 );
#else //__GR_DUMP_DONTWRITENAMES
_WriteLinkPtr( *_ppglb );
#endif //__GR_DUMP_DONTWRITENAMES
m_ros << ",";
}
// Write last link:
#ifdef __GR_DUMP_DONTWRITENAMES
_WriteLinkPtr( 0 );
#else //__GR_DUMP_DONTWRITENAMES
_WriteLinkPtr( *_ppglb );
#endif //__GR_DUMP_DONTWRITENAMES
m_ros << "].\n";
return ++iRelations;
}
else
{
return _pgnb->URelations( !m_fDirectionDown );
}
}
void _WriteNodeHeader( t_TyGraphNodeBase * _pgnb )
{
_WriteStateNumber();
if ( m_fOutputOn )
{
__THROWPT( e_ttFileOutput | e_ttMemory );
#ifdef __GR_DUMP_DONTWRITENAMES
_pgnb = 0;
#endif //__GR_DUMP_DONTWRITENAMES
m_ros << "Normal node: ";
_WriteNodePtr( _pgnb );
m_ros << ".\n";
}
}
void _WriteNodeFooter( t_TyGraphNodeBase * _pgnb )
{
if ( m_fOutputOn )
{
__THROWPT( e_ttFileOutput | e_ttMemory );
#ifdef __GR_DUMP_DONTWRITENAMES
_pgnb = 0;
#endif //__GR_DUMP_DONTWRITENAMES
m_ros << "Node footer: ";
_WriteNodePtr( _pgnb );
m_ros << ".\n";
}
}
void _WriteLinkHeader( t_TyGraphLinkBase * _pglb,
t_TyGraphNodeBase * _pgnbUnfinished )
{
_WriteStateNumber();
if ( m_fOutputOn )
{
__THROWPT( e_ttFileOutput | e_ttMemory );
#ifdef __GR_DUMP_DONTWRITENAMES
t_TyGraphLinkBase * _pglbWrite = 0;
#else //__GR_DUMP_DONTWRITENAMES
t_TyGraphLinkBase *& _pglbWrite = _pglb;
#endif //__GR_DUMP_DONTWRITENAMES
m_ros << "Link: ";
_WriteLinkPtr( _pglbWrite );
if ( _pgnbUnfinished )
{
#ifdef __GR_DUMP_DONTWRITENAMES
_pgnbUnfinished = 0;
#endif //__GR_DUMP_DONTWRITENAMES
m_ros << " iterating unfinished node ";
_WriteNodePtr( _pgnbUnfinished );
}
m_ros << ".\n";
if ( !_pglb->FIsConstructed() )
{
m_ros << " ***Link is Empty***.\n";
}
}
}
void _WriteLinkFooter( t_TyGraphLinkBase * _pglb,
t_TyGraphNodeBase * _pgnb )
{
if ( m_fOutputOn )
{
__THROWPT( e_ttFileOutput | e_ttMemory );
#ifdef __GR_DUMP_DONTWRITENAMES
_pglb = 0;
#endif //__GR_DUMP_DONTWRITENAMES
m_ros << "Link footer: ";
_WriteLinkPtr( _pglb );
if ( _pgnb )
{
#ifdef __GR_DUMP_DONTWRITENAMES
_pgnb = 0;
#endif //__GR_DUMP_DONTWRITENAMES
m_ros << " enters into unfinished node ";
_WriteNodePtr( _pgnb );
}
m_ros << ".\n";
}
}
void _WriteGraphFooter()
{
if ( m_fOutputOn )
{
__THROWPT( e_ttFileOutput | e_ttMemory );
m_ros << "< Graph Footer >\n";
}
}
};
template < class t_TyGraphNode, class t_TyGraphLink,
class t_TyStreamObject, class t_TyAllocator >
struct _dump_object
: public _dump_object_base< typename t_TyGraphNode::_TyGraphNodeBaseBase,
typename t_TyGraphLink::_TyGraphLinkBaseBase,
t_TyStreamObject, t_TyAllocator >
{
private:
typedef _dump_object< t_TyGraphNode, t_TyGraphLink,
t_TyStreamObject, t_TyAllocator > _TyThis;
typedef _dump_object_base< typename t_TyGraphNode::_TyGraphNodeBaseBase,
typename t_TyGraphLink::_TyGraphLinkBaseBase,
t_TyStreamObject, t_TyAllocator > _TyBase;
public:
typedef _TyBase _TyOutputStreamBase;
typedef typename _TyBase::_TyInitArg _TyInitArg;
typedef typename _TyBase::_TyIONodeEl _TyIONodeEl;
typedef typename _TyBase::_TyIOLinkEl _TyIOLinkEl;
_dump_object( _TyInitArg _ros, bool _fDirectionDown,
_TyIONodeEl const & _rione,
_TyIOLinkEl const & _riole,
t_TyAllocator const & _rA )
: _TyBase( _ros, _fDirectionDown, _rione, _riole, _rA )
{
}
void _WriteNode( const t_TyGraphNode * _pgn )
{
if ( _TyBase::m_fOutputOn )
{
__THROWPT( e_ttFileOutput | e_ttMemory );
_TyBase::m_ros.WriteNodeEl( _pgn->RElConst() );
}
}
void _WriteLink( const t_TyGraphLink * _pgl )
{
if ( _TyBase::m_fOutputOn )
{
__THROWPT( e_ttFileOutput | e_ttMemory );
_TyBase::m_ros.WriteLinkEl( _pgl->RElConst() );
}
}
};
__DGRAPH_END_NAMESPACE
#endif //__GR_DUMP_H