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