From 9799f781ecf0bf73e3266d22e2e2478c60c20ee1 Mon Sep 17 00:00:00 2001
From: Romain BERNARD <romain.bernard@uca.fr>
Date: Mon, 11 Mar 2024 17:29:00 +0100
Subject: [PATCH] fix conditional state expansion

---
 .../Transit/TransitAlgorithmState.h           | 24 ++++++++++++++++++
 .../Transit/TransitShortestPathPrecompute.cpp | 25 +++++++++++--------
 2 files changed, 38 insertions(+), 11 deletions(-)

diff --git a/src/ShortestPath/Transit/TransitAlgorithmState.h b/src/ShortestPath/Transit/TransitAlgorithmState.h
index 26767ef..adbc5a3 100644
--- a/src/ShortestPath/Transit/TransitAlgorithmState.h
+++ b/src/ShortestPath/Transit/TransitAlgorithmState.h
@@ -83,10 +83,34 @@ public:
         return _connections;
     }
 
+    [[nodiscard]] bool isEmpty() const {
+        return _connections.empty();
+    }
+
+    [[nodiscard]] bool canAddConnection() const {
+        return _connections.size() < 2;
+    }
+
     [[nodiscard]] size_t getNbConnections() const {
         return _connections.size();
     }
 
+    [[nodiscard]] LineStop getLastConnectionLineStop() const {
+        return _connections.back();
+    }
+
+    [[nodiscard]] Line getLastConnectionLine() const {
+        return _connections.back().getLineRef();
+    }
+
+    [[nodiscard]] int getPrecedingNodeIndex() const {
+        return _connections.back().getPrecedingNodeIndex();
+    }
+
+    [[nodiscard]] int getNextNodeIndex() const {
+        return _connections.back().getNextNodeIndex();
+    }
+
     void setNodeIndex(int nodeIndex) {
         _nodeIndex = nodeIndex;
     }
diff --git a/src/ShortestPath/Transit/TransitShortestPathPrecompute.cpp b/src/ShortestPath/Transit/TransitShortestPathPrecompute.cpp
index e5d5ed0..01a72f4 100644
--- a/src/ShortestPath/Transit/TransitShortestPathPrecompute.cpp
+++ b/src/ShortestPath/Transit/TransitShortestPathPrecompute.cpp
@@ -38,18 +38,21 @@ TransitStateContainer TransitShortestPathPrecompute::executeAlgorithm(const Grap
                 //If there is a proper next node and if it's not the same as our last used line stop predecessor
                 if(nextNode != -1 && (currentState.getConnections().empty() || nextNode != currentState.getConnections().back().getPrecedingNodeIndex())) {
                     TransitAlgorithmState newState; //define variable before conditionals
-                    if(currentState.getConnections().empty() || (currentState.getConnections().back().getLineRef() != lineStop.getLineRef()
-                    && currentState.getConnections().size() < 2)) // if new line is different than current line
+                    if(currentState.isEmpty() || currentState.getLastConnectionLine() != lineStop.getLineRef()) // if new line is different than current line
                     {
-                        int nextPassageIndex = lineStop.getLineRef().findNextScheduledPassage(lineStop.getStopIndex(),currentState.getInstant());
-                        if(nextPassageIndex == lineStop.getLineRef().scheduleSize()) {
-                            newState.setInstant(INT16_MAX);
-                            break;
-                        } else {
-                            newState = TransitAlgorithmState(currentState, lineStop);
-                            newState.setNodeIndex(nextNode);
-                            newState.setPassageIndex(nextPassageIndex); //get next passage for new line
-                            newState.setInstant(lineStop.getLineRef().getInstant(lineStop.getStopIndex() + 1,nextPassageIndex)); //replace time with
+                        if(currentState.canAddConnection()) {
+                            int nextPassageIndex = lineStop.getLineRef().findNextScheduledPassage(
+                                    lineStop.getStopIndex(), currentState.getInstant());
+                            if (nextPassageIndex == lineStop.getLineRef().scheduleSize()) {
+                                newState.setInstant(INT16_MAX);
+                                break;
+                            } else {
+                                newState = TransitAlgorithmState(currentState, lineStop);
+                                newState.setNodeIndex(nextNode);
+                                newState.setPassageIndex(nextPassageIndex); //get next passage for new line
+                                newState.setInstant(lineStop.getInstant(lineStop.getStopIndex() + 1,
+                                                                        nextPassageIndex)); //replace time with arrival time on next node
+                            }
                         }
                     } else {
                         newState = TransitAlgorithmState(currentState);
-- 
GitLab