-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathID.cpp
More file actions
70 lines (58 loc) · 1.46 KB
/
ID.cpp
File metadata and controls
70 lines (58 loc) · 1.46 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "ID.h"
#include "Utils.h"
std::mt19937 ID::g_random_generator;
ID::ID() : m_id({0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}) {}
const std::array<uint8_t, DIGEST_BYTES>& ID::id() const {
return m_id;
}
ID ID::uniformRandomize()
{
std::uniform_int_distribution<> range(0, UINT8_MAX);
for(auto it = m_id.begin(); it != m_id.end(); ++it) {
*it = range(g_random_generator);
}
return *this;
}
ID ID::normalRandomize()
{
std::normal_distribution<> range{128, 127};
for(auto it = m_id.begin(); it != m_id.end(); ++it) {
*it = std::round(range(g_random_generator));
}
return *this;
}
int ID::equalPrefixLength(const ID & anotherId) const
{
int len = 0;
int j = 0;
for(; j < DIGEST_BYTES && m_id[j] == anotherId.m_id[j]; ++j)
{
len += 8;
}
if(j < DIGEST_BYTES) {
for(int i = 7; !(((m_id[j] >> i)&1) ^ ((anotherId.m_id[j] >> i)&1)) && i >= 0; --i)
{
++len;
}
}
return len;
}
bool operator < (const ID & l, const ID & r) {
return l.m_id < r.m_id;
}
bool operator == (const ID & l, const ID & r) {
return l.m_id == r.m_id;
}
bool operator != (const ID & l, const ID & r) {
return !(l == r);
}
std::ostream& operator<<(std::ostream& out, const ID& id) {
for(auto it = id.m_id.begin(); it != id.m_id.end(); ++it) {
out << std::hex << int(*it);
}
return out;
}
// MOD
ID createRandomId() {
return ID().uniformRandomize();
}