| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| #ifndef ABSL_RANDOM_ZIPF_DISTRIBUTION_H_ |
| #define ABSL_RANDOM_ZIPF_DISTRIBUTION_H_ |
|
|
| #include <cassert> |
| #include <cmath> |
| #include <istream> |
| #include <limits> |
| #include <ostream> |
| #include <type_traits> |
|
|
| #include "absl/random/internal/iostream_state_saver.h" |
| #include "absl/random/internal/traits.h" |
| #include "absl/random/uniform_real_distribution.h" |
|
|
| namespace absl { |
| ABSL_NAMESPACE_BEGIN |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| template <typename IntType = int> |
| class zipf_distribution { |
| public: |
| using result_type = IntType; |
|
|
| class param_type { |
| public: |
| using distribution_type = zipf_distribution; |
|
|
| |
| |
| |
| |
| |
| explicit param_type(result_type k = (std::numeric_limits<IntType>::max)(), |
| double q = 2.0, double v = 1.0); |
|
|
| result_type k() const { return k_; } |
| double q() const { return q_; } |
| double v() const { return v_; } |
|
|
| friend bool operator==(const param_type& a, const param_type& b) { |
| return a.k_ == b.k_ && a.q_ == b.q_ && a.v_ == b.v_; |
| } |
| friend bool operator!=(const param_type& a, const param_type& b) { |
| return !(a == b); |
| } |
|
|
| private: |
| friend class zipf_distribution; |
| inline double h(double x) const; |
| inline double hinv(double x) const; |
| inline double compute_s() const; |
| inline double pow_negative_q(double x) const; |
|
|
| |
| |
| IntType k_; |
| double q_; |
| double v_; |
|
|
| double one_minus_q_; |
| double s_; |
| double one_minus_q_inv_; |
| double hxm_; |
| double hx0_minus_hxm_; |
|
|
| static_assert(random_internal::IsIntegral<IntType>::value, |
| "Class-template absl::zipf_distribution<> must be " |
| "parameterized using an integral type."); |
| }; |
|
|
| zipf_distribution() |
| : zipf_distribution((std::numeric_limits<IntType>::max)()) {} |
|
|
| explicit zipf_distribution(result_type k, double q = 2.0, double v = 1.0) |
| : param_(k, q, v) {} |
|
|
| explicit zipf_distribution(const param_type& p) : param_(p) {} |
|
|
| void reset() {} |
|
|
| template <typename URBG> |
| result_type operator()(URBG& g) { |
| return (*this)(g, param_); |
| } |
|
|
| template <typename URBG> |
| result_type operator()(URBG& g, |
| const param_type& p); |
|
|
| result_type k() const { return param_.k(); } |
| double q() const { return param_.q(); } |
| double v() const { return param_.v(); } |
|
|
| param_type param() const { return param_; } |
| void param(const param_type& p) { param_ = p; } |
|
|
| result_type(min)() const { return 0; } |
| result_type(max)() const { return k(); } |
|
|
| friend bool operator==(const zipf_distribution& a, |
| const zipf_distribution& b) { |
| return a.param_ == b.param_; |
| } |
| friend bool operator!=(const zipf_distribution& a, |
| const zipf_distribution& b) { |
| return a.param_ != b.param_; |
| } |
|
|
| private: |
| param_type param_; |
| }; |
|
|
| |
| |
| |
|
|
| template <typename IntType> |
| zipf_distribution<IntType>::param_type::param_type( |
| typename zipf_distribution<IntType>::result_type k, double q, double v) |
| : k_(k), q_(q), v_(v), one_minus_q_(1 - q) { |
| assert(q > 1); |
| assert(v > 0); |
| assert(k > 0); |
| one_minus_q_inv_ = 1 / one_minus_q_; |
|
|
| |
| |
| constexpr double kMax = 18446744073709549568.0; |
| double kd = static_cast<double>(k); |
| |
| |
| if (kd > kMax) { |
| |
| |
| kd = kMax; |
| } |
| hxm_ = h(kd + 0.5); |
|
|
| |
| const bool use_precomputed = (v == 1.0 && q == 2.0); |
| const double h0x5 = use_precomputed ? (-1.0 / 1.5) |
| : h(0.5); |
| const double elogv_q = (v_ == 1.0) ? 1 : pow_negative_q(v_); |
|
|
| |
| hx0_minus_hxm_ = (h0x5 - elogv_q) - hxm_; |
|
|
| |
| s_ = use_precomputed ? 0.46153846153846123 : compute_s(); |
| } |
|
|
| template <typename IntType> |
| double zipf_distribution<IntType>::param_type::h(double x) const { |
| |
| x += v_; |
| return (one_minus_q_ == -1.0) |
| ? (-1.0 / x) |
| : (std::exp(std::log(x) * one_minus_q_) * one_minus_q_inv_); |
| } |
|
|
| template <typename IntType> |
| double zipf_distribution<IntType>::param_type::hinv(double x) const { |
| |
| return -v_ + ((one_minus_q_ == -1.0) |
| ? (-1.0 / x) |
| : std::exp(one_minus_q_inv_ * std::log(one_minus_q_ * x))); |
| } |
|
|
| template <typename IntType> |
| double zipf_distribution<IntType>::param_type::compute_s() const { |
| |
| return 1.0 - hinv(h(1.5) - pow_negative_q(v_ + 1.0)); |
| } |
|
|
| template <typename IntType> |
| double zipf_distribution<IntType>::param_type::pow_negative_q(double x) const { |
| |
| return q_ == 2.0 ? (1.0 / (x * x)) : std::exp(std::log(x) * -q_); |
| } |
|
|
| template <typename IntType> |
| template <typename URBG> |
| typename zipf_distribution<IntType>::result_type |
| zipf_distribution<IntType>::operator()( |
| URBG& g, const param_type& p) { |
| absl::uniform_real_distribution<double> uniform_double; |
| double k; |
| for (;;) { |
| const double v = uniform_double(g); |
| const double u = p.hxm_ + v * p.hx0_minus_hxm_; |
| const double x = p.hinv(u); |
| k = rint(x); |
| if (k > static_cast<double>(p.k())) continue; |
| if (k - x <= p.s_) break; |
| const double h = p.h(k + 0.5); |
| const double r = p.pow_negative_q(p.v_ + k); |
| if (u >= h - r) break; |
| } |
| IntType ki = static_cast<IntType>(k); |
| assert(ki <= p.k_); |
| return ki; |
| } |
|
|
| template <typename CharT, typename Traits, typename IntType> |
| std::basic_ostream<CharT, Traits>& operator<<( |
| std::basic_ostream<CharT, Traits>& os, |
| const zipf_distribution<IntType>& x) { |
| using stream_type = |
| typename random_internal::stream_format_type<IntType>::type; |
| auto saver = random_internal::make_ostream_state_saver(os); |
| os.precision(random_internal::stream_precision_helper<double>::kPrecision); |
| os << static_cast<stream_type>(x.k()) << os.fill() << x.q() << os.fill() |
| << x.v(); |
| return os; |
| } |
|
|
| template <typename CharT, typename Traits, typename IntType> |
| std::basic_istream<CharT, Traits>& operator>>( |
| std::basic_istream<CharT, Traits>& is, |
| zipf_distribution<IntType>& x) { |
| using result_type = typename zipf_distribution<IntType>::result_type; |
| using param_type = typename zipf_distribution<IntType>::param_type; |
| using stream_type = |
| typename random_internal::stream_format_type<IntType>::type; |
| stream_type k; |
| double q; |
| double v; |
|
|
| auto saver = random_internal::make_istream_state_saver(is); |
| is >> k >> q >> v; |
| if (!is.fail()) { |
| x.param(param_type(static_cast<result_type>(k), q, v)); |
| } |
| return is; |
| } |
|
|
| ABSL_NAMESPACE_END |
| } |
|
|
| #endif |
|
|