-
Notifications
You must be signed in to change notification settings - Fork 141
Expand file tree
/
Copy pathgraph.cpp
More file actions
91 lines (76 loc) · 1.94 KB
/
graph.cpp
File metadata and controls
91 lines (76 loc) · 1.94 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
# include "stdafx.h"
# include "graph.h"
graph::graph()
{
}
graph::~graph()
{ for( int i = 0; i < (int)edges.size(); i++ )
{ if( edges[i] != NULL )
delete edges[i];
edges[i] = NULL;
}
for( int j = 0; j < (int)graphnodes.size(); j++ )
{ delete graphnodes[j];
graphnodes[j] = NULL;
}
}
long graph::Insert( edge * e, void * exParam, bool (* CALLBACK_EQUALS)( void * dataOne, void * dataTwo, void * exParam ) )
{
e->one->edges.clear();
e->two->edges.clear();
bool one_equal = false;
bool two_equal = false;
long oneIndex = 0, twoIndex = 0;
for( int i = 0; i < (int)graphnodes.size(); i++ )
{
if( !one_equal )
{ if( CALLBACK_EQUALS(e->one->data, graphnodes[i]->data, exParam ) == true )
{ one_equal = true;
delete e->one;
e->one = graphnodes[i];
oneIndex = i;
}
}
if( !two_equal )
{ if( CALLBACK_EQUALS(e->two->data, graphnodes[i]->data, exParam ) == true )
{ two_equal = true;
delete e->two;
e->two = graphnodes[i];
twoIndex = i;
}
}
//short circuit the loop
if( one_equal && two_equal )
break;
}
if( one_equal == false )
{ oneIndex = graphnodes.size();
graphnodes.push_back(e->one);
}
if( two_equal == false )
{ twoIndex = graphnodes.size();
graphnodes.push_back(e->two);
}
e->oneIndex = oneIndex;
e->twoIndex = twoIndex;
long position = edges.size();
e->one->edges.push_back(position);
e->two->edges.push_back(position);
edges.push_back( e );
return position;
}
void graph::InsertBlank()
{ edges.push_back( NULL );
}
void graph::Save(const char * filename, void (*PRINT_DATA)( ofstream & out, void * data ) )
{
ofstream outf("graph.txt");
for( int j = 0; j < (int)graphnodes.size(); j++ )
{ PRINT_DATA( outf, graphnodes[j]->data );
outf<<"\t:";
for( int i = 0; i < (int)graphnodes[j]->edges.size(); i++ )
outf<<graphnodes[j]->edges[i]<<" ";
outf<<endl;
}
outf.close();
}