-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse_graph.cpp
More file actions
95 lines (80 loc) · 2.9 KB
/
parse_graph.cpp
File metadata and controls
95 lines (80 loc) · 2.9 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
#include <string>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <iostream>
#include "parse_graph.hpp"
#define SSSP_INF 1073741824
uint parse_graph::parse(
std::ifstream& inFile,
std::vector<initial_vertex>& initGraph,
const long long arbparam,
const bool nondirected ) {
const bool firstColumnSourceIndex = true;
std::string line;
char delim[3] = " \t"; //In most benchmarks, the delimiter is usually the space character or the tab character.
char* pch;
uint nEdges = 0;
unsigned int Additionalargc=0;
char* Additionalargv[ 61 ];
// Read the input graph line-by-line.
while( std::getline( inFile, line ) ) {
if( line[0] < '0' || line[0] > '9' ) // Skipping any line blank or starting with a character rather than a number.
continue;
char cstrLine[256];
std::strcpy( cstrLine, line.c_str() );
uint firstIndex, secondIndex;
pch = strtok(cstrLine, delim);
if( pch != NULL )
firstIndex = atoi( pch );
else
continue;
pch = strtok( NULL, delim );
if( pch != NULL )
secondIndex = atoi( pch );
else
continue;
uint theMax = std::max( firstIndex, secondIndex );
uint srcVertexIndex = firstColumnSourceIndex ? firstIndex : secondIndex;
uint dstVertexIndex = firstColumnSourceIndex ? secondIndex : firstIndex;
if( initGraph.size() <= theMax )
initGraph.resize(theMax+1);
{ //This is just a block
// Add the neighbor. A neighbor wraps edges
neighbor nbrToAdd;
nbrToAdd.srcIndex = srcVertexIndex;
Additionalargc=0;
Additionalargv[ Additionalargc ] = strtok( NULL, delim );
while( Additionalargv[ Additionalargc ] != NULL ){
Additionalargc++;
Additionalargv[ Additionalargc ] = strtok( NULL, delim );
}
initGraph.at(srcVertexIndex).vertexValue.distance = ( srcVertexIndex != arbparam ) ? SSSP_INF : 0;
initGraph.at(dstVertexIndex).vertexValue.distance = ( dstVertexIndex != arbparam ) ? SSSP_INF : 0;
nbrToAdd.edgeValue.weight = ( Additionalargc > 0 ) ? atoi(Additionalargv[0]) : 1;
initGraph.at(dstVertexIndex).nbrs.push_back( nbrToAdd );
nEdges++;
}
if( nondirected ) {
// Add the edge going the other way
uint tmp = srcVertexIndex;
srcVertexIndex = dstVertexIndex;
dstVertexIndex = tmp;
//swap src and dest and add as before
neighbor nbrToAdd;
nbrToAdd.srcIndex = srcVertexIndex;
Additionalargc=0;
Additionalargv[ Additionalargc ] = strtok( NULL, delim );
while( Additionalargv[ Additionalargc ] != NULL ){
Additionalargc++;
Additionalargv[ Additionalargc ] = strtok( NULL, delim );
}
initGraph.at(srcVertexIndex).vertexValue.distance = ( srcVertexIndex != arbparam ) ? SSSP_INF : 0;
initGraph.at(dstVertexIndex).vertexValue.distance = ( dstVertexIndex != arbparam ) ? SSSP_INF : 0;
nbrToAdd.edgeValue.weight = ( Additionalargc > 0 ) ? atoi(Additionalargv[0]) : 1;
initGraph.at(dstVertexIndex).nbrs.push_back( nbrToAdd );
nEdges++;
}
}
return nEdges;
}