Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 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_INTERNAL_SEQUENCE_URBG_H_ |
| 16 | #define ABSL_RANDOM_INTERNAL_SEQUENCE_URBG_H_ |
| 17 | |
| 18 | #include <cstdint> |
| 19 | #include <cstring> |
| 20 | #include <limits> |
| 21 | #include <type_traits> |
| 22 | #include <vector> |
| 23 | |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 24 | #include "absl/base/config.h" |
| 25 | |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 26 | namespace absl { |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 27 | ABSL_NAMESPACE_BEGIN |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 28 | namespace random_internal { |
| 29 | |
| 30 | // `sequence_urbg` is a simple random number generator which meets the |
| 31 | // requirements of [rand.req.urbg], and is solely for testing absl |
| 32 | // distributions. |
| 33 | class sequence_urbg { |
| 34 | public: |
| 35 | using result_type = uint64_t; |
| 36 | |
| 37 | static constexpr result_type(min)() { |
| 38 | return (std::numeric_limits<result_type>::min)(); |
| 39 | } |
| 40 | static constexpr result_type(max)() { |
| 41 | return (std::numeric_limits<result_type>::max)(); |
| 42 | } |
| 43 | |
| 44 | sequence_urbg(std::initializer_list<result_type> data) : i_(0), data_(data) {} |
| 45 | void reset() { i_ = 0; } |
| 46 | |
| 47 | result_type operator()() { return data_[i_++ % data_.size()]; } |
| 48 | |
| 49 | size_t invocations() const { return i_; } |
| 50 | |
| 51 | private: |
| 52 | size_t i_; |
| 53 | std::vector<result_type> data_; |
| 54 | }; |
| 55 | |
| 56 | } // namespace random_internal |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 57 | ABSL_NAMESPACE_END |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 58 | } // namespace absl |
| 59 | |
| 60 | #endif // ABSL_RANDOM_INTERNAL_SEQUENCE_URBG_H_ |