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