Skip to content

Commit 735c861

Browse files
laramielcopybara-github
authored andcommitted
absl/random: Convert absl::BitGen / absl::InsecureBitGen to classes from aliases.
PiperOrigin-RevId: 733063284 Change-Id: If0af60b24bcc59b6fe3a6882aebd427b8b10f3a0
1 parent fd86aa7 commit 735c861

File tree

3 files changed

+89
-55
lines changed

3 files changed

+89
-55
lines changed

absl/random/BUILD.bazel

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ cc_library(
4242
deps = [
4343
":distributions",
4444
":seed_sequences",
45+
"//absl/base:config",
4546
"//absl/random/internal:nonsecure_base",
4647
"//absl/random/internal:pcg_engine",
47-
"//absl/random/internal:pool_urbg",
4848
"//absl/random/internal:randen_engine",
4949
],
5050
)

absl/random/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ absl_cc_library(
2424
LINKOPTS
2525
${ABSL_DEFAULT_LINKOPTS}
2626
DEPS
27+
absl::config
2728
absl::random_distributions
2829
absl::random_internal_nonsecure_base
2930
absl::random_internal_pcg_engine
30-
absl::random_internal_pool_urbg
3131
absl::random_internal_randen_engine
3232
absl::random_seed_sequences
3333
)

absl/random/random.h

+87-53
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@
3434
#include <cstdint>
3535
#include <random>
3636

37+
#include "absl/base/config.h"
3738
#include "absl/random/distributions.h" // IWYU pragma: export
38-
#include "absl/random/internal/nonsecure_base.h" // IWYU pragma: export
39-
#include "absl/random/internal/pcg_engine.h" // IWYU pragma: export
40-
#include "absl/random/internal/pool_urbg.h"
39+
#include "absl/random/internal/nonsecure_base.h"
40+
#include "absl/random/internal/pcg_engine.h"
4141
#include "absl/random/internal/randen_engine.h"
4242
#include "absl/random/seed_sequences.h" // IWYU pragma: export
4343

@@ -95,31 +95,46 @@ ABSL_NAMESPACE_BEGIN
9595
// types on modern x86, ARM, and PPC architectures.
9696
//
9797
// This type is thread-compatible, but not thread-safe.
98-
99-
// ---------------------------------------------------------------------------
100-
// absl::BitGen member functions
101-
// ---------------------------------------------------------------------------
102-
103-
// absl::BitGen::operator()()
104-
//
105-
// Calls the BitGen, returning a generated value.
106-
107-
// absl::BitGen::min()
108-
//
109-
// Returns the smallest possible value from this bit generator.
110-
111-
// absl::BitGen::max()
112-
//
113-
// Returns the largest possible value from this bit generator.
114-
115-
// absl::BitGen::discard(num)
116-
//
117-
// Advances the internal state of this bit generator by `num` times, and
118-
// discards the intermediate results.
119-
// ---------------------------------------------------------------------------
120-
121-
using BitGen = random_internal::NonsecureURBGBase<
122-
random_internal::randen_engine<uint64_t>>;
98+
class BitGen : private random_internal::NonsecureURBGBase<
99+
random_internal::randen_engine<uint64_t>> {
100+
using Base = random_internal::NonsecureURBGBase<
101+
random_internal::randen_engine<uint64_t>>;
102+
103+
public:
104+
using result_type = typename Base::result_type;
105+
106+
// BitGen()
107+
// BitGen(SeedSequence seed_seq)
108+
//
109+
// Copy disallowed.
110+
// Move allowed.
111+
using Base::Base;
112+
using Base::operator=;
113+
114+
// BitGen::min()
115+
//
116+
// Returns the smallest possible value from this bit generator.
117+
using Base::min;
118+
119+
// BitGen::max()
120+
//
121+
// Returns the largest possible value from this bit generator.
122+
using Base::max;
123+
124+
// BitGen::discard(num)
125+
//
126+
// Advances the internal state of this bit generator by `num` times, and
127+
// discards the intermediate results.
128+
using Base::discard;
129+
130+
// BitGen::operator()()
131+
//
132+
// Invoke the URBG, returning a generated value.
133+
using Base::operator();
134+
135+
using Base::operator==;
136+
using Base::operator!=;
137+
};
123138

124139
// -----------------------------------------------------------------------------
125140
// absl::InsecureBitGen
@@ -157,32 +172,51 @@ using BitGen = random_internal::NonsecureURBGBase<
157172
// `absl::InsecureBitGen` is not cryptographically secure.
158173
//
159174
// Prefer `absl::BitGen` over `absl::InsecureBitGen` as the general type is
160-
// often fast enough for the vast majority of applications.
161-
162-
using InsecureBitGen =
163-
random_internal::NonsecureURBGBase<random_internal::pcg64_2018_engine>;
164-
165-
// ---------------------------------------------------------------------------
166-
// absl::InsecureBitGen member functions
167-
// ---------------------------------------------------------------------------
168-
169-
// absl::InsecureBitGen::operator()()
170-
//
171-
// Calls the InsecureBitGen, returning a generated value.
172-
173-
// absl::InsecureBitGen::min()
175+
// often fast enough for the vast majority of applications. However, it is
176+
// reasonable to use `absl::InsecureBitGen` in tests or when using a URBG
177+
// in small isolated tasks such as in `std::shuffle`.
174178
//
175-
// Returns the smallest possible value from this bit generator.
176-
177-
// absl::InsecureBitGen::max()
178-
//
179-
// Returns the largest possible value from this bit generator.
180-
181-
// absl::InsecureBitGen::discard(num)
182-
//
183-
// Advances the internal state of this bit generator by `num` times, and
184-
// discards the intermediate results.
185-
// ---------------------------------------------------------------------------
179+
// This type is thread-compatible, but not thread-safe.
180+
class InsecureBitGen : private random_internal::NonsecureURBGBase<
181+
random_internal::pcg64_2018_engine> {
182+
using Base =
183+
random_internal::NonsecureURBGBase<random_internal::pcg64_2018_engine>;
184+
185+
public:
186+
using result_type = typename Base::result_type;
187+
188+
// InsecureBitGen()
189+
// InsecureBitGen(SeedSequence seed_seq)
190+
//
191+
// Copy disallowed.
192+
// Move allowed.
193+
using Base::Base;
194+
using Base::operator=;
195+
196+
// InsecureBitGen::min()
197+
//
198+
// Returns the smallest possible value from this bit generator.
199+
using Base::min;
200+
201+
// InsecureBitGen::max()
202+
//
203+
// Returns the largest possible value from this bit generator.
204+
using Base::max;
205+
206+
// InsecureBitGen::discard(num)
207+
//
208+
// Advances the internal state of this bit generator by `num` times, and
209+
// discards the intermediate results.
210+
using Base::discard;
211+
212+
// InsecureBitGen::operator()()
213+
//
214+
// Invoke the URBG, returning a generated value.
215+
using Base::operator();
216+
217+
using Base::operator==;
218+
using Base::operator!=;
219+
};
186220

187221
ABSL_NAMESPACE_END
188222
} // namespace absl

0 commit comments

Comments
 (0)