forked from bliksemlabs/rrrr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.c
1554 lines (1339 loc) · 63 KB
/
router.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright 2013 Bliksem Labs.
* See the LICENSE file at the top-level directory of this distribution and at
* https://github.com/bliksemlabs/rrrr/
*/
/* router.c : the main routing algorithm */
#include "router.h" /* first to ensure it works alone */
#include "router_request.h"
#include "router_dump.h"
#include "util.h"
#include "config.h"
#include "tdata.h"
#include "bitset.h"
#include "hashgrid.h"
#include "set.h"
#include <stdlib.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <stdint.h>
#include <math.h>
#include <sys/types.h>
router_t router_create() { router_t struct_obj;
memset(&struct_obj, 0, sizeof(struct_obj));
return struct_obj; }
bool router_setup(router_t *router, tdata_t *tdata) {
uint64_t n_states = tdata->n_stop_points * RRRR_DEFAULT_MAX_ROUNDS;
router->tdata = tdata;
router->best_time = (rtime_t *) malloc(sizeof(rtime_t) * tdata->n_stop_points);
router->states_back_journey_pattern = (jpidx_t *) malloc(sizeof(jpidx_t) * n_states);
router->states_back_vehicle_journey = (jp_vjoffset_t *) malloc(sizeof(jp_vjoffset_t) * n_states);
router->states_ride_from = (spidx_t *) malloc(sizeof(spidx_t) * n_states);
router->states_walk_from = (spidx_t *) malloc(sizeof(spidx_t) * n_states);
router->states_walk_time = (rtime_t *) malloc(sizeof(rtime_t) * n_states);
router->states_time = (rtime_t *) malloc(sizeof(rtime_t) * n_states);
router->states_board_time = (rtime_t *) malloc(sizeof(rtime_t) * n_states);
#ifdef RRRR_FEATURE_REALTIME_EXPANDED
router->states_back_journey_pattern_point = (jppidx_t *) malloc(sizeof(jppidx_t) * n_states);
router->states_journey_pattern_point = (jppidx_t *) malloc(sizeof(jppidx_t) * n_states);
#endif
router->updated_stop_points = bitset_new(tdata->n_stop_points);
router->updated_walk_stop_points = bitset_new(tdata->n_stop_points);
router->updated_journey_patterns = bitset_new(tdata->n_journey_patterns);
#if RRRR_BANNED_JOURNEY_PATTERNS_BITMASK == 1
router->banned_journey_patterns = bitset_new(tdata->n_journey_patterns);
#endif
if ( ! (router->best_time
&& router->states_back_journey_pattern
&& router->states_back_vehicle_journey
&& router->states_ride_from
&& router->states_walk_from
&& router->states_walk_time
&& router->states_time
&& router->states_board_time
#ifdef RRRR_FEATURE_REALTIME_EXPANDED
&& router->states_back_journey_pattern_point
&& router->states_journey_pattern_point
#endif
&& router->updated_stop_points
&& router->updated_walk_stop_points
&& router->updated_journey_patterns
#if RRRR_BANNED_JOURNEY_PATTERNS_BITMASK == 1
&& router->banned_journey_patterns
#endif
)
) {
fprintf(stderr, "failed to allocate router scratch space");
return false;
}
return true;
}
void router_teardown(router_t *router) {
free(router->best_time);
free(router->states_back_journey_pattern);
free(router->states_back_vehicle_journey);
free(router->states_ride_from);
free(router->states_walk_from);
free(router->states_walk_time);
free(router->states_time);
free(router->states_board_time);
#ifdef RRRR_FEATURE_REALTIME_EXPANDED
free(router->states_back_journey_pattern_point);
free(router->states_journey_pattern_point);
#endif
bitset_destroy(router->updated_stop_points);
bitset_destroy(router->updated_walk_stop_points);
bitset_destroy(router->updated_journey_patterns);
#if RRRR_BANNED_JOURNEY_PATTERNS_BITMASK == 1
bitset_destroy(router->banned_journey_patterns);
#endif
}
void router_reset(router_t *router) {
/* Make sure both origin and target are initialised with NONE, so it
* becomes possible to validate that they have been set to a valid
* stop_point index.
*/
router->origin = STOP_NONE;
router->target = STOP_NONE;
/* The best times to arrive at a stop_point scratch space is initialised with
* UNREACHED. This allows to compare for a lesser time candidate in the
* search.
*/
rrrr_memset (router->best_time, UNREACHED, router->tdata->n_stop_points);
rrrr_memset (router->states_board_time, UNREACHED, router->tdata->n_stop_points);
}
static bool initialize_states (router_t *router) {
uint64_t i_state = ((uint64_t) RRRR_DEFAULT_MAX_ROUNDS) * router->tdata->n_stop_points;
do {
i_state--;
router->states_time[i_state] = UNREACHED;
router->states_walk_time[i_state] = UNREACHED;
} while (i_state);
return true;
}
/* One serviceday_t for each of: yesterday, today, tomorrow (for overnight
* searches). Note that yesterday's bit flag will be 0 if today is the
* first day of the calendar.
*/
static bool initialize_servicedays (router_t *router, router_request_t *req) {
/* One bit for the calendar day on which realtime data should be
* applied (applying only on the true current calendar day)
*/
serviceday_t yesterday, today, tomorrow;
uint8_t day_i = 0;
calendar_t realtime_mask;
router->day_mask = req->day_mask;
/* Fake realtime_mask if we are QA testing. */
#ifdef RRRR_FAKE_REALTIME
realtime_mask = ~((calendar_t) 0);
#else
realtime_mask = ((calendar_t) 1) << ((time(NULL) - router->tdata->calendar_start_time) /
SEC_IN_ONE_DAY);
#endif
yesterday.midnight = 0;
yesterday.mask = router->day_mask >> 1;
yesterday.apply_realtime = (bool) (yesterday.mask & realtime_mask);
today.midnight = RTIME_ONE_DAY;
today.mask = router->day_mask;
today.apply_realtime = (bool) (today.mask & realtime_mask);
tomorrow.midnight = RTIME_TWO_DAYS;
tomorrow.mask = router->day_mask << 1;
tomorrow.apply_realtime = (bool) (tomorrow.mask & realtime_mask);
router->day_mask = today.mask;
/* Iterate backward over days for arrive-by searches. */
if (req->arrive_by) {
if (req->time > tomorrow.midnight) {
/* Departuretime includes trips running tomorrow */
router->servicedays[day_i] = tomorrow;
router->day_mask |= tomorrow.mask;
day_i++;
}
if (req->time_cutoff < (today.midnight + router->tdata->max_time) && req->time > today.midnight) {
router->servicedays[day_i] = today;
router->day_mask |= today.mask;
day_i++;
}
if (req->time_cutoff < router->tdata->max_time){
/* Cut-off request includes vehicle_journey running tomorrow */
router->servicedays[day_i] = yesterday;
router->day_mask |= yesterday.mask;
day_i++;
}
} else {
if (req->time < router->tdata->max_time){
/* There are still some journeys running of previous day on departure_time of journey */
router->day_mask |= yesterday.mask;
router->servicedays[day_i] = yesterday;
day_i++;
}
if (req->time_cutoff >= today.midnight && req->time < (today.midnight + router->tdata->max_time)) {
router->servicedays[day_i] = today;
router->day_mask |= today.mask;
day_i++;
}
if (req->time_cutoff > tomorrow.midnight){
/* Cut-off request includes vehicle_journey running tomorrow */
router->day_mask |= tomorrow.mask;
router->servicedays[day_i] = tomorrow;
day_i++;
}
}
router->n_servicedays = day_i;
#ifdef RRRR_DEBUG
{
uint8_t i;
for (i = 0; i < 3; ++i) service_day_dump (&router->servicedays[i]);
day_mask_dump (router->day_mask);
}
#endif
return true;
}
/* Given a stop_point index, mark all journey_patterns that serve it as updated. */
static void flag_journey_patterns_for_stop_point(router_t *router, router_request_t *req,
spidx_t sp_index) {
jpidx_t *journey_patterns;
uint32_t i_jp = tdata_journey_patterns_for_stop_point(router->tdata, sp_index,
&journey_patterns);
if (i_jp == 0) return;
do {
calendar_t jp_active_flags;
i_jp--;
#ifdef RRRR_INFO
fprintf (stderr, "flagging journey_pattern %d at stop_point %d\n",
journey_patterns[i_jp], sp_index);
#endif
jp_active_flags = router->tdata->journey_pattern_active[journey_patterns[i_jp]];
/* CHECK that there are any vehicle_journeys running on this journey_pattern
* (another bitfield)
* fprintf(stderr, "journey_pattern flags %d", jp_active_flags);
* printBits(4, &jp_active_flags);
*/
/* & journey_pattern_active_flags seems to provide about 14% increase
* in throughput
*/
if ((router->day_mask & jp_active_flags) &&
(req->mode & router->tdata->journey_patterns[journey_patterns[i_jp]].attributes) > 0) {
bitset_set (router->updated_journey_patterns, journey_patterns[i_jp]);
#ifdef RRRR_INFO
fprintf (stderr, " journey_pattern running\n");
#endif
}
} while (i_jp);
#ifdef RRRR_FEATURE_REALTIME_EXPANDED
if (router->servicedays[1].apply_realtime &&
router->tdata->rt_journey_patterns_at_stop_point[sp_index]) {
journey_patterns = router->tdata->rt_journey_patterns_at_stop_point[sp_index]->list;
i_jp = router->tdata->rt_journey_patterns_at_stop_point[sp_index]->len;
if (i_jp == 0) return;
do {
i_jp--;
#ifdef RRRR_INFO
fprintf (stderr, " flagging changed journey_pattern %d at stop_point %d\n",
journey_patterns[i_jp], sp_index);
#endif
/* extra journey_patterns should only be applied on the current day */
if ((req->mode & router->tdata->journey_patterns[journey_patterns[i_jp]].attributes) > 0) {
bitset_set (router->updated_journey_patterns, journey_patterns[i_jp]);
#ifdef RRRR_INFO
fprintf (stderr, " journey_pattern running\n");
#endif
}
} while (i_jp);
}
#endif
}
#if RRRR_MAX_BANNED_JOURNEY_PATTERNS > 0
#if RRRR_BANNED_JOURNEY_PATTERNS_BITMASK == 1
static void unflag_banned_journey_patterns (router_t *router, router_request_t *req) {
if (req->n_banned_journey_patterns == 0) return;
bitset_mask_and (router->updated_journey_patterns, router->banned_journey_patterns);
}
static void initialize_banned_journey_patterns (router_t *router, router_request_t *req) {
uint8_t i_banned_jp = req->n_banned_journey_patterns;
bitset_black(router->banned_journey_patterns);
if (i_banned_jp == 0) return;
do {
i_banned_jp--;
bitset_unset (router->banned_journey_patterns,
req->banned_journey_patterns[i_banned_jp]);
} while (i_banned_jp);
}
#else
static void unflag_banned_journey_patterns(router_t *router, router_request_t *req) {
uint8_t i_banned_jp = req->n_banned_journey_patterns;
if (i_banned_jp == 0) return;
do {
i_banned_jp--;
bitset_unset (router->updated_journey_patterns,
req->banned_journey_patterns[i_banned_jp]);
} while (i_banned_jp);
}
#endif
#endif
#if RRRR_MAX_BANNED_STOP_POINTS > 0
static void unflag_banned_stop_points(router_t *router, router_request_t *req) {
uint8_t i_banned_sp = req->n_banned_stops;
if (i_banned_sp == 0) return;
do {
i_banned_sp--;
bitset_unset (router->updated_stop_points,
req->banned_stops[i_banned_sp]);
} while (i_banned_sp);
}
#endif
#ifdef RRRR_FEATURE_LATLON
/* Because the first round begins with so few reached stops, the initial state
* doesn't get its own full array of states
* Instead we reuse one of the later rounds (round 1) for the initial state.
* This means we need to reset the walks in round 1 back to UNREACHED before
* using them in routing. We iterate over all of them, because we don't
* maintain a list of all the stops that might have been added by the hashgrid.
*/
static void initialize_transfers_full (router_t *router, uint32_t round) {
uint32_t i_state = router->tdata->n_stop_points;
rtime_t *states_walk_time = router->states_walk_time + (round * router->tdata->n_stop_points);
do {
i_state--;
states_walk_time[i_state] = UNREACHED;
} while (i_state);
}
#else
/* Because the first round begins with so few reached stops, the initial state
* doesn't get its own full array of states. Instead we reuse one of the later
* rounds (round 1) for the initial state. This means we need to reset the
* walks in round 1 back to UNREACHED before using them in routing. Rather
* than iterating over all of them, we only initialize the stops that can be
* reached by transfers.
* Alternatively we could instead initialize walks to UNREACHED at the
* beginning of the transfer calculation function.
* We should not however reset the best times for those stops reached from the
* initial stop_point on foot. This will prevent finding circuitous itineraries that
* return to them.
*/
static void initialize_transfers (router_t *router,
uint32_t round, spidx_t sp_index_from) {
rtime_t *states_walk_time = router->states_walk_time + (round * router->tdata->n_stop_points);
uint32_t t = router->tdata->stop_points[sp_index_from ].transfers_offset;
uint32_t tN = router->tdata->stop_points[sp_index_from + 1].transfers_offset;
states_walk_time[sp_index_from] = UNREACHED;
for ( ; t < tN ; ++t) {
spidx_t sp_index_to = router->tdata->transfer_target_stops[t];
states_walk_time[sp_index_to] = UNREACHED;
}
}
#endif
/* Get the departure or arrival time of the given vj on the given
* service day, applying realtime data as needed.
*/
static rtime_t
tdata_stoptime (tdata_t* tdata, serviceday_t *serviceday,
jpidx_t jp_index, jp_vjoffset_t vj_offset, jppidx_t journey_pattern_point,
bool arrive) {
rtime_t time, time_adjusted;
stoptime_t *vj_stoptimes;
/* This code is only required if want realtime support in
* the journey planner
*/
#ifdef RRRR_FEATURE_REALTIME_EXPANDED
/* given that we are at a serviceday the realtime scope applies to */
if (serviceday->apply_realtime) {
/* the expanded stoptimes can be found at the same row as the vehicle_journey */
vj_stoptimes = tdata->vj_stoptimes[tdata->journey_patterns[jp_index].vj_offset + vj_offset];
if (vj_stoptimes) {
/* if the expanded stoptimes have been added,
* the begin_time is precalculated
*/
time = 0;
} else {
/* if the expanded stoptimes have not been added,
* or our source is not time-expanded
*/
vehicle_journey_t *vj = tdata_vehicle_journeys_in_journey_pattern(tdata, jp_index) + vj_offset;
vj_stoptimes = &tdata->stop_times[vj->stop_times_offset];
time = vj->begin_time;
}
} else
#endif /* RRRR_FEATURE_REALTIME_EXPANDED */
{
vehicle_journey_t *vj = tdata_vehicle_journeys_in_journey_pattern(tdata, jp_index) + vj_offset;
vj_stoptimes = &tdata->stop_times[vj->stop_times_offset];
time = vj->begin_time;
}
if (arrive) {
time += vj_stoptimes[journey_pattern_point].arrival;
} else {
time += vj_stoptimes[journey_pattern_point].departure;
}
time_adjusted = time + serviceday->midnight;
/*
printf ("boarding at stop_point %d, time is: %s \n", journey_pattern_point, timetext (time));
printf (" after adjusting: %s \n", timetext (time_adjusted));
printf (" midnight: %d \n", serviceday->midnight);
*/
/* Detect overflow (this will still not catch wrapping due to negative
* delays on small positive times) actually this happens naturally with
* times like '03:00+1day' transposed to serviceday 'tomorrow'
*/
if (time_adjusted < time) return UNREACHED;
return time_adjusted;
}
/* TODO: change the function name of tdata_next */
static bool
tdata_next (router_t *router, router_request_t *req,
jpidx_t jp_index, jp_vjoffset_t vj_offset, rtime_t qtime,
spidx_t *ret_sp_index, rtime_t *ret_stop_time) {
spidx_t *journey_pattern_points = tdata_points_for_journey_pattern(router->tdata, jp_index);
journey_pattern_t *jp = router->tdata->journey_patterns + jp_index;
uint32_t jpp_i;
*ret_sp_index = STOP_NONE;
*ret_stop_time = UNREACHED;
for (jpp_i = 0; jpp_i < jp->n_stops; ++jpp_i) {
/* TODO: check if the arrive = false flag works with req->arrive_by */
rtime_t time = tdata_stoptime (router->tdata, &(router->servicedays[1]),
jp_index, vj_offset, (jppidx_t) jpp_i, false);
/* Find stop_point immediately after the given time on the given vj. */
if (req->arrive_by ? time > qtime : time < qtime) {
if (*ret_stop_time == UNREACHED ||
(req->arrive_by ? time < *ret_stop_time :
time > *ret_stop_time)) {
*ret_sp_index = (spidx_t) journey_pattern_points[jpp_i];
*ret_stop_time = time;
}
}
}
return (*ret_sp_index != STOP_NONE);
}
/* For each updated stop_point and each destination of a transfer from an updated
* stop_point, set the associated journey_patterns as updated. The journey_patterns bitset is cleared
* before the operation, and the stop_points bitset is cleared after all transfers
* have been computed and all journey_patterns have been set.
* Transfer results are computed within the same round, based on arrival time
* in the ride phase and stored in the walk time member of states.
*/
static void apply_transfers (router_t *router, router_request_t *req,
uint32_t round, bool transfer, bool initial) {
rtime_t *states_time = router->states_time + (round * router->tdata->n_stop_points);
rtime_t *states_walk_time = router->states_walk_time + (round * router->tdata->n_stop_points);
spidx_t *states_walk_from = router->states_walk_from + (round * router->tdata->n_stop_points);
uint32_t sp_index_from; /* uint32_t: because we need to compare to BITSET_NONE */
/* The transfer process will flag journey_patterns that should be explored in
* the next round.
*/
bitset_clear (router->updated_journey_patterns);
for (sp_index_from = bitset_next_set_bit (router->updated_stop_points, 0);
sp_index_from != BITSET_NONE;
sp_index_from = bitset_next_set_bit (router->updated_stop_points, sp_index_from + 1)) {
rtime_t time_from = states_time[sp_index_from];
#ifdef RRRR_INFO
printf ("stop_point %d was marked as updated \n", sp_index_from);
#endif
if (time_from == UNREACHED) {
fprintf (stderr, "ERROR: transferring from unreached stop_point %d in round %d. \n", sp_index_from, round);
continue;
}
/* At this point, the best time at the from stop_point may be different than
* the state_from->time, because the best time may have been updated
* by a transfer.
*/
#ifdef RRRR_DEBUG
if (time_from != router->best_time[sp_index_from]) {
char buf[13];
fprintf (stderr, "ERROR: time at stop_point %d in round %d " \
"is not the same as its best time. \n",
sp_index_from, round);
fprintf (stderr, " from time %s \n",
btimetext(time_from, buf));
fprintf (stderr, " walk time %s \n",
btimetext(states_walk_time[sp_index_from], buf));
fprintf (stderr, " best time %s \n",
btimetext(router->best_time[sp_index_from], buf));
continue;
}
#endif
#ifdef RRRR_INFO
fprintf (stderr, " applying transfer at %d (%s) \n", sp_index_from,
tdata_stop_point_name_for_index(router->tdata, sp_index_from));
#endif
if (states_time[sp_index_from] == router->best_time[sp_index_from]) {
rtime_t sp_waittime = tdata_stop_point_waittime(router->tdata, (spidx_t) sp_index_from);
/* This state's best time is still its own.
* No improvements from other transfers.
*/
if (initial){
states_walk_time[sp_index_from] = time_from;
}else if (req->arrive_by){
states_walk_time[sp_index_from] = time_from - sp_waittime;
}else{
states_walk_time[sp_index_from] = time_from + sp_waittime;
}
states_walk_from[sp_index_from] = (spidx_t) sp_index_from;
/* assert (router->best_time[stop_index_from] == time_from); */
bitset_set(router->updated_walk_stop_points, sp_index_from);
}
if (transfer) {
/* Then apply transfers from the stop_point to nearby stops */
uint32_t tr = router->tdata->stop_points[sp_index_from].transfers_offset;
uint32_t tr_end = router->tdata->stop_points[sp_index_from + 1].transfers_offset;
for ( ; tr < tr_end ; ++tr) {
/* Transfer durations are stored in r_time */
spidx_t sp_index_to = router->tdata->transfer_target_stops[tr];
rtime_t transfer_duration = router->tdata->transfer_durations[tr] + req->walk_slack;
rtime_t time_to = req->arrive_by ? time_from - transfer_duration
: time_from + transfer_duration;
/* Avoid reserved values including UNREACHED
* and catch wrapping/overflow due to limited range of rtime_t
* this happens normally on overnight routing but should
* be avoided rather than caught.
*/
if (time_to > RTIME_THREE_DAYS || (req->arrive_by ? time_to > time_from :time_to < time_from)) continue;
#ifdef RRRR_INFO
{
char buf[13];
fprintf (stderr, " target %d %s (%s) \n",
sp_index_to,
btimetext(router->best_time[sp_index_to], buf),
tdata_stop_point_name_for_index(router->tdata, sp_index_to));
fprintf (stderr, " transfer time %s\n",
btimetext(transfer_duration, buf));
fprintf (stderr, " transfer result %s\n",
btimetext(time_to, buf));
}
#endif
/* TODO verify state_to->walk_time versus
* router->best_time[sp_index_to] */
if (router->best_time[sp_index_to] == UNREACHED ||
(req->arrive_by ? time_to > router->best_time[sp_index_to]
: time_to < router->best_time[sp_index_to])) {
#ifdef RRRR_INFO
char buf[13];
fprintf (stderr, " setting %d to %s\n",
sp_index_to, btimetext(time_to, buf));
#endif
states_walk_time[sp_index_to] = time_to;
states_walk_from[sp_index_to] = (spidx_t) sp_index_from;
router->best_time[sp_index_to] = time_to;
bitset_set(router->updated_walk_stop_points, sp_index_to);
}
}
}
}
for (sp_index_from = bitset_next_set_bit (router->updated_walk_stop_points, 0);
sp_index_from != BITSET_NONE;
sp_index_from = bitset_next_set_bit (router->updated_walk_stop_points, sp_index_from + 1)) {
flag_journey_patterns_for_stop_point(router, req, (spidx_t) sp_index_from);
}
#if RRRR_MAX_BANNED_JOURNEY_PATTERNS > 0
unflag_banned_journey_patterns(router, req);
#endif
/* Done with all transfers, reset stop-reached bits for the next round */
bitset_clear (router->updated_stop_points);
bitset_clear (router->updated_walk_stop_points);
/* Check invariant: Every stop_point reached in this round should have a
* best time equal to its walk time, and
* a walk arrival time <= its ride arrival time.
*/
}
/**
* Search a better time for the best time to board when no journey_pattern before has been boarded along this journey_pattern
* Start with the best possibility (arrive_by: LAST departure, depart_after FIRST departure) and end with the worst possibility,
* unless we reach a vehicle_journey that matches our requirements (valid departure, valid calendar, valid trip_attributes)
*/
static void board_vehicle_journeys_within_days(router_t *router, router_request_t *req,
jpidx_t jp_index,
jppidx_t jpp_offset,
rtime_t prev_time,
serviceday_t **best_serviceday,
jp_vjoffset_t *best_vj, rtime_t *best_time) {
calendar_t *vj_masks = tdata_vj_masks_for_journey_pattern(router->tdata, jp_index);
vehicle_journey_t *vjs_in_journey_pattern = tdata_vehicle_journeys_in_journey_pattern(router->tdata, jp_index);
journey_pattern_t *jp = &(router->tdata->journey_patterns[jp_index]);
serviceday_t *serviceday;
bool jp_overlap = jp->min_time < (jp->max_time - RTIME_ONE_DAY);
/* Search through the servicedays that are assumed to be put in search-order (counterclockwise for arrive_by)
*/
for (serviceday = router->servicedays;
serviceday <= router->servicedays + router->n_servicedays;
++serviceday) {
int32_t i_vj_offset;
/* Check that this journey_pattern still has any vehicle_journeys
* running on this day.
*/
if (req->arrive_by ? prev_time < serviceday->midnight +
jp->min_time
: prev_time > serviceday->midnight +
jp->max_time) continue;
/* Check whether there's any chance of improvement by
* scanning additional days. Note that day list is
* reversed for arrive-by searches.
*/
if (*best_vj != NONE && !jp_overlap) break;
for (i_vj_offset = req->arrive_by ? jp->n_vjs - 1: 0;
req->arrive_by ? i_vj_offset >= 0
: i_vj_offset < jp->n_vjs;
req->arrive_by ? --i_vj_offset
: ++i_vj_offset) {
rtime_t time;
#ifdef RRRR_DEBUG
printBits(4, & (vj_masks[i_vj_offset]));
printBits(4, & (serviceday->mask));
fprintf(stderr, "\n");
#endif
#if RRRR_MAX_BANNED_VEHICLE_JOURNEYS > 0
/* skip this vj if it is banned */
if (set_in_vj (req->banned_vjs_journey_pattern, req->banned_vjs_offset,
req->n_banned_vjs, jp_index,
(jp_vjoffset_t) i_vj_offset)) continue;
#endif
/* skip this vj if it is not running on
* the current service day
*/
if ( ! (serviceday->mask & vj_masks[i_vj_offset])) continue;
/* skip this vj if it doesn't have all our
* required attributes
* Checking whether we have required req->vj_attributes at all, before checking the attributes of the vehicle_journeys
* is about 4% more efficient for journeys without specific vj attribute requirements.
*/
if (req->vj_attributes && ! ((req->vj_attributes & vjs_in_journey_pattern[i_vj_offset].vj_attributes) == req->vj_attributes)) continue;
/* consider the arrival or departure time on
* the current service day
*/
time = tdata_stoptime (router->tdata, serviceday, jp_index, (jp_vjoffset_t) i_vj_offset, jpp_offset, req->arrive_by);
#ifdef RRRR_DEBUG_VEHICLE_JOURNEY
fprintf(stderr, " board option %d at %s \n", i_vj_offset, "");
#endif
/* rtime overflow due to long overnight vehicle_journey's on day 2 */
if (time == UNREACHED) continue;
if (req->arrive_by ? time < req->time_cutoff
: time > req->time_cutoff){
return;
}
/* Mark vj for boarding if it improves on the last round's
* post-walk time at this stop. Note: we should /not/ be comparing
* to the current best known time at this stop, because it may have
* been updated in this round by another vj (in the pre-walk
* transit phase).
*/
if (req->arrive_by ? time <= prev_time && time > *best_time
: time >= prev_time && time < *best_time) {
*best_vj = (jp_vjoffset_t) i_vj_offset;
*best_time = time;
*best_serviceday = serviceday;
/* Since FIFO ordering of trips is ensured, we can immediately return if the JourneyPattern does not overlap.
* If the JourneyPattern does overlap, we can only return if the time does not fall in the overlapping period
*/
if (!jp_overlap || jp->min_time > time - RTIME_ONE_DAY)
return;
}
} /* end for (vehicle_journey's within this route) */
} /* end for (service days: yesterday, today, tomorrow) */
}
/**
* Search a better time for the best time to board when we already boarded a vehicle_journey on this journey_pattern.
* Assume the currently boarded VJ is the best and try to improve that time,
* since there usually non to very few better vehicle_journeys, this reduces the workload significantly
* We are scanning counterclockwise for arrive_by and clockwise for depart_after.
*/
static void reboard_vehicle_journeys_within_days(router_t *router, router_request_t *req,
jpidx_t jp_index,
jppidx_t jpp_offset,
serviceday_t *prev_serviceday,
jp_vjoffset_t prev_vj_offset,
rtime_t prev_time,
serviceday_t **best_serviceday,
jp_vjoffset_t *best_vj, rtime_t *best_time) {
calendar_t *vj_masks = tdata_vj_masks_for_journey_pattern(router->tdata, jp_index);
vehicle_journey_t *vjs_in_journey_pattern = tdata_vehicle_journeys_in_journey_pattern(router->tdata, jp_index);
journey_pattern_t *jp = &(router->tdata->journey_patterns[jp_index]);
serviceday_t *serviceday;
/* Search service_days in reverse order. (arrive_by low to high, else high to low) */
for (serviceday = prev_serviceday;
serviceday >= router->servicedays;
--serviceday) {
int32_t i_vj_offset;
/* Check that this journey_pattern still has any suitable vehicle_journeys
* running on this day.
*/
if (req->arrive_by ? prev_time < serviceday->midnight + jp->min_time
: prev_time > serviceday->midnight + jp->max_time) continue;
for (i_vj_offset = prev_vj_offset;
req->arrive_by ? i_vj_offset < jp->n_vjs :
i_vj_offset >= 0;
req->arrive_by ? ++i_vj_offset
: --i_vj_offset) {
rtime_t time;
#ifdef RRRR_DEBUG
printBits(4, & (vj_masks[i_vj_offset]));
printBits(4, & (serviceday->mask));
fprintf(stderr, "\n");
#endif
#if RRRR_MAX_BANNED_VEHICLE_JOURNEYS > 0
/* skip this vj if it is banned */
if (set_in_vj (req->banned_vjs_journey_pattern, req->banned_vjs_offset,
req->n_banned_vjs, jp_index,
(jp_vjoffset_t) i_vj_offset)) continue;
#endif
/* skip this vj if it is not running on
* the current service day
*/
if ( ! (serviceday->mask & vj_masks[i_vj_offset])) continue;
/* skip this vj if it doesn't have all our
* required attributes
* Checking whether we have required req->vj_attributes at all, before checking the attributes of the vehicle_journeys
* is about 4% more efficient for journeys without specific vj attribute requirements.
*/
if (req->vj_attributes && ! ((req->vj_attributes & vjs_in_journey_pattern[i_vj_offset].vj_attributes) == req->vj_attributes)) continue;
/* consider the arrival or departure time on
* the current service day
*/
time = tdata_stoptime (router->tdata, serviceday, jp_index, i_vj_offset, jpp_offset, req->arrive_by);
#ifdef RRRR_DEBUG_VEHICLE_JOURNEY
fprintf(stderr, " board option %d at %s \n", i_vj_offset, "");
#endif
/* rtime overflow due to long overnight vehicle_journey's on day 2 */
if (time == UNREACHED) continue;
if (req->arrive_by ? time > prev_time
: time < prev_time){
return;
}
/* Mark vj for boarding if it improves on the last round's
* post-walk time at this stop. Note: we should /not/ be comparing
* to the current best known time at this stop, because it may have
* been updated in this round by another vj (in the pre-walk
* transit phase).
*/
if (req->arrive_by ? time <= prev_time && time > *best_time
: time >= prev_time && time < *best_time) {
*best_vj = i_vj_offset;
*best_time = time;
*best_serviceday = serviceday;
}
} /* end for (vehicle_journey's within this route) */
/* Reset the VJ offset to the highest value, after we scanned the original serviceday */
prev_vj_offset = req->arrive_by ? 0 : jp->n_vjs - 1;
} /* end for (service days: yesterday, today, tomorrow) */
}
static bool
write_state(router_t *router, router_request_t *req,
uint8_t round, jpidx_t jp_index, jp_vjoffset_t vj_offset,
spidx_t sp_index, jppidx_t jpp_offset, rtime_t time,
spidx_t board_stop, jppidx_t board_jpp_offset,
rtime_t board_time) {
uint64_t i_state = ((uint64_t) round) * router->tdata->n_stop_points + sp_index;
#ifndef RRRR_REALTIME
UNUSED (jpp_offset);
UNUSED (board_jpp_offset);
#endif
#ifdef RRRR_INFO
{
char buf[13];
fprintf(stderr, " setting stop_point to %s \n", btimetext(time, buf));
}
#endif
router->best_time[sp_index] = time;
router->states_time[i_state] = time;
router->states_back_journey_pattern[i_state] = jp_index;
router->states_back_vehicle_journey[i_state] = vj_offset;
router->states_ride_from[i_state] = board_stop;
router->states_board_time[i_state] = board_time;
#ifdef RRRR_FEATURE_REALTIME_EXPANDED
router->states_back_journey_pattern_point[i_state] = board_jpp_offset;
router->states_journey_pattern_point[i_state] = jpp_offset;
#endif
#ifdef RRRR_STRICT
if (req->arrive_by && board_time < time) {
fprintf (stderr, "board time non-decreasing\n");
return false;
} else if (!req->arrive_by && board_time > time) {
fprintf (stderr, "board time non-increasing\n");
return false;
}
#else
UNUSED (req);
#endif
return true;
}
static void router_round(router_t *router, router_request_t *req, uint8_t round) {
/* TODO restrict pointers? */
rtime_t *states_walk_time = router->states_walk_time + (((round == 0) ? 1 : round - 1) * router->tdata->n_stop_points);
uint32_t jp_index;
#ifdef RRRR_INFO
fprintf(stderr, "round %d\n", round);
#endif
/* Iterate over all journey_patterns which contain a stop_point that was updated in the last round. */
for (jp_index = bitset_next_set_bit (router->updated_journey_patterns, 0);
jp_index != BITSET_NONE;
jp_index = bitset_next_set_bit (router->updated_journey_patterns, jp_index + 1)) {
journey_pattern_t *jp = &(router->tdata->journey_patterns[jp_index]);
spidx_t *journey_pattern_points = tdata_points_for_journey_pattern(router->tdata, (jpidx_t) jp_index);
uint8_t *journey_pattern_point_attributes = tdata_stop_point_attributes_for_journey_pattern(router->tdata, jp_index);
/* Service day on which that vj was boarded */
serviceday_t *board_serviceday = NULL;
/* vj index within the journey_pattern. NONE means not yet boarded. */
jp_vjoffset_t vj_index = NONE;
/* stop_point index where that vj was boarded */
spidx_t board_sp = 0;
/* journey_pattern_point index where that vj was boarded */
jppidx_t board_jpp = 0;
/* time when that vj was boarded */
rtime_t board_time = 0;
/* Iterate over stop_point indexes within the route. Each one corresponds to
* a global stop_point index. Note that the stop times array should be
* accessed with [vj_index][jpp_offset] not [vj_index][jpp_offset].
*
* The iteration variable is signed to allow ending the iteration at
* the beginning of the route, hence we decrement to 0 and need to
* test for >= 0. An unsigned variable would always be true.
*/
int32_t jpp_offset;
#ifdef FEATURE_OPERATOR_FILTER
if (req->operator != OPERATOR_UNFILTERED &&
req->operator != jp->operator_index) continue;
#endif
#if 0
if (jp_overlap) fprintf (stderr, "min time %d max time %d overlap %d \n", jp->min_time, jp->max_time, jp_overlap);
fprintf (stderr, "journey_pattern %d has min_time %d and max_time %d. \n", jp_index, jp->min_time, jp->max_time);
fprintf (stderr, " actual first time: %d \n", tdata_depart(router->tdata, jp_index, 0, 0));
fprintf (stderr, " actual last time: %d \n", tdata_arrive(router->tdata, jp_index, jp->n_vjs - 1, jp->n_stops - 1));
fprintf(stderr, " journey_pattern %d: %s;%s\n", jp_index, tdata_line_code_for_journey_pattern(router->tdata, jp_index), tdata_headsign_for_journey_pattern(router->tdata, jp_index));
tdata_dump_journey_pattern(router->tdata, jp_index, NONE);
#endif
for (jpp_offset = (req->arrive_by ? jp->n_stops - 1 : 0);
req->arrive_by ? jpp_offset >= 0 :
jpp_offset < jp->n_stops;
req->arrive_by ? --jpp_offset :
++jpp_offset) {
uint32_t sp_index = journey_pattern_points[jpp_offset];
rtime_t prev_time;
bool attempt_board = false;
bool forboarding = (journey_pattern_point_attributes[jpp_offset] & rsa_boarding);
bool foralighting = (journey_pattern_point_attributes[jpp_offset] & rsa_alighting);
#ifdef RRRR_INFO
char buf[13];
fprintf(stderr, " sp %2d [%d] %c%c %s %s\n", jpp_offset,
sp_index,
forboarding ? 'B' : ' ',
foralighting ? 'A' : ' ',
btimetext(router->best_time[sp_index], buf),
tdata_stop_point_name_for_index (router->tdata,
sp_index));
#endif
if (vj_index != NONE &&
/* When currently on a vehicle, skip stops where
* alighting is not allowed at the route-point.
*/
((!forboarding && req->arrive_by) ||
(!foralighting && !req->arrive_by))) {
continue;
} else if (vj_index == NONE &&
/* When looking to board a vehicle, skip stops where
* boarding is not allowed at the route-point.
*/
((!forboarding && !req->arrive_by) ||
(!foralighting && req->arrive_by))) {
continue;
}
#if RRRR_MAX_BANNED_STOP_POINTS_HARD > 0
/* If a stop_point in in banned_stop_points_hard, we do not want to transit
* through this stationwe reset the current vj to NONE and skip
* the currect stop. This effectively splits the journey_pattern in two,
* and forces a re-board afterwards.
*/
if (set_in_sp (req->banned_stop_points_hard, req->n_banned_stop_points_hard,
(spidx_t) sp_index)) {
vj_index = NONE;
continue;
}
#endif
/* If we are not already on a vj, or if we might be able to board
* a better vj on this journey_pattern at this location, indicate that we
* want to search for a vj.
*/
prev_time = states_walk_time[sp_index];
/* Only board at placed that have been reached. */
if (prev_time != UNREACHED) {
if (vj_index == NONE || req->via_stop_point == sp_index) {
attempt_board = true;
} else if (vj_index != NONE && req->via_stop_point != STOP_NONE &&
req->via_stop_point == board_sp) {
attempt_board = false;
} else {
rtime_t vj_stoptime = tdata_stoptime (router->tdata,
board_serviceday,
(jpidx_t) jp_index, vj_index, (jppidx_t) jpp_offset,
req->arrive_by);
if (vj_stoptime == UNREACHED) {