This repository has been archived by the owner on Sep 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
net2.c
1993 lines (1569 loc) · 58 KB
/
net2.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
// net2.c - precompute 2-stop connections
/*
This file is part of Tripover, a broad-search journey planner.
Copyright (C) 2014-2016 Joris van der Geer.
*/
/* Initialize the network once at startup :
for each dep
for each via1 = direct arr from dep
for each via2 = direct arr from via1
for each arr = direct arr from via2
create a pre-computed 2-stop connectivity network used in search
- Build connectivity matrix between any 2 full ports
derived matrix for each of n intermediate hops
each matrix contains a list of possible trips from port A to port B
the list is limited to a top-n on average trip duration, and trimmed on heuristics such as distance, cost, timing
base matrix for direct (non-stop) hops is prepared in net.c
*/
#include <string.h>
#include <pthread.h>
#include <sched.h>
#include "base.h"
#include "cfg.h"
#include "mem.h"
#include "os.h"
#include "math.h"
static ub4 msgfile;
#include "msg.h"
#include "iadr.h"
#include "util.h"
#include "time.h"
#include "net.h"
#include "net2.h"
#include "netev.h"
#undef hdrstop
static int vrbena;
void ininet2(void)
{
msgfile = setmsgfile(__FILE__);
iniassert();
vrbena = (getmsglvl() >= Vrb);
}
// max number of alternatives per dep,arr pair to consider
#define Durcnt 256
static const ub4 no_airsep = 1; // pending change to un/reserved
static const ub8 sumreach = 1000 * 100; // treat as hub if sum of direct reachable dest above
// heuristic limit for over-route net2 versus direct
static ub4 geodistlim(ub4 dir)
{
ub4 lim = 0;
if (dir <= 100) return dir * 10;
if (dir > 100) { lim += 100 * 10; dir -= 100; }
if (dir > 1000) { lim += 1000 * 5; dir -= 1000; }
if (dir > 10000) { lim += 10000 * 5; dir -= 10000; }
if (dir > 100000) { lim += 100000 * 4; dir -= 100000; }
if (dir > 500000) { lim += 500000 * 3; dir -= 500000; }
lim += dir * 2;
return lim;
}
// 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 ub4 getvarlim(struct port *pdep,struct port *parr,int *hub)
{
ub4 nda = max(pdep->nda,parr->nda);
*hub = 0;
if (nda < globs.net2con[0]) {
if (pdep->sumreach > sumreach || parr->sumreach >sumreach) return globs.net2limit[0];
else return 0;
}
else if (nda < globs.net2con[1]) return globs.net2limit[0];
else if (nda < globs.net2con[2]) return globs.net2limit[1];
else {
*hub = 1;
return globs.net2limit[2];
}
}
/*
mid1:12
mid2:12
v1:8
v2:8
v3:8
dur:16
*/
#define Lst2midbit 12
#define Lst2vbit 8
#define Lst2dbit 16
#define Lst2midcnt (1 << Lst2midbit)
#define Lst2mmask (Lst2midcnt - 1)
#define Lst2m2shift Lst2midbit
#define Lst2vcnt (1 << Lst2vbit)
#define Lst2vmask (Lst2vcnt - 1)
#define Lst2v1shift (Lst2midbit + Lst2midbit)
#define Lst2v2shift (Lst2v1shift + Lst2vbit)
#define Lst2v3shift (Lst2v2shift + Lst2vbit)
#define Lst2durshift (Lst2v3shift + Lst2vbit)
#if Lst2durshift > (64 - 16)
#error "Lst2durshift too large"
#endif
#define Lst2durcnt (1 << Lst2dbit)
// max number of each via stop to consider for net2
#define Viacnt2 Lst2midcnt
struct net2_p2_args {
// in
ub4 tid,tidcnt;
struct network *net;
ub4 depfrom,depto;
ub4 lowcon;
int nice;
ub4 varlimit,var12limit,cntonly;
ub4 distabove_da,distabove_da1;
int net1;
ub4 *depmid1s;
ub4 *depmid1durs;
ub4 *depmid1ds;
ub4 *depmid1cnts;
ub4 *dmid1cnts;
ub8 dmid1sort[Viacnt2];
ub4 *arrmid2s;
ub4 *arrmid2ds;
ub4 *arrmid2cnts;
ub4 *dmid2cnts;
// out
int rv;
ub8 lstlen;
};
// 2-stop net pass 1, runs a departure port set per thread
// count in precnts, durlegs01 and -2d
static int net2_pas1(struct net2_p2_args *argp)
{
ub4 tid = argp->tid;
ub4 tidcnt = argp->tidcnt;
ub4 msgtid;
ub4 *depmid1s = argp->depmid1s;
ub4 *dmid1ds,*depmid1ds = argp->depmid1ds;
ub4 *depmid1cnts = argp->depmid1cnts;
ub4 *dmid1cnts = argp->dmid1cnts;
ub4 *arrmid2s = argp->arrmid2s;
ub4 *dmid2ds,*arrmid2ds = argp->arrmid2ds;
ub4 *arrmid2cnts = argp->arrmid2cnts;
ub4 *dmid2cnts = argp->dmid2cnts;
ub4 *dmid1s,*dmid2s;
ub4 *dmid1acnt,*dmid2acnt;
ub4 dmid1cnt,dmid2cnt;
ub4 varlimlh;
ub4 varlimit = argp->varlimit;
ub4 var12limit = argp->var12limit;
ub4 cntonly = argp->cntonly;
ub4 lowcon = argp->lowcon;
ub4 distabove_da = argp->distabove_da;
ub4 distabove_da1 = argp->distabove_da1;
int net1 = argp->net1;
struct network *net = argp->net;
ub4 portcnt = net->portcnt;
struct port *pdep,*parr,*pmid1,*pmid2,*ports = net->ports;
iadrbase *cnts0,*cnts1,*precnts;
ub4 dep,mid1,mid2,arr;
ub4 dmid1,dmid2;
ub4 cnt,n1,n2,n3,n123;
ub4 dirdist,gdistlim,dirdistdm,dirdistm1a,dirdistma,dirdistm12;
ub4 cntlim,depair,arrair,deparrair;
int ishub;
ub4 seqdep,seqarr,seqm1;
ub4 gseqcnt = net->seqdlen;
ub2 *seqdist = net->seqdist;
ub1 lotx,*seqlotx = net->seqlotx;
ub8 stat_var12lim = 0,stat_distlim1 = 0,stat_distlim2 = 0,stat_distlim3 = 0,stat_distlim4 = 0;
ub8 stat_net1lim = 0,stat_abvdist = 0,stat_locon = 0,stat_lotx = 0;
struct eta eta;
ub8 lstlen = 0,pairs = 0;
ub4 depfrom = argp->depfrom;
ub4 depto = argp->depto;
ports = net->ports;
precnts = &net->conipos;
cnts0 = net->concnt;
cnts1 = net->concnt + 1;
iadrbase *lstitems = &net->lst2items;
if (tidcnt > 1) {
msgtid = (tid << Tidshift) | Tidbit;
info(msgtid,"start in thread %u",tid);
} else msgtid = 0;
// for each departure port
for (dep = depfrom; dep < depto; dep++) {
if (tprogress(tid,tidcnt,&eta,"p1 port %u/%u \ah%lu conns",dep - depfrom,depto - depfrom,lstlen)) return 1;
pdep = ports + dep;
if (pdep->valid == 0) continue;
depair = (pdep->modes & Airbit);
if (depair == 0 && pdep->nda < lowcon && pdep->sumreach < sumreach) { stat_locon++; continue; }
dmid1s = depmid1s + dep * Viacnt2;
dmid1ds = depmid1ds + dep * Viacnt2;
dmid1acnt = depmid1cnts + dep * Viacnt2;
dmid1cnt = dmid1cnts[dep];
if (dmid1cnt == 0) continue;
if (pairs + portcnt >= hi32) {
warn(0,"limiting pairs to \ah%lu at dep %u",pairs,dep);
break;
}
seqdep = pdep->gridseq;
// for each arrival port
for (arr = 0; arr < portcnt; arr++) {
if (arr == dep) continue;
parr = ports + arr;
if (parr->valid == 0) continue;
seqarr = parr->gridseq;
if (net->havelotx && seqdep != seqarr) {
lotx = seqlotx[(ub8)seqdep * gseqcnt + seqarr];
if (lotx > 2) { stat_lotx++; continue; }
}
arrair = (parr->modes & Airbit);
deparrair = depair | arrair;
if (deparrair == 0 && parr->nda < lowcon && parr->sumreach < sumreach) { stat_locon++; continue; }
varlimlh = getvarlim(pdep,parr,&ishub);
error_gt(varlimlh,varlimit,0);
if (varlimlh == 0) continue;
if (cntonly < 256) {
n1 = rdiadr2(cnts0,dep,arr);
if (n1 > cntonly) continue;
n1 += rdiadr2(cnts1,dep,arr);
if (n1 > cntonly) continue;
}
cnt = 0;
dmid2s = arrmid2s + arr * Viacnt2;
dmid2ds = arrmid2ds + arr * Viacnt2;
dmid2acnt = arrmid2cnts + arr * Viacnt2;
dmid2cnt = dmid2cnts[arr];
if (dmid2cnt == 0) continue;
seqarr = parr->gridseq;
dirdist = xgeodist(pdep,parr,seqdep,gseqcnt,seqdist);
if (dirdist < distabove_da) { stat_abvdist++; continue; }
else if (dirdist < distabove_da1) {
if ( !(pdep->modes & Walkbit) || !(parr->modes & Walkbit) ) continue;
}
gdistlim = geodistlim(dirdist);
// for each via1
for (dmid1 = 0; cnt < varlimlh && dmid1 < dmid1cnt; dmid1++) {
mid1 = dmid1s[dmid1];
if (mid1 == arr) continue;
dirdistdm = dmid1ds[dmid1];
if (dirdistdm > gdistlim) {
stat_distlim1++;
// info(Notty|Noiter|msgtid,"dir %u distdm %u above %u %s - %s",dirdist,dirdistdm,gdistlim,pdep->name,parr->name);
continue;
}
mid1 = dmid1s[dmid1];
if (mid1 == arr) continue;
if (net1) {
n1 = rdiadr2(cnts1,mid1,arr);
if (n1 == 0) { stat_net1lim++; continue; }
}
pmid1 = ports + mid1;
seqm1 = pmid1->gridseq;
dirdistm1a = xgeodist(pmid1,parr,seqm1,gseqcnt,seqdist);
if (dirdistdm + dirdistm1a > gdistlim) {
stat_distlim2++;
// info(Notty|Noiter|msgtid,"dir %u distdma %u above %u %s - %s",dirdist,dirdistdm + dirdistm1a,gdistlim,pdep->name,parr->name);
continue;
}
// if (deparrair == 0 && ishub == 0 && midair == 0 && dirdistm1a < distabove_da) continue;
if (dirdistm1a < distabove_da) continue;
n1 = dmid1acnt[dmid1];
error_z(n1,dmid1);
// for each via2
for (dmid2 = 0; cnt < varlimlh && dmid2 < dmid2cnt; dmid2++) {
mid2 = dmid2s[dmid2];
dirdistma = dmid2ds[dmid2];
if (dirdistdm + dirdistma > gdistlim) {
stat_distlim3++;
// info(Notty|Noiter|msgtid,"dir %u distdm %u above %u %s - %s",dirdist,dirdistdm + dirdistma,gdistlim,pdep->name,parr->name);
continue;
}
mid2 = dmid2s[dmid2];
if (mid2 == mid1 || mid2 == dep) continue;
n3 = dmid2acnt[dmid2];
error_z(n3,dmid2);
n2 = rdiadr2(cnts0,mid1,mid2);
if (n2 == 0) continue;
n2 = min(n2,Lst2vmask);
pmid2 = ports + mid2;
dirdistm12 = xgeodist(pmid1,pmid2,seqm1,gseqcnt,seqdist);
if (dirdistdm + dirdistm12 + dirdistma > gdistlim) {
stat_distlim4++;
// info(Notty|Noiter|msgtid,"dir %u distdm %u above %u %s - %s",dirdist,dirdistdm + dirdistm12,gdistlim,pdep->name,parr->name);
continue;
}
n123 = n1 * n2 * n3;
if (n123 > var12limit) { n123 = var12limit; stat_var12lim++; }
cnt += n123;
} // each mid2
} // each mid1
cntlim = min(cnt,varlimlh);
if (cntlim) {
if (iadrinc(precnts,dep,arr,tid)) return info(Ret1,"dep %u arr %u",dep,arr);
iadrincn(lstitems,dep,arr * varlimit,cntlim,tid);
lstlen += cntlim;
pairs++;
}
} // each arr
} // each dep
argp->lstlen = lstlen;
info(msgtid,"limits dist abv \ah%lu dm \ah%lu da \ah%lu m2a \ah%lu dma \ah%lu ",
stat_abvdist,stat_distlim1,stat_distlim2,stat_distlim3,stat_distlim4);
info(msgtid," net1 \ah%lu var \ah%lu locon \ah%lu lotx \ah%lu",stat_net1lim,stat_var12lim,stat_locon,stat_lotx);
return info(msgtid,"tid %u 2-stop pass 1 \ah%lu tentative triplets",tid,lstlen);
} // 2-stop pass 1
static void *net2_pass1(void *vp)
{
struct net2_p2_args *argp = (struct net2_p2_args *)vp;
ub4 tid = argp->tid;
globs.tids[tid] = 1;
ub4 msgtid = (tid << Tidshift) | Tidbit;
if (argp->nice) {
ossetprio(argp->nice);
info(msgtid,"nice value %d",argp->nice);
}
int rv = net2_pas1(argp);
argp->rv = rv;
globs.tids[tid] = 0;
return vp;
}
// 2-stop net pass 2, runs a departure port set per thread
// result counts stored in cnts, triplets in durlegs01 and -2d
static int net2_pas2(struct net2_p2_args *argp)
{
ub4 tid = argp->tid;
ub4 tidcnt = argp->tidcnt;
ub4 msgtid;
ub4 *depmid1s = argp->depmid1s;
ub4 *depmid1durs = argp->depmid1durs;
ub4 *dmid1ds,*depmid1ds = argp->depmid1ds;
ub4 *depmid1cnts = argp->depmid1cnts;
ub4 *dmid1cnts = argp->dmid1cnts;
ub8 x8,*dmid1sort = argp->dmid1sort;
ub4 *arrmid2s = argp->arrmid2s;
ub4 *dmid2ds,*arrmid2ds = argp->arrmid2ds;
ub4 *arrmid2cnts = argp->arrmid2cnts;
ub4 *dmid2cnts = argp->dmid2cnts;
ub4 *dmid1s,*dmid2s,*dmid1durs;
ub4 *dmid1acnt,*dmid2acnt;
ub4 dmid1cnt,sdmid1cnt,dmid2cnt;
ub4 varlimlh;
ub4 varlimit = argp->varlimit;
ub4 var12limit = argp->var12limit;
ub4 distabove_da = argp->distabove_da;
ub4 distabove_da1 = argp->distabove_da1;
ub4 distabove_da10 = distabove_da * 10;
ub4 cntonly = argp->cntonly;
ub4 lowcon = argp->lowcon;
int net1 = argp->net1;
struct network *net = argp->net;
ub4 portcnt = net->portcnt;
ub4 thopcnt = net->thopcnt;
ub4 whopcnt = net->whopcnt;
struct port *pdep,*parr,*pmid1,*pmid2,*ports = net->ports;
struct chop *hp2,*hp3,*hops = net->chops;
block *lstblk0;
ub4 *portsbyhop;
iadrbase *cnts0,*cnts1,*precnts,*cnts;
iadrbase *conofs0;
ub8 ofs1,ofs2,ofs3;
ub4 *conlst1,*lst1,*lst2,*lst3;
ub4 *hopdist = net->hopdist;
ub1 leg123mode,*hopmode = net->hopmodes;
ub4 air,aircnt;
ub4 lodur1,lodur2,lodur3;
ub4 rid1;
ub4 dep,mid1,mid2,arr;
ub4 dmid1,sdmid1,dmid2;
ub4 n1,n2,n3,n123,altcnt,v1,v2,v3,leg1,leg2,leg3;
ub8 lstlen;
ub4 dist1,dist2,dist3;
ub4 dirdist,gdistlim,dirdistdm,dirdistm1a,dirdistma,dirdistm12;
ub4 wxw,depair,arrair,deparrair;
ub4 sumwalkdist1,sumwalkdist2,sumwalkdist3;
ub4 cntlimdur,gen,outcnt;
int ishub;
ub8 timlim;
ub4 n,midur,durndx,durcnt,durlim,durlim1,durcntgnd,durlimgnd,durdist;
ub4 dtlo,dthi,typdt,dtcnt;
ub4 midurs[Durcnt],midursgnd[Durcnt];
ub4 sortdurs[Durcnt];
ub4 tmpmodes[Durcnt];
ub8 tmpitem,tmpitems[Durcnt],tmpitemsgnd[Durcnt];
ub4 sumwalklimit = net->sumwalklimit;
ub4 stat_partlimdur = 0;
ub8 stat_noestdur = 0,stat_estdur = 0;
ub8 stat_var12lim = 0,stat_distlim1 = 0,stat_distlim2 = 0,stat_distlim3 = 0,stat_distlim4 = 0;
ub8 stat_net1lim = 0,stat_timlim = 0,stat_timlim2 = 0,stat_lotx = 0,stat_varlim = 0;
ub8 stat_da1 = 0,stat_da2 = 0,stat_da3 = 0;
ub4 hialt = 0,altdep = 0,altarr = 0;
ub8 nogen = 0,sumoutcnt = 0;
struct eta eta;
int cmd;
lstlen = 0;
ub4 depfrom = argp->depfrom;
ub4 depto = argp->depto;
ub4 da_timlim = globs.net2timlim;
ub4 altlim,da_altlim = globs.net2altlim;
ports = net->ports;
portsbyhop = net->portsbyhop;
precnts = &net->conipos;
conofs0 = net->conofs;
cnts0 = net->concnt;
cnts1 = net->concnt + 1;
cnts = net->concnt + 2;
lstblk0 = net->conlst;
conlst1 = blkdata(lstblk0,0,ub4);
iadrbase *durlims1 = net->durlims + 1;
iadrbase *lstitems = &net->lst2items;
ub4 hindx,hidur;
ub4 seqdep,seqarr,seqm1;
ub4 gseqcnt = net->seqdlen;
ub2 *seqdist = net->seqdist;
ub1 lotx,*seqlotx = net->seqlotx;
if (tidcnt > 1) {
msgtid = (tid << Tidshift) | Tidbit;
info(msgtid,"start in thread %u",tid);
} else msgtid = 0;
for (dep = depfrom; dep < depto; dep++) {
pdep = ports + dep;
cmd = tprogress(tid,tidcnt,&eta,"p2 port %4u/%u \ah%lu con %17s",dep - depfrom,depto - depfrom,lstlen,pdep->prefix);
switch(cmd) {
case 0: break;
case 1: return 2;
case 2: info(0,"time limit from %u to %u",da_timlim,da_timlim / 10);
da_timlim /= 10;
break;
default: break;
}
if (pdep->valid == 0) continue;
depair = (pdep->modes & Airbit);
if (depair == 0 && pdep->nda < lowcon && pdep->sumreach < sumreach) continue;
dmid1s = depmid1s + dep * Viacnt2;
dmid1durs = depmid1durs + dep * Viacnt2;
dmid1ds = depmid1ds + dep * Viacnt2;
dmid1acnt = depmid1cnts + dep * Viacnt2;
dmid1cnt = dmid1cnts[dep];
if (dmid1cnt == 0) continue;
outcnt = 0;
if (sumoutcnt + portcnt >= hi32) {
warn(msgtid,"limiting pairs to \ah%lu at dep %u of %u",sumoutcnt,dep,portcnt);
break;
}
seqdep = pdep->gridseq;
// for each arrival port
for (arr = 0; arr < portcnt; arr++) {
if (arr == dep) continue;
parr = ports + arr;
if (parr->valid == 0) continue;
seqarr = parr->gridseq;
if (net->havelotx && seqdep != seqarr) {
lotx = seqlotx[(ub8)seqdep * gseqcnt + seqarr];
if (lotx > 2) { stat_lotx++; continue; }
}
arrair = (parr->modes & Airbit);
deparrair = depair | arrair;
if (deparrair == 0 && parr->nda < lowcon && parr->sumreach < sumreach) continue;
dmid2s = arrmid2s + arr * Viacnt2;
dmid2ds = arrmid2ds + arr * Viacnt2;
dmid2acnt = arrmid2cnts + arr * Viacnt2;
dmid2cnt = dmid2cnts[arr];
if (dmid2cnt == 0) continue;
if (cntonly < 256) {
n1 = rdiadr2(cnts0,dep,arr);
if (n1 > cntonly) continue;
n1 += rdiadr2(cnts1,dep,arr);
if (n1 > cntonly) continue;
}
varlimlh = getvarlim(pdep,parr,&ishub);
if (varlimlh == 0) { stat_varlim++; continue; }
error_gt(varlimlh,varlimit,arr);
timlim = da_timlim;
altlim = da_altlim;
if (ishub || deparrair) {
timlim *= 4;
altlim *= 10;
}
cntlimdur = min(varlimlh,Durcnt-1);
dirdist = xgeodist(pdep,parr,seqdep,gseqcnt,seqdist);
// if (deparrair == 0 && ishub == 0) {
if (dirdist < distabove_da) { stat_distlim1++; continue; }
else if (dirdist < distabove_da1) {
if ( (pdep->modes & Walkbit) && (parr->modes & Walkbit) ) wxw = 1;
else { stat_distlim1++; continue; }
} else wxw = 0;
// } else wxw = 0;
gdistlim = geodistlim(dirdist);
gen = 0;
// start with limits derived from previous nstop
// prepare sort on time.
if (dirdist < 10) { durlim = 60; timlim /= 500; }
else if (dirdist < 1 * 100) { durlim = 2 * 60; timlim /= 500; }
else if (dirdist < 10 * 100) { durlim = 12 * 60; timlim /= 200;}
else if (dirdist < 100 * 100) { durlim = 1 * 1440; timlim /= 40; }
else if (dirdist < 1000 * 100) { durlim = 4 * 1440; timlim /= 10; }
else if (dirdist < 10000 * 100) durlim = 8 * 1440;
else durlim = 14 * 1440;
durlimgnd = durlim;
timlim = max(timlim,1);
setthtimer(tid,(ub4)timlim);
if (net1) durlim1 = rdiadr2(durlims1,dep,arr);
else durlim1 = 0;
if (durlim1 == 0) durlim1 = hi16;
if (durlim1 != hi16) durlim1 = (durlim1 * 3) / 2 + dirdist / 100 + 30;
nsethi(midurs,Durcnt);
nsethi(midursgnd,cntlimdur);
// create time top-n list, derive threshold
altcnt = 0;
durcnt = 0;
durcntgnd = 0;
// sort and filter via1 on distance to arr
sdmid1cnt = 0;
for (dmid1 = 0; dmid1 < dmid1cnt; dmid1++) {
mid1 = dmid1s[dmid1];
if (mid1 == arr) continue;
pmid1 = ports + mid1;
seqm1 = pmid1->gridseq;
dirdistm1a = xgeodist(pmid1,parr,seqm1,gseqcnt,seqdist);
// if (deparrair == 0 && ishub == 0 && midair == 0 && dirdistm1a < distabove_da) continue;
if (dirdistm1a < distabove_da) { stat_distlim2++; continue; }
if (dirdistm1a > gdistlim) { stat_distlim3++; continue; }
durdist = dirdistm1a + dmid1durs[dmid1] * 50;
dmid1sort[sdmid1cnt++] = ((ub8)durdist << 32) | dmid1;
}
if (sdmid1cnt * dmid2cnt > 64 * 64) fsort8(dmid1sort,sdmid1cnt,1,FLN,"dmid1");
// each via1
for (sdmid1 = 0; sdmid1 < sdmid1cnt; sdmid1++) {
x8 = dmid1sort[sdmid1];
dirdistm1a = (ub4)(x8 >> 32);
dmid1 = x8 & hi32;
error_ge(dmid1,dmid1cnt);
dirdistdm = dmid1ds[dmid1];
if (dirdistdm > gdistlim) { stat_distlim4++; continue; }
mid1 = dmid1s[dmid1];
if (mid1 == arr) continue;
if (net1) {
n1 = rdiadr2(cnts1,mid1,arr);
if (n1 == 0) { stat_net1lim++; continue; }
}
if (dirdist < distabove_da10 && altcnt > altlim) break;
if (gen && isexpired(tid)) {
stat_timlim++;
if (dirdist >= distabove_da10) {
stat_timlim2++;
info(msgtid,"timeout %u ms at dist \ag%u dep %u arr %u %s",(ub4)timlim,dirdist,dep,arr,pdep->prefix);
info(msgtid," %s - %s",pdep->name,parr->iname);
}
break;
}
pmid1 = ports + mid1;
if (dirdistdm + dirdistm1a > gdistlim) continue;
n1 = dmid1acnt[dmid1];
ofs1 = rdiadr8(conofs0,dep,mid1);
seqm1 = pmid1->gridseq;
// each via2
for (dmid2 = 0; dmid2 < dmid2cnt; dmid2++) {
dirdistma = dmid2ds[dmid2];
if (dirdistdm + dirdistma > gdistlim) continue;
mid2 = dmid2s[dmid2];
if (mid2 == mid1 || mid2 == dep) continue;
n2 = rdiadr2(cnts0,mid1,mid2);
if (n2 == 0) continue;
n2 = min(n2,Lst2vmask);
pmid2 = ports + mid2;
dirdistm12 = xgeodist(pmid1,pmid2,seqm1,gseqcnt,seqdist);
if (dirdistdm + dirdistm12 + dirdistma > gdistlim) continue;
n3 = dmid2acnt[dmid2];
n123 = 0;
ofs2 = rdiadr8(conofs0,mid1,mid2);
ofs3 = rdiadr8(conofs0,mid2,arr);
error_eq(ofs1,hi32);
error_eq(ofs2,hi32);
error_eq(ofs3,hi32);
lst1 = conlst1 + ofs1;
lst2 = conlst1 + ofs2;
lst3 = conlst1 + ofs3;
bound(lstblk0,ofs1,ub4);
bound(lstblk0,ofs2,ub4);
bound(lstblk0,ofs3,ub4);
stat_da1++;
// each dep-via1-via2-arr alternative
for (v1 = 0; v1 < n1 && n123 < var12limit; v1++) {
leg1 = lst1[v1];
if (leg1 == hi32) {
error(msgtid,"invalid trip %u-%u-%u-%u",dep,mid1,mid2,arr);
break;
}
error_ge(leg1,whopcnt);
if (portsbyhop[leg1 * 2] != dep) {
info(msgtid,"hop %u %u-%u vs %u-%u",leg1,portsbyhop[leg1 * 2],portsbyhop[leg1 * 2 + 1],dep,mid1);
continue;
}
lodur1 = hops[leg1].lodur;
dist1 = hopdist[leg1];
if (dist1 > gdistlim) continue;
air = (hopmode[leg1] & Airbit) | no_airsep;
if (lodur1 > (air ? durlim : durlimgnd)) continue;
if (leg1 >= thopcnt) sumwalkdist1 = dist1;
else {
if (wxw) continue; // only walk-any-walk below a given dist
sumwalkdist1 = 0;
}
rid1 = hops[leg1].rid;
stat_da2++;
for (v2 = 0; v2 < n2 && n123 < var12limit; v2++) {
leg2 = lst2[v2];
hp2 = hops + leg2;
if (rid1 == hp2->rid && hp2->tripstart == 0) continue;
lodur2 = lodur1 + hops[leg2].lodur;
dist2 = dist1 + hopdist[leg2];
if (dist2 > gdistlim) continue;
sumwalkdist2 = sumwalkdist1;
if (leg2 >= thopcnt) {
if (leg1 >= thopcnt) continue;
sumwalkdist2 += hopdist[leg2];
}
if (sumwalkdist2 > sumwalklimit) continue;
stat_da3++;
for (v3 = 0; v3 < n3 && n123 < var12limit; v3++) {
n123++;
leg3 = lst3[v3];
hp3 = hops + leg3;
if (hp2->rid == hp3->rid && (hp2->tripend == 0 || hp3->tripstart == 0)) continue;
leg123mode = hopmode[leg1] | hopmode[leg2] | hopmode[leg3];
air = (leg123mode & Airbit) | no_airsep;
lodur3 = lodur2 + hops[leg3].lodur;
if (lodur3 > (air ? durlim : durlimgnd)) continue;
dist3 = dist2 + hopdist[leg3];
if (dist3 > gdistlim) continue;
if (leg3 >= thopcnt) {
if (leg2 >= thopcnt) continue;
sumwalkdist3 = sumwalkdist2 + hopdist[leg3];
if (sumwalkdist3 > sumwalklimit) continue;
} else if (wxw) continue; // only walk-any-walk below a given dist
// maintain top-n list with corresponding triplet
// insertion sort
dtcnt = estdur_3(net,leg1,leg2,leg3,&dtlo,&dthi,&typdt);
midur = typdt;
if (dtcnt == 0) {
stat_noestdur++;
// warn(msgtid,"no estdur for %u-%u-%u",leg1,leg2,leg3);
continue;
} else if (midur > Lst2durcnt) {
warn(0,"durlim %u exceeds %u",midur,Lst2durcnt);
continue;
}
altcnt++;
stat_estdur++;
if (no_airsep && midur > durlim) continue;
if (midur > (air ? durlim : durlimgnd)) continue;
tmpitem = (ub8)dmid1 | ((ub8)dmid2 << Lst2m2shift);
tmpitem |= ((ub8)v1 << Lst2v1shift) | ((ub8)v2 << Lst2v2shift) | ((ub8)v3 << Lst2v3shift);
tmpitem |= ((ub8)midur << Lst2durshift);
// store in both mixed mode and ground-only. select part of each if needed lateron
if (durcnt == 0) {
tmpitems[0] = tmpitem;
tmpmodes[0] = leg123mode;
midurs[0] = midur;
warncc(midur != hi32 && midur > 65534,Exit|msgtid,"durlim %u at 0 of %u exceeds 64k",midur,cntlimdur);
durcnt = 1;
gen++;
} else if (cntlimdur < varlimit && midur > durlim1 && durcnt >= cntlimdur / 2) { // second half must be better than net1
durlim = min(durlim,durlim1);
continue;
} else if (durcnt < cntlimdur) {
warncc(midur != hi32 && midur > 65534,Exit|msgtid,"durlim %u at %u of %u exceeds 64k",midur,durcnt,cntlimdur);
tmpitems[durcnt] = tmpitem;
tmpmodes[durcnt] = leg123mode;
midurs[durcnt++] = midur;
gen++;
} else {
hidur = hindx = 0;
for (durndx = 0; durndx < cntlimdur; durndx++) {
if (midurs[durndx] > hidur) {
hidur = midurs[durndx];
hindx = durndx;
}
warncc(midurs[durndx] != hi32 && midurs[durndx] > 65534,Exit|msgtid,"durlim %u at %u of %u exceeds 64k",midurs[durndx],durndx,cntlimdur);
}
if (midur < hidur) {
tmpitems[hindx] = tmpitem;
tmpmodes[hindx] = leg123mode;
midurs[hindx] = midur;
}
durlim = hidur;
warncc(durlim != hi32 && durlim > 65534,Exit|msgtid,"durlim %u at %u exceeds 64k",durlim,cntlimdur);
error_eq(durlim,hi32);
}
if (no_airsep || air) continue;
// ground-only
// todo: change into reserved versus unreserved to accomodate fare availability
if (durcntgnd == 0) {
midursgnd[0] = midur;
tmpitemsgnd[0] = tmpitem;
durcntgnd = 1;
} else if (durcntgnd < cntlimdur) {
tmpitemsgnd[durcntgnd] = tmpitem;
midursgnd[durcntgnd++] = midur;
} else {
hidur = hindx = 0;
for (durndx = 0; durndx < cntlimdur; durndx++) {
if (midursgnd[durndx] > hidur) { hidur = midursgnd[durndx]; hindx = durndx; }
}
durlimgnd = hidur;
error_eq(durlimgnd,hi32);
if (midur < hidur) {
midursgnd[hindx] = midur;
tmpitemsgnd[hindx] = tmpitem;
}
}
} // each v3
} // each v2
} // each v1
if (n123 == var12limit) stat_var12lim++;
} // each mid2
} // each mid1
if (altcnt > hialt) { hialt = altcnt; altdep = dep; altarr = arr; }
error_gt(durcnt,varlimlh,dep);
if (durcnt == cntlimdur) stat_partlimdur++;
else if (durcnt > cntlimdur) {
warn(msgtid,"durlim %u above varlimit %u/%u",durlim,cntlimdur,varlimit);
durcnt = cntlimdur;
}
warncc(durlim != hi32 && durlim > 65534,Exit|msgtid,"durlim %u exceeds 64k",durlim);
warncc(durlimgnd != hi32 && durlimgnd > 65534,Exit|msgtid,"durlim %u exceeds 64k",durlimgnd);
if (durcnt == 0) { nogen++; continue; }
iadrinc(cnts,dep,arr,tid);
outcnt++;
// emit mixed tmpitems only, or best half of tmpitems and -gnd if needed
aircnt = 0;
for (n = 0; n < durcnt; n++) {
if (tmpmodes[n] & Airbit) aircnt++;
}
if (no_airsep || aircnt * 2 <= cntlimdur) { // mixed only
gen = durcnt;
for (n = 0; n < durcnt; n++) {
if (midurs[n] == hi32) { warn(0,"dep %u arr %u no midur at %u",dep,arr,n); break; }
tmpitem = tmpitems[n];
if (wriadr8(lstitems,dep,arr * varlimit + n,tmpitem)) break;
}
if (n < durcnt) continue;
if (wriadr2(precnts,dep,arr,(ub2)n)) continue;
lstlen += n;
continue;
}
// best half of each
for (n = 0; n < durcnt; n++) sortdurs[n] = (midurs[n] << 16) | n;
sort4(sortdurs,durcnt,FLN,"net2 midurs");
n = gen = 0;
while (n < durcnt && gen < cntlimdur / 2) {
hindx = sortdurs[gen] & hi16;
error_ge(hindx,durcnt);
tmpitem = tmpitems[hindx];
if (midurs[hindx] == hi32) { warn(0,"dep %u arr %u no midur at %u",dep,arr,n); n = 0; break; }
if (wriadr8(lstitems,dep,arr * varlimit + gen,tmpitem)) { n = 0; break; }
gen++;
}
if (n == 0) continue;
lstlen += gen;
if (durcntgnd == 0) {
wriadr2(precnts,dep,arr,(ub2)gen);
continue;
}
for (n = 0; n < durcntgnd; n++) sortdurs[n] = (midursgnd[n] << 16) | n;
sort4(sortdurs,durcntgnd,FLN,"net2 midurs");
n = 0;
while (n < durcntgnd && gen < cntlimdur) {
hindx = sortdurs[n] & hi16;
error_ge(hindx,durcntgnd);
tmpitem = tmpitemsgnd[hindx];
if (midursgnd[hindx] == hi32) { warn(0,"dep %u arr %u no midur at %u",dep,arr,n); n = 0; break; }