explicit_seed_seq.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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_EXPLICIT_SEED_SEQ_H_
  15. #define ABSL_RANDOM_INTERNAL_EXPLICIT_SEED_SEQ_H_
  16. #include <algorithm>
  17. #include <cstddef>
  18. #include <cstdint>
  19. #include <initializer_list>
  20. #include <iterator>
  21. #include <vector>
  22. #include "absl/base/config.h"
  23. #include "absl/base/internal/endian.h"
  24. namespace absl {
  25. ABSL_NAMESPACE_BEGIN
  26. namespace random_internal {
  27. // This class conforms to the C++ Standard "Seed Sequence" concept
  28. // [rand.req.seedseq].
  29. //
  30. // An "ExplicitSeedSeq" is meant to provide a conformant interface for
  31. // forwarding pre-computed seed material to the constructor of a class
  32. // conforming to the "Uniform Random Bit Generator" concept. This class makes no
  33. // attempt to mutate the state provided by its constructor, and returns it
  34. // directly via ExplicitSeedSeq::generate().
  35. //
  36. // If this class is asked to generate more seed material than was provided to
  37. // the constructor, then the remaining bytes will be filled with deterministic,
  38. // nonrandom data.
  39. class ExplicitSeedSeq {
  40. public:
  41. using result_type = uint32_t;
  42. ExplicitSeedSeq() : state_() {}
  43. // Copy and move both allowed.
  44. ExplicitSeedSeq(const ExplicitSeedSeq& other) = default;
  45. ExplicitSeedSeq& operator=(const ExplicitSeedSeq& other) = default;
  46. ExplicitSeedSeq(ExplicitSeedSeq&& other) = default;
  47. ExplicitSeedSeq& operator=(ExplicitSeedSeq&& other) = default;
  48. template <typename Iterator>
  49. ExplicitSeedSeq(Iterator begin, Iterator end) {
  50. for (auto it = begin; it != end; it++) {
  51. state_.push_back(*it & 0xffffffff);
  52. }
  53. }
  54. template <typename T>
  55. ExplicitSeedSeq(std::initializer_list<T> il)
  56. : ExplicitSeedSeq(il.begin(), il.end()) {}
  57. size_t size() const { return state_.size(); }
  58. template <typename OutIterator>
  59. void param(OutIterator out) const {
  60. std::copy(std::begin(state_), std::end(state_), out);
  61. }
  62. template <typename OutIterator>
  63. void generate(OutIterator begin, OutIterator end) {
  64. for (size_t index = 0; begin != end; begin++) {
  65. *begin = state_.empty() ? 0 : state_[index++];
  66. if (index >= state_.size()) {
  67. index = 0;
  68. }
  69. }
  70. }
  71. protected:
  72. std::vector<uint32_t> state_;
  73. };
  74. } // namespace random_internal
  75. ABSL_NAMESPACE_END
  76. } // namespace absl
  77. #endif // ABSL_RANDOM_INTERNAL_EXPLICIT_SEED_SEQ_H_