-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_gr_outp.h
404 lines (369 loc) · 14 KB
/
_gr_outp.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
#ifndef __GR_OUTP_H
#define __GR_OUTP_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_outp.h
// This module defines the graph binary output iterator.
#include <stdexcept>
__DGRAPH_BEGIN_NAMESPACE
class bad_graph : public _t__Named_exception< __DGRAPH_DEFAULT_ALLOCATOR >
{
typedef bad_graph _TyThis;
typedef _t__Named_exception< __DGRAPH_DEFAULT_ALLOCATOR > _TyBase;
public:
bad_graph( const char * _pc )
: _TyBase( _pc )
{
}
bad_graph( const string_type & __s )
: _TyBase( __s )
{
}
};
// Function that knows how to write raw data to a stream:
template < class t_TyOutputStream, class t_TyWrite >
void
_RawWriteGraphEl( t_TyOutputStream & _ros, t_TyWrite const & _rel )
{
Assert( 0 ); // Should specialize for each stream type.
}
// Tokens for the binary representation:
template < class t_TyDummy = std::false_type > // make a template so that constants link.
struct _binary_rep_tokens
{
typedef unsigned char _TyToken;
static const _TyToken ms_ucDirectionUp = 0x01;
static const _TyToken ms_ucDirectionDown = 0x02;
static const _TyToken ms_ucContextPush = 0x03;
static const _TyToken ms_ucContextPop = 0x04;
static const _TyToken ms_ucNode = 0x05;
static const _TyToken ms_ucUnfinishedNode = 0x06;
#ifdef __GR_BINARY_WRITENODEFOOTER
static const _TyToken ms_ucNodeFooter = 0x07;
#endif //__GR_BINARY_WRITENODEFOOTER
static const _TyToken ms_ucLink = 0x08;
static const _TyToken ms_ucLinkFromUnfinished = 0x09;
static const _TyToken ms_ucNormalLinkFooter = 0x0a;
static const _TyToken ms_ucUnfinishedLinkFooter = 0x0b;
static const _TyToken ms_ucGraphFooter = 0x0c;
// Link construction indicators:
static const _TyToken ms_ucLinkConstructed = 0x01; // A constructed link.
static const _TyToken ms_ucLinkEmpty = 0x00; // An unconstructed link.
};
// Some compilers, intel for instance, require that these be declared externally to have linkage.
template < class t_TyDummy >
const typename _binary_rep_tokens< t_TyDummy >::_TyToken
_binary_rep_tokens< t_TyDummy >::ms_ucDirectionUp;
template < class t_TyDummy >
const typename _binary_rep_tokens< t_TyDummy >::_TyToken
_binary_rep_tokens< t_TyDummy >::ms_ucDirectionDown;
template < class t_TyDummy >
const typename _binary_rep_tokens< t_TyDummy >::_TyToken
_binary_rep_tokens< t_TyDummy >::ms_ucContextPush;
template < class t_TyDummy >
const typename _binary_rep_tokens< t_TyDummy >::_TyToken
_binary_rep_tokens< t_TyDummy >::ms_ucContextPop;
template < class t_TyDummy >
const typename _binary_rep_tokens< t_TyDummy >::_TyToken
_binary_rep_tokens< t_TyDummy >::ms_ucNode;
template < class t_TyDummy >
const typename _binary_rep_tokens< t_TyDummy >::_TyToken
_binary_rep_tokens< t_TyDummy >::ms_ucUnfinishedNode;
#ifdef __GR_BINARY_WRITENODEFOOTER
template < class t_TyDummy >
const typename _binary_rep_tokens< t_TyDummy >::_TyToken
_binary_rep_tokens< t_TyDummy >::ms_ucNodeFooter;
#endif //__GR_BINARY_WRITENODEFOOTER
template < class t_TyDummy >
const typename _binary_rep_tokens< t_TyDummy >::_TyToken
_binary_rep_tokens< t_TyDummy >::ms_ucLink;
template < class t_TyDummy >
const typename _binary_rep_tokens< t_TyDummy >::_TyToken
_binary_rep_tokens< t_TyDummy >::ms_ucLinkFromUnfinished;
template < class t_TyDummy >
const typename _binary_rep_tokens< t_TyDummy >::_TyToken
_binary_rep_tokens< t_TyDummy >::ms_ucNormalLinkFooter;
template < class t_TyDummy >
const typename _binary_rep_tokens< t_TyDummy >::_TyToken
_binary_rep_tokens< t_TyDummy >::ms_ucUnfinishedLinkFooter;
template < class t_TyDummy >
const typename _binary_rep_tokens< t_TyDummy >::_TyToken
_binary_rep_tokens< t_TyDummy >::ms_ucGraphFooter;
// Link construction indicators:
template < class t_TyDummy >
const typename _binary_rep_tokens< t_TyDummy >::_TyToken
_binary_rep_tokens< t_TyDummy >::ms_ucLinkConstructed; // A constructed link.
template < class t_TyDummy >
const typename _binary_rep_tokens< t_TyDummy >::_TyToken
_binary_rep_tokens< t_TyDummy >::ms_ucLinkEmpty; // An unconstructed link.
template < class t_TyGraphNodeBase, class t_TyGraphLinkBase,
class t_TyStreamObject,
// This causes all available information to be written - i.e.
// the names ( pointers ) of all links and nodes - this is not needed by all readers.
bool t_fWriteExtraInformation,
// Setting this to true allows the writer to write unconstucted links.
// If it is false and the writer encounters an unconstucted link it will throw
// a bad_graph exception.
bool t_fAllowUnconstructedLinks = false >
struct _binary_output_base
{
private:
typedef _binary_output_base< t_TyGraphNodeBase, t_TyGraphLinkBase,
t_TyStreamObject,
t_fWriteExtraInformation,
t_fAllowUnconstructedLinks > _TyThis;
public:
typedef t_TyGraphLinkBase _TyGraphLinkBase;
typedef t_TyGraphNodeBase _TyGraphNodeBase;
typedef typename t_TyStreamObject::_TyInitArg _TyInitArg;
typedef typename t_TyStreamObject::_TyStreamPos _TyStreamPos; // A position within a stream.
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_fOutputOn; // Allow caller to turn off output
// ( dangerous - if you want to read it in again ).
AssertStatement( bool m_fSetDirection )
_binary_output_base( _TyInitArg _ria, bool _fDirectionDown,
_TyIONodeEl const & _rione,
_TyIOLinkEl const & _riole )
: m_ros( _ria, _rione, _riole ),
m_fDirectionDown( _fDirectionDown ),
m_fOutputOn( true )
#if ASSERTSENABLED
,m_fSetDirection( 0 )
#endif //ASSERTSENABLED
{
}
void _SetDirection( bool _fDirectionDown )
{
AssertStatement( m_fSetDirection = 0 )
m_fDirectionDown = _fDirectionDown;
}
_TyStreamPos _Tell() { return m_ros.TellP(); }
void _Seek( _TyStreamPos _sp ) { m_ros.SeekP( _sp ); }
typedef typename _binary_rep_tokens< std::false_type >::_TyToken _TyToken;
void _WriteToken( _TyToken const & uc )
{
__THROWPT( e_ttFileOutput );
m_ros.Write( &uc, sizeof uc );
}
void _WritePtr( void * _pv )
{
__THROWPT( e_ttFileOutput );
m_ros.Write( &_pv, sizeof( _pv ) );
}
void _WriteContext( bool _fPush )
{
if ( m_fOutputOn )
{
__THROWPT( e_ttFileOutput );
_WriteToken( _fPush ?
_binary_rep_tokens< std::false_type >::ms_ucContextPush :
_binary_rep_tokens< std::false_type >::ms_ucContextPop );
}
}
void _WriteDirectionChange( bool _fDirectionDown )
{
// We should always be up-to-date:
Assert( !m_fSetDirection || ( m_fDirectionDown == !_fDirectionDown ) );
if ( m_fOutputOn )
{
__THROWPT( e_ttFileOutput );
_WriteToken( _fDirectionDown ?
_binary_rep_tokens< std::false_type >::ms_ucDirectionDown :
_binary_rep_tokens< std::false_type >::ms_ucDirectionUp );
}
__DEBUG_STMT( m_fSetDirection = 1 )
m_fDirectionDown = _fDirectionDown; // Change here in case of throw.
}
void _WriteNewUnfinishedNodeHeader( t_TyGraphNodeBase * _pgnb,
t_TyGraphLinkBase * _pglb )
{
if ( m_fOutputOn )
{
__THROWPT( e_ttFileOutput );
// Write the token, the node and the link:
_WriteToken( _binary_rep_tokens< std::false_type >::ms_ucUnfinishedNode );
_WritePtr( _pgnb );
_WritePtr( _pglb );
}
}
// Return the number of relatives in the opposite direction of the current iteration.
int _WriteUnfinishedNodeFooter( t_TyGraphNodeBase * _pgnb,
t_TyGraphLinkBase * _pglb )
{
// We write a list of links - terminated by a zero pointer.
if ( m_fOutputOn )
{
__THROWPT( e_ttFileOutput );
int iRelations = 0;
t_TyGraphLinkBase ** _ppglb;
for ( _ppglb = _pgnb->PPGLBRelationHead( !m_fDirectionDown );
;
_ppglb = (*_ppglb)->PPGLBGetNextRelation( !m_fDirectionDown ),
iRelations++ )
{
_WritePtr( *_ppglb );
if ( !*_ppglb )
break; // We want to write an extra zero on the end.
}
return iRelations;
}
else
{
return _pgnb->URelations( !m_fDirectionDown );
}
}
void _WriteNodeHeader( t_TyGraphNodeBase * _pgnb )
{
if ( m_fOutputOn )
{
__THROWPT( e_ttFileOutput );
_WriteToken( _binary_rep_tokens< std::false_type >::ms_ucNode );
if ( t_fWriteExtraInformation )
{
_WritePtr( _pgnb );
}
}
}
void _WriteNodeFooter( t_TyGraphNodeBase * _pgnb )
{
if ( m_fOutputOn )
{
__THROWPT( e_ttFileOutput );
#ifdef __GR_BINARY_WRITENODEFOOTER
// Not really necessary to write a node footer - but good for
// debugging ( and its only a byte ).
_WriteToken( _binary_rep_tokens::ms_ucNodeFooter );
#endif //__GR_BINARY_WRITENODEFOOTER
}
}
void _WriteLinkHeader( t_TyGraphLinkBase * _pglb,
t_TyGraphNodeBase * _pgnbUnfinished )
{
if ( m_fOutputOn )
{
__THROWPT( e_ttFileOutput );
if ( _pgnbUnfinished )
{
// Then we need to write the node name ( pointer ):
_WriteToken( _binary_rep_tokens< std::false_type >::ms_ucLinkFromUnfinished );
_WritePtr( _pglb );
if ( t_fWriteExtraInformation )
{
// This is unnecessary - the links are uniquely identified by their own name.
_WritePtr( _pgnbUnfinished );
}
}
else
{
_WriteToken( _binary_rep_tokens< std::false_type >::ms_ucLink );
if ( t_fWriteExtraInformation )
{
_WritePtr( _pglb );
}
}
if ( t_fAllowUnconstructedLinks )
{
// Write whether or not the link is constructed:
_WriteToken( _pglb->FIsConstructed() ?
_binary_rep_tokens< std::false_type >::ms_ucLinkConstructed :
_binary_rep_tokens< std::false_type >::ms_ucLinkEmpty );
}
else
{
if ( !_pglb->FIsConstructed() )
{
throw bad_graph( "_WriteLinkHeader(): Attempt to write an unconstructed link to stream when not permitted." );
}
}
}
}
void _WriteLinkFooter( t_TyGraphLinkBase * _pglb,
t_TyGraphNodeBase * _pgnbUnfinished )
{
if ( m_fOutputOn )
{
__THROWPT( e_ttFileOutput );
if ( _pgnbUnfinished )
{
// Write a footer record - the link and the node.
_WriteToken( _binary_rep_tokens< std::false_type >::ms_ucUnfinishedLinkFooter );
_WritePtr( _pgnbUnfinished );
if ( !t_fWriteExtraInformation && _pglb ) // If zero then we already wrote it in the header.
{
// We already wrote this in the header.
_WritePtr( _pglb );
}
}
else
{
// Normal link footer
_WriteToken( _binary_rep_tokens< std::false_type >::ms_ucNormalLinkFooter );
}
}
}
void _WriteGraphFooter()
{
if ( m_fOutputOn )
{
__THROWPT( e_ttFileOutput );
_WriteToken( _binary_rep_tokens< std::false_type >::ms_ucGraphFooter );
}
}
};
template < class t_TyGraphNode, class t_TyGraphLink,
class t_TyStreamObject, class t_TyAllocator,
bool t_fWriteExtraInformation,
bool t_fAllowUnconstructedLinks = false >
struct _binary_output_object
: public _binary_output_base< typename t_TyGraphNode::_TyGraphNodeBaseBase,
typename t_TyGraphLink::_TyGraphLinkBaseBase,
t_TyStreamObject,
t_fWriteExtraInformation,
t_fAllowUnconstructedLinks >
{
private:
typedef _binary_output_object< t_TyGraphNode, t_TyGraphLink,
t_TyStreamObject, t_TyAllocator,
t_fWriteExtraInformation,
t_fAllowUnconstructedLinks > _TyThis;
typedef _binary_output_base< typename t_TyGraphNode::_TyGraphNodeBaseBase,
typename t_TyGraphLink::_TyGraphLinkBaseBase,
t_TyStreamObject,
t_fWriteExtraInformation,
t_fAllowUnconstructedLinks > _TyBase;
public:
typedef _TyBase _TyOutputStreamBase;
typedef typename _TyBase::_TyInitArg _TyInitArg;
typedef typename _TyBase::_TyIONodeEl _TyIONodeEl;
typedef typename _TyBase::_TyIOLinkEl _TyIOLinkEl;
_binary_output_object( _TyInitArg _ros, bool _fDirectionDown,
_TyIONodeEl const & _rione,
_TyIOLinkEl const & _riole,
t_TyAllocator const & ) // don't need the allocator.
: _TyBase( _ros, _fDirectionDown, _rione, _riole )
{
}
void _WriteNode( const t_TyGraphNode * _pgn )
{
if ( _TyBase::m_fOutputOn )
{
__THROWPT( e_ttFileOutput );
_TyBase::m_ros.WriteNodeEl( _pgn->RElConst() );
}
}
void _WriteLink( const t_TyGraphLink * _pgl )
{
if ( _TyBase::m_fOutputOn )
{
__THROWPT( e_ttFileOutput );
_TyBase::m_ros.WriteLinkEl( _pgl->RElConst() );
}
}
};
__DGRAPH_END_NAMESPACE
#endif //__GR_OUTP_H