chiark / gitweb /
7c70ca4d800a647dbc2d322f8e9fdb59cc305480
[ypp-sc-tools.db-test.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(check)                                    \
12    DF(lp)
13
14 #define debug stdout
15 #define DEBUG_DEV "/dev/stdout"
16
17 #include "common.h"
18
19
20 #define COUNTER_LIST                            \
21    CTR(commodities_loaded)                      \
22    CTR(trades_loaded)                           \
23    CTR(quantities_loaded)                       \
24    CTR(routes_considered)                       \
25    CTR(routes_eliminated)                       \
26    CTR(routes_valued)                           \
27    CTR(subroute_tails_valued)                   \
28    CTR(subroutes_valued)                        \
29    CTR(subroutes_nonempty)
30 #define CTR(x) extern int ctr_##x;
31   COUNTER_LIST
32 #undef CTR
33
34 #define SQL_MUST( call ) ({                                              \
35     /* `call' is an expression returning result, using  const char *sqe; \
36      * chk1 and chk2 are blocks using sqe and  int sqr; */               \
37     const char *sql_must_call_string= #call;                             \
38     int sqr;                                                             \
39     if (DEBUGP(sql2)) fprintf(stderr,"SQL %s", sql_must_call_string);    \
40     sqr= (call);                                                         \
41     if (DEBUGP(sql2)) fprintf(stderr," = %d\n", sqr);                    \
42     if (sqr) sql_fatal("(unknown)", sqr, sql_must_call_string);          \
43   })                                                                     \
44
45 void sql_fatal(const char *stmt_what, int sqr, const char *act_what) NORET;
46
47 #define SQL_STEP(ssh) (sql_step((ssh), #ssh, __FILE__, __LINE__))
48 int sql_step(sqlite3_stmt *ssh, const char *ssh_string,
49              const char *file, int line);
50
51 #define SQL_DISTINCT_DECL(cols, nintcols)       \
52   int cols[nintcols];                           \
53   cols[0]= -1;
54 #define SQL_DISTINCT_STEP(ssh, cols, nkeycols)                           \
55   (sql_step_distinct((ssh), #ssh, __FILE__, __LINE__,                    \
56                      (cols), sizeof((cols))/sizeof((cols)[0]), nkeycols))
57 int sql_step_distinct(sqlite3_stmt *ssh, const char *ssh_string,
58                       const char *file, int line,
59                       int *cols, int ncols, int nkeycols);
60    /* These work if we're making a query whose columns consist of:
61     *  - keys: integer column(s) on which the results are sorted by the query
62     *  - consequences: zero or more integer cols strictly dependent on the keys
63     *  - extra: zero or more further (possibly non-integer) columns
64     *
65     * Call SQL_DISTINCT_DECL, passing intcols = the total number of keys and
66     * consequences; it will declare  int cols[intfields];
67     *
68     * Then each SQL_DISTINCT_STEP is like SQL_STEP only you have to
69     * pass the number of key columns and it only returns rows with
70     * distinct keys.  Rows with all-identical keys are asserted to
71     * have identical consequences.  After each call to
72     * SQL_DISTINCT_STEP the keys and consequences will be stored in
73     * cols.
74     */
75
76 int sql_single_int(const char *stmt);
77
78 #define SQL_PREPARE(ss,stmt) ((ss)= sql_prepare((stmt),#ss))
79 sqlite3_stmt *sql_prepare(const char *stmt, const char *what);
80
81 #define SQL_BIND(ss,index,value) (sql_bind((ss),(index),(value),#ss,#value))
82 void sql_bind(sqlite3_stmt *ss, int index, int value,
83               const char *ss_what, const char *val_what);
84
85 #define MAX_ROUTELEN 20
86
87 extern sqlite3 *db;
88
89 void setup_sql(void);
90
91
92 typedef struct {
93   double distance_loss_factor;
94   struct TradesBlock *trades;
95   double route_tail_value;
96 } IslandPair;
97
98 IslandPair *ipair_get_maybe(int si, int di);
99
100 double value_route(int nislands, const int *islands, int exclude_arbitrage);
101 void setup_value(void);
102
103 void setup_search(void);
104 void search(int start_isle);
105
106 extern double max_mass, max_volu, max_capi;
107 extern double distance_loss_factor_per_league;
108 extern int max_dist;
109
110 #define LOSS_FACTOR_PER_DELAY_SLOT (1-1e-8)
111
112 extern int islandtablesz;
113
114
115 #endif /*RSCOMMON_H*/