abseil-cpp/absl/random/internal/salted_seed_seq.h
Go to the documentation of this file.
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_SALTED_SEED_SEQ_H_
16 #define ABSL_RANDOM_INTERNAL_SALTED_SEED_SEQ_H_
17 
18 #include <cstdint>
19 #include <cstdlib>
20 #include <initializer_list>
21 #include <iterator>
22 #include <memory>
23 #include <type_traits>
24 #include <utility>
25 #include <vector>
26 
27 #include "absl/container/inlined_vector.h"
28 #include "absl/meta/type_traits.h"
29 #include "absl/random/internal/seed_material.h"
30 #include "absl/types/optional.h"
31 #include "absl/types/span.h"
32 
33 namespace absl {
35 namespace random_internal {
36 
37 // This class conforms to the C++ Standard "Seed Sequence" concept
38 // [rand.req.seedseq].
39 //
40 // A `SaltedSeedSeq` is meant to wrap an existing seed sequence and modify
41 // generated sequence by mixing with extra entropy. This entropy may be
42 // build-dependent or process-dependent. The implementation may change to be
43 // have either or both kinds of entropy. If salt is not available sequence is
44 // not modified.
45 template <typename SSeq>
47  public:
48  using inner_sequence_type = SSeq;
49  using result_type = typename SSeq::result_type;
50 
52 
53  template <typename Iterator>
55  : seq_(absl::make_unique<SSeq>(begin, end)) {}
56 
57  template <typename T>
58  SaltedSeedSeq(std::initializer_list<T> il)
59  : SaltedSeedSeq(il.begin(), il.end()) {}
60 
61  SaltedSeedSeq(const SaltedSeedSeq&) = delete;
62  SaltedSeedSeq& operator=(const SaltedSeedSeq&) = delete;
63 
64  SaltedSeedSeq(SaltedSeedSeq&&) = default;
66 
67  template <typename RandomAccessIterator>
68  void generate(RandomAccessIterator begin, RandomAccessIterator end) {
70 
71  // The common case is that generate is called with ContiguousIterators
72  // to uint arrays. Such contiguous memory regions may be optimized,
73  // which we detect here.
74  using TagType = absl::conditional_t<
77  std::is_same<RandomAccessIterator,
80  if (begin != end) {
81  generate_impl(TagType{}, begin, end, std::distance(begin, end));
82  }
83  }
84 
85  template <typename OutIterator>
86  void param(OutIterator out) const {
87  seq_->param(out);
88  }
89 
90  size_t size() const { return seq_->size(); }
91 
92  private:
94  struct DefaultTag {};
95 
96  // Generate which requires the iterators are contiguous pointers to uint32_t.
97  // Fills the initial seed buffer the underlying SSeq::generate() call,
98  // then mixes in the salt material.
99  template <typename Contiguous>
100  void generate_impl(ContiguousAndUint32Tag, Contiguous begin, Contiguous end,
101  size_t n) {
102  seq_->generate(begin, end);
104  auto span = absl::Span<uint32_t>(&*begin, n);
105  MixIntoSeedMaterial(absl::MakeConstSpan(&salt, 1), span);
106  }
107 
108  // The uncommon case for generate is that it is called with iterators over
109  // some other buffer type which is assignable from a 32-bit value. In this
110  // case we allocate a temporary 32-bit buffer and then copy-assign back
111  // to the initial inputs.
112  template <typename RandomAccessIterator>
113  void generate_impl(DefaultTag, RandomAccessIterator begin,
114  RandomAccessIterator, size_t n) {
115  // Allocates a seed buffer of `n` elements, generates the seed, then
116  // copies the result into the `out` iterator.
118  generate_impl(ContiguousAndUint32Tag{}, data.begin(), data.end(), n);
119  std::copy(data.begin(), data.end(), begin);
120  }
121 
122  // Because [rand.req.seedseq] is not required to be copy-constructible,
123  // copy-assignable nor movable, we wrap it with unique pointer to be able
124  // to move SaltedSeedSeq.
125  std::unique_ptr<SSeq> seq_;
126 };
127 
128 // is_salted_seed_seq indicates whether the type is a SaltedSeedSeq.
129 template <typename T, typename = void>
131 
132 template <typename T>
134  T, typename std::enable_if<std::is_same<
135  T, SaltedSeedSeq<typename T::inner_sequence_type>>::value>::type>
136  : public std::true_type {};
137 
138 // MakeSaltedSeedSeq returns a salted variant of the seed sequence.
139 // When provided with an existing SaltedSeedSeq, returns the input parameter,
140 // otherwise constructs a new SaltedSeedSeq which embodies the original
141 // non-salted seed parameters.
142 template <
143  typename SSeq, //
145 SSeq MakeSaltedSeedSeq(SSeq&& seq) {
146  return SSeq(std::forward<SSeq>(seq));
147 }
148 
149 template <
150  typename SSeq, //
153  using sseq_type = typename std::decay<SSeq>::type;
154  using result_type = typename sseq_type::result_type;
155 
157  seq.param(std::back_inserter(data));
158  return SaltedSeedSeq<sseq_type>(data.begin(), data.end());
159 }
160 
161 } // namespace random_internal
163 } // namespace absl
164 
165 #endif // ABSL_RANDOM_INTERNAL_SALTED_SEED_SEQ_H_
begin
char * begin
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:1007
absl::random_internal::SaltedSeedSeq::result_type
typename SSeq::result_type result_type
Definition: abseil-cpp/absl/random/internal/salted_seed_seq.h:49
absl::Span
Definition: abseil-cpp/absl/types/span.h:152
copy
static int copy(grpc_slice_buffer *input, grpc_slice_buffer *output)
Definition: message_compress.cc:145
google::protobuf.internal::true_type
integral_constant< bool, true > true_type
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/template_util.h:89
absl::conditional_t
typename std::conditional< B, T, F >::type conditional_t
Definition: abseil-cpp/absl/meta/type_traits.h:634
google::protobuf.internal::false_type
integral_constant< bool, false > false_type
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/template_util.h:90
absl::random_internal::SaltedSeedSeq::SaltedSeedSeq
SaltedSeedSeq(Iterator begin, Iterator end)
Definition: abseil-cpp/absl/random/internal/salted_seed_seq.h:54
absl::random_internal::SaltedSeedSeq::size
size_t size() const
Definition: abseil-cpp/absl/random/internal/salted_seed_seq.h:90
absl::enable_if_t
typename std::enable_if< B, T >::type enable_if_t
Definition: abseil-cpp/absl/meta/type_traits.h:631
absl::make_unique
memory_internal::MakeUniqueResult< T >::scalar make_unique(Args &&... args)
Definition: third_party/abseil-cpp/absl/memory/memory.h:168
ABSL_NAMESPACE_END
#define ABSL_NAMESPACE_END
Definition: third_party/abseil-cpp/absl/base/config.h:171
iterator
const typedef MCPhysReg * iterator
Definition: MCRegisterInfo.h:27
T
#define T(upbtypeconst, upbtype, ctype, default_value)
absl::random_internal::MakeSaltedSeedSeq
SSeq MakeSaltedSeedSeq(SSeq &&seq)
Definition: abseil-cpp/absl/random/internal/salted_seed_seq.h:145
absl::random_internal::SaltedSeedSeq::operator=
SaltedSeedSeq & operator=(const SaltedSeedSeq &)=delete
uint32_t
unsigned int uint32_t
Definition: stdint-msvc2008.h:80
absl::random_internal::SaltedSeedSeq::SaltedSeedSeq
SaltedSeedSeq(std::initializer_list< T > il)
Definition: abseil-cpp/absl/random/internal/salted_seed_seq.h:58
absl::random_internal::SaltedSeedSeq::generate
void generate(RandomAccessIterator begin, RandomAccessIterator end)
Definition: abseil-cpp/absl/random/internal/salted_seed_seq.h:68
absl::random_internal::SaltedSeedSeq::ContiguousAndUint32Tag
Definition: abseil-cpp/absl/random/internal/salted_seed_seq.h:93
ABSL_NAMESPACE_BEGIN
#define ABSL_NAMESPACE_BEGIN
Definition: third_party/abseil-cpp/absl/base/config.h:170
end
char * end
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:1008
absl::random_internal::SaltedSeedSeq::SaltedSeedSeq
SaltedSeedSeq()
Definition: abseil-cpp/absl/random/internal/salted_seed_seq.h:51
absl::optional::value_or
constexpr T value_or(U &&v) const &
Definition: abseil-cpp/absl/types/optional.h:506
result_type
const typedef int * result_type
Definition: bloaty/third_party/googletest/googlemock/test/gmock-matchers_test.cc:4325
absl::compare_internal::value_type
int8_t value_type
Definition: abseil-cpp/absl/types/compare.h:45
data
char data[kBufferLength]
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:1006
absl::random_internal::SaltedSeedSeq::DefaultTag
Definition: abseil-cpp/absl/random/internal/salted_seed_seq.h:94
n
int n
Definition: abseil-cpp/absl/container/btree_test.cc:1080
absl::random_internal::SaltedSeedSeq::generate_impl
void generate_impl(ContiguousAndUint32Tag, Contiguous begin, Contiguous end, size_t n)
Definition: abseil-cpp/absl/random/internal/salted_seed_seq.h:100
absl::random_internal::SaltedSeedSeq::inner_sequence_type
SSeq inner_sequence_type
Definition: abseil-cpp/absl/random/internal/salted_seed_seq.h:48
absl::random_internal::SaltedSeedSeq
Definition: abseil-cpp/absl/random/internal/salted_seed_seq.h:46
value
const char * value
Definition: hpack_parser_table.cc:165
absl::random_internal::is_salted_seed_seq
Definition: abseil-cpp/absl/random/internal/salted_seed_seq.h:130
absl::container_internal::internal_layout::EnableIf
typename std::enable_if< C, int >::type EnableIf
Definition: abseil-cpp/absl/container/internal/layout.h:316
absl::random_internal::MixIntoSeedMaterial
void MixIntoSeedMaterial(absl::Span< const uint32_t > sequence, absl::Span< uint32_t > seed_material)
Definition: abseil-cpp/absl/random/internal/seed_material.cc:216
absl::random_internal::SaltedSeedSeq::generate_impl
void generate_impl(DefaultTag, RandomAccessIterator begin, RandomAccessIterator, size_t n)
Definition: abseil-cpp/absl/random/internal/salted_seed_seq.h:113
absl::random_internal::SaltedSeedSeq::seq_
std::unique_ptr< SSeq > seq_
Definition: abseil-cpp/absl/random/internal/salted_seed_seq.h:125
std
Definition: grpcpp/impl/codegen/async_unary_call.h:407
absl::inlined_vector_internal::Iterator
Pointer< A > Iterator
Definition: abseil-cpp/absl/container/internal/inlined_vector.h:64
absl
Definition: abseil-cpp/absl/algorithm/algorithm.h:31
asyncio_get_stats.type
type
Definition: asyncio_get_stats.py:37
absl::out
char * out
Definition: abseil-cpp/absl/synchronization/mutex.h:1048
absl::InlinedVector
Definition: abseil-cpp/absl/container/inlined_vector.h:69
absl::random_internal::SaltedSeedSeq::param
void param(OutIterator out) const
Definition: abseil-cpp/absl/random/internal/salted_seed_seq.h:86
absl::random_internal::GetSaltMaterial
absl::optional< uint32_t > GetSaltMaterial()
Definition: abseil-cpp/absl/random/internal/seed_material.cc:248
absl::MakeConstSpan
constexpr Span< const T > MakeConstSpan(T *ptr, size_t size) noexcept
Definition: abseil-cpp/absl/types/span.h:707


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:09