@@ -13,10 +13,10 @@ export class TrainrunIterator {
1313 protected currentElement : TrainrunSectionNodePair = null ;
1414 protected pointerElement : TrainrunSectionNodePair = null ;
1515
16- private visitedNodes : TrainrunSectionNodePair [ ] = [ ] ;
16+ protected visitedNodes : TrainrunSectionNodePair [ ] = [ ] ;
1717
1818 constructor (
19- private logService : LogService ,
19+ protected logService : LogService ,
2020 private startNode : Node ,
2121 private startTrainrunSection : TrainrunSection ,
2222 ) {
@@ -73,6 +73,45 @@ export class TrainrunIterator {
7373 }
7474}
7575
76+ export class BackwardTrainrunIterator extends TrainrunIterator {
77+ public next ( ) : TrainrunSectionNodePair {
78+ const currentElement = Object . assign ( { } , this . pointerElement ) ;
79+ const trainrunSection = this . pointerElement . node . getPreviousTrainrunSection (
80+ this . pointerElement . trainrunSection ,
81+ ) ;
82+
83+ if ( trainrunSection === undefined ) {
84+ this . pointerElement = new TrainrunSectionNodePair ( undefined , undefined ) ;
85+ this . currentElement = currentElement ;
86+ return this . currentElement ;
87+ }
88+
89+ const node = this . pointerElement . node . getOppositeNode ( trainrunSection ) ;
90+ this . pointerElement = new TrainrunSectionNodePair ( node , trainrunSection ) ;
91+
92+ if (
93+ this . visitedNodes . find (
94+ ( element ) =>
95+ element . node . getId ( ) === node . getId ( ) &&
96+ element . trainrunSection . getId ( ) === trainrunSection . getId ( ) ,
97+ ) !== undefined
98+ ) {
99+ // The trainrun has a loop -> early break the avoid unfinitiy iterating
100+ this . currentElement = Object . assign ( { } , this . pointerElement ) ;
101+ this . pointerElement = new TrainrunSectionNodePair ( undefined , undefined ) ;
102+ // log the issue
103+ this . logService . error (
104+ $localize `:@@app.services.util.trainrun-iteration.error.infinity-loop:Iterator has detected an infinity loop. The iteration terminated early!` ,
105+ new Error ( ) . stack ,
106+ ) ;
107+ return this . currentElement ;
108+ }
109+ this . visitedNodes . push ( this . currentElement ) ;
110+ this . currentElement = currentElement ;
111+ return this . currentElement ;
112+ }
113+ }
114+
76115export class NonStopTrainrunIterator extends TrainrunIterator {
77116 public next ( ) : TrainrunSectionNodePair {
78117 if ( ! this . pointerElement . node . isNonStop ( this . pointerElement . trainrunSection ) ) {
@@ -84,3 +123,15 @@ export class NonStopTrainrunIterator extends TrainrunIterator {
84123 return super . next ( ) ;
85124 }
86125}
126+
127+ export class BackwardNonStopTrainrunIterator extends BackwardTrainrunIterator {
128+ public next ( ) : TrainrunSectionNodePair {
129+ if ( ! this . pointerElement . node . isNonStop ( this . pointerElement . trainrunSection ) ) {
130+ // The trainrun has a stop and break the backward iteration
131+ this . currentElement = Object . assign ( { } , this . pointerElement ) ;
132+ this . pointerElement = new TrainrunSectionNodePair ( undefined , undefined ) ;
133+ return this . currentElement ;
134+ }
135+ return super . next ( ) ;
136+ }
137+ }
0 commit comments