From 8aff3bd109b82b57c32a0b14a019c99c1ec35705 Mon Sep 17 00:00:00 2001 From: Chris Dyer Date: Mon, 16 Apr 2012 18:37:04 -0400 Subject: improved alias sampler --- utils/alias_sampler.h | 9 ++++++--- 1 file 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& p) : - cutoffs_(p.size()), - aliases_(p.size(), std::numeric_limits::max()) { + AliasSampler() {} + explicit AliasSampler(const std::vector& p) { Init(p); } + void Init(const std::vector& p) { const unsigned N = p.size(); + cutoffs_.resize(p.size()); + aliases_.clear(); + aliases_.resize(p.size(), std::numeric_limits::max()); std::vector s,g; for (unsigned i = 0; i < N; ++i) { const double cutoff = cutoffs_[i] = N * p[i]; -- cgit v1.2.3