-
Notifications
You must be signed in to change notification settings - Fork 1
/
utilities.cpp
325 lines (283 loc) · 7.73 KB
/
utilities.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#include "utilities.h"
#define DEBUG
#ifdef DEBUG
# define D(x) x
#else
# define D(x)
#endif // DEBUG
bool Proposal::operator==(const Proposal &p2) const {
return ((this->client_id == p2.client_id) && (this->chat_id == p2.chat_id) && (this->msg == p2.msg));
}
bool Triple::operator==(const Triple &t2) const {
return ((this->b == t2.b) && (this->s == t2.s) && (this->p == t2.p));
}
bool Ballot::operator>(const Ballot &b2) const {
if (this->seq_num > b2.seq_num)
return true;
else if (this->seq_num < b2.seq_num)
return false;
else if (this->seq_num == b2.seq_num) {
if (this->id > b2.id)
return true;
else
return false;
}
}
bool Ballot::operator<(const Ballot &b2) const {
return !((*this) >= b2);
}
bool Ballot::operator==(const Ballot &b2) const {
return ((this->seq_num == b2.seq_num) && (this->id == b2.id));
}
bool Ballot::operator>=(const Ballot &b2) const {
return ((*this) == b2 || (*this) > b2);
}
bool Ballot::operator<=(const Ballot &b2) const {
return !((*this) > b2);
}
// bool Ballot::operator()(const Ballot &b) const {
// return ((hash<int>() (b.id)
// ^ ( hash<int>() (b.seq_num) <<1 )));
// }
// bool Proposal::operator()(const Proposal &p) const {
// return ((hash<string>()(p.client_id)
// ^ (hash<string>()(p.chat_id) << 1)) >> 1)
// ^ (hash<string>()(p.msg) << 1);
// }
// bool Triple::operator()(const Triple &t) const{
// return ((hash<Ballot>()(t.b)
// ^ (hash<int>()(t.s) << 1)) >> 1)
// ^ (hash<Proposal>()(t.p) << 1);
// }
std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) {
std::stringstream ss(s);
std::string item;
while (std::getline(ss, item, delim)) {
elems.push_back(item);
}
return elems;
}
std::vector<std::string> split(const std::string &s, char delim) {
std::vector<std::string> elems;
if(s=="")
return elems;
split(s, delim, elems);
return elems;
}
string tripleToString(const Triple& t)
{
string s = to_string(t.b.id);
s+=kInternalStructDelim;
s += to_string(t.b.seq_num);
s+=kInternalStructDelim;
s += to_string(t.s);
s+=kInternalStructDelim;
s += t.p.client_id;
s+=kInternalStructDelim;
s += t.p.chat_id;
s+=kInternalStructDelim;
s += t.p.msg;
return s;
}
void union_set(unordered_set<Triple>& s1, unordered_set<Triple>&s2)
{
unordered_set <Triple> un;
unordered_set<Triple>::iterator got;
if(s2.size())
{
for(auto it=s2.begin(); it!=s2.end(); it++)
{
got = s1.find(*it);
if(got==s1.end())
s1.insert(*it);
}
}
}
string proposalToString(const Proposal& p)
{
string s = p.client_id;
s+=kInternalStructDelim;
s += p.chat_id;
s+=kInternalStructDelim;
s += p.msg;
return s;
}
string ballotToString(const Ballot& b)
{
string s = to_string(b.id);
s+=kInternalStructDelim;
s += to_string(b.seq_num);
return s;
}
string tripleSetToString(const unordered_set<Triple>& st)
{
string rval;
for (auto it=st.begin(); it!=st.end(); it++)
{
if(it!=st.begin())
rval += kInternalSetDelim;
rval += tripleToString(*it);
}
return rval;
}
string allDecisionsToString(const map<int, Proposal>& d)
{
string rval;
for (auto it=d.begin(); it!=d.end(); it++)
{
if(it!=d.begin())
rval += kInternalSetDelim;
rval += decisionToStringForAll(it->first, it->second);
}
return rval;
}
string decisionToStringForAll(const int& s, const Proposal& p)
{
string rval;
rval += to_string(s);
rval += kInternalStructDelim;
rval += proposalToString(p);
return rval;
}
string decisionToString(const int& s, const Proposal& p)
{
string rval;
rval += to_string(s);
rval += kInternalDelim;
rval += proposalToString(p);
return rval;
}
void stringToDecision(const string& dec, int& s, Proposal& p)
{
vector<string> parts = split(dec, kInternalStructDelim[0]);
if(parts.size()!=1)
D(cout<<"Cant convert to decision. Error."<<endl;)
s = stoi(parts[0]);
p = stringToProposal(parts[1]);
}
void stringToDecisionForAll(const string& dec, int& s, Proposal& p)
{
vector<string> parts = split(dec, kInternalStructDelim[0]);
// D(cout<<"parts size is "<<parts.size()<<endl;)
// D(cout<<parts[0]<<" "<<parts[1]<<" "<<parts[2]<<" "<<parts[3]<<endl;)
if(parts.size()!=4)
{
D(cout<<"Cant convert to decision. Error."<<endl;)
D(cout<<parts.size()<<" ----------------------------------------"<<dec<<endl;)
return;
}
s = stoi(parts[0]);
string prop = parts[1]+kInternalStructDelim+parts[2]+kInternalStructDelim+parts[3];
p = stringToProposal(prop);
}
void stringToAllDecisions(string s, map<int, Proposal>& decs)
{
decs.clear();
// cout<<"string is "<<s;
vector<string> decisions = split(s, kInternalSetDelim[0]);
// cout<<". dec size is "<<decisions.size()<<endl;
for(auto &d : decisions)
{
int s;
Proposal p;
// cout<<"one dec: "<<d<<endl;
stringToDecisionForAll(d, s, p);
decs[s]=p;
}
}
Triple stringToTriple(const string& s)
{
Triple t;
Proposal p;
Ballot b;
vector<string> parts = split(s, kInternalStructDelim[0]);
if(parts.size()==6)
{
b.id = stoi(parts[0]);
b.seq_num = stoi(parts[1]);
t.b = b;
t.s = stoi(parts[2]);
p.client_id = parts[3];
p.chat_id = parts[4];
p.msg = parts[5];
t.p = p;
}
return t;
}
unordered_set<Triple> stringToTripleSet(const string& s)
{
unordered_set<Triple> st;
vector<string> parts = split(s, kInternalSetDelim[0]);
for (auto it=parts.begin(); it!=parts.end(); it++)
{
Triple t = stringToTriple(*it);
st.insert(t);
}
}
Ballot stringToBallot(const string& s)
{
Ballot b;
vector<string> parts = split(s, kInternalStructDelim[0]);
// cout<<"string to ballot:"<<s<<parts.size()<<endl;
b.id = stoi(parts[0]);
b.seq_num = stoi(parts[1]);
return b;
}
Proposal stringToProposal(const string& s)
{
Proposal p;
vector<string> parts = split(s, kInternalStructDelim[0]);
p.client_id = parts[0];
p.chat_id = parts[1];
p.msg = parts[2];
return p;
}
map<int, Proposal> pmax(const unordered_set<Triple> &pvalues)
{
map<int, Proposal> rval;
// set<int> slot_added;
for(auto it=pvalues.begin(); it!=pvalues.end(); it++)
{
if(rval.find(it->s)==rval.end())
{
Ballot b_max = it->b;
Proposal p_max = it->p;
for(auto it2 = pvalues.begin(); it2!=pvalues.end(); it2++)
{
if(it2->s != it->s)continue;
if(it2->b > b_max)
{
b_max = it2->b;
p_max = it2->p;
}
}
rval[it->s] = p_max;
}
}
return rval;
}
map<int, Proposal> pairxor(const map<int, Proposal> &x,const map<int, Proposal> &y)
{
map<int, Proposal> rval = y;
for(auto itx = x.begin(); itx!=x.end(); itx++)
{
auto it = y.find(itx->first);
if(it==y.end())
{
rval[itx->first] = itx->second;
}
}
return rval;
}
/**
* creates a new thread
* @param f function pointer to be passed to the new thread
* @param arg arguments for the function f passed to the thread
* @param thread thread identifier for new thread
*/
void CreateThread(void* (*f)(void* ), void* arg, pthread_t &thread) {
if (pthread_create(&thread, NULL, f, arg)) {
D(cout << "U " << ": ERROR: Unable to create thread" << endl;)
pthread_exit(NULL);
}
}