move.[ch]: Add str2scoord(), export coord2bstr() to avoid mallocs
[pachi/peepo.git] / probdist.h
blob506dcc596093028ae953f8259056c21a5f1e5ebe
1 #ifndef ZZGO_PROBDIST_H
2 #define ZZGO_PROBDIST_H
4 /* Tools for picking an item according to a probability distribution. */
6 /* The probability distribution structure is designed to be once
7 * initialized, then random items assigned a value repeatedly and
8 * random items picked repeatedly as well. */
10 #include "move.h"
11 #include "util.h"
13 /* The interface looks a bit funny-wrapped since we used to switch
14 * between different probdist representations. */
16 struct probdist {
17 int n;
18 double *items; // [n], items[i] = P(pick==i)
19 double total;
21 #define probdist_total(pd) ((pd)->total)
22 #define probdist_one(pd, i) ((pd)->items[i])
23 /* Probability so small that it's same as zero; used to compensate
24 * for probdist.total inaccuracies. */
25 #define PROBDIST_EPSILON 0.05
27 static void probdist_set(struct probdist *pd, int i, double val);
29 int probdist_pick(struct probdist *pd);
32 /* We disable the assertions here since this is quite time-critical
33 * part of code, and also the compiler is reluctant to inline the
34 * functions otherwise. */
35 static inline void
36 probdist_set(struct probdist *pd, int i, double val)
38 #if 0
39 assert(i >= 0 && i < pd->n);
40 assert(val >= 0);
41 #endif
42 pd->total += val - pd->items[i];
43 pd->items[i] = val;
46 #endif