This repository was archived by the owner on Sep 11, 2022. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathsearch.c
More file actions
5833 lines (4707 loc) · 160 KB
/
Copy pathsearch.c
File metadata and controls
5833 lines (4707 loc) · 160 KB
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
// search.e - core trip planning
/*
This file is part of Tripover, a broad-search journey planner.
Copyright (C) 2014-2017 Joris van der Geer.
*/
/* perform the actual trip search.
see Transit directions at global scale, Jan 2016' http://arxiv.org/abs/1601.03633
*/
// !start
#include <string.h>
#include "base.h"
#include "cfg.h"
#include "mem.h"
#include "time.h"
#include "math.h"
#include "util.h"
#include "os.h"
static ub4 msgfile;
#include "msg.h"
#include "iadr.h"
#include "net.h"
#undef hdrstop
#ifdef __clang__
#pragma clang diagnostic ignored "-Wunused-variable"
#pragma clang diagnostic ignored "-Wunreachable-code"
#endif
#ifdef __GNUC__
#pragma GCC diagnostic ignored "-Wunused-variable"
#endif
// set to nonzero to include tracking, 0 for production
#define Track 0
#include "search.h"
// max number of vias in dyn1
#define Dyn1_midlim 4096
// max number of vias in dyn2
#define Dyn2_midlim 2048
// max number of vias in dyn3
#define Dyn3_mid1lim 4096
#define Dyn3_midlim 1024
static const ub4 altlimit2 = 1024 * 8; // todo
static const ub4 var1lim2 = 256;
static const ub4 altlimit3 = 1024 * 2;
// aimed number of departures for auto departure timespan
static const ub4 autodepdy = 3;
static const ub4 autodephr = 3;
static const ub4 autodepmax0 = 4 * 20 * 3;
static bool time_virtual = 0;
static const ub4 Maxevs = 4 * 1024;
static int vrbena;
static const char plancfgname[] = "plan";
struct srcplan {
ub4 ndx;
ub4 nleg1,nleg2,nleg3,nleg4; // designates dynx search on netn net
ub4 tlim; // scaled time to run
ub4 tuse; // scaled max spare time
ub4 p0,p1;
ub4 sort;
ub4 run;
ub4 cost;
};
// as above, init version
struct csrcplan {
ub4 nleg1,nleg2,nleg3,nleg4;
ub4 tlim;
ub4 tuse,sort;
};
static struct srcplan plan3[Planlen];
static struct srcplan plan2[Planlen];
static struct srcplan plan1[Planlen];
static struct srcplan plan0[Planlen];
enum evfld {
timefld,
tidfld,
durfld,
sdurfld,
costfld,
prvfld,
farefld,
sdafld,
#if Track
flnfld,
#endif
fldcnt
};
static ub4 costleads[6 << 9];
static const ub2 evmagic1 = 0xbdc9;
static const ub2 evmagic2 = 0x695c;
static struct srcnet {
struct network *net;
ub4 portcnt;
} sn;
static ub8 gstat_evcnt,gstat_lkcnt;
void inisrc(gnet *net,search *src,const char *desc,ub4 arg)
{
ub4 i,leg,t;
ub2 *ev,*evbase;
struct trip *stp;
ub4 portcnt = net->portcnt;
sn.net = net;
error_ovf(Maxevs,ub2);
fmtstring(src->desc,"%s %u",desc,arg);
src->costlim = hi32;
src->lodist = hi32;
src->lodt = hi32;
src->reslen = 0;
src->ntrip = 0;
src->de_prvhop = hi32;
for (t = 0; t < Elemcnt(src->trips); t++) {
stp = src->trips + t;
stp->len = 0;
stp->cost = hi32;
for (i = 0; i < Nleg; i++) {
stp->trip[i] = hi32;
stp->port[i] = hi32;
}
}
#if 0
if (src->evpool == NULL) {
info(0,"alloc new %u event pool",Maxevs);
evbase = src->evpool = alloc(Nleg * (Maxevs + 2) * fldcnt,ub2,0,"src events",Maxevs);
} else evbase = src->evpool;
ev = evbase;
for (leg = 0; leg < Nleg; leg++) { // add a redzone around each leg
ev = evbase + leg * (Maxevs + 2) * fldcnt;
*ev = evmagic1;
src->depevs[leg] = ev + fldcnt;
ev += (Maxevs + 1) * fldcnt;
*ev = evmagic2;
}
if (ev >= evbase + Nleg * (Maxevs + 2) * fldcnt) error(Exit,"ev %p above %p",(void *)ev,(void *)(evbase + Nleg * (Maxevs + 2) * fldcnt));
#endif
if (src->infconns) return;
src->infconns = alloc(portcnt,ub1,0,"src infcon",0);
src->infdaconns = alloc(portcnt * Nleg,ub4,0,"src infcon",0);
}
static void timelimit(search *src,ub4 slimit,const char *desc,int rel)
{
ub4 limit = max(slimit,1);
ub8 t1 = limit;
if (rel) t1 += gettime_msec();
else t1 += src->queryt0;
vrb0(0,"set timeout at %u * %u = %u msec for %s",slimit,src->limitfac,limit,desc);
src->querytlim = t1;
src->tlim = limit;
src->limitdesc = desc;
}
static int timeout(ub4 fln,search *src,struct srcplan *sp,ub4 arg1,ub4 arg2)
{
if (isalarm() == 0) return 0;
infofln(fln,0,"src.%u timeout at %u ms %u/%u",__LINE__,src->tlim,arg1,arg2);
src->tlim = 0;
if (sp) {
sp->p0 = arg1;
sp->p1 = arg2;
}
return 1;
}
static void fmtsum(ub4 fln,struct trip *stp,ub4 dt,ub4 nxt,ub4 dist,ub4 fare,ub4 lodt,const char *ref)
{
ub4 len = (ub4)sizeof(stp->desc);
ub4 pos = mysnprintf0(stp->desc,0,len,"#item\ttime\tdistance\tstops\tfare\tref\n");
if (nxt == 0) nxt = hi32;
pos += mysnprintf(stp->desc,pos,len,"sum\t\at%u\t\ag%u\t%u\t%u\t\at%u\t%s-%u-%u\n",dt,dist,stp->len - 1,fare,nxt,ref,fln & hi16,lodt);
stp->desclen = pos;
}
// heuristic limit for over-route versus direct
static ub4 geodistlim(ub4 dir)
{
if (dir < 100) return dir * 8;
else if (dir < 1000) return dir * 7;
else if (dir < 10000) return dir * 6;
else if (dir < 100000) return dir * 5;
else if (dir < 500000) return dir * 4;
else return dir * 3;
}
// geographical direct line distance from grid or direct
static ub4 xgeodist(struct port *pdep,struct port *parr,ub4 seqdep,ub8 gseqcnt,ub2 *seqdist)
{
ub4 dir;
if (seqdist && (pdep->gridlat + 1 < parr->gridlat || pdep->gridlon + 1 < parr->gridlon
|| parr->gridlat + 1 < pdep->gridlat || parr->gridlon + 1 < pdep->gridlon) ) {
dir = (ub4)seqdist[(ub8)seqdep * gseqcnt + parr->gridseq] << Gridshift;
} else dir = fgeodist(pdep,parr,1);
return dir;
}
static int chkdev(ub2 *dev,ub4 leg)
{
if (dev == NULL) return errorfln(FLN,0,0,"leg %u nil dev",leg);
ub2 *d1 = dev - fldcnt;
ub2 *d2 = dev + Maxevs * fldcnt;
if (*d1 != evmagic1) return errorfln(FLN,0,0,"leg %u magic1 mismatch %x at %p",leg,*d1,(void *)d1);
else if (*d2 != evmagic2) return errorfln(FLN,0,0,"leg %u magic2 mismatch %x at %p",leg,*d2,(void *)d2);
else return 0;
}
#if 0
// get suitable departure window given ev frequency
// based on leg with lowest frequency around deptime
static ub4 getdepwin(search *src,lnet *net,ub4 *legs,ub4 nleg)
{
ub4 deptmin = src->deptmin;
// ub4 winmax = src->deptmin;
ub4 gt0 = net->t0;
ub4 hrt0,rt0 = deptmin - gt0;
ub1 *covhrp,*covhr = net->covhr;
ub2 *covdyp,*covdyp0,*covdy = net->covdy;
ub8 rnghr = net->rnghr;
ub4 rngdy = net->rngdy;
ub4 leg,hop;
struct chop *hp,*hp0,*hops = net->chops;
ub1 *modes = net->hopmodes;
ub4 chopcnt = net->chopcnt;
ub4 depwin,hidepwin = 0,hidhleg = 0;
ub4 days,hours,sumhr,sumdy,sumdy0;
ub4 covdyndx,covdyndx0,covhrndx,covdy0,covhr0;
error_z(nleg,0);
hop = legs[0];
for (leg = 0; leg < nleg; leg++) {
hop = legs[leg];
if (hop < chopcnt && modes[hop] != Taxibit) break;
}
hp0 = hops + hop;
covdyp0 = covdy + hp0->mapofsdy;
hrt0 = rt0;
for (leg = 0; leg < nleg; leg++) {
hop = legs[leg];
hp = hops + hop;
if (hop >= chopcnt || modes[hop] == Taxibit) { // walk/taxi link
hrt0 += hp->lodur;
continue;
}
covhrp = covhr + hp->mapofshr;
covdyp = covdy + hp->mapofsdy;
covhrndx = covhr0 = hrt0 / 60;
covdyndx = covdy0 = covhr0 / 24;
// count days until desired min #events reached, limited by leg0 events
sumdy = sumhr = sumdy0 = 0;
covdyndx0 = rt0 / 60;
covdyndx0 /= 24;
while (sumdy < autodepdy && covdyndx < rngdy && covdyndx0 < rngdy && sumdy0 < autodepmax0) {
sumdy += covdyp[covdyndx++];
sumdy0 += covdyp0[covdyndx0++];
}
while (sumdy == 0 && covdyndx < rngdy) sumdy += covdyp[covdyndx++];
days = covdyndx - covdy0;
if (days > 2) {
depwin = days * 24 + 12;
if (depwin > hidepwin) {
hidepwin = depwin;
hidhleg = leg | (days << 8);
}
// infocc(dbg,Noiter,"hop %u days %u dycnt %u evs %u at \ad%u",hop,days,sumdy,hp->evcnt,hrt0 + gt0);
hrt0 += hp->lodur;
continue;
}
while (sumhr < autodephr && covhrndx < rnghr) sumhr += covhrp[covhrndx++];
hours = covhrndx - covhr0;
// infocc(dbg,Noiter,"hop %u days %u cnt %u hours %u cnt %u evs %u at \ad%u",hop,days,sumdy,hours,sumhr,hp->evcnt,hrt0 + gt0);
depwin = hours;
if (depwin > hidepwin) {
hidepwin = depwin;
hidhleg = leg | (days << 8) | (hours << 16);
}
hrt0 += hp->lodur;
}
leg = hidhleg & 0xff;
hop = legs[leg];
src->dwleg = leg;
src->dwhop = hop;
src->dwday = (hidhleg >> 8) & 0xff;
src->dwhr = hidhleg >> 16;
return hidepwin + 1;
}
#endif
#if Track
#define track(d,v) (d)[flnfld] = (v)
#else
#define track(d,v)
#endif
#define Hitime(dt,df) (ub2)((dt) + ((df) >> 4))
#define Hifare(dt,df) (ub2)((df) + ((dt) >> 4))
#if 0
// example
%generate mode=Srclotime,Srchitime dofare=0,1
static ub4 mkdepevs(search *src,lnet *net,ub4 hop,ub4 costlim,int dbg)
{
ub4 deptmin = src->deptmin;
%mode=Srchitime cost = hitime
x
%mode=Srclotime cost = lotime
y
%dofare=1 cost += fare
}
rv = mkdepevs(%mode,%dofare,a,b,c) && return 1;
#endif
// %line
/*
step 1:
search first few evs for each leg, using first ev or preceding leg and lodur + mintt
step 2:
starting from lowest freq:
for each previous leg, for each ev fill about 5 preceding evs with basic info
for each next leg, for each ev fill about 5 preceding evs with basic info
step 3
compose trips and assign cost
step 4
if not enough trips, extend more: step 2
no sdeparr offsets: consider only smaller grouplimit
depwin : user specifies travel window, possibly including arrival window
use events outside window if none within and curcost high
src effort level and curcost determine #events to consider
autodep no longer sensible : default flexdays to 3 ?
'metro mode' if span equal and short : forward only
*/
// get first handful of evs in depwin, report timespan. store tentatively
#if 0
/* find tf start pos
daymap : ofs 36 64 bits
50m chops * 200d = 80gb
acc 6b next day 6b next week
index into hop[warptab]
*/
#define hrwarp 60
// struct chop *hp,ub4 tf,ub4 t,ub4 ei,ub8 x
%expand locate(net,hp,tf,t,ei,x)
{
{
ub4 tofs;
ub8 dx,*dmp = net->daymap + hp->dayofs;
ub4 evcnt = hp->evcnt;
ub4 zei,dayno;
const ub4 day = 1440;
const ub8 rel = 1UL << 63;
ub8 *ev = net->cevents + hp->evofs;
error_ge(ei,evcnt);
x = ev[ei];
t = x & Timlim;
if (hp->haveacc) {
while(ei < evcnt && t < tf) {
if (tf - t >= day) {
ei += x; /* dywarp */
} else if (x) { /* rel */
dayno = x; /* dur */
dx = dmp[dayno];
zei = net->reltab[dx]; /* ofs */
tofs = dx; /* tofs */
do {
if (tf - t >= hrwarp) {
zei += 1; /* x.hrwarp; */
}
x = ev[zei];
t = x & Timlim;
t += tofs;
} while( (x & rel) && t < tf);
} else {
if (tf - t >= hrwarp) {
ei += x; /* hrwarp */
}
x = ev[++ei];
t = x & Timlim;
}
}
} else { /* no acc -> no compress */
do {
x = ev[ei];
t = x & Timlim;
} while(t < tf && ++ei < evcnt);
}
}
}
#endif
#if 0
struct evsrc {
ub4 span;
ub4 ts[Nsrcev];
ub4 durs[Nsrcev];
};
// find first few evs from given start. return first
static ub4 firstevs(search *src,ub4 leg,struct chop *hp,ub4 t0,struct evsrc *ep)
{
ub4 lodur = hp->lodur;
ub4 t,tf;
ub4 span;
locate(tday0,dt,t,gndx,cnt24)
span = t - tf;
ep->span = span;
return tf;
}
#define Nsrcev 16
static ub4 evalevs(search *src,ub4 *legs,ub4 nleg,ub4 costlim,ub4 *ts,ub4 *durs)
{
}
static ub4 srcevs(search *src,lnet *net,ub4 *legs,ub4 nleg,ub4 costlim)
{
struct chop *hp,*ahp,*hops = net->chops;
ub4 deptmin = src->deptmin;
ub4 *mintts = src->mintts;
ub4 gt0 = net->t0;
ub4 t0 = deptmin - gt0;
struct evsrc evsrcs[Nleg];
struct evsrc *ep;
ub4 leg,h;
ub4 dur;
ub4 tf = t0;
ahp = hops + legs[0];
for (leg = 0; leg < nleg; leg++) {
h = legs[leg];
ahp = hp;
hp = hops + h;
ep = evsrcs + leg;
if (hp->hasfrq) {
tf = firstevs(src,leg,hp,tf,ep);
tf += getmintt(mintts,ahp->kind,hp->kind);
} else tf += hp->lodur;
span = ep->span;
if (span > hispan) {
hispan = span; hileg = leg;
}
if (span < lospan) {
lospan = span; loleg = leg;
}
}
if (lospan * 2 > hispan) { // comparable freqs: no need to fill
return evalevs(src,net,nleg,evsrcs);
}
do {
while (hileg) {
span = bckevs(src,hileg,costlim);
hileg--;
}
while (hileg < nleg1) {
span = fwdevs(src,hileg,costlim);
hileg++;
}
evalevs(src,net,nleg,evsrcs);
} while (dcnt < 4);
/*
*/
return 0;
}
#else
static ub4 gs_useacc;
static ub4 locate_acc(ub4 fln,struct route *rp,ub8 *ev,ub4 evcnt,ub4 tf,ub4 *pei,ub8 *px,ub8 *pprvx)
{
ub8 prvx,x;
ub4 t;
ub4 acc,iacc;
ub4 ei = 0;
ub2 *acc1tab = rp->acc1tab;
ub4 acc1len = rp->acc1len;
x = prvx = ev[0];
t = x & Timlim;
if (tf <= t) {
*pei = 0;
*px = x;
*pprvx = 0;
return t;
}
while (t + Acc1iv < tf) {
acc = (x >> Acc1shift) & Acc1lim;
// if (acc >= acc1len) fatalfln(FLN,0,fln,"rid %u ev %u/%u acc %u ge %u",rp->rid,ei,evcnt,acc,acc1len);
iacc = acc1tab[acc];
ei += iacc;
if (ei >= evcnt) break;
x = ev[ei];
t = x & Timlim;
}
while (t < tf && ei + 1 < evcnt) {
x = ev[++ei];
t = x & Timlim;
}
if (ei >= evcnt) {
prvx = ev[evcnt - 1];
t = prvx & Timlim;
} else if (ei) prvx = ev[ei-1];
*pprvx = prvx;
*px = x;
return t;
}
// return first time and pos at or after tf. caller checks range
static ub4 locate(ub4 fln,struct route *rp,ub8 *ev,ub4 evcnt,ub4 tf,ub4 *pei,ub8 *px,ub8 *pprvx)
{
ub8 prvx,x;
ub4 t;
ub4 ei;
if (gs_useacc) return locate_acc(fln,rp,ev,evcnt,tf,pei,px,pprvx);
ei = 0;
gstat_lkcnt++;
x = ev[0];
do {
prvx = x;
x = ev[ei];
t = x & Timlim;
} while(t < tf && ++ei < evcnt);
gstat_evcnt += ei;
*px = x;
*pprvx = prvx;
*pei = ei;
return t;
}
static ub4 getres(search *src,lnet *net)
{
struct chop *hp,*hops = net->chops;
ub4 altpos = src->altpos;
ub4 chopcnt = net->chopcnt;
struct srcres *rp = src->curres + altpos;
struct srcxres *xp = src->curxres + altpos;
struct trip *tp = src->trips;
struct route *rtp,*routes = net->routes;
ub4 h,l,nleg,*legs;
ub4 evcnt,ei;
ub4 t,prvt,td;
ub4 rid;
ub4 gt0 = net->t0;
ub4 gt1 = net->t1;
ub4 dur;
ub4 dtid,tid;
ub2 srda,srdep,srarr;
ub2 *srdap,*srdalst = net->evsrdas;
ub8 x,prvx,*ev,*events = net->cevents;
ub4 cost = src->costlim;
info(0,"getres pos %u d%u %u-%u-%u-%u %u-%u cost %u",altpos,xp->ndyn,xp->nleg0,xp->nleg1,xp->nleg2,xp->nleg3,xp->i0,xp->i1,cost);
nleg = rp->nleg;
if (nleg == 0) return warn(0,"nil result for pos %u",altpos);
t = rp->td[0];
for (l = 0; l < nleg; l++) {
h = rp->hop[l];
prvt = t;
t = rp->td[l];
if (t == 0) return hopmsg(Warn,0,h,"leg %u/%u rt 0 at \ad%u",l,nleg,gt0);
else if (t > gt1) return hopmsg(Warn,0,h,"leg %u/%u rt %u above \ad%u",l,nleg,t,gt1);
else if (t < prvt) return hopmsg(Warn,0,h,"leg %u/%u t %u before %u \ad%u < \ad%u",l,nleg,t,prvt,t + gt0,prvt + gt0);
hp = hops + h;
if (h < chopcnt) {
rtp = routes + hp->rid;
ev = events + hp->evofs;
evcnt = hp->evcnt;
td = locate(FLN,rtp,ev,evcnt,t,&ei,&x,&prvx);
if (td != t) return hopmsg(Warn,0,h,"t \ad%u not found at leg %u: \ad%u",t + gt0,l,td + gt0);
} else {
td = t;
ei = 0;
x = rp->x[l];
}
rid = hp->rid;
if (rid >= net->ridcnt) hopmsg(Warn,0,h,"leg %u has invalid rid %u",l,rid);
rtp = routes + rid;
dur = (x >> Durshift) & Durlim;
dtid = (x >> Dtidshift) & Dtidlim;
tid = rtp->lochain + dtid;
if (h < chopcnt && tid >= rtp->hichain) hopmsg(Warn,0,h,"leg %u rid %u has invalid tid %u above %u, dtid 0x%x",l,rid,tid,rtp->hichain,dtid);
if (hp->varsda) {
srdap = srdalst + hp->sdaofs;
srda = srdap[ei];
srdep = srda >> 8;
srarr = srda & 0xff;
} else {
srdep = hp->srdep;
srarr = hp->srarr;
}
tp->trip[l] = h;
tp->dur[l] = dur;
tp->t[l] = t + gt0;
tp->tid[l] = tid;
tp->srdep[l] = (srdep == 0xff ? hi16 : srdep);
tp->srarr[l] = (srarr == 0xff ? hi16 : srarr);
}
tp->len = nleg;
src->ntrip = 1;
return 1;
}
static ub4 srcevs1(ub4 fln,search *src,lnet *net,ub4 h,ub4 costlim)
{
struct chop *hp,*prvhp,*hops = net->chops;
struct port *pdep,*ports = net->ports;
struct route *rtp,*routes = net->routes;
ub4 chopcnt = net->chopcnt;
ub4 deptmin = src->deptmin;
ub4 deptmax = src->deptmax;
ub8 x1,x2,prvx,*ev,*ev2,*events = net->cevents;
struct srcres *rp,*res = src->res;
ub4 evcnt,evcnt2,l,prvh;
ub4 t1,t2,t22,dur,dur2,dt,prvt1,prvt2,t2e,t1b;
ub4 tdep,tarr,cost;
ub4 dtndx,dtcnt = 0;
ub4 gt0 = net->t0;
ub4 th,tf = deptmin - gt0;
ub4 ei1 = 0,ei2 = 0;
ub4 td = 0,ta;
ub4 eiarr,ei = 0;
ub4 rescnt,ri = 0;
ub8 x;
if (h >= chopcnt) { errorfln(FLN,0,fln,"leg 0 hop %u >= %u",h,chopcnt); return costlim; }
hp = hops + h;
evcnt = hp->evcnt;
if (evcnt == 0) return costlim;
rtp = routes + hp->rid;
ev = events + hp->evofs;
rp = res;
tdep = locate(FLN,rtp,ev,evcnt,tf,&ei,&x,&prvx);
if (tdep < tf) return costlim;
rp->td[0] = tdep;
rp->hop[0] = h;
dur = (x >> Durshift) & Durlim;
ta = tdep + dur;
cost = ta - tdep;
if (cost >= costlim) return costlim;
rp->cost = cost;
rp->nleg = 1;
src->locost = cost;
src->lores = 0;
src->rescnt = ri;
return cost;
}
static ub4 srcevs(ub4 fln,search *src,lnet *net,ub4 *legs,ub4 *predurs,ub4 nleg,ub4 costlim)
{
struct chop *hp,*prvhp,*hops = net->chops;
struct port *pdep,*ports = net->ports;
struct route *rtp,*routes = net->routes;
ub4 chopcnt = net->chopcnt;
ub4 deptmin = src->deptmin;
ub4 deptmax = src->deptmax;
ub4 ttdurs[Nleg];
ub8 x1,x2,prvx,*ev,*ev2,*events = net->cevents;
struct srcres *rp,*res = src->res;
ub4 evcnt,evcnt2,l,h,prvh;
ub4 t1,t2,t22,dur,dur2,dt,prvt1,prvt2,t2e,t1b;
ub4 tdep,tarr,cost;
ub4 dtndx,dtcnt = 0;
ub4 gt0 = net->t0;
ub4 th,tf = deptmin - gt0;
ub4 ei1 = 0,ei2 = 0;
ub4 ttmin;
ub4 td = 0,ta;
ub4 eiarr,ei = 0;
ub4 rescnt,ri = 0;
ub8 x;
if (nleg > Nleg) { errorfln(FLN,0,fln,"nleg %u above max %u",nleg,Nleg); return costlim; }
else if (nleg == 0) { errorfln(FLN,0,fln,"nil nlegs"); return costlim; }
else if (nleg == 1) { errorfln(FLN,0,fln,"nleg 1"); return costlim; }
h = legs[0];
if (h >= chopcnt) { errorfln(FLN,0,fln,"leg 0 hop %u >= %u",h,chopcnt); return costlim; }
hp = hops + h;
evcnt = hp->evcnt;
if (evcnt == 0) return costlim;
rtp = routes + hp->rid;
ev = events + hp->evofs;
rp = res;
tdep = locate(FLN,rtp,ev,evcnt,tf,&ei,&x,&prvx);
if (tdep < tf) return costlim;
rp->td[0] = tdep;
rp->hop[0] = h;
dur = (x >> Durshift) & Durlim;
ta = tdep + dur;
/* ---\
...---\
mtt ...---
mtt \
*/
for (l = 1; l < nleg; l++) {
prvh = h;
prvhp = hp;
h = legs[l];
hp = hops + h;
rtp = routes + hp->rid;
ttdurs[l] = predurs[l] + prvhp->hidur;
ttmin = predurs[l];
tf = ta + ttmin;
ev = events + hp->evofs;
evcnt = hp->evcnt;
if (evcnt == 0) return costlim;
td = locate(FLN,rtp,ev,evcnt,tf,&ei,&x,&prvx);
if (td < tf) return costlim;
rp->td[l] = td;
rp->x[l] = x;
rp->hop[l] = h;
dur = (x >> Durshift) & Durlim;
ta = td + dur;
}
tarr = ta;
if (tarr < tdep) {
warn(0,"tarr \ad%u below tdep \ad%u",tarr + gt0,tdep + gt0);
return costlim;
}
l = nleg - 1;
#if 1
// adjust backward for later initial departures
while (l) {
prvh = h;
prvhp = hp;
h = legs[l];
hp = hops + h;
rtp = routes + hp->rid;
th = td - min(ttdurs[l],td); // latest allowed
l--;
if (th <= rp->td[l]) continue;
ev = events + hp->evofs;
evcnt = hp->evcnt;
if (evcnt == 0) break;
td = locate(FLN,rtp,ev,evcnt,th,&ei,&x,&prvx);
if (td < th) break;
else if (td <= rp->td[l]) continue;
else if (td > th) {
td = prvx & Timlim;
rp->x[l] = prvx;
} else rp->x[l] = x;
if (td <= rp->td[l]) continue;
rp->td[l] = td;
}
if (l == 1 && tdep > td) tdep = td;
#endif
// for reserved transport legs, search for alternatives earlier
if (tarr < tdep) {
warn(0,"tarr \ad%u below tdep \ad%u",tarr + gt0,tdep + gt0);
return costlim;
}
cost = tarr - tdep;
// adjust for #transfers and walk dist
if (cost >= costlim) return cost;
rp->cost = cost;
rp->nleg = (ub2)nleg;
src->locost = cost;
src->lores = 0;
src->rescnt = ri;
return cost;
}
#endif
// create list of candidate events for first leg
static ub4 mkdepevs(search *src,lnet *net,ub4 hop,ub4 costlim,int dbg)
{
ub4 deptmin = src->deptmin;
ub4 deptmax = src->deptmax;
ub4 udeptmax = src->udeptmax;
ub4 dcnt = 0,lodev,lodev2,gencnt;
ub4 gndx,dmax = Maxevs;
ub4 t,dur;
ub2 *dev,*devp;
ub2 srda;
ub2 dtid;
struct chop *chp,*chops = net->chops;
struct port *pdep,*ports = net->ports;
ub4 *portsbyhop = net->portsbyhop;
ub4 chopcnt = net->chopcnt;
ub8 cx,*cev,*cevents = net->cevents;
ub4 cx2,*cev2,*cevents2 = net->cevents2;
// ub1 *srarrp,*srarrs = net->evsrarrs;
ub4 ofs;
ub2 fare;
ub2 *farepos,*fareposbase = net->fareposbase;
ub4 cost = 0,locost;
ub4 gt0 = net->t0;
enum srcmodes mode = src->srcmode;
src->dcnts[0] = src->dcnts[1] = 0;
src->hops[0] = src->hops[1] = hi32;
src->nleg = 0;
if (hop >= chopcnt) return error(Ret0,"unexpected walk link %u on initial leg",hop);
chp = chops + hop;
pdep = ports + portsbyhop[hop * 2];
gencnt = chp->evcnt;
ub4 t0 = chp->t0;
ub4 t1 = chp->t1;
ub4 extracost;
// srarrp = srarrs + chp->sdaofs;
infocc(dbg,0,"leg 0 hop %u evs %u tt range \ad%u-\ad%u, dep window \ad%u-\ad%u %s",hop,gencnt,t0,t1,deptmin,deptmax,pdep->iname);
if (gencnt == 0) return info(Notty,"no events for hop %u at \ad%u - \ad%u",hop,t0,t1);
if (t0 == t1) return info(Iter|Notty,"hop %u tt range %u-%u, dep window %u-%u",hop,t0,t1,deptmin,deptmax);
locost = hi32;
cev = cevents + chp->evofs;
cev2 = cevents2 + chp->evofs;
dev = src->depevs[0];
if (chkdev(dev,0)) return 0;
// if out of departure window, take a single event
if (t0 > deptmax) {
gencnt = 1;
info(Notty|Iter,"hop %u \ad%u - \ad%u after dep window \ad%u - \ad%u",hop,t0,t1,deptmin,deptmax);
} else if (t1 <= deptmin) {
info(Notty|Iter,"hop %u \ad%u - \ad%u before dep window \ad%u - \ad%u",hop,t0,t1,deptmin,deptmax);
return 0;
} else if (deptmin < gt0 || deptmax <= gt0) return info(Notty,"dep window \ad%u - \ad%u before net star \ad%u",deptmin,deptmax,gt0);
deptmin -= gt0;
deptmax -= gt0;
ub4 lodur = chp->lodur;
if (chp->reserve && net->fhopofs) {
ofs = net->fhopofs[hop];
farepos = fareposbase + ofs * Faregrp;
} else farepos = NULL;
fare = chp->fare;
t = chp->t0 - gt0;
lodev = lodev2 = hi32;
for (gndx = 0; gndx < gencnt; gndx++) {
cx = cev[gndx];
t = cx & Timlim;
if (t < deptmin) continue;
else if (t >= deptmax) {
if (gencnt > 1) break;
if (udeptmax) extracost = t - deptmax;
else extracost = 0;
} else extracost = 0;
// add minor bias to prefer times around given date/time
#if 0
if (t < deptmid) {
deptbias = deptmid - t;
extracost += min(deptbias,1440) / 50;
if (deptbias > 1440) extracost += (deptbias - 1440) / 200;
} else if (t - deptmid > 180) {
deptbias = t - deptmid;
extracost += min(deptbias,1440) / 200;
if (deptbias > 1440) extracost += (deptbias - 1440) / 400;
}
#endif
dur = (ub4)(cx >> Durshift) & Durlim;
if (farepos) {
fare = farepos[gndx * Faregrp];
if (fare == hi16) continue;
}