sequence_urbg.h 1.7 KB

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