Built motion from commit 6a09e18b.|2.6.11
[motion2.git] / legacy-libs / grpc / deps / grpc / third_party / abseil-cpp / absl / random / uniform_real_distribution.h
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 // -----------------------------------------------------------------------------
16 // File: uniform_real_distribution.h
17 // -----------------------------------------------------------------------------
18 //
19 // This header defines a class for representing a uniform floating-point
20 // distribution over a half-open interval [a,b). You use this distribution in
21 // combination with an Abseil random bit generator to produce random values
22 // according to the rules of the distribution.
23 //
24 // `absl::uniform_real_distribution` is a drop-in replacement for the C++11
25 // `std::uniform_real_distribution` [rand.dist.uni.real] but is considerably
26 // faster than the libstdc++ implementation.
27 //
28 // Note: the standard-library version may occasionally return `1.0` when
29 // default-initialized. See https://bugs.llvm.org//show_bug.cgi?id=18767
30 // `absl::uniform_real_distribution` does not exhibit this behavior.
31
32 #ifndef ABSL_RANDOM_UNIFORM_REAL_DISTRIBUTION_H_
33 #define ABSL_RANDOM_UNIFORM_REAL_DISTRIBUTION_H_
34
35 #include <cassert>
36 #include <cmath>
37 #include <cstdint>
38 #include <istream>
39 #include <limits>
40 #include <type_traits>
41
42 #include "absl/random/internal/distribution_impl.h"
43 #include "absl/random/internal/fast_uniform_bits.h"
44 #include "absl/random/internal/iostream_state_saver.h"
45
46 namespace absl {
47
48 // absl::uniform_real_distribution<T>
49 //
50 // This distribution produces random floating-point values uniformly distributed
51 // over the half-open interval [a, b).
52 //
53 // Example:
54 //
55 //   absl::BitGen gen;
56 //
57 //   // Use the distribution to produce a value between 0.0 (inclusive)
58 //   // and 1.0 (exclusive).
59 //   int value = absl::uniform_real_distribution<double>(0, 1)(gen);
60 //
61 template <typename RealType = double>
62 class uniform_real_distribution {
63  public:
64   using result_type = RealType;
65
66   class param_type {
67    public:
68     using distribution_type = uniform_real_distribution;
69
70     explicit param_type(result_type lo = 0, result_type hi = 1)
71         : lo_(lo), hi_(hi), range_(hi - lo) {
72       // [rand.dist.uni.real] preconditions 2 & 3
73       assert(lo <= hi);
74       // NOTE: For integral types, we can promote the range to an unsigned type,
75       // which gives full width of the range. However for real (fp) types, this
76       // is not possible, so value generation cannot use the full range of the
77       // real type.
78       assert(range_ <= (std::numeric_limits<result_type>::max)());
79     }
80
81     result_type a() const { return lo_; }
82     result_type b() const { return hi_; }
83
84     friend bool operator==(const param_type& a, const param_type& b) {
85       return a.lo_ == b.lo_ && a.hi_ == b.hi_;
86     }
87
88     friend bool operator!=(const param_type& a, const param_type& b) {
89       return !(a == b);
90     }
91
92    private:
93     friend class uniform_real_distribution;
94     result_type lo_, hi_, range_;
95
96     static_assert(std::is_floating_point<RealType>::value,
97                   "Class-template absl::uniform_real_distribution<> must be "
98                   "parameterized using a floating-point type.");
99   };
100
101   uniform_real_distribution() : uniform_real_distribution(0) {}
102
103   explicit uniform_real_distribution(result_type lo, result_type hi = 1)
104       : param_(lo, hi) {}
105
106   explicit uniform_real_distribution(const param_type& param) : param_(param) {}
107
108   // uniform_real_distribution<T>::reset()
109   //
110   // Resets the uniform real distribution. Note that this function has no effect
111   // because the distribution already produces independent values.
112   void reset() {}
113
114   template <typename URBG>
115   result_type operator()(URBG& gen) {  // NOLINT(runtime/references)
116     return operator()(gen, param_);
117   }
118
119   template <typename URBG>
120   result_type operator()(URBG& gen,  // NOLINT(runtime/references)
121                          const param_type& p);
122
123   result_type a() const { return param_.a(); }
124   result_type b() const { return param_.b(); }
125
126   param_type param() const { return param_; }
127   void param(const param_type& params) { param_ = params; }
128
129   result_type(min)() const { return a(); }
130   result_type(max)() const { return b(); }
131
132   friend bool operator==(const uniform_real_distribution& a,
133                          const uniform_real_distribution& b) {
134     return a.param_ == b.param_;
135   }
136   friend bool operator!=(const uniform_real_distribution& a,
137                          const uniform_real_distribution& b) {
138     return a.param_ != b.param_;
139   }
140
141  private:
142   param_type param_;
143   random_internal::FastUniformBits<uint64_t> fast_u64_;
144 };
145
146 // -----------------------------------------------------------------------------
147 // Implementation details follow
148 // -----------------------------------------------------------------------------
149 template <typename RealType>
150 template <typename URBG>
151 typename uniform_real_distribution<RealType>::result_type
152 uniform_real_distribution<RealType>::operator()(
153     URBG& gen, const param_type& p) {  // NOLINT(runtime/references)
154   using random_internal::PositiveValueT;
155   while (true) {
156     const result_type sample = random_internal::RandU64ToReal<
157         result_type>::template Value<PositiveValueT, true>(fast_u64_(gen));
158     const result_type res = p.a() + (sample * p.range_);
159     if (res < p.b() || p.range_ <= 0 || !std::isfinite(p.range_)) {
160       return res;
161     }
162     // else sample rejected, try again.
163   }
164 }
165
166 template <typename CharT, typename Traits, typename RealType>
167 std::basic_ostream<CharT, Traits>& operator<<(
168     std::basic_ostream<CharT, Traits>& os,  // NOLINT(runtime/references)
169     const uniform_real_distribution<RealType>& x) {
170   auto saver = random_internal::make_ostream_state_saver(os);
171   os.precision(random_internal::stream_precision_helper<RealType>::kPrecision);
172   os << x.a() << os.fill() << x.b();
173   return os;
174 }
175
176 template <typename CharT, typename Traits, typename RealType>
177 std::basic_istream<CharT, Traits>& operator>>(
178     std::basic_istream<CharT, Traits>& is,     // NOLINT(runtime/references)
179     uniform_real_distribution<RealType>& x) {  // NOLINT(runtime/references)
180   using param_type = typename uniform_real_distribution<RealType>::param_type;
181   using result_type = typename uniform_real_distribution<RealType>::result_type;
182   auto saver = random_internal::make_istream_state_saver(is);
183   auto a = random_internal::read_floating_point<result_type>(is);
184   if (is.fail()) return is;
185   auto b = random_internal::read_floating_point<result_type>(is);
186   if (!is.fail()) {
187     x.param(param_type(a, b));
188   }
189   return is;
190 }
191 }  // namespace absl
192
193 #endif  // ABSL_RANDOM_UNIFORM_REAL_DISTRIBUTION_H_