-
Notifications
You must be signed in to change notification settings - Fork 16
/
ID_Value.Tc
155 lines (136 loc) · 3.37 KB
/
ID_Value.Tc
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include "ID_Value.h"
ID_Value::ID_Value() {
setNull();
}
ID_Value::ID_Value(byte newid[20]) {
for(int i=0; i<20; i++)
id[i] = newid[i];
null = false;
}
ID_Value::ID_Value(rpc_hash newid) {
//const char * strnew = newid.cstr();
//LOG_WARN << strnew[0];
//id[0] = strnew[0];
for(int i=0; i<20; i++)
id[i] = (char)newid[i];
null = false;
}
ID_Value::~ID_Value() {
}
void ID_Value::randomize() {
for(int i=0; i<20; i++)
id[i] = rand() % 256;
null = false;
}
bool ID_Value::isNull() {
return null;
}
void ID_Value::setNull() {
for(int i=0; i<20; i++)
id[i] = 0;
null = true;
}
int ID_Value::hex_to_int(char x) {
if(x >= '0' && x <= '9') {
return x-'0';
} else if(x >= 'a' && x <= 'f') {
return x-'a' + 10;
} else {
assert(false);
}
}
void ID_Value::fromString(const string s) {
string ret;
assert(s.length() == 40);
for(int i=0; i<20; i++) {
int high = hex_to_int(s[i*2]);
int low = hex_to_int(s[i*2+1]);
id[i] = (high << 4) | (low);
}
}
string ID_Value::toString() const {
static const char hexval[16] = {'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
string ret;
for(int i=0; i<20; i++) {
ret += hexval[((id[i] >> 4) & 0xF)];
ret += hexval[(id[i]) & 0x0F];
}
return ret;
}
rpc_hash ID_Value::get_rpc_id() const {
//this function is fucked up
//mstr m(20);
vec<char> m;
m.setsize(20);
//LOG_WARN << "test1\n";
rpc_hash ret;
//LOG_WARN << "test2\n";
for(int i=0; i<20; i++) {
//LOG_WARN << "test3 " << (int)id[i] << "\n";
m[i] = (char)id[i];
}
//LOG_WARN << "test4\n";
rpc_vec<char, 20> dude;
dude = m;
//LOG_WARN << "test0\n";
ret = dude;
//LOG_WARN << "test5\n";
//LOG_WARN << "before check\n";
//LOG_WARN << "After check\n";
return ret;
}
void ID_Value::set_from_rpc(rpc_hash newid) {
for(int i=0; i<20; i++)
id[i] = (char)newid[i];
null = false;
}
bool ID_Value::between(ID_Value x, ID_Value y) const {
//Three cases because the circle could have wrapped around
// 1. x < this <= y
// 2. this <= y < x (wraps)
// 3. y < x < this (wraps)
return ((*this > x && *this <= y) || (*this <= y && x > y) || (y < x && x < *this));
}
bool ID_Value::betweenIncl(ID_Value x, ID_Value y) const {
//Three cases because the circle could have wrapped around
// 1. x <= this <= y
// 2. this <= y <= x (wraps)
// 3. y <= x <= this (wraps)
return ((*this >= x && *this <= y) || (*this <= y && x >= y) || (y <= x && x <= *this));
}
bool ID_Value::operator == (const ID_Value &other) const {
for(int i=0; i<20; i++)
if(other.id[i] != this->id[i])
return false;
return true;
}
bool ID_Value::operator < (const ID_Value &other) const {
for(int i=0; i<20; i++) {
if(this->id[i] < other.id[i]) return true;
if(this->id[i] > other.id[i]) return false;
}
return false;
}
bool ID_Value::operator > (const ID_Value &other) const {
for(int i=0; i<20; i++) {
if(this->id[i] > other.id[i]) return true;
if(this->id[i] < other.id[i]) return false;
}
return false;
}
bool ID_Value::operator <= (const ID_Value &other) const { return !(*this > other); }
bool ID_Value::operator >= (const ID_Value &other) const { return !(*this < other); }
bool ID_Value::operator != (const ID_Value &other) const { return !(*this == other); }
ID_Value & ID_Value::operator ++ () {
unsigned int i = 20;
do {
i--;
if(id[i] == 255) {
id[i] = 0;
} else {
id[i]++;
}
} while(i > 0 && id[i] == 0);
return *this;
}