-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.cpp
54 lines (39 loc) · 1.24 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
#include "util.hpp"
#include <cassert>
#include <cstdlib>
// Return a random number uniformly distributed in [0, 1]
double rand01() {
return (double)rand() / (double)RAND_MAX;
}
// Return a random integer between [0, end)
unsigned int randRange(unsigned int end) {
assert(end <= RAND_MAX);
// Generate an integer between [0, end) uniformly
int r = rand();
const int remainder = RAND_MAX % end;
while (r < remainder) r = rand();
return r % end;
}
// Return a random number between [start, end)
int randRange(int start, int end) {
assert(start < end);
return start + randRange(end - start);
}
// Decodes the value encoded on the end of a list of symbols
unsigned int decode(const symbol_list_t &symlist, unsigned int bits) {
assert(bits <= symlist.size());
unsigned int value = 0;
symbol_list_t::const_reverse_iterator it = symlist.rbegin();
symbol_list_t::const_reverse_iterator end = it + bits;
for( ; it != end; ++it) {
value = (*it ? 1 : 0) + 2 * value;
}
return value;
}
// Encodes a value onto the end of a symbol list using "bits" symbols
void encode(symbol_list_t &symlist, unsigned int value, unsigned int bits) {
for (unsigned int i = 0; i < bits; i++, value /= 2) {
symbol_t sym = value & 1;
symlist.push_back(sym);
}
}