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 | 8aff3bd109b82b57c32a0b14a019c99c1ec35705 (patch) | |
tree | 6bbb78d76df619d16f402672c8796b2939cc4780 /utils/alias_sampler.h | |
parent | 4b38556c88c739de82b9c298261a262ec620280e (diff) |
improved alias sampler
Diffstat (limited to 'utils/alias_sampler.h')
-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]; |