HiPOP 1.1.2__tar.gz → 2.0.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {hipop-1.1.2 → hipop-2.0.0}/.clang-tidy +6 -5
- {hipop-1.1.2 → hipop-2.0.0}/PKG-INFO +1 -1
- hipop-2.0.0/VERSION.txt +1 -0
- {hipop-1.1.2 → hipop-2.0.0}/cpp/include/hipop/create.h +1 -1
- hipop-2.0.0/cpp/include/hipop/graph.h +514 -0
- {hipop-1.1.2 → hipop-2.0.0}/cpp/include/hipop/graph_path_cost.h +1 -1
- {hipop-1.1.2 → hipop-2.0.0}/cpp/include/hipop/shortest_path.h +4 -4
- {hipop-1.1.2 → hipop-2.0.0}/cpp/src/create.cpp +19 -19
- hipop-2.0.0/cpp/src/graph.cpp +216 -0
- {hipop-1.1.2 → hipop-2.0.0}/cpp/src/shortest_path.cpp +118 -120
- {hipop-1.1.2 → hipop-2.0.0}/cpp/tests/CMakeLists.txt +0 -1
- {hipop-1.1.2 → hipop-2.0.0}/cpp/tests/helpers.h +21 -0
- hipop-2.0.0/cpp/tests/testGraph.cpp +612 -0
- hipop-2.0.0/cpp/tests/testGraphMerge.cpp +43 -0
- hipop-2.0.0/cpp/tests/testManhattan.cpp +38 -0
- hipop-2.0.0/python/hipop/cpp/graph.cpp +281 -0
- {hipop-1.1.2 → hipop-2.0.0}/python/hipop/graph.py +2 -1
- hipop-2.0.0/python/tests/test_Graph.py +366 -0
- hipop-1.1.2/VERSION.txt +0 -1
- hipop-1.1.2/cpp/include/hipop/graph.h +0 -190
- hipop-1.1.2/cpp/src/graph.cpp +0 -234
- hipop-1.1.2/cpp/tests/testCopyGraph.cpp +0 -82
- hipop-1.1.2/cpp/tests/testGraph.cpp +0 -37
- hipop-1.1.2/cpp/tests/testGraphMerge.cpp +0 -44
- hipop-1.1.2/cpp/tests/testManhattan.cpp +0 -19
- hipop-1.1.2/python/hipop/cpp/graph.cpp +0 -63
- hipop-1.1.2/python/tests/test_CopyGraph.py +0 -52
- hipop-1.1.2/python/tests/test_Graph.py +0 -24
- {hipop-1.1.2 → hipop-2.0.0}/.gitattributes +0 -0
- {hipop-1.1.2 → hipop-2.0.0}/.github/workflows/ci.yaml +0 -0
- {hipop-1.1.2 → hipop-2.0.0}/.github/workflows/deploy.yaml +0 -0
- {hipop-1.1.2 → hipop-2.0.0}/.gitignore +0 -0
- {hipop-1.1.2 → hipop-2.0.0}/.pre-commit-config.yaml +0 -0
- {hipop-1.1.2 → hipop-2.0.0}/CMakeLists.txt +0 -0
- {hipop-1.1.2 → hipop-2.0.0}/CONTRIBUTING.md +0 -0
- {hipop-1.1.2 → hipop-2.0.0}/DEVELOPMENT.md +0 -0
- {hipop-1.1.2 → hipop-2.0.0}/LICENSE +0 -0
- {hipop-1.1.2 → hipop-2.0.0}/README.md +0 -0
- {hipop-1.1.2 → hipop-2.0.0}/cmake/ClangTidySettings.cmake +0 -0
- {hipop-1.1.2 → hipop-2.0.0}/cmake/IPOSettings.cmake +0 -0
- {hipop-1.1.2 → hipop-2.0.0}/cmake/MainSettings.cmake +0 -0
- {hipop-1.1.2 → hipop-2.0.0}/conda/env.yaml +0 -0
- {hipop-1.1.2 → hipop-2.0.0}/cpp/CMakeLists.txt +0 -0
- {hipop-1.1.2 → hipop-2.0.0}/cpp/cmake/HipopConfig.cmake.in +0 -0
- {hipop-1.1.2 → hipop-2.0.0}/cpp/examples/.clang-tidy +0 -0
- {hipop-1.1.2 → hipop-2.0.0}/cpp/examples/CMakeLists.txt +0 -0
- {hipop-1.1.2 → hipop-2.0.0}/cpp/examples/ParallelKShortestPath.cpp +0 -0
- {hipop-1.1.2 → hipop-2.0.0}/cpp/include/hipop/string_util.h +0 -0
- {hipop-1.1.2 → hipop-2.0.0}/cpp/src/graph_path_cost.cpp +0 -0
- {hipop-1.1.2 → hipop-2.0.0}/cpp/tests/.clang-tidy +0 -0
- {hipop-1.1.2 → hipop-2.0.0}/cpp/tests/sample_graphs.h +0 -0
- {hipop-1.1.2 → hipop-2.0.0}/cpp/tests/testAStar.cpp +0 -0
- {hipop-1.1.2 → hipop-2.0.0}/cpp/tests/testDijkstra.cpp +0 -0
- {hipop-1.1.2 → hipop-2.0.0}/cpp/tests/testDijkstraSingleSource.cpp +0 -0
- {hipop-1.1.2 → hipop-2.0.0}/cpp/tests/testKShortestPath.cpp +0 -0
- {hipop-1.1.2 → hipop-2.0.0}/cpp/tests/testStringUtil.cpp +0 -0
- {hipop-1.1.2 → hipop-2.0.0}/cpp/tests/testYenKShortestPath.cpp +0 -0
- {hipop-1.1.2 → hipop-2.0.0}/cpp/tests/testparallelKShortestPath.cpp +0 -0
- {hipop-1.1.2 → hipop-2.0.0}/pyproject.toml +0 -0
- {hipop-1.1.2 → hipop-2.0.0}/pyrefly.toml +0 -0
- {hipop-1.1.2 → hipop-2.0.0}/pytest.toml +0 -0
- {hipop-1.1.2 → hipop-2.0.0}/python/CMakeLists.txt +0 -0
- {hipop-1.1.2 → hipop-2.0.0}/python/examples/UpdateCosts.py +0 -0
- {hipop-1.1.2 → hipop-2.0.0}/python/examples/ksp.py +0 -0
- {hipop-1.1.2 → hipop-2.0.0}/python/examples/merge.py +0 -0
- {hipop-1.1.2 → hipop-2.0.0}/python/examples/render.py +0 -0
- {hipop-1.1.2 → hipop-2.0.0}/python/examples/test.py +0 -0
- {hipop-1.1.2 → hipop-2.0.0}/python/hipop/__init__.py.template +0 -0
- {hipop-1.1.2 → hipop-2.0.0}/python/hipop/cpp/main.cpp +0 -0
- {hipop-1.1.2 → hipop-2.0.0}/python/hipop/cpp/shortest_path.cpp +0 -0
- {hipop-1.1.2 → hipop-2.0.0}/python/hipop/py.typed +0 -0
- {hipop-1.1.2 → hipop-2.0.0}/python/hipop/render.py +0 -0
- {hipop-1.1.2 → hipop-2.0.0}/python/hipop/shortest_path.py +0 -0
- {hipop-1.1.2 → hipop-2.0.0}/python/tests/test_ShortestPath.py +0 -0
- {hipop-1.1.2 → hipop-2.0.0}/python/tests/test_VersionConsistency.py +0 -0
- {hipop-1.1.2 → hipop-2.0.0}/ruff.toml +0 -0
|
@@ -10,6 +10,12 @@ Checks: # See https://clang.llvm.org/extra/clang-tidy/checks/list.html
|
|
|
10
10
|
- modernize-*
|
|
11
11
|
- performance-*
|
|
12
12
|
|
|
13
|
+
# This check requires single-argument constructors and conversion operators to be marked as explicit.
|
|
14
|
+
# Disabled because it leaves no way to define an intentional implicit conversion between classes
|
|
15
|
+
# (at least in C++17). To be reconsidered if moving to C++20, where `explicit(false)` can be used
|
|
16
|
+
# to mark an implicit conversion as intentional.
|
|
17
|
+
- -cppcoreguidelines-explicit-constructor
|
|
18
|
+
|
|
13
19
|
# This check assumes that each `T&&` function argument (with `T` a template parameter of the function)
|
|
14
20
|
# is meant to be forwarded (see F.19 principle from C++ Core Guidelines, as referred in
|
|
15
21
|
# https://clang.llvm.org/extra/clang-tidy/checks/cppcoreguidelines/missing-std-forward.html).
|
|
@@ -24,11 +30,6 @@ Checks: # See https://clang.llvm.org/extra/clang-tidy/checks/list.html
|
|
|
24
30
|
# the inputs and outputs of graph algorithm functions).
|
|
25
31
|
- -cppcoreguidelines-narrowing-conversions
|
|
26
32
|
|
|
27
|
-
# FIXME Definitively worth enabling, but this would require to modify the graph data-structure
|
|
28
|
-
# (to use smart pointers to manage Node/Link allocations - or even better to allocate them in-place
|
|
29
|
-
# in the `mnodes`/`mlinks` maps), with impacts on the Python API. To be addressed in a dedicated change.
|
|
30
|
-
- -cppcoreguidelines-owning-memory
|
|
31
|
-
|
|
32
33
|
# These checks require to use bounds-checked accessors on containers (e.g. `.at()` instead of `operator[]`).
|
|
33
34
|
# Disabled because of the performance cost in hot loops, and because they raise false positives:
|
|
34
35
|
# the index may be valid by the general logic of the algorithm, even if the checks can't deduce it.
|
hipop-2.0.0/VERSION.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
2.0.0
|
|
@@ -0,0 +1,514 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include <hipop/string_util.h>
|
|
4
|
+
|
|
5
|
+
#include <array>
|
|
6
|
+
#include <cstddef>
|
|
7
|
+
#include <iterator>
|
|
8
|
+
#include <set>
|
|
9
|
+
#include <stdexcept>
|
|
10
|
+
#include <string>
|
|
11
|
+
#include <type_traits>
|
|
12
|
+
#include <unordered_map>
|
|
13
|
+
#include <utility>
|
|
14
|
+
#include <vector>
|
|
15
|
+
|
|
16
|
+
using setstring = std::set<std::string>;
|
|
17
|
+
using vecstring = std::vector<std::string>;
|
|
18
|
+
using mapsets = std::unordered_map<std::string, std::set<std::string>>;
|
|
19
|
+
using mapcosts = std::unordered_map<std::string, std::unordered_map<std::string, double>>;
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
namespace hipop
|
|
23
|
+
{
|
|
24
|
+
class Node;
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class Link {
|
|
28
|
+
public:
|
|
29
|
+
std::string mid;
|
|
30
|
+
const Node *mup;
|
|
31
|
+
const Node *mdown;
|
|
32
|
+
mapcosts mcosts; // mcosts[cost-family][cost-metric] -> cost-value
|
|
33
|
+
std::string mlabel;
|
|
34
|
+
double mlength;
|
|
35
|
+
|
|
36
|
+
Link(std::string id, const Node *up, const Node *down, double length, mapcosts costs, std::string label) :
|
|
37
|
+
mid(std::move(id)),
|
|
38
|
+
mup(up),
|
|
39
|
+
mdown(down),
|
|
40
|
+
mcosts(std::move(costs)),
|
|
41
|
+
mlabel(std::move(label)),
|
|
42
|
+
mlength(length)
|
|
43
|
+
{}
|
|
44
|
+
|
|
45
|
+
// Relationships between Node and Link instances are materialized through raw pointers.
|
|
46
|
+
// Copying and/or moving these instances would break these relationships.
|
|
47
|
+
Link(const Link &other) = delete;
|
|
48
|
+
Link(Link &&other) = delete;
|
|
49
|
+
Link &operator=(const Link &other) = delete;
|
|
50
|
+
Link &operator=(Link &&other) = delete;
|
|
51
|
+
~Link() = default;
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Whether a cost value is set for the current link, assuming the given cost family and cost metric.
|
|
55
|
+
*/
|
|
56
|
+
[[nodiscard]] bool hasCost(const std::string &costFamily, const std::string &costMetric) const {
|
|
57
|
+
auto it1 = mcosts.find(costFamily);
|
|
58
|
+
if (it1 == mcosts.end()) {
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
auto it2 = it1->second.find(costMetric);
|
|
62
|
+
if (it2 == it1->second.end()) {
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
return true;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Read the cost value associated to the current link, assuming the given cost family and cost metric.
|
|
70
|
+
*
|
|
71
|
+
* @return 0 if no cost value is explicitly set for the given cost family and/or cost metric.
|
|
72
|
+
* FIXME It would be probably better to either throw an exception or return a +inf value
|
|
73
|
+
* (i.e. considering that the underlying link is effectively impassable) if no cost value
|
|
74
|
+
* is explicitly set. Still, 0 is returned to match the legacy behavior.
|
|
75
|
+
*/
|
|
76
|
+
[[nodiscard]] double cost(const std::string &costFamily, const std::string &costMetric) const {
|
|
77
|
+
auto it1 = mcosts.find(costFamily);
|
|
78
|
+
if (it1 == mcosts.end()) {
|
|
79
|
+
return 0;
|
|
80
|
+
}
|
|
81
|
+
auto it2 = it1->second.find(costMetric);
|
|
82
|
+
if (it2 == it1->second.end()) {
|
|
83
|
+
return 0;
|
|
84
|
+
}
|
|
85
|
+
return it2->second;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
void updateCosts(mapcosts costs) {
|
|
89
|
+
mcosts = std::move(costs);
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
class Node {
|
|
95
|
+
public:
|
|
96
|
+
std::string mid;
|
|
97
|
+
std::array<double, 2> mposition;
|
|
98
|
+
std::unordered_map<std::string, Link* > madj;
|
|
99
|
+
std::unordered_map<std::string, Link* > mradj;
|
|
100
|
+
std::string mlabel;
|
|
101
|
+
mapsets mexclude_movements;
|
|
102
|
+
|
|
103
|
+
Node(std::string id, double x, double y, std::string label, mapsets exclude_movements) :
|
|
104
|
+
mid(std::move(id)),
|
|
105
|
+
mposition{ x, y },
|
|
106
|
+
mlabel(std::move(label)),
|
|
107
|
+
mexclude_movements(std::move(exclude_movements))
|
|
108
|
+
{}
|
|
109
|
+
|
|
110
|
+
// Relationships between Node and Link instances are materialized through raw pointers.
|
|
111
|
+
// Copying and/or moving these instances would break these relationships.
|
|
112
|
+
Node(const Node &other) = delete;
|
|
113
|
+
Node(Node &&other) = delete;
|
|
114
|
+
Node &operator=(const Node &other) = delete;
|
|
115
|
+
Node &operator=(Node &&other) = delete;
|
|
116
|
+
~Node() = default;
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Assuming we follow a path in the graph, and that we come from node `predecessor`,
|
|
120
|
+
* this method computes all the allowed exit links from the current node, and invokes
|
|
121
|
+
* the given callback for each allowed exit link.
|
|
122
|
+
*
|
|
123
|
+
* The allowed exit links are always a subset of the outgoing links of the current node.
|
|
124
|
+
* Still some outgoing links might be disallowed due to movement restrictions.
|
|
125
|
+
*
|
|
126
|
+
* @param predecessor ID of the previous node on a path.
|
|
127
|
+
* @param callback Must be a callable object with the following signature: `void(const Link *link)`.
|
|
128
|
+
* The callback must not modify the graph structure.
|
|
129
|
+
*/
|
|
130
|
+
template<typename Callback>
|
|
131
|
+
void forEachExit(const std::string &predecessor, Callback &&callback) const {
|
|
132
|
+
for (const auto &l : madj) {
|
|
133
|
+
auto it = mexclude_movements.find(predecessor);
|
|
134
|
+
if (it == mexclude_movements.end() || it->second.find(l.second->mdown->mid) == it->second.end()) {
|
|
135
|
+
callback(const_cast<const Link*>(l.second));
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Assuming we follow a path in the graph, and that we come from node `predecessor`,
|
|
142
|
+
* this method returns all the allowed exit links from the current node.
|
|
143
|
+
*
|
|
144
|
+
* The allowed exit links are always a subset of the outgoing links of the current node.
|
|
145
|
+
* Still some outgoing links might be disallowed due to movement restrictions.
|
|
146
|
+
*
|
|
147
|
+
* Time-critical functions should prefer using forEachExit() instead (less memory allocation overhead).
|
|
148
|
+
*
|
|
149
|
+
* @param predecessor ID of the previous node on a path.
|
|
150
|
+
*/
|
|
151
|
+
[[nodiscard]] std::vector<const Link*> getExits(const std::string &predecessor) const {
|
|
152
|
+
std::vector<const Link*> result;
|
|
153
|
+
forEachExit(predecessor, [&result](const Link *link) { result.emplace_back(link); });
|
|
154
|
+
return result;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
class OrientedGraph {
|
|
161
|
+
|
|
162
|
+
// Must be node-based containers (no flat hash map): Node/Link addresses must remain stable.
|
|
163
|
+
std::unordered_map<std::string, Node> nodes_;
|
|
164
|
+
std::unordered_map<std::string, Link> links_;
|
|
165
|
+
|
|
166
|
+
template<typename Element>
|
|
167
|
+
class ElementRange;
|
|
168
|
+
|
|
169
|
+
template<typename Element> // Element is `Node`, `const Node`, `Link` or `const Link`.
|
|
170
|
+
class ElementIterator {
|
|
171
|
+
|
|
172
|
+
using Container = std::unordered_map<std::string, std::remove_const_t<Element>>;
|
|
173
|
+
using ContainerIterator = std::conditional_t<
|
|
174
|
+
std::is_const_v<Element>,
|
|
175
|
+
typename Container::const_iterator,
|
|
176
|
+
typename Container::iterator
|
|
177
|
+
>;
|
|
178
|
+
|
|
179
|
+
ContainerIterator it_;
|
|
180
|
+
|
|
181
|
+
explicit ElementIterator(ContainerIterator it) : it_(it) {}
|
|
182
|
+
|
|
183
|
+
friend class ElementRange<Element>;
|
|
184
|
+
template<typename> friend class ElementIterator;
|
|
185
|
+
|
|
186
|
+
public:
|
|
187
|
+
|
|
188
|
+
using iterator_category = std::forward_iterator_tag;
|
|
189
|
+
using difference_type = std::ptrdiff_t;
|
|
190
|
+
using value_type = Element *;
|
|
191
|
+
using pointer = void;
|
|
192
|
+
using reference = value_type;
|
|
193
|
+
|
|
194
|
+
ElementIterator() = default;
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Implicit conversion from the read-write iterator to the read-only one.
|
|
198
|
+
*/
|
|
199
|
+
template<
|
|
200
|
+
typename Other,
|
|
201
|
+
typename = std::enable_if_t<std::is_same_v<const Other, Element> && !std::is_same_v<Other, Element>>
|
|
202
|
+
>
|
|
203
|
+
ElementIterator(const ElementIterator<Other> &other) : it_(other.it_) {}
|
|
204
|
+
|
|
205
|
+
[[nodiscard]] reference operator*() const { return &it_->second; }
|
|
206
|
+
|
|
207
|
+
ElementIterator &operator++() {
|
|
208
|
+
++it_;
|
|
209
|
+
return *this;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
ElementIterator operator++(int) {
|
|
213
|
+
ElementIterator result = *this;
|
|
214
|
+
++it_;
|
|
215
|
+
return result;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
[[nodiscard]] friend bool operator==(const ElementIterator &lhs, const ElementIterator &rhs) {
|
|
219
|
+
return lhs.it_ == rhs.it_;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
[[nodiscard]] friend bool operator!=(const ElementIterator &lhs, const ElementIterator &rhs) {
|
|
223
|
+
return lhs.it_ != rhs.it_;
|
|
224
|
+
}
|
|
225
|
+
};
|
|
226
|
+
|
|
227
|
+
template<typename Element> // Element is `Node`, `const Node`, `Link` or `const Link`.
|
|
228
|
+
class ElementRange {
|
|
229
|
+
|
|
230
|
+
using Container = std::conditional_t<
|
|
231
|
+
std::is_const_v<Element>,
|
|
232
|
+
const std::unordered_map<std::string, std::remove_const_t<Element>>,
|
|
233
|
+
std::unordered_map<std::string, Element>
|
|
234
|
+
>;
|
|
235
|
+
|
|
236
|
+
Container *container_;
|
|
237
|
+
|
|
238
|
+
explicit ElementRange(Container &container) : container_(&container) {}
|
|
239
|
+
|
|
240
|
+
friend class OrientedGraph;
|
|
241
|
+
template<typename> friend class ElementRange;
|
|
242
|
+
|
|
243
|
+
public:
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Implicit conversion from the read-write range to the read-only one.
|
|
247
|
+
*/
|
|
248
|
+
template<
|
|
249
|
+
typename Other,
|
|
250
|
+
typename = std::enable_if_t<std::is_same_v<const Other, Element> && !std::is_same_v<Other, Element>>
|
|
251
|
+
>
|
|
252
|
+
ElementRange(const ElementRange<Other> &other) : container_(other.container_) {}
|
|
253
|
+
|
|
254
|
+
[[nodiscard]] ElementIterator<Element> begin() const {
|
|
255
|
+
return ElementIterator<Element>(container_->begin());
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
[[nodiscard]] ElementIterator<Element> end() const {
|
|
259
|
+
return ElementIterator<Element>(container_->end());
|
|
260
|
+
}
|
|
261
|
+
};
|
|
262
|
+
|
|
263
|
+
public:
|
|
264
|
+
|
|
265
|
+
using ConstNodeIterator = ElementIterator<const Node>;
|
|
266
|
+
using ConstNodeRange = ElementRange<const Node>;
|
|
267
|
+
using NodeIterator = ElementIterator<Node>;
|
|
268
|
+
using NodeRange = ElementRange<Node>;
|
|
269
|
+
using ConstLinkIterator = ElementIterator<const Link>;
|
|
270
|
+
using ConstLinkRange = ElementRange<const Link>;
|
|
271
|
+
using LinkIterator = ElementIterator<Link>;
|
|
272
|
+
using LinkRange = ElementRange<Link>;
|
|
273
|
+
|
|
274
|
+
OrientedGraph() = default;
|
|
275
|
+
OrientedGraph(OrientedGraph &&other) noexcept = default;
|
|
276
|
+
OrientedGraph &operator=(OrientedGraph &&other) noexcept = default;
|
|
277
|
+
~OrientedGraph() = default;
|
|
278
|
+
|
|
279
|
+
OrientedGraph(const OrientedGraph &other) {
|
|
280
|
+
AddAllNodesAndLinks(other);
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
OrientedGraph &operator=(const OrientedGraph &other) {
|
|
284
|
+
if (this != &other) {
|
|
285
|
+
// Create a temporary OrientedGraph instance + move-assign it to `this`.
|
|
286
|
+
// This ensures that the current OrientedGraph instance remains unchanged on failure.
|
|
287
|
+
*this = OrientedGraph(other);
|
|
288
|
+
}
|
|
289
|
+
return *this;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
* Number of nodes in the graph.
|
|
294
|
+
*/
|
|
295
|
+
[[nodiscard]] std::size_t nodeCount() const {
|
|
296
|
+
return nodes_.size();
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* Whether a node corresponding to the given ID exists in the graph.
|
|
301
|
+
*/
|
|
302
|
+
[[nodiscard]] bool hasNode(const std::string &nodeId) const {
|
|
303
|
+
return nodes_.find(nodeId) != nodes_.end();
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* Return the node corresponding to the given ID (read-only version).
|
|
308
|
+
*
|
|
309
|
+
* @return Guaranteed to be non-null.
|
|
310
|
+
* @throws std::invalid_argument if no node corresponds to the given ID.
|
|
311
|
+
*/
|
|
312
|
+
[[nodiscard]] const Node *node(const std::string &nodeId) const {
|
|
313
|
+
auto it = nodes_.find(nodeId);
|
|
314
|
+
if (it == nodes_.end()) {
|
|
315
|
+
throw std::invalid_argument(StrCat("[OrientedGraph::node] Unknown node ID [", nodeId, "]."));
|
|
316
|
+
}
|
|
317
|
+
return &it->second;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/**
|
|
321
|
+
* Return the node corresponding to the given ID (read-write version).
|
|
322
|
+
*
|
|
323
|
+
* @return Guaranteed to be non-null.
|
|
324
|
+
* @throws std::invalid_argument if no node corresponds to the given ID.
|
|
325
|
+
*/
|
|
326
|
+
[[nodiscard]] Node *node(const std::string &nodeId) {
|
|
327
|
+
|
|
328
|
+
// Casting away const is safe here: the node is owned by this graph, which is itself
|
|
329
|
+
// non-const in this method, so the node is not actually a const object.
|
|
330
|
+
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast)
|
|
331
|
+
return const_cast<Node *>(const_cast<const OrientedGraph *>(this)->node(nodeId));
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/**
|
|
335
|
+
* Return the node corresponding to the given ID if it exists (read-only version).
|
|
336
|
+
*
|
|
337
|
+
* @return nullptr if the node does not exist.
|
|
338
|
+
*/
|
|
339
|
+
[[nodiscard]] const Node *findNode(const std::string &nodeId) const {
|
|
340
|
+
auto it = nodes_.find(nodeId);
|
|
341
|
+
return it == nodes_.end() ? nullptr : &it->second;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/**
|
|
345
|
+
* Return the node corresponding to the given ID if it exists (read-write version).
|
|
346
|
+
*
|
|
347
|
+
* @return nullptr if the node does not exist.
|
|
348
|
+
*/
|
|
349
|
+
[[nodiscard]] Node *findNode(const std::string &nodeId) {
|
|
350
|
+
|
|
351
|
+
// Casting away const is safe here: the node is owned by this graph, which is itself
|
|
352
|
+
// non-const in this method, so the node is not actually a const object.
|
|
353
|
+
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast)
|
|
354
|
+
return const_cast<Node *>(const_cast<const OrientedGraph *>(this)->findNode(nodeId));
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
/**
|
|
358
|
+
* Lightweight view of the nodes of the current graph, enumerated in no particular order (read-only version).
|
|
359
|
+
*
|
|
360
|
+
* Do not add or remove nodes on the graph while iterating on the range.
|
|
361
|
+
*/
|
|
362
|
+
[[nodiscard]] ConstNodeRange nodes() const {
|
|
363
|
+
return ConstNodeRange(nodes_);
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* Lightweight view of the nodes of the current graph, enumerated in no particular order (read-write version).
|
|
368
|
+
*
|
|
369
|
+
* Do not add or remove nodes on the graph while iterating on the range.
|
|
370
|
+
*/
|
|
371
|
+
[[nodiscard]] NodeRange nodes() {
|
|
372
|
+
return NodeRange(nodes_);
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
/**
|
|
376
|
+
* Number of links in the graph.
|
|
377
|
+
*/
|
|
378
|
+
[[nodiscard]] std::size_t linkCount() const {
|
|
379
|
+
return links_.size();
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
/**
|
|
383
|
+
* Whether a link corresponding to the given ID exists in the graph.
|
|
384
|
+
*/
|
|
385
|
+
[[nodiscard]] bool hasLink(const std::string &linkId) const {
|
|
386
|
+
return links_.find(linkId) != links_.end();
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
/**
|
|
390
|
+
* Return the link corresponding to the given ID (read-only version).
|
|
391
|
+
*
|
|
392
|
+
* @return Guaranteed to be non-null.
|
|
393
|
+
* @throws std::invalid_argument if no link corresponds to the given ID.
|
|
394
|
+
*/
|
|
395
|
+
[[nodiscard]] const Link *link(const std::string &linkId) const {
|
|
396
|
+
auto it = links_.find(linkId);
|
|
397
|
+
if (it == links_.end()) {
|
|
398
|
+
throw std::invalid_argument(StrCat("[OrientedGraph::link] Unknown link ID [", linkId, "]."));
|
|
399
|
+
}
|
|
400
|
+
return &it->second;
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
/**
|
|
404
|
+
* Return the link corresponding to the given ID (read-write version).
|
|
405
|
+
*
|
|
406
|
+
* @return Guaranteed to be non-null.
|
|
407
|
+
* @throws std::invalid_argument if no link corresponds to the given ID.
|
|
408
|
+
*/
|
|
409
|
+
[[nodiscard]] Link *link(const std::string &linkId) {
|
|
410
|
+
|
|
411
|
+
// Casting away const is safe here: the link is owned by this graph, which is itself
|
|
412
|
+
// non-const in this method, so the link is not actually a const object.
|
|
413
|
+
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast)
|
|
414
|
+
return const_cast<Link *>(const_cast<const OrientedGraph *>(this)->link(linkId));
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
/**
|
|
418
|
+
* Return the link corresponding to the given ID if it exists (read-only version).
|
|
419
|
+
*
|
|
420
|
+
* @return nullptr if the link does not exist.
|
|
421
|
+
*/
|
|
422
|
+
[[nodiscard]] const Link *findLink(const std::string &linkId) const {
|
|
423
|
+
auto it = links_.find(linkId);
|
|
424
|
+
return it == links_.end() ? nullptr : &it->second;
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
/**
|
|
428
|
+
* Return the link corresponding to the given ID if it exists (read-write version).
|
|
429
|
+
*
|
|
430
|
+
* @return nullptr if the link does not exist.
|
|
431
|
+
*/
|
|
432
|
+
[[nodiscard]] Link *findLink(const std::string &linkId) {
|
|
433
|
+
|
|
434
|
+
// Casting away const is safe here: the link is owned by this graph, which is itself
|
|
435
|
+
// non-const in this method, so the link is not actually a const object.
|
|
436
|
+
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast)
|
|
437
|
+
return const_cast<Link *>(const_cast<const OrientedGraph *>(this)->findLink(linkId));
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
/**
|
|
441
|
+
* Lightweight view of the links of the current graph, enumerated in no particular order (read-only version).
|
|
442
|
+
*
|
|
443
|
+
* Do not add or remove links on the graph while iterating on the range.
|
|
444
|
+
*/
|
|
445
|
+
[[nodiscard]] ConstLinkRange links() const {
|
|
446
|
+
return ConstLinkRange(links_);
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
/**
|
|
450
|
+
* Lightweight view of the links of the current graph, enumerated in no particular order (read-write version).
|
|
451
|
+
*
|
|
452
|
+
* Do not add or remove links on the graph while iterating on the range.
|
|
453
|
+
*/
|
|
454
|
+
[[nodiscard]] LinkRange links() {
|
|
455
|
+
return LinkRange(links_);
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
/**
|
|
459
|
+
* Add all the nodes and links from `graph` to the current OrientedGraph.
|
|
460
|
+
*
|
|
461
|
+
* The pre-existing nodes and links of the current OrientedGraph are preserved.
|
|
462
|
+
*/
|
|
463
|
+
void AddAllNodesAndLinks(const OrientedGraph &graph);
|
|
464
|
+
|
|
465
|
+
/**
|
|
466
|
+
* Create and add a new Node to the graph.
|
|
467
|
+
*
|
|
468
|
+
* @throw std::invalid_argument if another node with the same ID already exist in the graph.
|
|
469
|
+
*/
|
|
470
|
+
void AddNode(std::string nodeId, double x, double y, std::string label = "", mapsets excludeMovements = {});
|
|
471
|
+
|
|
472
|
+
/**
|
|
473
|
+
* Create and add a new Link to the graph.
|
|
474
|
+
*
|
|
475
|
+
* @param up ID of the upstream node of the link.
|
|
476
|
+
* @param down ID of the downstream node of the link.
|
|
477
|
+
* @throw std::invalid_argument if another link with the same ID already exist in the graph,
|
|
478
|
+
* or if either up or down do not correspond to an existing node.
|
|
479
|
+
*/
|
|
480
|
+
void AddLink(std::string linkId, const std::string &up, const std::string &down, double length,
|
|
481
|
+
mapcosts costs, std::string label = "");
|
|
482
|
+
|
|
483
|
+
/**
|
|
484
|
+
* Remove the node corresponding to the given ID, and all the adjacent links (both from or to this node).
|
|
485
|
+
*
|
|
486
|
+
* Nothing happens if no node corresponds to the given ID.
|
|
487
|
+
*/
|
|
488
|
+
void DeleteNode(const std::string &nodeId);
|
|
489
|
+
|
|
490
|
+
/**
|
|
491
|
+
* Remove the link corresponding to the given ID.
|
|
492
|
+
*
|
|
493
|
+
* Nothing happens if no link corresponds to the given ID.
|
|
494
|
+
*/
|
|
495
|
+
void DeleteLink(const std::string &linkId);
|
|
496
|
+
|
|
497
|
+
void UpdateLinkCosts(const std::string &linkId, mapcosts costs);
|
|
498
|
+
void UpdateCosts(const std::unordered_map<std::string, mapcosts> &linkIdToCosts);
|
|
499
|
+
double getLength(const std::string &up, const std::string &down);
|
|
500
|
+
|
|
501
|
+
/**
|
|
502
|
+
* Return the IDs of the links for which the considered cost has not been set.
|
|
503
|
+
*/
|
|
504
|
+
[[nodiscard]] std::vector<std::string> GetLinksWithoutCost(const std::string &costMetric,
|
|
505
|
+
const std::unordered_map<std::string, std::string> &labelToCostFamily) const;
|
|
506
|
+
|
|
507
|
+
void ShowNodes() const;
|
|
508
|
+
void ShowLinks() const;
|
|
509
|
+
|
|
510
|
+
};
|
|
511
|
+
|
|
512
|
+
OrientedGraph mergeOrientedGraph(const std::vector<const OrientedGraph*> &graphs);
|
|
513
|
+
|
|
514
|
+
} // namespace hipop
|
|
@@ -33,7 +33,7 @@ namespace hipop {
|
|
|
33
33
|
);
|
|
34
34
|
double result = 0;
|
|
35
35
|
for (std::size_t i = 0; i + 1 < path.size(); ++i) {
|
|
36
|
-
const Link *link = G.
|
|
36
|
+
const Link *link = G.node(path[i])->madj.at(path[i + 1]);
|
|
37
37
|
result += costFunction(link);
|
|
38
38
|
}
|
|
39
39
|
return result;
|
|
@@ -66,8 +66,8 @@ namespace hipop {
|
|
|
66
66
|
"Cost function must have signature: double(const Link *link)"
|
|
67
67
|
);
|
|
68
68
|
|
|
69
|
-
const Node *origin_node = G.
|
|
70
|
-
const Node *destination_node = G.
|
|
69
|
+
const Node *origin_node = G.node(origin);
|
|
70
|
+
const Node *destination_node = G.node(destination);
|
|
71
71
|
|
|
72
72
|
// Return a 0-node path with zero cost if origin and destination are identical.
|
|
73
73
|
// Remark: it would be more consistent with the general case to return 1-node path
|
|
@@ -81,8 +81,8 @@ namespace hipop {
|
|
|
81
81
|
|
|
82
82
|
std::unordered_map<const Node*, double> dist; // No nullptr keys in this map.
|
|
83
83
|
std::unordered_map<const Node*, const Node*> prev; // No nullptr (neither as key nor as value) in this map.
|
|
84
|
-
dist.reserve(G.
|
|
85
|
-
prev.reserve(G.
|
|
84
|
+
dist.reserve(G.nodeCount());
|
|
85
|
+
prev.reserve(G.nodeCount());
|
|
86
86
|
dist[origin_node] = 0;
|
|
87
87
|
// `prev[origin_node]` is intentionally left unset (the origin node has no predecessor).
|
|
88
88
|
|