forked from shangqimonash/SDd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBid.cpp
190 lines (166 loc) · 3.88 KB
/
Bid.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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include <algorithm>
#include "Bid.h"
Bid::Bid() {
std::fill(id.begin(), id.end(), 0);
}
Bid::Bid(string value) {
std::fill(id.begin(), id.end(), 0);
std::copy(value.begin(), value.end(), id.begin());
}
Bid::~Bid() {
}
Bid::Bid(int value) {
std::fill(id.begin(), id.end(), 0);
for (int i = 0; i < 4; i++) {
id[3 - i] = (byte_t) (value >> (i * 8));
}
}
Bid::Bid(std::array<byte_t, ID_SIZE> value) {
std::copy(value.begin(), value.end(), id.begin());
}
Bid Bid::operator++() {
id[ID_SIZE - 1]++;
for (int i = ID_SIZE - 1; i > 0; i--) {
if (id[i] == 0) {
id[i - 1]++;
} else {
break;
}
}
return *this;
}
Bid& Bid::operator=(int other) {
std::fill(id.begin(), id.end(), 0);
for (int i = 0; i < 4; i++) {
id[3 - i] = (byte_t) (other >> (i * 8));
}
return *this;
}
bool Bid::operator!=(const int rhs) const {
for (int i = 0; i < 4; i++) {
if (id[3 - i] != (rhs >> (i * 8))) {
return true;
}
}
return false;
}
bool Bid::operator!=(const Bid rhs) const {
for (int i = 0; i < ID_SIZE; i++) {
if (id[i] != rhs.id[i]) {
return true;
}
}
return false;
}
bool Bid::operator<(const Bid& b)const {
for (int i = 0; i < ID_SIZE; i++) {
if (id[i] < b.id[i]) {
return true;
} else if (id[i] > b.id[i]) {
return false;
}
}
return false;
}
bool Bid::operator<(int b) const {
int result = 0;
result += id[3];
result += (id[2] << 8);
result += id[1] << 16;
result += id[0] << 24;
return result < b;
}
bool Bid::operator<=(const Bid& b)const {
for (int i = 0; i < ID_SIZE; i++) {
if (id[i] < b.id[i]) {
return true;
} else if (id[i] > b.id[i]) {
return false;
}
}
return true;
}
bool Bid::operator>(const Bid& b)const {
for (int i = 0; i < ID_SIZE; i++) {
if (id[i] > b.id[i]) {
return true;
} else if (id[i] < b.id[i]) {
return false;
}
}
return false;
}
bool Bid::operator>=(const Bid& b)const {
for (int i = 0; i < ID_SIZE; i++) {
if (id[i] > b.id[i]) {
return true;
} else if (id[i] < b.id[i]) {
return false;
}
}
return true;
}
bool Bid::operator>=(int b) const {
int result = 0;
result += id[3];
result += (id[2] << 8);
result += id[1] << 16;
result += id[0] << 24;
return result >= b;
}
bool Bid::operator==(const int rhs) const {
for (int i = 0; i < 4; i++) {
if (id[3 - i] != (rhs >> (i * 8))) {
return false;
}
}
return true;
}
bool Bid::operator==(const Bid rhs) const {
for (int i = 0; i < ID_SIZE; i++) {
if (id[i] != rhs.id[i]) {
return false;
}
}
return true;
}
Bid& Bid::operator=(std::vector<byte_t> other) {
for (int i = 0; i < ID_SIZE; i++) {
id[i] = other[i];
}
return *this;
}
Bid& Bid::operator=(Bid const &other) {
for (int i = 0; i < ID_SIZE; i++) {
id[i] = other.id[i];
}
return *this;
}
int Bid::getValue() {
int result = 0;
result += id[3];
result += (id[2] << 8);
result += id[1] << 16;
result += id[0] << 24;
return result;
}
void Bid::setValue(int other) {
std::fill(id.begin(), id.end(), 0);
for (int i = 0; i < 4; i++) {
id[3 - i] = (byte_t) (other >> (i * 8));
}
}
ostream& operator<<(ostream &o, Bid& bid) {
for (int i = 0; i < ID_SIZE; i++) {
o << (int) bid.id[i] << "-";
}
return o;
}
Bid Bid::get_bid(string input, int num) {
std::array< uint8_t, ID_SIZE> value{};
std::fill(value.begin(), value.end(), 0);
std::copy(input.begin(), input.end(), value.begin());
*(int*) (&value[AES_BLOCK_SIZE - 4]) = num;
Bid res(value);
return res;
}