Pattern feature_id: Change PF_ from bitmasks to bit indexes
[pachi.git] / probdist.h
blob51e511d7774935c60acc79559f0946011af1bb06
1 #ifndef ZZGO_PROBDIST_H
2 #define ZZGO_PROBDIST_H
4 /* Tools for picking a move according to a probability distribution. */
6 #include "move.h"
8 struct probdist {
9 int bsize2;
10 float *moves; // [bsize2]
11 float total;
14 struct probdist *probdist_init(struct probdist *pd, int bsize2);
15 static void probdist_add(struct probdist *pd, coord_t c, float val);
16 static void probdist_mul(struct probdist *pd, coord_t c, float val);
17 static void probdist_punch(struct probdist *pd, coord_t c); // Remove c from probability distribution
18 coord_t probdist_pick(struct probdist *pd);
19 void probdist_done(struct probdist *pd); // Doesn't free pd itself
22 static inline void
23 probdist_add(struct probdist *pd, coord_t c, float val)
25 assert(c >= 0 && c < pd->bsize2);
26 assert(val >= 0);
27 pd->moves[c] += val;
28 pd->total += val;
31 static inline void
32 probdist_mul(struct probdist *pd, coord_t c, float val)
34 assert(c >= 0 && c < pd->bsize2);
35 assert(val >= 0);
36 float t = pd->total - pd->moves[c];
37 pd->moves[c] *= val;
38 pd->total = t + pd->moves[c];
41 static inline void
42 probdist_punch(struct probdist *pd, coord_t c)
44 assert(c >= 0 && c < pd->bsize2);
45 pd->total -= pd->moves[c];
46 pd->moves[c] = 0;
49 #endif