abseil-cpp/absl/random/exponential_distribution.h
Go to the documentation of this file.
1 // Copyright 2017 The Abseil Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef ABSL_RANDOM_EXPONENTIAL_DISTRIBUTION_H_
16 #define ABSL_RANDOM_EXPONENTIAL_DISTRIBUTION_H_
17 
18 #include <cassert>
19 #include <cmath>
20 #include <istream>
21 #include <limits>
22 #include <type_traits>
23 
24 #include "absl/meta/type_traits.h"
25 #include "absl/random/internal/fast_uniform_bits.h"
26 #include "absl/random/internal/generate_real.h"
27 #include "absl/random/internal/iostream_state_saver.h"
28 
29 namespace absl {
31 
32 // absl::exponential_distribution:
33 // Generates a number conforming to an exponential distribution and is
34 // equivalent to the standard [rand.dist.pois.exp] distribution.
35 template <typename RealType = double>
37  public:
38  using result_type = RealType;
39 
40  class param_type {
41  public:
43 
45  assert(lambda > 0);
47  }
48 
49  result_type lambda() const { return lambda_; }
50 
51  friend bool operator==(const param_type& a, const param_type& b) {
52  return a.lambda_ == b.lambda_;
53  }
54 
55  friend bool operator!=(const param_type& a, const param_type& b) {
56  return !(a == b);
57  }
58 
59  private:
61 
64 
65  static_assert(
67  "Class-template absl::exponential_distribution<> must be parameterized "
68  "using a floating-point type.");
69  };
70 
72 
74 
75  explicit exponential_distribution(const param_type& p) : param_(p) {}
76 
77  void reset() {}
78 
79  // Generating functions
80  template <typename URBG>
81  result_type operator()(URBG& g) { // NOLINT(runtime/references)
82  return (*this)(g, param_);
83  }
84 
85  template <typename URBG>
86  result_type operator()(URBG& g, // NOLINT(runtime/references)
87  const param_type& p);
88 
89  param_type param() const { return param_; }
90  void param(const param_type& p) { param_ = p; }
91 
92  result_type(min)() const { return 0; }
93  result_type(max)() const {
94  return std::numeric_limits<result_type>::infinity();
95  }
96 
97  result_type lambda() const { return param_.lambda(); }
98 
100  const exponential_distribution& b) {
101  return a.param_ == b.param_;
102  }
104  const exponential_distribution& b) {
105  return a.param_ != b.param_;
106  }
107 
108  private:
111 };
112 
113 // --------------------------------------------------------------------------
114 // Implementation details follow
115 // --------------------------------------------------------------------------
116 
117 template <typename RealType>
118 template <typename URBG>
121  URBG& g, // NOLINT(runtime/references)
122  const param_type& p) {
125  using real_type =
127 
128  const result_type u = GenerateRealFromBits<real_type, GenerateNegativeTag,
129  false>(fast_u64_(g)); // U(-1, 0)
130 
131  // log1p(-x) is mathematically equivalent to log(1 - x) but has more
132  // accuracy for x near zero.
133  return p.neg_inv_lambda_ * std::log1p(u);
134 }
135 
136 template <typename CharT, typename Traits, typename RealType>
137 std::basic_ostream<CharT, Traits>& operator<<(
138  std::basic_ostream<CharT, Traits>& os, // NOLINT(runtime/references)
142  os << x.lambda();
143  return os;
144 }
145 
146 template <typename CharT, typename Traits, typename RealType>
147 std::basic_istream<CharT, Traits>& operator>>(
148  std::basic_istream<CharT, Traits>& is, // NOLINT(runtime/references)
149  exponential_distribution<RealType>& x) { // NOLINT(runtime/references)
151  using param_type = typename exponential_distribution<RealType>::param_type;
152  result_type lambda;
153 
155  lambda = random_internal::read_floating_point<result_type>(is);
156  if (!is.fail()) {
157  x.param(param_type(lambda));
158  }
159  return is;
160 }
161 
163 } // namespace absl
164 
165 #endif // ABSL_RANDOM_EXPONENTIAL_DISTRIBUTION_H_
absl::random_internal::GenerateNegativeTag
Definition: abseil-cpp/absl/random/internal/generate_real.h:37
absl::random_internal::make_istream_state_saver
istream_state_saver< std::basic_istream< CharT, Traits > > make_istream_state_saver(std::basic_istream< CharT, Traits > &is, std::ios_base::fmtflags flags=std::ios_base::dec|std::ios_base::scientific|std::ios_base::skipws)
Definition: abseil-cpp/absl/random/internal/iostream_state_saver.h:149
absl::exponential_distribution::param
param_type param() const
Definition: abseil-cpp/absl/random/exponential_distribution.h:89
absl::exponential_distribution::param_type::lambda
result_type lambda() const
Definition: abseil-cpp/absl/random/exponential_distribution.h:49
absl::exponential_distribution::exponential_distribution
exponential_distribution()
Definition: abseil-cpp/absl/random/exponential_distribution.h:71
absl::exponential_distribution::operator!=
friend bool operator!=(const exponential_distribution &a, const exponential_distribution &b)
Definition: abseil-cpp/absl/random/exponential_distribution.h:103
absl::operator<<
ABSL_NAMESPACE_BEGIN std::ostream & operator<<(std::ostream &os, absl::LogSeverity s)
Definition: abseil-cpp/absl/base/log_severity.cc:24
google::protobuf.internal::real_type
FieldDescriptor::Type real_type(FieldType type)
Definition: bloaty/third_party/protobuf/src/google/protobuf/extension_set_heavy.cc:128
absl::exponential_distribution::operator()
result_type operator()(URBG &g)
Definition: abseil-cpp/absl/random/exponential_distribution.h:81
absl::conditional_t
typename std::conditional< B, T, F >::type conditional_t
Definition: abseil-cpp/absl/meta/type_traits.h:634
absl::exponential_distribution
Definition: abseil-cpp/absl/random/exponential_distribution.h:36
u
OPENSSL_EXPORT pem_password_cb void * u
Definition: pem.h:351
a
int a
Definition: abseil-cpp/absl/container/internal/hash_policy_traits_test.cc:88
xds_manager.p
p
Definition: xds_manager.py:60
absl::exponential_distribution::param_type::param_type
param_type(result_type lambda=1)
Definition: abseil-cpp/absl/random/exponential_distribution.h:44
ABSL_NAMESPACE_END
#define ABSL_NAMESPACE_END
Definition: third_party/abseil-cpp/absl/base/config.h:171
absl::exponential_distribution::lambda
result_type lambda() const
Definition: abseil-cpp/absl/random/exponential_distribution.h:97
absl::random_internal::GenerateRealFromBits
RealType GenerateRealFromBits(uint64_t bits, int exp_bias=0)
Definition: abseil-cpp/absl/random/internal/generate_real.h:70
ABSL_NAMESPACE_BEGIN
#define ABSL_NAMESPACE_BEGIN
Definition: third_party/abseil-cpp/absl/base/config.h:170
absl::exponential_distribution::fast_u64_
random_internal::FastUniformBits< uint64_t > fast_u64_
Definition: abseil-cpp/absl/random/exponential_distribution.h:110
absl::exponential_distribution::operator==
friend bool operator==(const exponential_distribution &a, const exponential_distribution &b)
Definition: abseil-cpp/absl/random/exponential_distribution.h:99
absl::exponential_distribution::max
result_type() max() const
Definition: abseil-cpp/absl/random/exponential_distribution.h:93
result_type
const typedef int * result_type
Definition: bloaty/third_party/googletest/googlemock/test/gmock-matchers_test.cc:4325
absl::exponential_distribution::param_type::neg_inv_lambda_
result_type neg_inv_lambda_
Definition: abseil-cpp/absl/random/exponential_distribution.h:63
absl::exponential_distribution::exponential_distribution
exponential_distribution(const param_type &p)
Definition: abseil-cpp/absl/random/exponential_distribution.h:75
x
int x
Definition: bloaty/third_party/googletest/googlemock/test/gmock-matchers_test.cc:3610
absl::exponential_distribution::param_type
Definition: abseil-cpp/absl/random/exponential_distribution.h:40
absl::exponential_distribution::param_type::lambda_
result_type lambda_
Definition: abseil-cpp/absl/random/exponential_distribution.h:62
absl::exponential_distribution::param_type::operator!=
friend bool operator!=(const param_type &a, const param_type &b)
Definition: abseil-cpp/absl/random/exponential_distribution.h:55
b
uint64_t b
Definition: abseil-cpp/absl/container/internal/layout_test.cc:53
g
struct @717 g
absl::random_internal::stream_precision_helper
Definition: abseil-cpp/absl/random/internal/iostream_state_saver.h:107
value
const char * value
Definition: hpack_parser_table.cc:165
absl::exponential_distribution::param_type::operator==
friend bool operator==(const param_type &a, const param_type &b)
Definition: abseil-cpp/absl/random/exponential_distribution.h:51
absl::operator>>
constexpr uint128 operator>>(uint128 lhs, int amount)
Definition: abseil-cpp/absl/numeric/int128.h:917
absl::exponential_distribution::min
result_type() min() const
Definition: abseil-cpp/absl/random/exponential_distribution.h:92
absl::exponential_distribution::result_type
RealType result_type
Definition: abseil-cpp/absl/random/exponential_distribution.h:38
absl::exponential_distribution::param
void param(const param_type &p)
Definition: abseil-cpp/absl/random/exponential_distribution.h:90
absl::exponential_distribution::exponential_distribution
exponential_distribution(result_type lambda)
Definition: abseil-cpp/absl/random/exponential_distribution.h:73
absl::exponential_distribution::param_type::exponential_distribution
friend class exponential_distribution
Definition: abseil-cpp/absl/random/exponential_distribution.h:60
absl::exponential_distribution::param_
param_type param_
Definition: abseil-cpp/absl/random/exponential_distribution.h:109
absl
Definition: abseil-cpp/absl/algorithm/algorithm.h:31
absl::random_internal::make_ostream_state_saver
ostream_state_saver< std::basic_ostream< CharT, Traits > > make_ostream_state_saver(std::basic_ostream< CharT, Traits > &os, std::ios_base::fmtflags flags=std::ios_base::dec|std::ios_base::left|std::ios_base::scientific)
Definition: abseil-cpp/absl/random/internal/iostream_state_saver.h:82
absl::random_internal::FastUniformBits< uint64_t >
absl::exponential_distribution::reset
void reset()
Definition: abseil-cpp/absl/random/exponential_distribution.h:77


grpc
Author(s):
autogenerated on Fri May 16 2025 02:58:20