-
Notifications
You must be signed in to change notification settings - Fork 7
/
UndoMachine.h
732 lines (638 loc) · 14 KB
/
UndoMachine.h
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
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
/*******************************************************************************
UndoMachine.h
Data structure for recording and undoing tree alterations
Copyright 2012-2014 Chris Whidden
http://kiwi.cs.dal.ca/Software/RSPR
March 3, 2014
Version 1.2.1
This file is part of rspr.
rspr is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
rspr is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with rspr. If not, see <http://www.gnu.org/licenses/>.
*******************************************************************************/
#ifndef DEFINE_UNDOMACHINE
#define DEFINE_UNDOMACHINE
#include "Node.h"
#include "Forest.h"
#include <list>
#include <typeinfo>
class Node;
class Undoable {
public:
virtual ~Undoable() {}
virtual void undo() = 0;
};
class UndoMachine {
public:
list<Undoable *> events;
int size;
UndoMachine() {
events = list<Undoable *>();
size = 0;
}
void add_event(Undoable *event) {
events.push_back(event);
size++;
}
void insert_event(list<Undoable *>::iterator i, Undoable *event) {
list<Undoable *>::iterator j = i;
j++;
events.insert(j, event);
size++;
}
list<Undoable *>::iterator get_bookmark() {
list<Undoable *>::iterator bookmark = events.end();
if (size > 0)
bookmark--;
else
bookmark = events.begin();
return bookmark;
}
void undo() {
if (!events.empty()) {
Undoable *event = events.back();
#ifdef DEBUG_UNDO
cout << typeid(*event).name() << endl;
#endif
events.pop_back();
event->undo();
delete event;
size--;
}
}
void undo(int num) {
int temp_num = num;
while (temp_num > 0) {
undo();
temp_num--;
}
}
void undo_all() {
undo_to(0);
}
void undo_to(int to) {
while (size > to)
undo();
}
void clear_to(int to) {
while (size > to) {
Undoable *event = events.back();
events.pop_back();
delete event;
size--;
}
}
int num_events() {
return size;
}
};
void ContractEvent(UndoMachine *um, Node *n);
class AddRho : public Undoable {
public:
Forest *F;
AddRho(Forest *f) {
F = f;
}
void undo() {
F->set_rho(false);
F->get_component(F->num_components()-1)->delete_tree();
F->erase_components(F->num_components()-1,F->num_components());
}
};
class AddComponent : public Undoable {
public:
Forest *F;
AddComponent(Forest *f) {
F = f;
}
void undo() {
F->erase_components(F->num_components()-1,F->num_components());
}
};
class AddComponentToFront : public Undoable {
public:
Forest *F;
AddComponentToFront(Forest *f) {
F = f;
}
void undo() {
F->erase_components(0,1);
}
};
// TODO: use new insert_child function with a stored successor sibling
// does the end work? maybe a seperate variable for that?
class CutParent : public Undoable {
public:
Node *child;
Node *parent;
int branch;
int depth;
CutParent(Node *c) {
child = c;
parent = c->parent();
branch = 0;
depth = c->get_depth();
if (parent != NULL) {
if (parent->lchild() == child)
branch = 1;
else
branch = 2;
}
}
void undo() {
if (branch == 1) {
if (parent->is_leaf())
parent->add_child(child);
else
parent->insert_child(parent->get_children().front(), child);
}
else if (branch == 2)
parent->add_child(child);
child->set_depth(depth);
}
};
class ClearSiblingPair : public Undoable {
public:
Node *a;
Node *c;
int a_status;
int c_status;
ClearSiblingPair(Node *x, Node *y) {
if (x->get_sibling_pair_status() == 1 ||
y->get_sibling_pair_status() == 2) {
a = x;
c = y;
}
else {
a = y;
c = x;
}
a_status = a->get_sibling_pair_status();
c_status = c->get_sibling_pair_status();
}
void undo() {
a->set_sibling_pair_status(1);
c->set_sibling_pair_status(2);
if (a_status == 0) {
c->set_sibling(a);
}
else if (c_status == 0) {
a->set_sibling(c);
}
}
};
class PopClearedSiblingPair : public Undoable {
public:
Node *a;
Node *c;
int a_status;
int c_status;
list<Node *> *sibling_pairs;
PopClearedSiblingPair(Node *x, Node *y, list<Node *> *s) {
sibling_pairs = s;
a = x;
c = y;
}
void undo() {
sibling_pairs->push_back(c);
sibling_pairs->push_back(a);
}
};
class PopSiblingPair : public Undoable {
public:
Node *a;
Node *c;
list<Node *> *sibling_pairs;
PopSiblingPair(Node *x, Node *y, list<Node *> *s) {
sibling_pairs = s;
a = x;
c = y;
}
void undo() {
sibling_pairs->push_back(c);
sibling_pairs->push_back(a);
}
};
class ContractSiblingPair : public Undoable {
public:
Node *node;
int c1_depth;
int c2_depth;
bool binary_node;
bool node_protected;
ContractSiblingPair(Node *n) {
init(n);
if (n->is_protected())
node_protected = true;
else
node_protected = false;
}
ContractSiblingPair(Node *n, Node *child1, Node *child2,
UndoMachine *um) {
if (n->get_children().size() == 2)
init(n);
else {
um->add_event(new CutParent(child1));
um->add_event(new CutParent(child2));
binary_node = false;
node = n;
}
if (n->is_protected())
node_protected = true;
else
node_protected = false;
}
void init(Node *n) {
node = n;
if (n->lchild() != NULL)
c1_depth = n->lchild()->get_depth();
else
c1_depth = -1;
if (n->rchild() != NULL)
c2_depth = n->rchild()->get_depth();
else
c2_depth = -1;
binary_node = true;
}
void undo() {
if (binary_node) {
node->undo_contract_sibling_pair();
if (c1_depth > -1)
node->lchild()->set_depth(c1_depth);
if (c2_depth > -1)
node->rchild()->set_depth(c2_depth);
}
if (node_protected)
node->protect_edge();
}
};
class AddToFrontSiblingPairs : public Undoable {
public:
list<Node *> *sibling_pairs;
AddToFrontSiblingPairs(list<Node *> *s) {
sibling_pairs = s;
}
void undo() {
if (!sibling_pairs->empty()) {
sibling_pairs->pop_front();
sibling_pairs->pop_front();
}
}
};
class AddToSiblingPairs : public Undoable {
public:
list<Node *> *sibling_pairs;
AddToSiblingPairs(list<Node *> *s) {
sibling_pairs = s;
}
void undo() {
if (!sibling_pairs->empty()) {
sibling_pairs->pop_back();
sibling_pairs->pop_back();
}
}
};
class AddToSetSiblingPairs : public Undoable {
public:
set<SiblingPair> *sibling_pairs;
SiblingPair pair;
AddToSetSiblingPairs(set<SiblingPair> *sp, SiblingPair p) {
sibling_pairs = sp;
//pair = SiblingPair(p->a,p->c);
pair = p;
}
void undo() {
if (!sibling_pairs->empty()) {
sibling_pairs->erase(pair);
}
}
};
class RemoveSetSiblingPairs : public Undoable {
public:
set<SiblingPair> *sibling_pairs;
SiblingPair pair;
RemoveSetSiblingPairs(set<SiblingPair> *sp, SiblingPair p) {
sibling_pairs = sp;
pair = p;
}
void undo() {
sibling_pairs->insert(pair);
}
};
class AddInSiblingPairs : public Undoable {
public:
list<Node *> *sibling_pairs;
int pos;
AddInSiblingPairs(list<Node *> *s, int p) {
sibling_pairs = s;
pos = p;
}
void undo() {
if (!sibling_pairs->empty()) {
list<Node *>::iterator c = sibling_pairs->begin();
for(int i = 0; i <= pos && c != sibling_pairs->end(); i++) {
c++;
}
if (c != sibling_pairs->end()) {
list<Node *>::iterator rem = c;
c++;
sibling_pairs->erase(rem);
rem = c;
c++;
sibling_pairs->erase(rem);
}
}
}
};
class SetTwin : public Undoable {
public:
Node *node;
Node *twin;
SetTwin(Node *n) {
node = n;
twin = n->get_twin();
}
void undo() {
node->set_twin(twin);
}
};
class ChangeName : public Undoable {
public:
Node *node;
string name;
ChangeName(Node *n) {
node = n;
name = string(n->get_name());
}
void undo() {
node->set_name(name);
}
};
class ChangeEdgePreInterval : public Undoable {
public:
Node *node;
int start;
int end;
ChangeEdgePreInterval(Node *n) {
start = n->get_edge_pre_start();
end = n->get_edge_pre_end();
node = n;
}
void undo() {
node->set_edge_pre_start(start);
node->set_edge_pre_end(end);
}
};
class ChangePreNum : public Undoable {
public:
Node *node;
int prenum;
ChangePreNum(Node *n) {
prenum = n->get_preorder_number();
node = n;
}
void undo() {
node->set_preorder_number(prenum);
}
};
class ChangeRightChild : public Undoable {
public:
Node *node;
Node *rchild;
int rchild_depth;
ChangeRightChild(Node *n) {
node = n;
rchild = n->rchild();
if (rchild != NULL)
rchild_depth = rchild->get_depth();
}
void undo() {
if (rchild != NULL) {
//node->add_child_keep_depth(rchild);
node->add_child(rchild);
rchild->set_depth(rchild_depth);
}
else
if (node->rchild() != NULL)
node->rchild()->cut_parent();
}
};
class ChangeLeftChild : public Undoable {
public:
Node *node;
Node *lchild;
int lchild_depth;
ChangeLeftChild(Node *n) {
node = n;
lchild = n->lchild();
if (lchild != NULL)
lchild_depth = lchild->get_depth();
}
void undo() {
if (lchild != NULL) {
//node->add_child_keep_depth(lchild);
node->add_child(lchild);
lchild->set_depth(lchild_depth);
}
else
if (node->lchild() != NULL)
node->lchild()->cut_parent();
}
};
class AddChild : public Undoable {
public:
Node *child;
int depth;
AddChild(Node *c) {
child = c;
if (c != NULL)
depth = c->get_depth();
}
void undo() {
if (child != NULL) {
child->cut_parent();
child->set_depth(depth);
}
}
};
class AddContractedLC : public Undoable {
public:
Node *node;
AddContractedLC(Node *n) {
node = n;
}
void undo() {
node->set_contracted_lc(NULL);
}
};
class AddContractedRC : public Undoable {
public:
Node *node;
AddContractedRC(Node *n) {
node = n;
}
void undo() {
node->set_contracted_rc(NULL);
}
};
class CreateNode : public Undoable {
public:
Node *node;
CreateNode(Node *n) {
node = n;
}
void undo() {
if (node != NULL)
delete node;
}
};
class ProtectEdge : public Undoable {
public:
Node *node;
ProtectEdge(Node *n) {
node = n;
}
void undo() {
if (node != NULL)
node->unprotect_edge();
}
};
class UnprotectEdge : public Undoable {
public:
Node *node;
UnprotectEdge(Node *n) {
node = n;
}
void undo() {
if (node != NULL)
node->protect_edge();
}
};
class ListPushBack : public Undoable {
public:
list<Node *> *l;
ListPushBack(list<Node *> *x) {
l = x;
}
void undo() {
l->pop_back();
}
};
class ListPopBack : public Undoable {
public:
list<Node *> *l;
Node *node;
ListPopBack(list<Node *> *x) {
l = x;
if (!l->empty())
node = l->back();
else
node = NULL;
}
void undo() {
if (node != NULL)
l->push_back(node);
}
};
void ContractEvent(UndoMachine *um, Node *n, list<Undoable *>::iterator
bookmark) {
Node *parent = n->parent();
Node *child;
Node *lc = n->lchild();
Node *rc = n->rchild();
Node *ret = NULL;
// contract out this node and give child to parent
if (parent != NULL) {
if (lc && !rc) {
child = lc;
um->add_event(new ChangeEdgePreInterval(child));
um->add_event(new CutParent(child));
um->add_event(new CutParent(n));
if (n->is_protected() && !child->is_protected())
um->add_event(new ProtectEdge(child));
}
else if (rc && !lc) {
child = rc;
um->add_event(new ChangeEdgePreInterval(child));
um->add_event(new CutParent(child));
um->add_event(new CutParent(n));
if (n->is_protected() && !child->is_protected())
um->add_event(new ProtectEdge(child));
}
else if (lc == NULL && rc == NULL) {
um->insert_event(bookmark, new CutParent(n));
parent->delete_child(n);
ContractEvent(um, parent);
parent->add_child(n);
}
}
// if no parent then take children of single child and remove it
else {
// dead component or singleton, will be cleaned up by the forest
if (n->get_children().empty()) {
um->add_event(new ChangeName(n));
}
else if (n->get_children().size() == 1) {
child = n->get_children().front();
// if (rc == NULL) {
// um->add_event(new ChangeRightChild(n));
// child = lc;
// }
// else {
// um->add_event(new ChangeLeftChild(n));
// child = rc;
// }
um->add_event(new CutParent(child));
/* cluster hack - if we delete a cluster node then
* we may try to use it later. This only happens once
* per cluster so we can spend linear time to update
* the forest
*/
if (child->get_num_clustered_children() > 0) {
//um->add_event(new CutParent(n));
}
if (child->get_num_clustered_children() <= 0) {
// if child is a leaf then get rid of this so we don't lose refs
// problem: if the child is not c, then we want to copy
// otherwise we don't
// copy other parameters and join the twin
//to this if the child is a label
Node *new_lc = child->lchild();
Node *new_rc = child->rchild();
if (child->is_leaf()) {
if (child->get_twin() != NULL) {
um->add_event(new SetTwin(n));
um->add_event(new SetTwin(child->get_twin()));
}
um->add_event(new ChangeName(n));
}
um->add_event(new ChangePreNum(n));
// redundant?
//um->add_event(new CutParent(child));
list<Node *>::iterator c;
for(c = child->get_children().begin();
c != child->get_children().end();
c++) {
um->add_event(new CutParent(*c));
}
if (child->get_contracted_lc() != NULL)
um->add_event(new AddContractedLC(n));
if (child->get_contracted_rc() != NULL)
um->add_event(new AddContractedRC(n));
}
}
}
}
void ContractEvent(UndoMachine *um, Node *n) {
list<Undoable *>::iterator bookmark = um->get_bookmark();
ContractEvent(um, n, bookmark);
}
#endif