-static void ipair_gettrades(int si, int di) {
- char *stmt= masprintf
- ("SELECT\n"
- " sell.price src_price,\n"
- " sum(sell.qty) src_qty,\n"
- " buy.price dst_price,\n"
- " sum(buy.qty) dst_qty,\n"
- " commods.commodid commodid,\n"
- " commods.unitmass unitmass,\n"
- " commods.unitvolume unitvolume\n"
- " FROM commods\n"
- " JOIN sell ON commods.commodid = sell.commodid\n"
- " JOIN buy ON commods.commodid = buy.commodid\n"
- " WHERE buy.price > sell.price\n"
- " AND sell.islandid=%d\n"
- " AND buy.islandid=%d\n"
- " GROUP BY commods.commodid, sell.price, buy.price\n",
- si, di);
+//DEBUG_DEFINE_SOME_DEBUGF(sql,sql_dprintf);
+
+typedef struct {
+ int commodid, src_price, src_qty, dst_price, dst_qty;
+} Trade;
+
+#define TRADES_PER_BLOCK 10
+
+typedef struct TradesBlock{
+ struct TradesBlock *next;
+ Trade t[TRADES_PER_BLOCK];
+} TradesBlock;
+
+typedef struct {
+ int ntrades;
+ TradesBlock *trades;
+} IslandPair;
+
+int nislands=100;
+IslandPair ***ipairs; /* ipairs[sislandid][dislandid] */
+
+static IslandPair *ipair_get(int si, int di) {
+ IslandPair *ip, **ipa;
+
+ assert(si < nislands);
+ assert(di < nislands);
+
+ if (!(ipa= ipairs[si])) {
+ ipa= ipairs[si]= mcalloc(sizeof(*ipa) * nislands);
+ }
+ if ((ip= ipa[di]))
+ return ip;