-
Notifications
You must be signed in to change notification settings - Fork 7
/
util.cpp
56 lines (50 loc) · 1.53 KB
/
util.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include "util.h"
#include <cstdlib>
#include <cstdio>
template <typename T>
void sequence(T n, std::vector<T>& out) {
out.clear();
out.reserve(static_cast<std::size_t>(n));
for (T i = 0; i < n; i++) out.push_back(i);
}
template <typename T>
void shuffle(std::vector<T>& v) {
unsigned int seed =
0; // std::chrono::system_clock::now().time_since_epoch().count();
std::shuffle(v.begin(), v.end(), std::default_random_engine(seed));
// std::size_t count = v.size();
// for (std::size_t i = 0; i < count; i++) {
// std::size_t j = i + (rand() % (count - i));
// std::swap(v[i], v[j]);
// }
}
template void sequence(uint64_t n, std::vector<uint64_t>& out);
template void shuffle(std::vector<uint64_t>& v);
template void sequence(uint32_t n, std::vector<uint32_t>& out);
template void shuffle(std::vector<uint32_t>& v);
void uniform_pdf(uint64_t n, std::vector<double>& out_pdf) {
out_pdf.clear();
out_pdf.reserve(n);
for (uint64_t i = 0; i < n; i++) out_pdf.push_back(1.);
}
void pdf_to_cdf(const std::vector<double>& pdf, std::vector<double>& out_cdf) {
std::size_t count = pdf.size();
out_cdf.clear();
out_cdf.reserve(count);
double s = 0.;
for (std::size_t i = 0; i < count; i++) {
s += pdf[i];
out_cdf.push_back(s);
}
}
// def sample(cdf, count):
// """Gets samples from CDF."""
// r = random.random
// b = bisect.bisect_left
// s = cdf[-1]
// result = [0] * count
// for i in range(count):
// v = r() * s
// k = b(cdf, v)
// result[i] = k
// return result