You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

sequence_urbg.h 2.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. {
  24. ABSL_NAMESPACE_BEGIN
  25. namespace random_internal
  26. {
  27. // `sequence_urbg` is a simple random number generator which meets the
  28. // requirements of [rand.req.urbg], and is solely for testing absl
  29. // distributions.
  30. class sequence_urbg
  31. {
  32. public:
  33. using result_type = uint64_t;
  34. static constexpr result_type(min)()
  35. {
  36. return (std::numeric_limits<result_type>::min)();
  37. }
  38. static constexpr result_type(max)()
  39. {
  40. return (std::numeric_limits<result_type>::max)();
  41. }
  42. sequence_urbg(std::initializer_list<result_type> data) :
  43. i_(0),
  44. data_(data)
  45. {
  46. }
  47. void reset()
  48. {
  49. i_ = 0;
  50. }
  51. result_type operator()()
  52. {
  53. return data_[i_++ % data_.size()];
  54. }
  55. size_t invocations() const
  56. {
  57. return i_;
  58. }
  59. private:
  60. size_t i_;
  61. std::vector<result_type> data_;
  62. };
  63. } // namespace random_internal
  64. ABSL_NAMESPACE_END
  65. } // namespace absl
  66. #endif // ABSL_RANDOM_INTERNAL_SEQUENCE_URBG_H_