Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame^] | 1 | // Copyright 2019 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_INTERNAL_DISTRIBUTIONS_H_ |
| 16 | #define ABSL_RANDOM_INTERNAL_DISTRIBUTIONS_H_ |
| 17 | |
| 18 | #include <type_traits> |
| 19 | |
| 20 | #include "absl/meta/type_traits.h" |
| 21 | #include "absl/random/internal/distribution_caller.h" |
| 22 | #include "absl/random/internal/traits.h" |
| 23 | #include "absl/random/internal/uniform_helper.h" |
| 24 | |
| 25 | namespace absl { |
| 26 | namespace random_internal { |
| 27 | template <typename D> |
| 28 | struct DistributionFormatTraits; |
| 29 | |
| 30 | // UniformImpl implements the core logic of the Uniform<T> call, which is to |
| 31 | // select the correct distribution type, compute the bounds based on the |
| 32 | // interval tag, and then generate a value. |
| 33 | template <typename NumType, typename TagType, typename URBG> |
| 34 | NumType UniformImpl(TagType tag, |
| 35 | URBG& urbg, // NOLINT(runtime/references) |
| 36 | NumType lo, NumType hi) { |
| 37 | static_assert( |
| 38 | std::is_arithmetic<NumType>::value, |
| 39 | "absl::Uniform<T>() must use an integer or real parameter type."); |
| 40 | |
| 41 | using distribution_t = |
| 42 | UniformDistributionWrapper<absl::decay_t<TagType>, NumType>; |
| 43 | using format_t = random_internal::DistributionFormatTraits<distribution_t>; |
| 44 | auto a = uniform_lower_bound(tag, lo, hi); |
| 45 | auto b = uniform_upper_bound(tag, lo, hi); |
| 46 | |
| 47 | // TODO(lar): it doesn't make a lot of sense to ask for a random number in an |
| 48 | // empty range. Right now we just return a boundary--even though that |
| 49 | // boundary is not an acceptable value! Is there something better we can do |
| 50 | // here? |
| 51 | if (a > b) return a; |
| 52 | |
| 53 | using gen_t = absl::decay_t<URBG>; |
| 54 | return DistributionCaller<gen_t>::template Call<distribution_t, format_t>( |
| 55 | &urbg, tag, lo, hi); |
| 56 | } |
| 57 | |
| 58 | // In the absence of an explicitly provided return-type, the template |
| 59 | // "uniform_inferred_return_t<A, B>" is used to derive a suitable type, based on |
| 60 | // the data-types of the endpoint-arguments {A lo, B hi}. |
| 61 | // |
| 62 | // Given endpoints {A lo, B hi}, one of {A, B} will be chosen as the |
| 63 | // return-type, if one type can be implicitly converted into the other, in a |
| 64 | // lossless way. The template "is_widening_convertible" implements the |
| 65 | // compile-time logic for deciding if such a conversion is possible. |
| 66 | // |
| 67 | // If no such conversion between {A, B} exists, then the overload for |
| 68 | // absl::Uniform() will be discarded, and the call will be ill-formed. |
| 69 | // Return-type for absl::Uniform() when the return-type is inferred. |
| 70 | template <typename A, typename B> |
| 71 | using uniform_inferred_return_t = |
| 72 | absl::enable_if_t<absl::disjunction<is_widening_convertible<A, B>, |
| 73 | is_widening_convertible<B, A>>::value, |
| 74 | typename std::conditional< |
| 75 | is_widening_convertible<A, B>::value, B, A>::type>; |
| 76 | |
| 77 | } // namespace random_internal |
| 78 | } // namespace absl |
| 79 | |
| 80 | #endif // ABSL_RANDOM_INTERNAL_DISTRIBUTIONS_H_ |