-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrefnode.cc
107 lines (96 loc) · 2.2 KB
/
refnode.cc
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
/*
@copyright Russell Standish 2000-2013
@author Russell Standish
This file is part of Classdesc
Open source licensed under the MIT license. See LICENSE for details.
*/
/* this example exercises the graphnode functionality. An NxN grid of
nodes is created, connected toroidally, so there are plenty of
loops. Then it is packed into a pack_t, and exploded out into a second
graph. Finally, the graph's stucture is checked to be identical to the
first, but occupying a different part of the heap. */
#include <stdio.h>
#include "pack_base.h"
#include "pack_graph.h"
#include "refnode.h"
#include "refnode.cd"
#include "classdesc_epilogue.h"
using namespace classdesc;
int foonode::nodecntr=0;
ref<foonode> generate_graph()
{
const int N=20;
ref<foonode> graphbegin, rowbegin, lastrow, left, current;
int i,j;
for (graphbegin=current=foonode(), i=0; i<N; i++, current=foonode())
{
if (lastrow)
{
lastrow->down=current;
current->up=lastrow;
}
for (rowbegin=current, j=1; j<N; j++)
{
left=current;
current=foonode();
current->left=left;
left->right=current;
if (lastrow)
{
lastrow=lastrow->right;
current->up=lastrow;
lastrow->down=current;
}
}
lastrow=rowbegin;
current->right=rowbegin;
rowbegin->left=current;
}
/* toroidally connect first and last rows */
current=graphbegin; lastrow=rowbegin;
do
{
current->up=lastrow;
lastrow->down=current;
current=current->right;
lastrow=lastrow->right;
} while (current!=graphbegin);
return graphbegin;
}
int main()
{
ref<foonode> A=generate_graph(), B;
pack_t p;
try
{
p << A;
p >> B;
}
catch (const pack_error& x)
{
puts(x.what()); exit(1);
}
ref<foonode> Arow=A, Brow=B;
do
{
ref<foonode> Aptr=Arow, Bptr=Brow;
do
{
if (Aptr->nodeid!=Bptr->nodeid)
{
printf("nodeids: %d %d differ!\n",Aptr->nodeid,Bptr->nodeid);
exit(1);
}
if (Aptr==Bptr)
{
puts("Aptr & Bptr identical");
exit(1);
}
Aptr=Aptr->right;
Bptr=Bptr->right;
} while (Aptr!=Arow);
Arow=Arow->down;
Brow=Brow->down;
} while (Arow!=A);
return 0;
}