Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/base
diff options
context:
space:
mode:
authorVladimir Byko-Ianko <v.bykoianko@corp.mail.ru>2017-03-03 19:57:03 +0300
committerVladimir Byko-Ianko <v.bykoianko@corp.mail.ru>2017-03-06 10:59:45 +0300
commit8186a78cd634a863999cb30c438b98876d24881c (patch)
tree08ba50e12ca4476ec2465cac435a94b37836a93a /base
parent4d66ab38cec6c276a4bb21abb6a33664d040470c (diff)
Moving RandomSample to base.
Diffstat (limited to 'base')
-rw-r--r--base/base.pro2
-rw-r--r--base/random.cpp22
-rw-r--r--base/random.hpp9
3 files changed, 33 insertions, 0 deletions
diff --git a/base/base.pro b/base/base.pro
index 7eda31055f..71073004a6 100644
--- a/base/base.pro
+++ b/base/base.pro
@@ -18,6 +18,7 @@ SOURCES += \
logging.cpp \
lower_case.cpp \
normalize_unicode.cpp \
+ random.cpp \
shared_buffer_manager.cpp \
src_point.cpp \
string_format.cpp \
@@ -59,6 +60,7 @@ HEADERS += \
mutex.hpp \
newtype.hpp \
observer_list.hpp \
+ random.hpp \
range_iterator.hpp \
ref_counted.hpp \
regexp.hpp \
diff --git a/base/random.cpp b/base/random.cpp
new file mode 100644
index 0000000000..a6decf8498
--- /dev/null
+++ b/base/random.cpp
@@ -0,0 +1,22 @@
+#include "base/random.hpp"
+
+#include <algorithm>
+#include <numeric>
+
+namespace my
+{
+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;
+}
+} // my
diff --git a/base/random.hpp b/base/random.hpp
new file mode 100644
index 0000000000..b17189e36c
--- /dev/null
+++ b/base/random.hpp
@@ -0,0 +1,9 @@
+#pragma once
+
+#include <random>
+#include <vector>
+
+namespace my
+{
+std::vector<size_t> RandomSample(size_t n, size_t k, std::minstd_rand & rng);
+} // my