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