chiark / gitweb /
WIP routesearch; before one query per pair
authorIan Jackson <ian@liberator.(none)>
Sat, 26 Sep 2009 18:01:45 +0000 (19:01 +0100)
committerIan Jackson <ian@liberator.(none)>
Sat, 26 Sep 2009 18:01:45 +0000 (19:01 +0100)
.gitignore
yarrg/Makefile
yarrg/rscommon.h [new file with mode: 0644]
yarrg/rsmain.c [new file with mode: 0644]
yarrg/rsvalue.c [new file with mode: 0644]
yarrg/x.gdb

index a058839..9beb60b 100644 (file)
@@ -9,6 +9,7 @@ yarrg/t.*
 yarrg/u.*
 
 yarrg/yarrg
+yarrg/routesearch
 
 yarrg/_*.*
 yarrg/OCEAN-*.db
index 446ef98..708bfa2 100644 (file)
@@ -33,15 +33,23 @@ CFLAGS += $(WARNINGS) $(WERROR) $(OPTIMISE) $(DEBUG)
 
 TARGETS= yarrg
 
-all: clean-other-directory $(TARGETS)
+default: clean-other-directory $(TARGETS)
+all: default routesearch
 
 CONVERT_OBJS= convert.o ocr.o pages.o structure.o common.o rgbimage.o resolve.o
 
+ROUTESEARCH_OBJS= rsvalue.o rsmain.o
+
 yarrg: $(CONVERT_OBJS) -lnetpbm -lXtst -lX11 -lpcre -lm
        $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LDLIBS)
 
 $(CONVERT_OBJS): ocr.h convert.h structure.h common.h
 
+routesearch:   $(ROUTESEARCH_OBJS)
+       $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LDLIBS)
+
+$(ROUTESEARCH_OBJS): rscommon.h
+
 clean:
        rm -f *.o core core.* *~ vgcore.*
        rm -f t t.* u u.* v v.* *.tmp *.orig *.rej
diff --git a/yarrg/rscommon.h b/yarrg/rscommon.h
new file mode 100644 (file)
index 0000000..658bbdd
--- /dev/null
@@ -0,0 +1,14 @@
+#ifndef RSCOMMON_H
+#define RSCOMMON_H
+
+#include <stdio.h>
+
+#include <sqlite3.h>
+#include <assert.h>
+#include <stdlib.h>
+
+extern struct sqlite *db;
+
+void value_route(int nislands, const int *islands);
+
+#endif /*RSCOMMON_H*/
diff --git a/yarrg/rsmain.c b/yarrg/rsmain.c
new file mode 100644 (file)
index 0000000..00e918e
--- /dev/null
@@ -0,0 +1,13 @@
+/**/
+
+#include "rscommon.h"
+
+int main(int argc, const char **argv) {
+  int ia[argc], ni=0;
+  const char *arg;
+  while ((arg= *++argv)) {
+    ia[ni++]= atoi(arg);
+  }
+  value_route(ni, ia);
+  return 0;
+}
diff --git a/yarrg/rsvalue.c b/yarrg/rsvalue.c
new file mode 100644 (file)
index 0000000..6778523
--- /dev/null
@@ -0,0 +1,54 @@
+/**/
+
+#include "rscommon.h"
+
+void value_route(int nislands, const int *islands) {
+  char stmt[1024+80*nislands+40*nislands*nislands], *sp;
+  int s,d;
+  
+  sp= stmt;
+  sp += sprintf(sp,
+               "SELECT\n"
+               " sell.islandid         src_id,\n"
+               " sell.price            src_price,\n"
+               " sum(sell.qty)         src_qty,\n"
+               " buy.islandid          dst_id,\n"
+               " buy.price             dst_price,\n"
+               " sum(buy.qty)          dst_qty,\n"
+               " commods.commodid      commodid,\n"
+               " commods.unitmass      unitmass,\n"
+               " commods.unitvolume    unitvolume,\n"
+               " dist                  dist,\n"
+               " buy.price-sell.price  unitprofit\n"
+               " FROM commods\n"
+               " JOIN sell ON commods.commodid = sell.commodid\n"
+               " JOIN buy  ON commods.commodid = buy.commodid\n"
+               " JOIN dists ON aiid = sell.islandid AND biid = buy.islandid\n"
+               " WHERE buy.price > sell.price\n"
+               "       AND (");
+  for (s=0; s<nislands; s++) {
+    sp += sprintf(sp, "%s(sell.islandid=%d AND (",
+                 !s ? "" : "\n   OR ",
+                 islands[s]);
+    for (d=s; d<nislands; d++) {
+      sp += sprintf(sp, "%sbuy.islandid=%d",
+                   d==s ? "" : " OR ",
+                   islands[d]);
+    }
+    sp += sprintf(sp, "))");
+  }
+  sp += sprintf(sp,
+               ")\n"
+               " GROUP BY commods.commodid,\n"
+               "       sell.islandid, sell.price,\n"
+               "       buy.islandid, buy.price\n"
+               );
+
+  assert(sp < stmt + sizeof(stmt) - 1);
+
+  printf("SQL\n[\n%s\n]\n", stmt);
+
+  //char *tail;
+  //struct sqlite_vm *sth;
+  //r= sqlite_compile(db, stmt, &tail, &sth, 
+}
index 8336e4e..5d4e663 100644 (file)
@@ -1,5 +1,5 @@
-file yarrg
-set args -Drect 2>u --edit-charset --find-island --ocean sage
+file routesearch
+set args 1 2 3
 #break structure.c:596 if here!=aa_background
-break mustfail2
+#break mustfail2
 run