-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathQuickFind.ts
116 lines (85 loc) · 2.74 KB
/
QuickFind.ts
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
/**
* File created by Philip Hoang 06.2.
* written by Øyvind
* based on QuickFind.Java
*/
///<reference path="Controller.ts"/>
class QuickFind implements IAlgorithm {
DELAY: number = 100;
arr: number[];
name: string = "Quick Find";
// noninspection JSAnnotator
constructor(size: number) {
this.arr = [];
for (let i = 0; i < size; i++) {
this.arr[i] = i;
}
}
union(aIndex: number, bIndex: number) {
let aRoot: number = this.arr[aIndex];
let bRoot: number = this.arr[bIndex];
if (aRoot == bRoot) {
control.setSelectedIndex(aIndex, false);
control.setSelectedIndex(bIndex, false);
return;
}
control.saveState(this.arr);
for (let i = 0; i < this.arr.length; i++) {
control.setArrow(i);
if (this.arr[i] == aRoot) {
control.setValueAtIndex(i, bRoot);
control.connectNodes(i, bRoot);
this.arr[i] = bRoot;
}
}
control.setArrow(-1);
control.setSelectedIndex(bIndex, false);
control.setSelectedIndex(aIndex, false);
}
connected(aIndex: number, bIndex: number) {
let connected: boolean = this.simpleFind(aIndex, "orange") == this.simpleFind(bIndex, "orange");
if (connected) {
control.highlightNode(this.arr[aIndex], "green");
control.highlightNode(this.arr[bIndex], "green");
control.checkMark(aIndex, bIndex, true);
} else
control.redCross(aIndex, bIndex, true);
control.setSelectedIndex(bIndex, false);
control.removeHighlight(this.arr[bIndex]);
control.setSelectedIndex(aIndex, false);
control.removeHighlight(this.arr[aIndex]);
control.checkMark(aIndex, bIndex, false);
control.redCross(aIndex, bIndex, false);
return connected
}
getName() {
return this.name;
}
find(pIndex: number) {
let root = this.simpleFind(pIndex, "green");
control.removeHighlight(root);
control.setSelectedIndex(pIndex, false);
return root;
}
simpleFind(pIndex: number, color: string) {
let root: number = this.arr[pIndex];
if (pIndex != root) {
control.highlightNode(pIndex, "orange");
control.removeHighlight(pIndex);
}
control.highlightNode(root, color);
return root;
}
getArray() {
return this.arr;
}
setArray(array: number[]) {
this.arr = array;
}
connectedNoGUIUpdate(a: number, b: number) {
return this.arr[a] == this.arr[b];
}
getDelayTime() {
return this.DELAY + control.getSpeed();
}
}