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