/
State.cpp
567 lines (471 loc) · 12.2 KB
/
State.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
#include <iostream>
#include <sstream>
#include "State.h"
#include "global.h"
using namespace std;
State::State():m_isTop(false)
{
}
State::State(const State& a_other)
{
debug("State::CTOR - Copying state: " + string(a_other));
m_isTop = a_other.isTop();
if (m_isTop)
{
return;
}
// Probably better to do outside of CTOR
const NodeSet& otherRootSet = a_other.getRootSet();
for (NodeSetConstIter iter = otherRootSet.begin(); iter != otherRootSet.end(); iter++)
{
TreeNode* node = new TreeNode(*(*iter));
m_rootSet.insert(node);
}
buildVariableMap();
debug("State::CTOR - Copied state: " + string(*this));
}
State::~State()
{
for (NodeSetIter iter = m_rootSet.begin(); iter != m_rootSet.end(); iter++)
{
delete (*iter);
}
}
bool State::checkValidity()
{
if (m_isTop)
{
return false;
}
for (NodeSetIter iter = m_rootSet.begin(); iter != m_rootSet.end(); iter++)
{
TreeNode* node = *iter;
if (node == NULL)
{
return false;
}
if (!(node->checkValidity(INT_MIN, INT_MAX)))
{
return false;
}
}
return true;
}
void State::join(const State& a_otherState)
{
NodePairSet commonRoots;
NodeSet myUniqueRoots;
NodeSet otherUniqueRoots;
debug("State::join - joining state: " + string(*this));
debug("State::join - with state: " + string(a_otherState));
divideTrees(a_otherState, commonRoots, myUniqueRoots, otherUniqueRoots);
m_rootSet.clear();
for (NodePairSetIter iter = commonRoots.begin(); iter != commonRoots.end(); iter++)
{
TreeNode* myTree = iter->first;
TreeNode* otherTree = iter->second;
myTree->join(otherTree);
m_rootSet.insert(myTree);
}
for (NodeSetIter iter = myUniqueRoots.begin(); iter != myUniqueRoots.end(); iter++)
{
m_rootSet.insert(*iter);
}
for (NodeSetIter iter = otherUniqueRoots.begin(); iter != otherUniqueRoots.end(); iter++)
{
TreeNode* node = new TreeNode(*(*iter));
m_rootSet.insert(node);
}
buildVariableMap();
debug("State::join - joined state: " + string(*this));
}
void State::buildVariableMap()
{
m_variableMap.clear();
for (NodeSetIter iter = m_rootSet.begin(); iter != m_rootSet.end(); iter++)
{
addNodeNames(*iter);
}
}
void State::addNodeNames(TreeNode* a_node)
{
if (a_node == NULL)
{
return;
}
const NameSet& nameSet = a_node->getNameSet();
for (NameSetConstIter iter = nameSet.begin(); iter != nameSet.end(); iter++)
{
addNodeName(*iter, a_node);
}
addNodeNames(a_node->getLeftChild());
addNodeNames(a_node->getRightChild());
}
void State::addNodeName(const string& a_name, TreeNode* a_node)
{
VariableMapIter iter = m_variableMap.find(a_name);
if (iter == m_variableMap.end())
{
m_variableMap.insert(make_pair(a_name, NodeSet()));
}
m_variableMap[a_name].insert(a_node);
}
void State::removeTrees(const NodeSet& a_nodeSet)
{
for (NodeSetConstIter iter = a_nodeSet.begin(); iter != a_nodeSet.end(); iter++)
{
removeTree(*iter);
}
}
void State::removeTree(TreeNode* a_node)
{
NodeSetIter iter = m_rootSet.find(a_node);
if (iter == m_rootSet.end())
{
return;
}
delete a_node;
m_rootSet.erase(a_node);
buildVariableMap();
}
void State::divideTrees(const State& a_other, NodePairSet& a_commonRoots, NodeSet& a_myUniqueRoots, NodeSet& a_otherUniqueRoots)
{
NodeSet otherRootSet = a_other.getRootSet(); // Copy the entire set
a_commonRoots.clear();
a_myUniqueRoots.clear();
a_otherUniqueRoots.clear();
for (NodeSetIter iter = m_rootSet.begin(); iter != m_rootSet.end(); iter++)
{
bool isUnique = true;
string rootName;
if (!((*iter)->getUniqueName(rootName)))
{
error("State::divideTrees - Multiple roots (in this state) with the same name " + rootName + " exist! " + string(*this), true);
}
for (NodeSetIter otherIter = otherRootSet.begin(); otherIter != otherRootSet.end(); otherIter++)
{
string otherRootName;
if (!((*otherIter)->getUniqueName(otherRootName)))
{
error("State::divideTrees - Multiple roots (in joined state) with the same name " + otherRootName + " exist! " + string(a_other), true);
}
if (rootName == otherRootName)
{
a_commonRoots.insert(make_pair((*iter), (*otherIter)));
otherRootSet.erase(otherIter);
isUnique = false;
break;
}
}
if (isUnique)
{
a_myUniqueRoots.insert(*iter);
}
}
a_otherUniqueRoots = otherRootSet;
}
TreeNode* State::getUniqueRoot(const string& a_name)
{
for (NodeSetIter iter = m_rootSet.begin(); iter != m_rootSet.end(); iter++)
{
string rootName;
if (!((*iter)->getUniqueName(rootName)))
{
error("State::getUniqueRoot - Multiple roots with the same name " + a_name + " exist! " + string(*this), true);
}
if (a_name == rootName)
{
return (*iter);
}
}
return NULL;
}
NodeSet* State::getVariableNodes(const string a_name)
{
VariableMapIter iter = m_variableMap.find(a_name);
if (iter == m_variableMap.end())
{
verbose("State::getVariableNodes - name wasn't found");
return NULL;
}
return &(iter->second);
}
void State::runFunction(const Function& a_func)
{
if (isTop())
{
return;
}
verbose("State::runFunction - Running function: " + string(a_func));
debug("State::runFunction - State before running function: " + string(*this));
FunctionName name = a_func.getName();
switch (name)
{
case e_createNode:
functionCreateNode(a_func.getFirstVar(), a_func.getValue());
break;
case e_setLeft:
functionSetLeft(a_func.getFirstVar(), a_func.getSecondVar());
break;
case e_setRight:
functionSetRight(a_func.getFirstVar(), a_func.getSecondVar());
break;
case e_setValue:
functionSetValue(a_func.getFirstVar(), a_func.getValue());
break;
case e_less:
functionLessEqual(a_func.getFirstVar(), a_func.getValue()-1);
break;
case e_lessEqual:
functionLessEqual(a_func.getFirstVar(), a_func.getValue());
break;
case e_greater:
functionGreaterEqual(a_func.getFirstVar(), a_func.getValue()+1);
break;
case e_greaterEqual:
functionGreaterEqual(a_func.getFirstVar(), a_func.getValue());
break;
case e_increment:
functionIncrement(a_func.getFirstVar(), a_func.getValue());
break;
case e_decrement:
functionIncrement(a_func.getFirstVar(), -1 * a_func.getValue());
break;
}
debug("State::runFunction - State after running function: " + string(*this));
}
void State::functionCreateNode(const string& a_name, int a_value)
{
TreeNode* root = getUniqueRoot(a_name);
if (root != NULL)
{
verbose("State::functionCreateNode - A root called " + a_name + " already exists");
int minVal = root->getMinValue();
int maxVal = root->getMaxValue();
if (a_value < minVal)
{
root->setMinValue(a_value);
}
if (a_value > maxVal)
{
root->setMaxValue(a_value);
}
return;
}
verbose("State::functionCreateNode - Creating a new node: " + a_name + " with value: " + to_string(a_value));
TreeNode* node = new TreeNode(a_name, NULL, NULL, NULL, a_value, a_value);
m_rootSet.insert(node);
addNodeName(a_name, node);
}
void State::functionSetLeft(const string& a_parent, const string& a_child)
{
TreeNode* childNode = getUniqueRoot(a_child);
if (childNode == NULL)
{
error("State::functionSetLeft - Can't set a node as a child if it already has a parent", false);
return;
}
NodeSet* parentSet = getVariableNodes(a_parent);
if (parentSet == NULL)
{
error("State::functionSetLeft - variable " + a_parent + " doesn't exist", false);
return;
}
for (NodeSetConstIter iter = parentSet->begin(); iter != parentSet->end(); iter++)
{
if ((*iter) == NULL)
{
error("State::functionSetLeft - iterator for variable " + a_parent + " is NULL", false);
continue;
}
TreeNode* leftChild = (*iter)->getLeftChild();
if (leftChild == NULL)
{
TreeNode* node = new TreeNode(*childNode);
node->setParent(*iter);
(*iter)->setLeftChild(node);
}
else {
leftChild->join(childNode);
}
}
m_rootSet.erase(childNode);
delete childNode;
buildVariableMap();
}
void State::functionSetRight(const string& a_parent, const string& a_child)
{
TreeNode* childNode = getUniqueRoot(a_child);
if (childNode == NULL)
{
error("State::functionSetRight - Can't set a node as a child if it already has a parent", false);
return;
}
NodeSet* parentSet = getVariableNodes(a_parent);
if (parentSet == NULL)
{
error("State::functionSetRight - variable " + a_parent + " doesn't exist", false);
return;
}
for (NodeSetConstIter iter = parentSet->begin(); iter != parentSet->end(); iter++)
{
if ((*iter) == NULL)
{
error("State::functionSetRight - iterator for variable " + a_parent + " is NULL", false);
continue;
}
TreeNode* rightChild = (*iter)->getRightChild();
if (rightChild == NULL)
{
TreeNode* node = new TreeNode(*childNode);
node->setParent(*iter);
(*iter)->setRightChild(node);
}
else
{
rightChild->join(childNode);
}
}
m_rootSet.erase(childNode);
delete childNode;
buildVariableMap();
}
void State::functionSetValue(const string& a_name, int a_value)
{
NodeSet* parentSet = getVariableNodes(a_name);
if (parentSet == NULL)
{
stringstream ss;
error("State::functionSetValue - variable " + a_name + " doesn't exist", false);
return;
}
for (NodeSetConstIter iter = parentSet->begin(); iter != parentSet->end(); iter++)
{
if ((*iter) == NULL)
{
error("State::functionSetValue - iterator for variable " + a_name + " is NULL", false);
continue;
}
(*iter)->setMaxValue(a_value);
(*iter)->setMinValue(a_value);
}
}
void State::functionLessEqual(const string& a_name, int a_value)
{
NodeSet* parentSet = getVariableNodes(a_name);
if (parentSet == NULL)
{
error("State::functionLessEqual - variable " + a_name + " doesn't exist", false);
return;
}
NodeSet removedRoots;
for (NodeSetConstIter iter = parentSet->begin(); iter != parentSet->end(); iter++)
{
if ((*iter) == NULL)
{
stringstream ss;
ss << "State::functionLessEqual - iterator for variable " << a_name << " is NULL";
error(ss.str(), false);
continue;
}
if ((*iter)->getMinValue() > a_value)
{
debug("State::functionLessEqual - Adding " + a_name + "'s root to removedRoots set");
removedRoots.insert((*iter)->getRoot());
continue;
}
if ((*iter)->getMaxValue() > a_value)
{
(*iter)->setMaxValue(a_value);
}
}
debug("State::functionLessEqual - removing trees");
removeTrees(removedRoots);
}
void State::functionGreaterEqual(const string& a_name, int a_value)
{
NodeSet* parentSet = getVariableNodes(a_name);
if (parentSet == NULL)
{
error("State::functionGreaterEqual - variable " + a_name + " doesn't exist" , false);
return;
}
NodeSet removedRoots;
for (NodeSetConstIter iter = parentSet->begin(); iter != parentSet->end(); iter++)
{
if ((*iter) == NULL)
{
error("State::functionGreaterEqual - iterator for variable " + a_name + " is NULL", false);
continue;
}
if ((*iter)->getMaxValue() < a_value)
{
debug("State::functionGreaterEqual - Adding " + a_name + "'s root to removedRoots set");
removedRoots.insert((*iter)->getRoot());
continue;
}
if ((*iter)->getMinValue() < a_value)
{
(*iter)->setMinValue(a_value);
}
}
debug("State::functionGreaterEqual - removing trees");
removeTrees(removedRoots);
}
void State::functionIncrement(const string& a_name, int a_value)
{
NodeSet* parentSet = getVariableNodes(a_name);
if (parentSet == NULL)
{
verbose("State::functionIncrement - variable " + a_name + " doesn't exist");
return;
}
for (NodeSetConstIter iter = parentSet->begin(); iter != parentSet->end(); iter++)
{
if ((*iter) == NULL)
{
error("State::functionIncrement - iterator for variable " + a_name + " is NULL", false);
continue;
}
int maxValue = (*iter)->getMaxValue();
int minValue = (*iter)->getMinValue();
(*iter)->setMaxValue(maxValue + a_value);
(*iter)->setMinValue(minValue + a_value);
}
}
void State::printState() const
{
cout << string(*this);
}
State::operator string() const
{
stringstream ss;
ss << "State: ";
if (m_isTop)
{
ss << "Top" << endl;
}
else
{
ss << endl << "\tVariables: ";
for (VariableMapConstIter iter = m_variableMap.begin(); iter != m_variableMap.end(); iter++)
{
ss << iter->first << ",";
}
ss << endl << "\tTrees:" << endl;
for (NodeSetConstIter iter = m_rootSet.begin(); iter != m_rootSet.end(); iter++)
{
ss << "\t";
if ((*iter) == NULL)
{
ss << "NULL";
}
else
{
ss << string(*(*iter));
}
ss << endl;
}
}
return ss.str();
}