-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.cpp
executable file
·89 lines (65 loc) · 2.15 KB
/
utils.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
#include <sys/resource.h>
#include <sys/time.h>
#include <cstdlib>
#include <iostream>
#include <numeric>
#include "utils.hpp"
double mytimer(void)
{
static long int start = 0L, startu;
const double million = 1000000.0;
timeval tp;
if (start == 0L) {
gettimeofday(&tp, NULL);
start = tp.tv_sec;
startu = tp.tv_usec;
}
gettimeofday(&tp, NULL);
return (static_cast<double>(tp.tv_sec - start) + (static_cast<double>(tp.tv_usec - startu) /
million));
}
// Random number generator from B. Stroustrup:
// http://www.stroustrup.com/C++11FAQ.html#std-random
GraphWeight genRandom(GraphWeight low, GraphWeight high)
{
static std::default_random_engine re {};
using Dist = std::uniform_real_distribution<GraphWeight>;
static Dist uid {};
return uid(re, Dist::param_type{low,high});
}
void processGraphData(Graph &g, std::vector<GraphElem> &edgeCount,
std::vector<GraphElemTuple> &edgeList,
const GraphElem nv, const GraphElem ne)
{
std::vector<GraphElem> ecTmp(nv + 1);
std::partial_sum(edgeCount.begin(), edgeCount.end(), ecTmp.begin());
edgeCount = ecTmp;
g.setEdgeStartForVertex(0, 0);
for (GraphElem i = 0; i < nv; i++)
g.setEdgeStartForVertex(i + 1, edgeCount[i + 1]);
edgeCount.clear();
auto ecmp = [] (GraphElemTuple const& e0, GraphElemTuple const& e1)
{ return ((e0.i_ < e1.i_) || ((e0.i_ == e1.i_) && (e0.j_ < e1.j_))); };
if (!std::is_sorted(edgeList.begin(), edgeList.end(), ecmp)) {
std::cout << "Edge list is not sorted" << std::endl;
std::sort(edgeList.begin(), edgeList.end(), ecmp);
}
else
std::cout << "Edge list is sorted!" << std::endl;
GraphElem ePos = 0;
for (GraphElem i = 0; i < nv; i++) {
GraphElem e0, e1;
g.getEdgeRangeForVertex(i, e0, e1);
if ((i % 100000) == 0)
std::cout << "Processing edges for vertex: " << i << ", range(" << e0 << ", " << e1 <<
")" << std::endl;
for (GraphElem j = e0; j < e1; j++) {
Edge &edge = g.getEdge(j);
assert(ePos == j);
assert(i == edgeList[ePos].i_);
edge.tail = edgeList[ePos].j_;
edge.weight = edgeList[ePos].w_;
ePos++;
}
}
} // processGraphData