Skip to content

Commit

Permalink
Add makeTrafficLight function to Graph (#146)
Browse files Browse the repository at this point in the history
* Add yellow delay to trafficlight class

* New set_delay - wip: no tests

* New `setDelay` for traffic light

* Update version

* Add `importCoords` function to Graph class

* Rework `meanTravelTimes`: now can delete data after get

* Add `isSpire` to Street class

* Update version

* Add `makeTrafficLight` function to Graph class

* Formatting

* Fix typo

* Formatting

---------

Co-authored-by: Simone Balducci <[email protected]>
  • Loading branch information
Grufoony and sbaldu authored Mar 16, 2024
1 parent 77e9861 commit bae3905
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.16.0)

project(dms VERSION 1.1.4 LANGUAGES CXX)
project(dms VERSION 1.1.5 LANGUAGES CXX)

# set the C++ standard
set(CMAKE_CXX_STANDARD 20)
Expand Down
25 changes: 25 additions & 0 deletions src/dsm/headers/Graph.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,14 @@ namespace dsm {
(is_node_v<std::remove_reference_t<Tn>> && ...)
void addNodes(T1&& node, Tn&&... nodes);

/// @brief Convert an existing node to a traffic light
/// @tparam Delay The type of the traffic light's delay
/// @param nodeId The id of the node to convert to a traffic light
/// @throws std::invalid_argument if the node does not exist
template <typename Delay>
requires(std::unsigned_integral<Delay>)
void makeTrafficLight(Id nodeId);

/// @brief Add a street to the graph
/// @param street A std::shared_ptr to the street to add
void addStreet(shared<Street<Id, Size>> street);
Expand Down Expand Up @@ -507,6 +515,23 @@ namespace dsm {
addNodes(std::forward<Tn>(nodes)...);
}

template <typename Id, typename Size>
requires(std::unsigned_integral<Id> && std::unsigned_integral<Size>)
template <typename Delay>
requires(std::unsigned_integral<Delay>)
void Graph<Id, Size>::makeTrafficLight(Id nodeId) {
if (!m_nodes.contains(nodeId)) {
throw std::invalid_argument(buildLog("Node does not exist."));
}
auto& pNode = m_nodes[nodeId];
auto pTrafficLight = std::dynamic_pointer_cast<TrafficLight<Id, Size, Delay>>(pNode);
if (!pTrafficLight) {
pTrafficLight = std::make_shared<TrafficLight<Id, Size, Delay>>(
*static_cast<TrafficLight<Id, Size, Delay>*>(pNode.get()));
}
pNode = pTrafficLight;
}

template <typename Id, typename Size>
requires(std::unsigned_integral<Id> && std::unsigned_integral<Size>)
void Graph<Id, Size>::addStreet(shared<Street<Id, Size>> street) {
Expand Down
2 changes: 1 addition & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.16.0)

project(dsm_tests VERSION 1.1.4 LANGUAGES CXX)
project(dsm_tests VERSION 1.1.5 LANGUAGES CXX)

# Set the C++ standard
set(CMAKE_CXX_STANDARD 20)
Expand Down
14 changes: 14 additions & 0 deletions test/Test_graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,20 @@ TEST_CASE("Graph") {
CHECK_EQ(street2->capacity(), 1);
CHECK(!graph.street(1, 0).has_value());
}
SUBCASE("make trafficlight") {
GIVEN("A graph object with two nodes and one street") {
Graph graph{};
graph.addStreet(Street{1, 1, 1., std::make_pair(0, 1)});
graph.buildAdj();
WHEN("We make node 0 a traffic light") {
graph.makeTrafficLight<uint8_t>(0);
THEN("The node 0 is a traffic light") {
CHECK(std::dynamic_pointer_cast<dsm::TrafficLight<uint, uint, uint8_t>>(
graph.nodeSet().at(0)));
}
}
}
}
}

TEST_CASE("Dijkstra") {
Expand Down

0 comments on commit bae3905

Please sign in to comment.