Allow defining total time not just time per move.
[pachi.git] / probdist.h
blob2cdf455252a975de5dfdb2e8013fd896fd368845
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 each item sequentially assigned a value one,
8 * then multiple times an item picked randomly. */
10 #include "move.h"
11 #include "util.h"
13 struct probdist {
14 int n;
15 float *items; // [n], probability pick<=i
17 #define probdist_total(pd) ((pd)->items[(pd)->n - 1])
18 #define probdist_one(pd, i) ((pd)->items[i] - (likely(i > 0) ? (pd)->items[i - 1] : 0))
20 /* You must call this for all items, *in sequence* (0, 1, ...).
21 * @val is probability of item @i (as opposed to items[i], which
22 * is probability of item <=i, thus includes the sum of predecessors
23 * as well). */
24 static void probdist_set(struct probdist *pd, int i, float val);
26 int probdist_pick(struct probdist *pd);
29 /* We disable the assertions here since this is quite time-critical
30 * part of code, and also the compiler is reluctant to inline the
31 * functions otherwise. */
32 static inline void
33 probdist_set(struct probdist *pd, int i, float val)
35 #if 0
36 assert(i >= 0 && i < pd->n);
37 assert(val >= 0);
38 #endif
39 pd->items[i] = (likely(i > 0) ? pd->items[i - 1] : 0) + val;
42 #endif