Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/dsm/dsm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

static constexpr uint8_t DSM_VERSION_MAJOR = 2;
static constexpr uint8_t DSM_VERSION_MINOR = 6;
static constexpr uint8_t DSM_VERSION_PATCH = 9;
static constexpr uint8_t DSM_VERSION_PATCH = 10;

static auto const DSM_VERSION =
std::format("{}.{}.{}", DSM_VERSION_MAJOR, DSM_VERSION_MINOR, DSM_VERSION_PATCH);
Expand Down
11 changes: 6 additions & 5 deletions src/dsm/headers/TrafficLight.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ namespace dsm {

class TrafficLight : public Intersection {
private:
std::unordered_map<Id, std::array<TrafficLightCycle, 3>> m_cycles;
std::unordered_map<Id, std::unordered_map<Direction, TrafficLightCycle>> m_cycles;
Delay m_cycleTime; // The total time of a red-green cycle
Delay m_counter;

Expand Down Expand Up @@ -104,9 +104,9 @@ namespace dsm {
/// @param cycle The traffic light cycle
void setCycle(Id const streetId, Direction direction, TrafficLightCycle const& cycle);
/// @brief Set the traffic light's cycles
/// @param cycles std::unordered_map<Id, std::array<TrafficLightCycle, 3>> The traffic light's cycles
/// @param cycles std::unordered_map<Id, std::unordered_map<Direction, TrafficLightCycle>> The traffic light's cycles
inline void setCycles(
std::unordered_map<Id, std::array<TrafficLightCycle, 3>> cycles) {
std::unordered_map<Id, std::unordered_map<Direction, TrafficLightCycle>> cycles) {
m_cycles = std::move(cycles);
}
/// @brief Set the complementary cycle for a street
Expand All @@ -129,8 +129,9 @@ namespace dsm {
/// @param delta Delay, the time to increase or decrease the green times
void decreaseGreenTimes(Delay const delta);
/// @brief Get the traffic light's cycles
/// @return std::unordered_map<Id, std::array<TrafficLightCycle, 3>>& The traffic light's cycles
inline std::unordered_map<Id, std::array<TrafficLightCycle, 3>> const& cycles() const {
/// @return std::unordered_map<Id, std::unordered_map<Direction, TrafficLightCycle>> const& The traffic light's cycles
inline std::unordered_map<Id, std::unordered_map<Direction, TrafficLightCycle>> const&
cycles() const {
return m_cycles;
}
/// @brief Returns true if all the cycles are set to their default values
Expand Down
2 changes: 1 addition & 1 deletion src/dsm/sources/RoadNetwork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
}
if (node->isTrafficLight()) {
auto& trafficLight = dynamic_cast<TrafficLight&>(*node);
std::unordered_map<Id, std::array<TrafficLightCycle, 3>> newCycles;
std::unordered_map<Id, std::unordered_map<Direction, TrafficLightCycle>> newCycles;

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 12.3 rule Note

MISRA 12.3 rule
for (auto const& [streetId, cycles] : trafficLight.cycles()) {
newCycles.emplace(newStreetIds.at(streetId), std::move(cycles));
}
Expand Down
88 changes: 27 additions & 61 deletions src/dsm/sources/TrafficLight.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,32 +26,7 @@
cycle.phase(),
m_cycleTime));
}
if (direction == Direction::UTURN) {
direction = Direction::LEFT;
}
if (!m_cycles.contains(streetId)) {
TrafficLightCycle defaultCycle(m_cycleTime, 0);
std::array<TrafficLightCycle, 3> cycles{defaultCycle, defaultCycle, defaultCycle};
m_cycles.emplace(streetId, cycles);
}
switch (direction) {
case Direction::RIGHTANDSTRAIGHT:
m_cycles.at(streetId)[Direction::RIGHT] = cycle;
m_cycles.at(streetId)[Direction::STRAIGHT] = cycle;
break;
case Direction::LEFTANDSTRAIGHT:
m_cycles.at(streetId)[Direction::LEFT] = cycle;
m_cycles.at(streetId)[Direction::STRAIGHT] = cycle;
break;
case Direction::ANY:
m_cycles.at(streetId)[Direction::RIGHT] = cycle;
m_cycles.at(streetId)[Direction::STRAIGHT] = cycle;
m_cycles.at(streetId)[Direction::LEFT] = cycle;
break;
default:
m_cycles.at(streetId)[direction] = cycle;
break;
}
m_cycles[streetId].emplace(direction, cycle);
}

void TrafficLightCycle::reset() {
Expand All @@ -68,7 +43,7 @@
throw std::invalid_argument(Logger::buildExceptionMessage("Cycle does not exist."));
}
m_cycles.emplace(streetId, m_cycles.at(existingCycle));
for (auto& cycle : m_cycles.at(streetId)) {
for (auto& [direction, cycle] : m_cycles.at(streetId)) {

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 12.3 rule Note

MISRA 12.3 rule
cycle = TrafficLightCycle(m_cycleTime - cycle.greenTime(),
cycle.phase() + m_cycleTime - cycle.greenTime());
}
Expand All @@ -93,11 +68,11 @@
Delay maxTime{0};
for (auto const& [streetId, cycles] : m_cycles) {
if (priorityStreets && m_streetPriorities.contains(streetId)) {
for (auto const& cycle : cycles) {
for (auto const& [direction, cycle] : cycles) {

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 12.3 rule Note

MISRA 12.3 rule
maxTime = std::max(maxTime, cycle.greenTime());
}
} else {
for (auto const& cycle : cycles) {
for (auto const& [direction, cycle] : cycles) {

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 12.3 rule Note

MISRA 12.3 rule
maxTime = std::max(maxTime, cycle.greenTime());
}
}
Expand All @@ -109,11 +84,11 @@
Delay minTime{std::numeric_limits<Delay>::max()};
for (auto const& [streetId, cycles] : m_cycles) {
if (priorityStreets && m_streetPriorities.contains(streetId)) {
for (auto const& cycle : cycles) {
for (auto const& [direction, cycle] : cycles) {

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 12.3 rule Note

MISRA 12.3 rule
minTime = std::min(minTime, cycle.greenTime());
}
} else {
for (auto const& cycle : cycles) {
for (auto const& [direction, cycle] : cycles) {

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 12.3 rule Note

MISRA 12.3 rule
minTime = std::min(minTime, cycle.greenTime());
}
}
Expand All @@ -132,8 +107,8 @@
cycles.end(),
0.0, // Initial value (double)
std::plus<double>(), // Reduction function (addition)
[](const TrafficLightCycle& cycle) -> double {
return static_cast<double>(cycle.greenTime());
[](const auto& pair) -> double {
return static_cast<double>(pair.second.greenTime());

Check warning on line 111 in src/dsm/sources/TrafficLight.cpp

View check run for this annotation

Codecov / codecov/patch

src/dsm/sources/TrafficLight.cpp#L110-L111

Added lines #L110 - L111 were not covered by tests

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 15.5 rule Note

MISRA 15.5 rule
});
nCycles += cycles.size();
}
Expand All @@ -144,11 +119,11 @@
void TrafficLight::increaseGreenTimes(Delay const delta) {
for (auto& [streetId, cycles] : m_cycles) {
if (m_streetPriorities.contains(streetId)) {
for (auto& cycle : cycles) {
for (auto& [direction, cycle] : cycles) {

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 12.3 rule Note

MISRA 12.3 rule
cycle = TrafficLightCycle(cycle.greenTime() + delta, cycle.phase());
}
} else {
for (auto& cycle : cycles) {
for (auto& [direction, cycle] : cycles) {

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 12.3 rule Note

MISRA 12.3 rule
cycle = TrafficLightCycle(cycle.greenTime() - delta, cycle.phase() + delta);
}
}
Expand All @@ -158,11 +133,11 @@
void TrafficLight::decreaseGreenTimes(Delay const delta) {
for (auto& [streetId, cycles] : m_cycles) {
if (!m_streetPriorities.contains(streetId)) {
for (auto& cycle : cycles) {
for (auto& [direction, cycle] : cycles) {

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 12.3 rule Note

MISRA 12.3 rule
cycle = TrafficLightCycle(cycle.greenTime() + delta, cycle.phase());
}
} else {
for (auto& cycle : cycles) {
for (auto& [direction, cycle] : cycles) {

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 12.3 rule Note

MISRA 12.3 rule
cycle = TrafficLightCycle(cycle.greenTime() - delta, cycle.phase() + delta);
}
}
Expand All @@ -171,7 +146,7 @@

bool TrafficLight::isDefault() const {
for (auto const& [streetId, cycles] : m_cycles) {
for (auto const& cycle : cycles) {
for (auto const& [direction, cycle] : cycles) {

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 12.3 rule Note

MISRA 12.3 rule
if (!cycle.isDefault()) {
return false;
}
Expand All @@ -181,42 +156,33 @@
}

bool TrafficLight::isGreen(Id const streetId, Direction direction) const {
if (m_cycles.empty()) {
return true;
}
if (!m_cycles.contains(streetId)) {
throw std::invalid_argument(Logger::buildExceptionMessage(
std::format("Street id {} is not valid for node {}.", streetId, id())));
}
switch (direction) {
case Direction::UTURN:
if (!m_cycles.at(streetId).contains(direction)) {
if (direction != Direction::UTURN) {
return true;
}
if (m_cycles.at(streetId).contains(Direction::LEFT)) {

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 14.4 rule Note

MISRA 14.4 rule
direction = Direction::LEFT;
break;
case Direction::RIGHTANDSTRAIGHT:
return m_cycles.at(streetId)[Direction::RIGHT].isGreen(m_cycleTime, m_counter) &&
m_cycles.at(streetId)[Direction::STRAIGHT].isGreen(m_cycleTime, m_counter);
case Direction::LEFTANDSTRAIGHT:
return m_cycles.at(streetId)[Direction::LEFT].isGreen(m_cycleTime, m_counter) &&
m_cycles.at(streetId)[Direction::STRAIGHT].isGreen(m_cycleTime, m_counter);
case Direction::ANY:
return m_cycles.at(streetId)[Direction::RIGHT].isGreen(m_cycleTime, m_counter) &&
m_cycles.at(streetId)[Direction::STRAIGHT].isGreen(m_cycleTime,
m_counter) &&
m_cycles.at(streetId)[Direction::LEFT].isGreen(m_cycleTime, m_counter);
default:
break;
}
return m_cycles.at(streetId)[direction].isGreen(m_cycleTime, m_counter);
} else if (m_cycles.at(streetId).contains(Direction::LEFTANDSTRAIGHT)) {

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 14.4 rule Note

MISRA 14.4 rule
direction = Direction::LEFTANDSTRAIGHT;

Check warning on line 170 in src/dsm/sources/TrafficLight.cpp

View check run for this annotation

Codecov / codecov/patch

src/dsm/sources/TrafficLight.cpp#L170

Added line #L170 was not covered by tests

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 17.8 rule Note

MISRA 17.8 rule
} else {
return true;

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 15.5 rule Note

MISRA 15.5 rule
}
}
return m_cycles.at(streetId).at(direction).isGreen(m_cycleTime, m_counter);
}

bool TrafficLight::isFavouringDirection(bool const priority) const {
for (auto const& [streetId, cycles] : m_cycles) {
if ((priority && m_streetPriorities.contains(streetId)) ||
(!priority && !m_streetPriorities.contains(streetId))) {
for (auto const& cycle : cycles) {
for (auto const& [direction, cycle] : cycles) {

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 12.3 rule Note

MISRA 12.3 rule
if (!cycle.isGreenTimeIncreased()) {
return false;
}

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 15.5 rule Note

MISRA 15.5 rule
}
}
}
Expand All @@ -225,7 +191,7 @@

void TrafficLight::resetCycles() {
for (auto& [streetId, cycles] : m_cycles) {
for (auto& cycle : cycles) {
for (auto& [direction, cycle] : cycles) {

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 12.3 rule Note

MISRA 12.3 rule
cycle.reset();
}
}
Expand Down
Loading
Loading