-
Notifications
You must be signed in to change notification settings - Fork 1
/
adjnet.cpp
111 lines (102 loc) · 2.04 KB
/
adjnet.cpp
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
#include<memory>
template<typename T>
struct anode
{
T x;
int k;
anode** sib;
};
template<typename T>
class adjnet
{
private:
anode* v;
int n;
bool fixed;
//node initialization; assuming maximum size of sibilings
void init_nodes()
{
v = (anode*)malloc(sizeof(anode)*n);
for(int i = 0; i < n; i++)
{
v[i].k = 0;
v[i].sib = (anode**)malloc(sizeof(anode*)*n);
}
fixed = false;
}
//node initialization; fixed degree
void init_nodes(int* degrees, int** sibilings)
{
v = (anode*)malloc(sizeof(anode)*n);
for(int i = 0; i < n; i++)
{
v[i] = anode();
v[i].k = degrees[i];
if(v[i].k>0)
{
v[i].sib = (anode**)malloc(sizeof(anode*)*v[i].k);
}
}
fixed = true;
for(int i = 0; i < n; i++)
{
for(int j = 0; j < v[i].k; j++)
{
v[i].sib[j] = v + sibilings[i][j];
}
}
}
public:
adjnet(int nNodes)
{
n = nNodes;
init_nodes();
}
adjnet(int nNodes, int* degs, int** sibs)
{
n = nNodes;
init_nodes(degs, sibs);
}
~adjnet()
{
for(int i = 0; i < n; i++)
{
free(v[i].sib);
}
free(v);
}
bool add_edge(int from, int to, bool bothWays = false)
{
if(fixed)
{
return false;
}
v[from].sib[to] = (v+to);
if(bothWays)
{
v[to].sib[from] = (v+from);
}
return true;
}
bool remove_edge(int from, int to, bool bothWays = false)
{
if(fixed)
{
return false;
}
v[from].sib[to] = NULL;
if(bothWays)
{
v[to].sib[from] = NULL;
}
return true;
}
T& operator[](const int index)
{
return v[i].x;
}
const T& operator[](const int index) const
{
return v[i].x;
}
};