-
Notifications
You must be signed in to change notification settings - Fork 4
/
graph.hpp
320 lines (266 loc) · 8.64 KB
/
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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
Cycle-routing does multi-criteria route planning for bicycles.
Copyright (C) 2017 Florian Barth
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef GRAPH_H
#define GRAPH_H
#include "namedType.hpp"
#include <atomic>
#include <boost/property_map/dynamic_property_map.hpp>
#include <fstream>
#include <iostream>
#include <optional>
#include <set>
#include <unordered_set>
#include <vector>
boost::dynamic_properties& get_graph_properties();
using OsmId = NamedType<size_t, struct OsmIdParameter>;
using NodeId = NamedType<size_t, struct NodeIdParameter>;
using NodePos = NamedType<size_t, struct NodePosParameter>;
using EdgeId = NamedType<size_t, struct EdgeParameter>;
using Lat = NamedType<double, struct LatParameter>;
using Lng = NamedType<double, struct LngParameter>;
using Height = NamedType<double, struct HeightParameter>;
using Length = NamedType<double, struct LengthParameter>;
using Unsuitability = NamedType<double, struct UnsuitabilityParameter>;
class Dijkstra;
class NormalDijkstra;
struct Config;
struct Cost {
static const size_t dim = 4;
std::array<double, dim> values;
Cost(const std::vector<double>& values)
{
for (size_t i = 0; i < dim; ++i) {
this->values[i] = values[i];
if (std::abs(this->values[i]) < 0.0001) {
this->values[i] = 0;
}
}
}
Cost()
{
for (size_t i = 0; i < Cost::dim; ++i) {
values[i] = 0;
}
}
Cost(const Cost& other) { values = other.values; }
Cost operator=(const Cost& other)
{
values = other.values;
return *this;
}
Cost(Cost&& other) noexcept { values = other.values; }
Cost operator=(Cost&& other) noexcept
{
values = other.values;
return *this;
}
Cost(const std::array<double, Cost::dim> values)
{
for (size_t i = 0; i < dim; ++i) {
this->values[i] = values[i];
if (std::abs(this->values[i]) < 0.0001) {
this->values[i] = 0;
}
}
}
double operator*(const Config& conf) const;
Cost operator+(const Cost& c) const
{
std::array<double, Cost::dim> newValues;
for (size_t i = 0; i < dim; ++i) {
newValues[i] = values[i] + c.values[i];
}
return newValues;
};
Cost operator-(const Cost& c) const
{
std::array<double, Cost::dim> newValues;
for (size_t i = 0; i < dim; ++i) {
newValues[i] = values[i] - c.values[i];
}
return newValues;
};
bool operator==(const Cost& c) const
{
for (size_t i = 0; i < Cost::dim; ++i) {
if (std::abs(values[i] - c.values[i]) >= 0.0001) {
return false;
}
}
return true;
};
bool operator!=(const Cost& c) const { return !(*this == c); }
};
struct HalfEdge {
EdgeId id;
NodePos end;
NodePos begin;
Cost cost;
double costByConfiguration(const Config& conf) const;
};
struct NodeOffset {
size_t in { 0 };
size_t out { 0 };
NodeOffset() = default;
NodeOffset(size_t in, size_t out)
: in(in)
, out(out)
{
}
};
using ReplacedEdge = std::optional<EdgeId>;
class Edge {
public:
Edge() = default;
Edge(NodeId source, NodeId dest);
Edge(NodeId source, NodeId dest, ReplacedEdge edgeA, ReplacedEdge edgeB);
Edge(const Edge& other) = default;
Edge(Edge&& other) noexcept = default;
virtual ~Edge() noexcept = default;
Edge& operator=(const Edge& other) = default;
Edge& operator=(Edge&& other) noexcept = default;
NodeId getSourceId() const;
NodeId getDestId() const;
const ReplacedEdge& getEdgeA() const;
const ReplacedEdge& getEdgeB() const;
NodePos sourcePos() const;
NodePos destPos() const;
void sourcePos(NodePos source);
void destPos(NodePos dest);
EdgeId getId() const;
void setId(EdgeId id);
void set_extrenal_id(const std::string& external_id);
const std::string& external_id() const;
const Cost& getCost() const;
double costByConfiguration(const Config& conf) const;
void setCost(Cost c);
HalfEdge makeHalfEdge(NodePos begin, NodePos end) const;
static Edge createFromText(const std::string& text);
void writeToStream(std::ostream& out) const;
static std::vector<EdgeId> administerEdges(std::vector<Edge>&& edges);
static const Edge& getEdge(EdgeId id);
static Edge& getMutEdge(EdgeId id);
friend void testEdgeInternals(const Edge& e, NodeId source, NodeId destination, Length length,
Height height, Unsuitability unsuitability, const ReplacedEdge& edgeA,
const ReplacedEdge& edgeB);
friend Edge createEdge(std::ifstream&, std::ifstream&);
private:
friend class boost::serialization::access;
std::string external_id_;
EdgeId internalId;
NodeId source;
NodeId destination;
Cost cost;
ReplacedEdge edgeA;
ReplacedEdge edgeB;
NodePos sourcePos_;
NodePos destPos_;
static std::vector<Edge> edges;
};
class Node {
public:
Node() = default;
Node(const std::string external_node_id,
NodeId id); //, size_t osmId, Lat lat, Lng lng, double height);
Node(const Node& other) = default;
Node(Node&& other) noexcept = default;
virtual ~Node() noexcept = default;
Node& operator=(const Node& other) = default;
Node& operator=(Node&& other) noexcept = default;
void assignLevel(size_t level);
size_t getLevel() const;
NodeId id() const;
friend std::ostream& operator<<(std::ostream& os, const Node& n);
static Node createFromText(const std::string& text);
void writeToStream(std::ostream& out) const;
friend void testNodeInternals(const Node& n, NodeId id, Lat lat, Lng lng, size_t level);
const std::string& external_id() const;
// Lat lat() const;
// Lng lng() const;
// short height() const;
private:
friend class boost::serialization::access;
std::string external_node_id_;
NodeId id_;
// size_t osmId;
// Lat lat_;
// Lng lng_;
size_t level = 0;
// double height_;
};
class Grid;
class EdgeRange;
class Graph {
public:
Graph(std::vector<Node>&& nodes, std::vector<Edge>&& edges);
Graph(std::vector<Node>&& nodes, std::vector<EdgeId>&& edges);
Graph(const Graph& other) = delete;
Graph(Graph&& other) noexcept = default;
virtual ~Graph() noexcept = default;
Graph& operator=(const Graph& other) = delete;
Graph& operator=(Graph&& other) noexcept = default;
friend std::ostream& operator<<(std::ostream& /*s*/, const Graph& /*g*/);
std::vector<NodeOffset> const& getOffsets() const;
Dijkstra createDijkstra();
NormalDijkstra createNormalDijkstra(bool unpack = false);
Grid createGrid(long sideLength = 100) const;
EdgeRange getOutgoingEdgesOf(NodePos pos) const;
EdgeRange getIngoingEdgesOf(NodePos pos) const;
size_t getLevelOf(NodePos pos) const;
static Graph createFromStream(std::istream& file);
void writeToStream(std::ostream& out) const;
const Node& getNode(NodePos pos) const;
std::optional<NodePos> nodePosById(NodeId id) const;
size_t getNodeCount() const;
size_t getEdgeCount() const;
size_t getInTimesOutDegree(NodePos node) const;
std::unordered_map<NodeId, const Node*> getNodePosByIds(
const std::unordered_set<NodeId>& ids) const;
NodePos getNodePos(const Node* n) const;
private:
void init(std::vector<Node>&& nodes, std::vector<EdgeId>&& edges);
void connectEdgesToNodes(const std::vector<Node>& nodes, const std::vector<EdgeId>& edges);
std::vector<Node> nodes;
std::vector<NodeOffset> offsets;
std::vector<HalfEdge> inEdges;
std::vector<HalfEdge> outEdges;
std::vector<size_t> level;
size_t edgeCount;
};
class EdgeRange {
public:
using iterator = std::vector<HalfEdge>::const_iterator;
EdgeRange(iterator begin, iterator end)
: begin_(begin)
, end_(end)
{
}
EdgeRange(const EdgeRange& other) = default;
EdgeRange(EdgeRange&& other) noexcept = default;
virtual ~EdgeRange() noexcept = default;
EdgeRange& operator=(const EdgeRange& other) = default;
EdgeRange& operator=(EdgeRange&& other) noexcept = default;
iterator begin() const { return begin_; }
iterator end() const { return end_; }
protected:
private:
iterator begin_;
iterator end_;
};
struct RouteWithCount;
struct Route;
void printRoutes(std::ofstream& dotFile, const Graph& graph, const RouteWithCount& route1,
const Route& route2, const Config& config, const std::set<NodePos>& set = {});
#endif /* GRAPH_H */