chiark / gitweb /
routesearch: allow constraining destination island
[ypp-sc-tools.db-live.git] / yarrg / rscommon.h
1 #ifndef RSCOMMON_H
2 #define RSCOMMON_H
3
4 #include <sqlite3.h>
5
6 #define DEBUG_FLAG_LIST                         \
7    DF(sql)                                      \
8    DF(sql2)                                     \
9    DF(value)                                    \
10    DF(search)                                   \
11    DF(filter)                                   \
12    DF(check)                                    \
13    DF(lp)
14
15 #define debug stdout
16 #define DEBUG_DEV "/dev/stdout"
17
18 #include "common.h"
19
20
21 #define COUNTER_LIST                            \
22    CTR(commodities_loaded)                      \
23    CTR(trades_loaded)                           \
24    CTR(islands_arbitrage)                       \
25    CTR(ipairs_relevant)                         \
26    CTR(quantities_loaded)                       \
27    CTR(routes_considered)                       \
28    CTR(routes_wrongfinalelim)                   \
29    CTR(routes_quickelim)                        \
30    CTR(routes_stratelim)                        \
31    CTR(routes_valued)                           \
32    CTR(routes_wrongfinal)                       \
33    CTR(newbests_strat_absolute)                 \
34    CTR(newbests_strat_perleague)                \
35    CTR(subroute_tails_valued)                   \
36    CTR(subroutes_valued)                        \
37    CTR(subroutes_nonempty)
38 #define CTR(x) extern int ctr_##x;
39   COUNTER_LIST
40 #undef CTR
41
42 #define SQL_MUST( call ) ({                                              \
43     /* `call' is an expression returning result, using  const char *sqe; \
44      * chk1 and chk2 are blocks using sqe and  int sqr; */               \
45     const char *sql_must_call_string= #call;                             \
46     int sqr;                                                             \
47     if (DEBUGP(sql2)) fprintf(stderr,"SQL %s", sql_must_call_string);    \
48     sqr= (call);                                                         \
49     if (DEBUGP(sql2)) fprintf(stderr," = %d\n", sqr);                    \
50     if (sqr) sql_fatal("(unknown)", sqr, sql_must_call_string);          \
51   })                                                                     \
52
53 void sql_fatal(const char *stmt_what, int sqr, const char *act_what) NORET;
54
55 #define SQL_STEP(ssh) (sql_step((ssh), #ssh, __FILE__, __LINE__))
56 int sql_step(sqlite3_stmt *ssh, const char *ssh_string,
57              const char *file, int line);
58
59 #define SQL_DISTINCT_DECL(cols, nintcols)       \
60   int cols[nintcols];                           \
61   cols[0]= -1;
62 #define SQL_DISTINCT_STEP(ssh, cols, nkeycols)                           \
63   (sql_step_distinct((ssh), #ssh, __FILE__, __LINE__,                    \
64                      (cols), sizeof((cols))/sizeof((cols)[0]), nkeycols))
65 int sql_step_distinct(sqlite3_stmt *ssh, const char *ssh_string,
66                       const char *file, int line,
67                       int *cols, int ncols, int nkeycols);
68    /* These work if we're making a query whose columns consist of:
69     *  - keys: integer column(s) on which the results are sorted by the query
70     *  - consequences: zero or more integer cols strictly dependent on the keys
71     *  - extra: zero or more further (possibly non-integer) columns
72     *
73     * Call SQL_DISTINCT_DECL, passing intcols = the total number of keys and
74     * consequences; it will declare  int cols[intfields];
75     *
76     * Then each SQL_DISTINCT_STEP is like SQL_STEP only you have to
77     * pass the number of key columns and it only returns rows with
78     * distinct keys.  Rows with all-identical keys are asserted to
79     * have identical consequences.  After each call to
80     * SQL_DISTINCT_STEP the keys and consequences will be stored in
81     * cols.
82     */
83
84 int sql_single_int(const char *stmt);
85
86 #define SQL_PREPARE(ss,stmt) ((ss)= sql_prepare((stmt),#ss))
87 sqlite3_stmt *sql_prepare(const char *stmt, const char *what);
88
89 #define SQL_BIND(ss,index,value) (sql_bind((ss),(index),(value),#ss,#value))
90 void sql_bind(sqlite3_stmt *ss, int index, int value,
91               const char *ss_what, const char *val_what);
92
93 #define MAX_ROUTELEN 20
94
95 extern sqlite3 *db;
96
97 void setup_sql(void);
98
99
100 typedef struct {
101   double distance_loss_factor;
102   struct TradesBlock *trades;
103   double route_tail_value;
104 } IslandPair;
105
106 IslandPair *ipair_get_maybe(int si, int di);
107
108 double value_route(int nislands, const int *islands, int exclude_arbitrage);
109 void setup_value(void);
110
111 typedef struct {
112   double absolute, perleague;
113   int absolute_ports[MAX_ROUTELEN], perleague_ports[MAX_ROUTELEN];
114 } PotentialResult;
115
116 void setup_search(void);
117 void search(int start_isle, int final_isle /* -1 means any */,
118             PotentialResult ****strat_base_io
119                 /* strat_base[finalarch][midarch]-> */);
120
121 extern double max_mass, max_volu, max_capi;
122 extern double distance_loss_factor_per_league;
123 extern int max_dist;
124
125 #define LOSS_FACTOR_PER_DELAY_SLOT (1-1e-8)
126
127 extern int islandtablesz;
128
129 extern int narches;
130 extern char **archnames;
131 extern int *islandid2arch;
132
133
134 #define NEW(ptr) ((ptr)= mmalloc(sizeof(*ptr)))
135
136 #define MCALLOC(array, count) ((array)= mcalloc(sizeof(*(array)) * (count)))
137
138 #define MCALLOC_INITEACH(array, count, init_this) ({                   \
139     MCALLOC((array), (count));                                         \
140     int initi;                                                         \
141     typeof(&(array)[0]) this;                                          \
142     for (initi=0, this=(array); initi<(count); initi++, this++) {      \
143       init_this;                                                       \
144     }                                                                  \
145   })
146
147
148 typedef struct {
149   double value;
150   PotentialResult *pr;
151 } HighScoreEntry;
152
153 extern int nhighscores_absolute, nhighscores_perleague;
154 extern HighScoreEntry *highscores_absolute;
155 extern HighScoreEntry *highscores_perleague;
156
157
158 #define ONDEMAND(pointer_lvalue, calloc_size_count)                          \
159   ((pointer_lvalue) ? :                                                      \
160    ((pointer_lvalue) = mcalloc(sizeof(*(pointer_lvalue)) * calloc_size_count)))
161
162
163 static inline int isle2arch(int isle) {
164   int arch= islandid2arch[isle];
165   assert(arch>=0);
166   return arch;
167 }
168
169 static inline int route2midarch(const int *ports, int nports) {
170   int archs[nports], last_arch=-1, narchs=0, i;
171   for (i=0; i<nports; i++) {
172     int arch= isle2arch(ports[i]);
173     if (arch==last_arch) continue;
174     archs[narchs++]= last_arch= arch;
175   }
176   return archs[narchs/2];
177 }
178
179
180 #endif /*RSCOMMON_H*/