-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGraphSequence.cpp
More file actions
136 lines (110 loc) · 3.61 KB
/
Copy pathGraphSequence.cpp
File metadata and controls
136 lines (110 loc) · 3.61 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
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
/* WaveletTreeNoptrs.h
* Copyright (C) 2013, Nicolás Lehmann.
*
* Graph implementation using a sequence representation
* for the concatenation of the adjacency lists
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include "GraphSequence.h"
namespace cds_static
{
GraphSequence::GraphSequence(uint **_adjacencyList, size_t nodes, SequenceBuilder *sb, bool debug){
this->nodes = nodes;
this->edges = 0;
this->falseEdges = 0;
for(uint n = 0; n < nodes; ++n){
this->edges += _adjacencyList[n][0];
if(_adjacencyList[n][0] == 0){
this->falseEdges++;
}
}
uint *seq = new uint[this->edges + this->falseEdges];
BitString bitstring(this->edges + this->falseEdges);
uint k = 0;
for(uint n = 0; n < nodes; ++n){
bitstring.setBit(k,1);
uint neighbors = _adjacencyList[n][0];
for(uint i = 0; i < neighbors; ++i){
if(i > 0)
bitstring.setBit(k,0);
seq[k++] = _adjacencyList[n][i+1];
}
if(neighbors == 0)
seq[k++] = this->nodes+1;
//delete [] _adjacencyList[n];
}
//delete [] _adjacencyList;
if(debug)
printf(" [GRPH] Building Bitsequence\n");
this->bitseq = new BitSequenceRRR(bitstring);
if(debug)
printf(" [GRPH] Building Sequence\n");
this->adjacencyList = sb->build(seq, this->edges + this->falseEdges);
}
GraphSequence::GraphSequence(){
nodes = 0;
edges = falseEdges = 0;
adjacencyList = NULL;
bitseq = NULL;
}
GraphSequence::~GraphSequence(){
delete adjacencyList;
delete bitseq;
}
vector<uint> *GraphSequence::getNeighbors(uint n) const{
size_t pos = bitseq->select1(n+1);
size_t next = bitseq->select1(n+2);
size_t len = (next == -1?bitseq->getLength() : next) - pos;
vector<uint> *neighbors = new vector<uint>(len);
for(int i = 0; i < len; ++i){
(*neighbors)[i] = adjacencyList->access(pos + i);
if(neighbors->at(i) == this->nodes +1)
neighbors->clear();
}
return neighbors;
}
vector<uint> *GraphSequence::getReverseNeighbors(uint n) const{
size_t len = adjacencyList->rank(n, adjacencyList->getLength());
vector<uint> *neighbors = new vector<uint>(len);
for(int i = 0; i < len; ++i){
(*neighbors)[i] = bitseq->rank1(adjacencyList->select(n, i+1)) - 1;
}
return neighbors;
}
size_t GraphSequence::getSize() const{
size_t ptrs = sizeof(Sequence)+ sizeof(BitSequence);
size_t data = sizeof(long long)*2+sizeof(size_t);
return adjacencyList->getSize()+bitseq->getSize()+ptrs+data;
}
void GraphSequence::save(ofstream & fp) const
{
saveValue<size_t>(fp, nodes);
saveValue<long long>(fp, edges);
saveValue<long long>(fp, falseEdges);
adjacencyList->save(fp);
bitseq->save(fp);
}
GraphSequence * GraphSequence::load(ifstream & fp) {
GraphSequence *ret = new GraphSequence();
ret->nodes = loadValue<size_t>(fp);
ret->edges = loadValue<long long>(fp);
ret->falseEdges = loadValue<long long>(fp);
ret->adjacencyList = RePairWaveletTree::load(fp);
ret->bitseq = BitSequence::load(fp);
return ret;
}
};