chiark / gitweb /
routesearch: actually print number of routes requested, not one less
[ypp-sc-tools.db-live.git] / yarrg / rssearch.c
index 5ad6136..881d5de 100644 (file)
@@ -38,7 +38,7 @@ static Neighbour *get_neighbours(int isle) {
 }
 
 
-static PotentialResult ***buckets_base[GRANUS];
+static Bucket ***buckets_base[GRANUS];
 
 
 static double process_route(int nports, int totaldist,
@@ -79,8 +79,8 @@ static double process_route(int nports, int totaldist,
       return guess[A];
     }
 
-    if (guess[A] <= highscores[GRANUS-1][A][0].value &&
-       guess[P] <= highscores[GRANUS-1][P][0].value) {
+    if (guess[A] <= highscores[granus-1][A][0].value &&
+       guess[P] <= highscores[granus-1][P][0].value) {
       ctr_routes_quickelim++;
       debugf(" QELIM %f %f\n", guess[A], guess[P]);
       return guess[A];
@@ -93,15 +93,15 @@ static double process_route(int nports, int totaldist,
   int midisle= ports[nports/2];
   int midarch= route2midarch(ports,nports);
 
-  PotentialResult *buckets[GRANUS];
+  Bucket *buckets[GRANUS];
   int granui;
-  for (granui=mingranu; granui<GRANUS; granui++) {
-    PotentialResult **buckets_fin;
+  for (granui=0; granui<granus; granui++) {
+    Bucket **buckets_fin;
     int mid, fin;
     switch (granui) {
-    case 0: fin=finisle; mid=midisle; break;
+    case 0: fin=finarch; mid=midarch; break;
     case 1: fin=finisle; mid=midarch; break;
-    case 2: fin=finarch; mid=midarch; break;
+    case 2: fin=finisle; mid=midisle; break;
     default: abort();
     }
     buckets_fin= ONDEMAND(buckets_base[granui][fin], granusz_mid[granui]);
@@ -109,8 +109,8 @@ static double process_route(int nports, int totaldist,
   }
 
   if (nports>=2) {
-    if (guess[A] <= buckets[mingranu]->value[A] &&
-       guess[P] <= buckets[mingranu]->value[P]) {
+    if (guess[A] <= buckets[0]->prs[A].value[A] &&
+       guess[P] <= buckets[0]->prs[P].value[P]) {
       ctr_routes_bucketelim++;
       debugf(" ELIM %f %f\n", guess[A], guess[P]);
       return guess[A];
@@ -129,39 +129,40 @@ static double process_route(int nports, int totaldist,
     return value[0];
   }
 
-  for (granui=mingranu; granui<GRANUS; granui++) {
-    PotentialResult *bucket= buckets[granui];
+  for (granui=granus-1; granui>=0; granui--) {
+    Bucket *bucket= buckets[granui];
 
-    if (value[A] <= bucket->value[A] &&
-       value[P] <= bucket->value[P])
+    if (value[A] <= bucket->prs[A].value[A] &&
+       value[P] <= bucket->prs[P].value[P])
       continue;
 
     debugf(" SOMEHOW %d BEST\n",granui);
 
-    fildebugf("final %d:%3d mid %d ",finarch,finisle,midarch);
+    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];
+      int nscores= nhighscores[granui][ap];
 
       fildebugf("ap=%d %15f", ap, value[ap]);
-      if (value[ap] < bucket->value[ap]) {
+      if (value[ap] < bucket->prs[ap].value[ap]) {
        debugf("      ");
       } else {
        int pos;
-       ctr_newbests_granu[ap]++;
-       bucket->value[ap]= value[ap];
-       memcpy(bucket->ports[ap], ports, sizeof(*ports) * nports);
-       if (nports < MAX_ROUTELEN-1) bucket->ports[ap][nports]= -1;
+       ctr_newbests_granu[granui*AP+ap]++;
+       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].pr == bucket) goto found;
+       for (pos=0; pos < nscores; pos++)
+         if (scores[pos].bucket == bucket) goto found;
        /* not found */
        pos= -1;
       found:
        for (;;) {
          pos++;
-         if (pos >= *nscores-1) break; /* new top */
+         if (pos >= nscores) break; /* new top */
          if (scores[pos].value >= value[ap]) break; /* found spot */
          if (pos>0)
            scores[pos-1]= scores[pos];
@@ -169,12 +170,20 @@ static double process_route(int nports, int totaldist,
        pos--;
        if (pos>0) {
          scores[pos].value= value[ap];
-         scores[pos].pr= bucket;
+         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", pos);
-      }
-    }
-  }
+       fildebugf("@%2d/%2d ", pos, nscores);
+      } /* new best */
+    } /* ap */
+  } /* granui */
 
   fildebugf(" route");
 
@@ -203,7 +212,7 @@ static void recurse(int last_isle,
 }
 
 void search(int start_isle, int final_isle_spec,
-           PotentialResult ****buckets_base_io[GRANUS]) {
+           Bucket ****buckets_base_io[GRANUS]) {
   int granui;
   for (granui=0; granui<GRANUS; granui++)
     buckets_base[granui]=
@@ -215,7 +224,7 @@ void search(int start_isle, int final_isle_spec,
 
 int nhighscores[GRANUS][AP];
 HighScoreEntry *highscores[GRANUS][AP];
-int mingranu, granusz_fin[GRANUS], granusz_mid[GRANUS];
+int granus=GRANUS, granusz_fin[GRANUS], granusz_mid[GRANUS];
 
 int narches;
 char **archnames;
@@ -254,7 +263,7 @@ void setup_search(void) {
   }
   sqlite3_finalize(archs);
 
-  granusz_fin[0]=                granusz_mid[0]= islandtablesz;
+  granusz_fin[0]=                granusz_mid[0]= narches;
   granusz_fin[1]= islandtablesz; granusz_mid[1]= narches;
-  granusz_fin[2]=                granusz_mid[2]= narches;
+  granusz_fin[2]=                granusz_mid[2]= islandtablesz;
 }