forked from Project-OSRM/osrm-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenumber.cpp
74 lines (65 loc) · 2.65 KB
/
renumber.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
#include "partition/renumber.hpp"
#include <tbb/parallel_sort.h>
namespace osrm
{
namespace partition
{
namespace
{
// Returns a vector that is indexed by node ID marking the level at which it is a border node
std::vector<LevelID> getHighestBorderLevel(const DynamicEdgeBasedGraph &graph,
const std::vector<Partition> &partitions)
{
std::vector<LevelID> border_level(graph.GetNumberOfNodes(), 0);
for (const auto level : util::irange<LevelID>(1, partitions.size() + 1))
{
const auto &partition = partitions[level - 1];
for (auto node : util::irange<NodeID>(0, graph.GetNumberOfNodes()))
{
for (auto edge : graph.GetAdjacentEdgeRange(node))
{
auto target = graph.GetTarget(edge);
if (partition[node] != partition[target])
{
// level is monotone increasing so we wil
// always overwrite here with a value equal
// or greater then the current border_level
border_level[node] = level;
border_level[target] = level;
}
}
}
}
return border_level;
}
}
std::vector<std::uint32_t> makePermutation(const DynamicEdgeBasedGraph &graph,
const std::vector<Partition> &partitions)
{
std::vector<std::uint32_t> ordering(graph.GetNumberOfNodes());
std::iota(ordering.begin(), ordering.end(), 0);
// Sort the nodes by cell ID recursively:
// Nodes in the same cell will be sorted by cell ID on the level below
for (const auto &partition : partitions)
{
std::stable_sort(
ordering.begin(), ordering.end(), [&partition](const auto lhs, const auto rhs) {
return partition[lhs] < partition[rhs];
});
}
// Now sort the nodes by the level at which they are a border node, descening.
// That means nodes that are border nodes on the highest level will have a very low ID,
// whereas nodes that are nerver border nodes are sorted to the end of the array.
// Note: Since we use a stable sort that preserves the cell sorting within each level
auto border_level = getHighestBorderLevel(graph, partitions);
std::stable_sort(
ordering.begin(), ordering.end(), [&border_level](const auto lhs, const auto rhs) {
return border_level[lhs] > border_level[rhs];
});
std::vector<std::uint32_t> permutation(ordering.size());
for (auto index : util::irange<std::uint32_t>(0, ordering.size()))
permutation[ordering[index]] = index;
return permutation;
}
}
}