+ debugf(" %d",ports[i]);
+ debugf("\n");
+
+ double guess[AP]={0,0};
+ if (nports>=2) {
+ int pair[2], i;
+ pair[1]= ports[nports-1];
+ guess[A]= overestimate_excepting_tail;
+
+ for (i=0; i<nports; i++) {
+ pair[0]= ports[i];
+ IslandPair *ip= ipair_get_maybe(pair[0], pair[1]);
+ if (!ip) continue;
+ if (ip->route_tail_value < 0) {
+ ctr_subroute_tails_valued++;
+ ip->route_tail_value= value_route(2, pair, pair[0]!=pair[1]);
+ }
+ guess[A] += ip->route_tail_value;
+ }
+ guess[P]= guess[A] / leagues_divisor;
+
+ if (wrong_final) {
+ ctr_routes_wrongfinalelim++;
+ debugf(" WFELIM\n");
+ return guess[A];
+ }
+
+ for (granui=0; granui<granus; granui++) {
+ if (guess[A] > highscores[granui][A][0].value ||
+ guess[P] > highscores[granui][P][0].value)
+ goto not_quickelim;
+ }
+ ctr_routes_quickelim++;
+ debugf(" QELIM %f %f\n", guess[A], guess[P]);
+ return guess[A];
+ not_quickelim:;
+ }
+
+ int finisle= ports[nports-1];
+ int finarch= isle2arch(finisle);
+
+ int midisle= ports[nports/2];
+ int midarch= route2midarch(ports,nports);
+
+ Bucket *buckets[GRANUS];
+ for (granui=0; granui<granus; granui++) {
+ Bucket **buckets_fin;
+ int mid, fin;
+ switch (granui) {
+ case 0: fin=finarch; mid=midarch; break;
+ case 1: fin=finisle; mid=midarch; break;
+ case 2: fin=finisle; mid=midisle; break;
+ default: abort();
+ }
+ buckets_fin= ONDEMAND(buckets_base[granui][fin], granusz_mid[granui]);
+ buckets[granui]= ONDEMAND(buckets_fin[mid], 1);
+ }
+
+ if (nports>=2) {
+ for (granui=0; granui<granus; granui++)
+ for (ap=0; ap<AP; ap++)
+ if (guess[ap] > buckets[granui]->prs[ap].value[ap] &&
+ guess[ap] > highscores[granui][ap][0].value)
+ goto not_bucketelim;
+ ctr_routes_bucketelim++;
+ debugf(" ELIM %f %f\n", guess[A], guess[P]);
+ return guess[A];
+ not_bucketelim:
+ debugf(" COMPUTE %f %f\n", guess[A], guess[P]);
+ }
+
+ ctr_routes_valued++;
+
+ double value[AP];
+ value[A]= value_route(nports, ports, 0);
+ value[P]= value[A] / leagues_divisor;
+
+ if (wrong_final) {
+ ctr_routes_wrongfinal++;
+ return value[0];
+ }
+
+ for (granui=granus-1; granui>=0; granui--) {
+ Bucket *bucket= buckets[granui];
+
+ if (value[A] <= bucket->prs[A].value[A] &&
+ value[P] <= bucket->prs[P].value[P])
+ continue;
+
+ debugf(" SOMEHOW %d BEST\n",granui);
+
+ fildebugf("granu %d f%d:%3d mid%d:%3d ",granui,
+ finarch,finisle,midarch,midisle);
+
+ for (ap=0; ap<AP; ap++) {
+ HighScoreEntry *scores= highscores[granui][ap];
+ int nscores= nhighscores[granui][ap];
+
+ fildebugf("ap=%d %15f", ap, value[ap]);
+ if (value[ap] < bucket->prs[ap].value[ap]) {
+ debugf(" ");
+ } else {
+ int pos;
+ ctr_newbests_granu[granui*AP+ap]++;
+ bucket->prs[ap].length= totaldist;
+ memcpy(bucket->prs[ap].value, value, sizeof(value));
+ memcpy(bucket->prs[ap].ports, ports, sizeof(*ports) * nports);
+ if (nports < MAX_ROUTELEN-1) bucket->prs[ap].ports[nports]= -1;
+ fildebugf("** ");
+ for (pos=0; pos < nscores; pos++)
+ if (scores[pos].bucket == bucket) goto found;
+ /* not found */
+ pos= -1;
+ found:
+ for (;;) {
+ pos++;
+ if (pos >= nscores) break; /* new top */
+ if (scores[pos].value >= value[ap]) break; /* found spot */
+ if (pos>0)
+ scores[pos-1]= scores[pos];
+ }
+ pos--;
+ if (pos>0) {
+ scores[pos].value= value[ap];
+ scores[pos].bucket= bucket;
+ if (granui < granus-1 &&
+ highscores[granui][A][0].bucket &&
+ highscores[granui][P][0].bucket) {
+ /* both absolute and perleague are full at this granularity,
+ * so we don't care about anything more granular */
+ fildebugf("\n STOP-GRANU ");
+ granus= granui+1;
+ }
+ }
+ fildebugf("@%2d/%2d ", pos, nscores);
+ } /* new best */
+ } /* ap */
+ } /* granui */
+
+ fildebugf(" route");
+
+ for (i=0; i<nports; i++)
+ fildebugf(" %d",ports[i]);
+ fildebugf("\n");
+
+ return value[0];