probdist_mute(): Introduce helper
[pachi.git] / probdist.h
blobcef03f2ea7460dcbfde9742424efbf41f7c974c9
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);
28 static void probdist_mute(struct probdist *pd, int i);
30 /* Pick a random item. ignore is a zero-terminated sorted array of items
31 * that are not to be considered (and whose values are not in @total). */
32 int probdist_pick(struct probdist *pd, int *ignore);
35 /* We disable the assertions here since this is quite time-critical
36 * part of code, and also the compiler is reluctant to inline the
37 * functions otherwise. */
38 static inline void
39 probdist_set(struct probdist *pd, int i, double val)
41 #if 0
42 assert(i >= 0 && i < pd->n);
43 assert(val >= 0);
44 #endif
45 pd->total += val - pd->items[i];
46 pd->items[i] = val;
49 /* Remove the item from the totals; this is used when you then
50 * pass it in the ignore list to probdist_pick(). */
51 static inline void
52 probdist_mute(struct probdist *pd, int i)
54 pd->total -= pd->items[i];
57 #endif