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