diff options
author | Chris Dyer <cdyer@cs.cmu.edu> | 2012-04-16 18:37:04 -0400 |
---|---|---|
committer | Chris Dyer <cdyer@cs.cmu.edu> | 2012-04-16 18:37:04 -0400 |
commit | 964b58ae8302e0fadcceb982c92d49ab25538cd5 (patch) | |
tree | 2a61ac9de5304e11f0e38b0c711abada0bb6fd35 | |
parent | caf1a688db7b446642581c3f69a9aea720735a8f (diff) |
improved alias sampler
-rw-r--r-- | utils/alias_sampler.h | 9 |
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]; |