Skip to content

Commit 2f339f1

Browse files
authored
Add node and street getters to Graph class (#247)
* Add and getters to Graph class * Update version * Formatting * Formatting again
1 parent 0c9f992 commit 2f339f1

File tree

9 files changed

+27
-36
lines changed

9 files changed

+27
-36
lines changed

examples/slow_charge_tl.cpp

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -178,17 +178,11 @@ int main(int argc, char** argv) {
178178
const auto& col = adj.getCol(nodeId, true);
179179
std::set<Unit> streets;
180180
const auto id = col.begin();
181-
const auto& refLat = graph.nodeSet()
182-
.at(graph.streetSet().at(id->first)->nodePair().second)
183-
->coords()
184-
.value()
185-
.first;
181+
const auto& refLat =
182+
graph.node(graph.street(id->first)->nodePair().second)->coords().value().first;
186183
for (const auto& [c, value] : col) {
187-
const auto& lat = graph.nodeSet()
188-
.at(graph.streetSet().at(c)->nodePair().first)
189-
->coords()
190-
.value()
191-
.first;
184+
const auto& lat =
185+
graph.node(graph.street(c)->nodePair().first)->coords().value().first;
192186
// std::cout << "Lat: " << lat << " RefLat: " << refLat << '\n';
193187
if (lat == refLat) {
194188
streets.emplace(c);

examples/stalingrado.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ int main() {
7575
graph.buildAdj();
7676
graph.adjustNodeCapacities();
7777
graph.makeSpireStreet(19);
78-
auto& spire = dynamic_cast<SpireStreet&>(*graph.streetSet().at(19));
78+
auto& spire = dynamic_cast<SpireStreet&>(*graph.street(19));
7979

8080
std::cout << "Intersections: " << graph.nNodes() << '\n';
8181
std::cout << "Streets: " << graph.nEdges() << '\n';

src/dsm/dsm.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
static constexpr uint8_t DSM_VERSION_MAJOR = 2;
88
static constexpr uint8_t DSM_VERSION_MINOR = 3;
9-
static constexpr uint8_t DSM_VERSION_PATCH = 3;
9+
static constexpr uint8_t DSM_VERSION_PATCH = 4;
1010

1111
static auto const DSM_VERSION =
1212
std::format("{}.{}.{}", DSM_VERSION_MAJOR, DSM_VERSION_MINOR, DSM_VERSION_PATCH);

src/dsm/headers/Dynamics.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ namespace dsm {
426426

427427
template <typename agent_t>
428428
double Dynamics<agent_t>::streetMeanSpeed(Id streetId) const {
429-
auto const& pStreet{m_graph.streetSet().at(streetId)};
429+
auto const& pStreet{m_graph.street(streetId)};
430430
auto const nAgents{pStreet->nAgents()};
431431
if (nAgents == 0) {
432432
return 0.;

src/dsm/headers/FirstOrderDynamics.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ namespace dsm {
4747
}
4848

4949
double FirstOrderDynamics::streetMeanSpeed(Id streetId) const {
50-
const auto& street{this->m_graph.streetSet().at(streetId)};
50+
const auto& street{this->m_graph.street(streetId)};
5151
if (street->nAgents() == 0) {
5252
return street->maxSpeed();
5353
}
@@ -69,7 +69,7 @@ namespace dsm {
6969
}
7070
}
7171
}
72-
const auto& node = this->m_graph.nodeSet().at(street->nodePair().second);
72+
const auto& node = this->m_graph.node(street->nodePair().second);
7373
if (node->isIntersection()) {
7474
auto& intersection = dynamic_cast<Intersection&>(*node);
7575
for (const auto& [angle, agentId] : intersection.agents()) {

src/dsm/headers/Graph.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,9 @@ namespace dsm {
229229
/// @brief Get the graph's node map
230230
/// @return A std::unordered_map containing the graph's nodes
231231
std::unordered_map<Id, std::unique_ptr<Node>>& nodeSet() { return m_nodes; }
232+
/// @brief Get a node from the graph
233+
/// @param id The node's id
234+
const std::unique_ptr<Node>& node(Id id) const { return m_nodes.at(id); }
232235
/// @brief Get the Graph's number of streets
233236
/// @return size_t The number of streets in the graph
234237
size_t nEdges() const { return m_streets.size(); }
@@ -241,6 +244,9 @@ namespace dsm {
241244
/// @return A std::unordered_map containing the graph's streets
242245
std::unordered_map<Id, std::unique_ptr<Street>>& streetSet() { return m_streets; }
243246
/// @brief Get a street from the graph
247+
/// @param id The street's id
248+
const std::unique_ptr<Street>& street(Id id) const { return m_streets.at(id); }
249+
/// @brief Get a street from the graph
244250
/// @param source The source node
245251
/// @param destination The destination node
246252
/// @return A std::unique_ptr to the street if it exists, nullptr otherwise

src/dsm/headers/RoadDynamics.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,7 @@ namespace dsm {
225225
p = moveDist(this->m_generator);
226226
iterator = possibleMoves.begin();
227227
std::advance(iterator, p);
228-
} while (!this->m_graph.nodeSet().at(nodeId)->isRoundabout() and
229-
streetId.has_value() and
228+
} while (!this->m_graph.node(nodeId)->isRoundabout() and streetId.has_value() and
230229
(this->m_graph.streetSet()[iterator->first]->nodePair().second ==
231230
this->m_graph.streetSet()[streetId.value()]->nodePair().first) and
232231
(possibleMoves.size() > 1));

test/Test_dynamics.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,23 +75,21 @@ TEST_CASE("Dynamics") {
7575
auto& tl = graph.makeTrafficLight(0, 2);
7676
Dynamics dynamics{graph, 69};
7777
THEN("The node is a traffic light") {
78-
CHECK(dynamics.graph().nodeSet().at(0)->isTrafficLight());
78+
CHECK(dynamics.graph().node(0)->isTrafficLight());
7979
CHECK_EQ(tl.cycleTime(), 2);
8080
}
8181
}
8282
WHEN("We transform a node into a roundabout and create the dynamics") {
8383
graph.makeRoundabout(0);
8484
Dynamics dynamics{graph, 69};
8585
THEN("The node is a roundabout") {
86-
CHECK(dynamics.graph().nodeSet().at(0)->isRoundabout());
86+
CHECK(dynamics.graph().node(0)->isRoundabout());
8787
}
8888
}
8989
WHEN("We transorm a street into a spire and create the dynamcis") {
9090
graph.makeSpireStreet(8);
9191
Dynamics dynamics{graph, 69};
92-
THEN("The street is a spire") {
93-
CHECK(dynamics.graph().streetSet().at(8)->isSpire());
94-
}
92+
THEN("The street is a spire") { CHECK(dynamics.graph().street(8)->isSpire()); }
9593
}
9694
}
9795
}
@@ -910,8 +908,8 @@ TEST_CASE("Dynamics") {
910908
for (const auto& [agentId, agent] : dynamics.agents()) {
911909
meanSpeed += agent->speed();
912910
}
913-
meanSpeed /= (dynamics.graph().streetSet().at(1)->nExitingAgents() +
914-
dynamics.graph().streetSet().at(1)->movingAgents().size());
911+
auto const& pStreet{dynamics.graph().street(1)};
912+
meanSpeed /= (pStreet->nExitingAgents() + pStreet->movingAgents().size());
915913
CHECK_EQ(dynamics.streetMeanSpeed(1), meanSpeed);
916914
// I don't think the mean speed of agents should be equal to the street's
917915
// one... CHECK_EQ(dynamics.streetMeanSpeed().mean,
@@ -930,8 +928,8 @@ TEST_CASE("Dynamics") {
930928
meanSpeed += agent->speed();
931929
}
932930
}
933-
meanSpeed /= dynamics.graph().streetSet().at(1)->queue(0).size();
934-
CHECK_EQ(dynamics.graph().streetSet().at(1)->queue(0).size(), 3);
931+
meanSpeed /= pStreet->queue(0).size();
932+
CHECK_EQ(pStreet->queue(0).size(), 3);
935933
CHECK_EQ(dynamics.streetMeanSpeed(1), meanSpeed);
936934
}
937935
SUBCASE("Intersection priorities") {
@@ -952,7 +950,7 @@ TEST_CASE("Dynamics") {
952950
graph2.addEdge<Street>(7, std::make_pair(4, 0), 10., 10.);
953951
graph2.buildAdj();
954952
Dynamics dynamics{graph2, 69};
955-
dynamics.graph().nodeSet().at(0)->setCapacity(3);
953+
dynamics.graph().node(0)->setCapacity(3);
956954
Itinerary itinerary{0, 2};
957955
Itinerary itinerary2{1, 1};
958956
dynamics.addItinerary(itinerary);

test/Test_graph.cpp

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -271,9 +271,7 @@ TEST_CASE("Graph") {
271271
graph.buildAdj();
272272
WHEN("We make node 0 a traffic light") {
273273
auto& tl = graph.makeTrafficLight(0, 60);
274-
THEN("The node 0 is a traffic light") {
275-
CHECK(graph.nodeSet().at(0)->isTrafficLight());
276-
}
274+
THEN("The node 0 is a traffic light") { CHECK(graph.node(0)->isTrafficLight()); }
277275
THEN("The traffic light has the correct parameters") {
278276
CHECK_EQ(tl.id(), 0);
279277
CHECK_EQ(tl.cycleTime(), 60);
@@ -288,9 +286,7 @@ TEST_CASE("Graph") {
288286
graph.buildAdj();
289287
WHEN("We make node 0 a roundabout") {
290288
graph.makeRoundabout(0);
291-
THEN("The node 0 is a roundabout") {
292-
CHECK(graph.nodeSet().at(0)->isRoundabout());
293-
}
289+
THEN("The node 0 is a roundabout") { CHECK(graph.node(0)->isRoundabout()); }
294290
}
295291
}
296292
}
@@ -301,9 +297,7 @@ TEST_CASE("Graph") {
301297
graph.buildAdj();
302298
WHEN("We make the street a spire street") {
303299
graph.makeSpireStreet(1);
304-
THEN("The street is a spire street") {
305-
CHECK(graph.streetSet().at(1)->isSpire());
306-
}
300+
THEN("The street is a spire street") { CHECK(graph.street(1)->isSpire()); }
307301
}
308302
}
309303
}

0 commit comments

Comments
 (0)