-
Notifications
You must be signed in to change notification settings - Fork 0
/
AdjMatrix.h
156 lines (135 loc) · 3.21 KB
/
AdjMatrix.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
/*
* AdjMatrix.h
*
* Created on: Sep 11, 2011
* Author: correa
*/
#ifndef ADJMATRIX_H_
#define ADJMATRIX_H_
#include <cstring>
using namespace std;
static inline int inline_ceillog2(int n) {
int c = 0;
int l = 0;
while (n > 1) {
c |= (n & 1);
n >>= 1;
l++;
}
l += c;
return l;
}
static inline char edgeMask(const int j) {
return 1 << (char) (j & 0x07);
}
// Abstract class
class AdjMatrix {
char * matrix;
long matrixsize;
int logrowsize;
// number of vertices
int nv;
// number of edges
long long * ne;
bool islinked;
long edgeIndex(const int i, const int j);
protected:
int nechanges;
void copyTo(AdjMatrix * orig);
void linkTo(AdjMatrix * orig);
AdjMatrix() {};
AdjMatrix(int sz);
public:
virtual ~AdjMatrix();
// vertex and edge manipulation
virtual int nverts();
virtual long long int nedges();
virtual bool addEdge(const int i, const int j);
virtual bool delEdge(const int i, const int j);
virtual void addAllEdges();
virtual void delAllEdges();
// adjacency manipulation
virtual bool hasEdge(const int i, const int j);
virtual int vertex(const int i);
// first vertex of i's neighbor array is the vertex i if the adj matrix has the loop (i,i)
int adjToArray(const int i, const int asz, int * const ang, int * const surplus_p);
int adjToArrays(const int asz, int * const ad, int * const ang, int * const surplus_p);
int adjSize(const int i, int * const deg);
int adjSizes(int * const deg);
};
inline AdjMatrix::AdjMatrix(int sz) {
logrowsize = (inline_ceillog2(sz)-3);
matrixsize = ((long) 1 << logrowsize)*sz;
matrix = new char[matrixsize];
nv = sz;
ne = new long long;
*ne = 0;
nechanges = 0;
islinked = false;
}
inline AdjMatrix::~AdjMatrix() {
if (!islinked) {
delete ne;
delete matrix;
}
}
inline void AdjMatrix::copyTo(AdjMatrix * orig) {
matrixsize = orig->matrixsize;
logrowsize = orig->logrowsize;
memcpy(matrix, (void *) orig->matrix, matrixsize);
nv = orig->nverts();
*ne = orig->nedges();
}
inline void AdjMatrix::linkTo(AdjMatrix * orig) {
matrix = orig->matrix;
matrixsize = orig->matrixsize;
logrowsize = orig->logrowsize;
nv = orig->nverts();
ne = orig->ne;
islinked = true;
}
inline long AdjMatrix::edgeIndex(const int i, const int j) {
return ((long) i << logrowsize)+(j >> 3);
}
inline int AdjMatrix::nverts() {
return nv;
}
inline long long int AdjMatrix::nedges() {
return *ne;
}
inline bool AdjMatrix::hasEdge(const int i, const int j) {
return matrix[edgeIndex(i, j)] & edgeMask(j);
}
inline bool AdjMatrix::addEdge(const int i, const int j) {
bool nhe = !AdjMatrix::hasEdge(i, j);
if (nhe) {
(*ne)++;
nechanges++;
matrix[edgeIndex(i, j)] |= edgeMask(j);
}
return nhe;
}
inline bool AdjMatrix::delEdge(const int i, const int j) {
bool he = AdjMatrix::hasEdge(i, j);
if (he) {
(*ne)--;
nechanges++;
matrix[edgeIndex(i, j)] &= ~edgeMask(j);
}
return he;
}
inline void AdjMatrix::addAllEdges() {
memset((void *) matrix, 0xFF, matrixsize);
int nv = AdjMatrix::nverts();
*ne = nv*nv;
nechanges++;
}
inline void AdjMatrix::delAllEdges() {
memset((void *) matrix, 0, matrixsize);
*ne = 0;
nechanges++;
}
inline int AdjMatrix::vertex(const int i) {
return i;
}
#endif /* ADJMATRIX_H_ */