chiark / gitweb /
routetrade: Do not instantiate useless island pairs
authorIan Jackson <ian@liberator.relativity.greenend.org.uk>
Sun, 4 Oct 2009 22:44:33 +0000 (23:44 +0100)
committerIan Jackson <ian@liberator.relativity.greenend.org.uk>
Sun, 4 Oct 2009 23:22:42 +0000 (00:22 +0100)
yarrg/rscommon.h
yarrg/rssearch.c
yarrg/rsvalue.c

index d91765e83b4b8ed0d5426897a69fc784d499dc9a..965b6db7205413286a3ba65a2cf7966809a915cb 100644 (file)
@@ -81,7 +81,7 @@ typedef struct {
   double route_tail_value;
 } IslandPair;
 
-IslandPair *ipair_get(int si, int di);
+IslandPair *ipair_get_maybe(int si, int di);
 
 double value_route(int nislands, const int *islands, int exclude_arbitrage);
 void setup_value(void);
index eb40a47fbdeba5cb144118e53fc40b9de099ad82..9404cb2619e764e9d914b7872d1e34589a05d67a 100644 (file)
@@ -53,7 +53,8 @@ static double process_route(int nports, int totaldist,
     
     for (i=0; i<nports; i++) {
       pair[0]= ports[i];
-      IslandPair *ip= ipair_get(ports[nports-2], ports[nports-1]);
+      IslandPair *ip= ipair_get_maybe(ports[nports-2], ports[nports-1]);
+      if (!ip) continue;
       if (ip->route_tail_value < 0)
        ip->route_tail_value= value_route(2, pair, pair[0]!=pair[1]);
       guess_absolute += ip->route_tail_value;
index c3fac1a01f9260c0812ea58c0f0bffe23f395fe0..d2190dc289a9a3cc4879333da794b4c9c5f1b691 100644 (file)
@@ -118,7 +118,17 @@ static void add_leg_c(int startrow, int leg, double value) {
   add_constraint(startrow+leg, value);
 }
 
-IslandPair *ipair_get(int si, int di) {
+IslandPair *ipair_get_maybe(int si, int di) {
+  IslandPair **ipa;
+
+  assert(si < islandtablesz);
+  assert(di < islandtablesz);
+
+  if (!(ipa= ipairs[si])) return 0;
+  return ipa[di];
+}
+
+static IslandPair *ipair_get_create(int si, int di) {
   IslandPair *ip, **ipa;
 
   assert(si < islandtablesz);
@@ -218,9 +228,9 @@ double value_route(int nislands, const int *islands, int exclude_arbitrage) {
 
       /*----- actually add these trades to the LP problem -----*/
       
-      IslandPair *ip= ipair_get(islands[s], islands[d]);
+      IslandPair *ip= ipair_get_maybe(islands[s], islands[d]);
 
-      if (!ip->trades)
+      if (!ip || !ip->trades)
        goto next_d;
 
       double loss_factor= delay_slot_loss_factor * ip->distance_loss_factor;
@@ -317,7 +327,7 @@ static void read_trades(void) {
 
   SQL_DISTINCT_DECL(cols,5);
   while (SQL_DISTINCT_STEP(ss_trades,cols,5)) {    
-    IslandPair *ip= ipair_get(cols[1], cols[3]);
+    IslandPair *ip= ipair_get_create(cols[1], cols[3]);
     TradesBlock *block= ip->trades;
     if (!block || ip->trades->ntrades >= TRADES_PER_BLOCK) {
       block= mmalloc(sizeof(*block));