summaryrefslogtreecommitdiff
path: root/utils/alias_sampler.h
diff options
context:
space:
mode:
authorChris Dyer <cdyer@cs.cmu.edu>2012-04-16 18:37:04 -0400
committerChris Dyer <cdyer@cs.cmu.edu>2012-04-16 18:37:04 -0400
commit964b58ae8302e0fadcceb982c92d49ab25538cd5 (patch)
tree2a61ac9de5304e11f0e38b0c711abada0bb6fd35 /utils/alias_sampler.h
parentcaf1a688db7b446642581c3f69a9aea720735a8f (diff)
improved alias sampler
Diffstat (limited to 'utils/alias_sampler.h')
-rw-r--r--utils/alias_sampler.h9
1 files changed, 6 insertions, 3 deletions
diff --git a/utils/alias_sampler.h b/utils/alias_sampler.h
index 85da9944..81541f7a 100644
--- a/utils/alias_sampler.h
+++ b/utils/alias_sampler.h
@@ -14,10 +14,13 @@
// constructor computes the F's and L's given an arbitrary multionimial p in
// O(n) time and Draw returns samples in O(1) time.
struct AliasSampler {
- explicit AliasSampler(const std::vector<double>& p) :
- cutoffs_(p.size()),
- aliases_(p.size(), std::numeric_limits<unsigned>::max()) {
+ AliasSampler() {}
+ explicit AliasSampler(const std::vector<double>& p) { Init(p); }
+ void Init(const std::vector<double>& p) {
const unsigned N = p.size();
+ cutoffs_.resize(p.size());
+ aliases_.clear();
+ aliases_.resize(p.size(), std::numeric_limits<unsigned>::max());
std::vector<unsigned> s,g;
for (unsigned i = 0; i < N; ++i) {
const double cutoff = cutoffs_[i] = N * p[i];