-
Notifications
You must be signed in to change notification settings - Fork 0
/
static_graph.hpp
87 lines (72 loc) · 1.97 KB
/
static_graph.hpp
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
#ifndef STATIC_GRAPH_HPP
#define STATIC_GRAPH_HPP
#include <vector>
#include <iostream>
#include <boost/assert.hpp>
template<typename EdgeT>
class static_graph
{
public:
static_graph(unsigned num_nodes, std::vector<EdgeT>&& in_edges)
: num_nodes(num_nodes), edges(in_edges)
{
BOOST_ASSERT(num_nodes > 0);
#ifndef NDEBUG
// check if edges are sorted by source and then target
for (auto i = 1u; i < edges.size(); ++i)
{
BOOST_ASSERT(edges[i-1].first < edges[i].first || (edges[i-1].first == edges[i].first && edges[i-1].last <= edges[i].last));
}
#endif
edge_index.resize(num_nodes+1);
auto source = 0u;
for (auto i = 0u; i < edges.size();)
{
while (source < edges[i].first)
{
edge_index[source] = i;
source++;
}
BOOST_ASSERT(source == edges[i].first);
auto interval_begin = i;
while (i < edges.size() && edges[i].first == source)
{
i++;
}
edge_index[source] = interval_begin;
source++;
}
// fill up senitels
while (source < edge_index.size())
{
edge_index[source++] = edges.size();
}
}
unsigned begin_outgoing(unsigned node_id) const
{
BOOST_ASSERT(node_id < num_nodes);
return edge_index[node_id];
}
unsigned end_outgoing(unsigned node_id) const
{
BOOST_ASSERT(node_id < num_nodes);
return edge_index[node_id+1];
}
unsigned number_of_nodes() const
{
return num_nodes;
}
unsigned outgoing_degree(unsigned node_id) const
{
return end_outgoing(node_id) - begin_outgoing(node_id);
}
const EdgeT& get_edge(unsigned edge_id) const
{
return edges[edge_id];
}
private:
unsigned num_nodes;
std::vector<unsigned> edge_index;
std::vector<EdgeT> edges;
};
#endif