[base] Added UniformRandom helper class.

Signed-off-by: Viktor Govako <viktor.govako@gmail.com>
This commit is contained in:
Viktor Govako 2024-06-10 17:48:15 -03:00 committed by Viktor Havaka
parent 250bf24c71
commit 3b479fa18f
5 changed files with 21 additions and 41 deletions

View file

@ -56,7 +56,6 @@ set(SRC
observer_list.hpp observer_list.hpp
pprof.cpp pprof.cpp
pprof.hpp pprof.hpp
random.cpp
random.hpp random.hpp
range_iterator.hpp range_iterator.hpp
ref_counted.hpp ref_counted.hpp

View file

@ -1,21 +0,0 @@
#include "base/random.hpp"
#include <numeric>
namespace base
{
std::vector<size_t> RandomSample(size_t n, size_t k, std::minstd_rand & rng)
{
std::vector<size_t> result(std::min(k, n));
std::iota(result.begin(), result.end(), 0);
for (size_t i = k; i < n; ++i)
{
size_t const j = rng() % (i + 1);
if (j < k)
result[j] = i;
}
return result;
}
} // base

View file

@ -1,10 +1,22 @@
#pragma once #pragma once
#include <limits>
#include <random> #include <random>
#include <vector>
namespace base namespace base
{ {
// Selects a fair random subset of size min(|n|, |k|) from [0, 1, 2, ..., n - 1]. template <class T> class UniformRandom
std::vector<size_t> RandomSample(size_t n, size_t k, std::minstd_rand & rng); {
} // base static_assert(std::is_integral<T>::value);
std::random_device m_rd;
std::mt19937 m_gen;
std::uniform_int_distribution<T> m_distr;
public:
UniformRandom(T min, T max) : m_gen(m_rd()), m_distr(min, max) {}
UniformRandom() : UniformRandom(std::numeric_limits<T>::min(), std::numeric_limits<T>::max()) {}
T operator()() { return m_distr(m_gen); }
};
} // namespace base

View file

@ -4,10 +4,10 @@
#include "base/file_name_utils.hpp" #include "base/file_name_utils.hpp"
#include "base/logging.hpp" #include "base/logging.hpp"
#include "base/random.hpp"
#include "base/string_utils.hpp" #include "base/string_utils.hpp"
#include <algorithm> #include <algorithm>
#include <random>
#include <thread> #include <thread>
#include "private.h" #include "private.h"
@ -18,17 +18,15 @@ namespace
{ {
std::string RandomString(size_t length) std::string RandomString(size_t length)
{ {
/// @todo Used for temp file name, so lower-upper case is strange here, no?
static std::string_view constexpr kCharset = static std::string_view constexpr kCharset =
"0123456789" "0123456789"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"; "abcdefghijklmnopqrstuvwxyz";
std::random_device rd;
std::mt19937 gen(rd()); base::UniformRandom<size_t> rand(0, kCharset.size() - 1);
std::uniform_int_distribution<size_t> dis(0, kCharset.size() - 1);
std::string str(length, 0); std::string str(length, 0);
std::generate_n(str.begin(), length, [&]() { std::generate_n(str.begin(), length, [&rand]() { return kCharset[rand()]; });
return kCharset[dis(gen)];
});
return str; return str;
} }

View file

@ -78,9 +78,7 @@
3D78157B1F3D89EC0068B6AC /* waiter.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 3D78157A1F3D89EC0068B6AC /* waiter.hpp */; }; 3D78157B1F3D89EC0068B6AC /* waiter.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 3D78157A1F3D89EC0068B6AC /* waiter.hpp */; };
56290B8A206D0887003892E0 /* lru_cache.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 56290B89206D0887003892E0 /* lru_cache.hpp */; }; 56290B8A206D0887003892E0 /* lru_cache.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 56290B89206D0887003892E0 /* lru_cache.hpp */; };
564BB445206E89ED00BDD211 /* fifo_cache.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 564BB444206E89ED00BDD211 /* fifo_cache.hpp */; }; 564BB445206E89ED00BDD211 /* fifo_cache.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 564BB444206E89ED00BDD211 /* fifo_cache.hpp */; };
564C197B254AF896007471CE /* optional_lock_guard.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 564C197A254AF895007471CE /* optional_lock_guard.hpp */; };
564C197F254AF8AF007471CE /* optional_lock_guard_tests.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 564C197E254AF8AF007471CE /* optional_lock_guard_tests.cpp */; }; 564C197F254AF8AF007471CE /* optional_lock_guard_tests.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 564C197E254AF8AF007471CE /* optional_lock_guard_tests.cpp */; };
56B1A0741E69DE4D00395022 /* random.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 56B1A0711E69DE4D00395022 /* random.cpp */; };
56B1A0751E69DE4D00395022 /* random.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 56B1A0721E69DE4D00395022 /* random.hpp */; }; 56B1A0751E69DE4D00395022 /* random.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 56B1A0721E69DE4D00395022 /* random.hpp */; };
56B1A0761E69DE4D00395022 /* small_set.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 56B1A0731E69DE4D00395022 /* small_set.hpp */; }; 56B1A0761E69DE4D00395022 /* small_set.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 56B1A0731E69DE4D00395022 /* small_set.hpp */; };
670E39441C46C76900E9C0A6 /* sunrise_sunset.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 670E39421C46C76900E9C0A6 /* sunrise_sunset.cpp */; }; 670E39441C46C76900E9C0A6 /* sunrise_sunset.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 670E39421C46C76900E9C0A6 /* sunrise_sunset.cpp */; };
@ -228,9 +226,7 @@
56290B8B206D0937003892E0 /* lru_cache_tests.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = lru_cache_tests.cpp; sourceTree = "<group>"; }; 56290B8B206D0937003892E0 /* lru_cache_tests.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = lru_cache_tests.cpp; sourceTree = "<group>"; };
564BB444206E89ED00BDD211 /* fifo_cache.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = fifo_cache.hpp; sourceTree = "<group>"; }; 564BB444206E89ED00BDD211 /* fifo_cache.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = fifo_cache.hpp; sourceTree = "<group>"; };
564BB446206E8A4D00BDD211 /* fifo_cache_test.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = fifo_cache_test.cpp; sourceTree = "<group>"; }; 564BB446206E8A4D00BDD211 /* fifo_cache_test.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = fifo_cache_test.cpp; sourceTree = "<group>"; };
564C197A254AF895007471CE /* optional_lock_guard.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = optional_lock_guard.hpp; sourceTree = "<group>"; };
564C197E254AF8AF007471CE /* optional_lock_guard_tests.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = optional_lock_guard_tests.cpp; sourceTree = "<group>"; }; 564C197E254AF8AF007471CE /* optional_lock_guard_tests.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = optional_lock_guard_tests.cpp; sourceTree = "<group>"; };
56B1A0711E69DE4D00395022 /* random.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = random.cpp; sourceTree = "<group>"; };
56B1A0721E69DE4D00395022 /* random.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = random.hpp; sourceTree = "<group>"; }; 56B1A0721E69DE4D00395022 /* random.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = random.hpp; sourceTree = "<group>"; };
56B1A0731E69DE4D00395022 /* small_set.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = small_set.hpp; sourceTree = "<group>"; }; 56B1A0731E69DE4D00395022 /* small_set.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = small_set.hpp; sourceTree = "<group>"; };
670E39421C46C76900E9C0A6 /* sunrise_sunset.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = sunrise_sunset.cpp; sourceTree = "<group>"; }; 670E39421C46C76900E9C0A6 /* sunrise_sunset.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = sunrise_sunset.cpp; sourceTree = "<group>"; };
@ -451,10 +447,8 @@
397DC50C22BB8EBF007126DB /* non_intersecting_intervals.hpp */, 397DC50C22BB8EBF007126DB /* non_intersecting_intervals.hpp */,
675341A21A3F57E400A0A8C3 /* normalize_unicode.cpp */, 675341A21A3F57E400A0A8C3 /* normalize_unicode.cpp */,
672DD4B71E04255F0078E13C /* observer_list.hpp */, 672DD4B71E04255F0078E13C /* observer_list.hpp */,
564C197A254AF895007471CE /* optional_lock_guard.hpp */,
3917FA5A211E00BA00937DF4 /* pprof.cpp */, 3917FA5A211E00BA00937DF4 /* pprof.cpp */,
3917FA5B211E00BB00937DF4 /* pprof.hpp */, 3917FA5B211E00BB00937DF4 /* pprof.hpp */,
56B1A0711E69DE4D00395022 /* random.cpp */,
56B1A0721E69DE4D00395022 /* random.hpp */, 56B1A0721E69DE4D00395022 /* random.hpp */,
672DD4B81E0425600078E13C /* range_iterator.hpp */, 672DD4B81E0425600078E13C /* range_iterator.hpp */,
672DD4B91E0425600078E13C /* ref_counted.hpp */, 672DD4B91E0425600078E13C /* ref_counted.hpp */,
@ -556,7 +550,6 @@
397DC50D22BB8EBF007126DB /* bidirectional_map.hpp in Headers */, 397DC50D22BB8EBF007126DB /* bidirectional_map.hpp in Headers */,
3446C6711DDCA96300146687 /* dfa_helpers.hpp in Headers */, 3446C6711DDCA96300146687 /* dfa_helpers.hpp in Headers */,
6753420C1A3F57E400A0A8C3 /* threaded_list.hpp in Headers */, 6753420C1A3F57E400A0A8C3 /* threaded_list.hpp in Headers */,
564C197B254AF896007471CE /* optional_lock_guard.hpp in Headers */,
675341DB1A3F57E400A0A8C3 /* exception.hpp in Headers */, 675341DB1A3F57E400A0A8C3 /* exception.hpp in Headers */,
6753453E1A3F6F6A00A0A8C3 /* message.hpp in Headers */, 6753453E1A3F6F6A00A0A8C3 /* message.hpp in Headers */,
39BC0FD01FD057FA00B6C276 /* control_flow.hpp in Headers */, 39BC0FD01FD057FA00B6C276 /* control_flow.hpp in Headers */,
@ -747,7 +740,6 @@
670E39441C46C76900E9C0A6 /* sunrise_sunset.cpp in Sources */, 670E39441C46C76900E9C0A6 /* sunrise_sunset.cpp in Sources */,
6753420E1A3F57E400A0A8C3 /* timer.cpp in Sources */, 6753420E1A3F57E400A0A8C3 /* timer.cpp in Sources */,
675341F61A3F57E400A0A8C3 /* shared_buffer_manager.cpp in Sources */, 675341F61A3F57E400A0A8C3 /* shared_buffer_manager.cpp in Sources */,
56B1A0741E69DE4D00395022 /* random.cpp in Sources */,
675341DA1A3F57E400A0A8C3 /* exception.cpp in Sources */, 675341DA1A3F57E400A0A8C3 /* exception.cpp in Sources */,
3D74EF111F8B902C0081202C /* suffix_array.cpp in Sources */, 3D74EF111F8B902C0081202C /* suffix_array.cpp in Sources */,
3917FA57211E009700937DF4 /* geo_object_id.cpp in Sources */, 3917FA57211E009700937DF4 /* geo_object_id.cpp in Sources */,