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