-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmoduleClasses.opal
225 lines (179 loc) · 5.63 KB
/
moduleClasses.opal
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
package copy: import deepcopy;
@total_ordering;
new class Sort {
new method __init__(category, name, listName, usesDynamicAux = False, enabled = True) {
this.category = category;
this.name = name;
this.listName = listName;
this.dynAux = usesDynamicAux;
this.enabled = enabled;
this.func = None;
}
new method __call__(func) {
if DEBUG_MODE || this.enabled {
new Sort sort = Sort(this.category, this.name, this.listName, this.dynAux);
sort.func = deepcopy(func);
del func;
sortingVisualizer.addSort(sort);
} else {
IO.out(this.name, " is manually disabled\n");
}
}
new method __eq__(other) {
return this.listName == other.listName;
}
new method __lt__(other) {
return this.listName < other.listName;
}
}
@total_ordering;
new class Shuffle {
new method __init__(name, usesDynamicAux = False) {
this.name = name;
this.dynAux = usesDynamicAux;
this.func = None;
}
new method __call__(func) {
new Shuffle shuffle = Shuffle(this.name, this.dynAux);
shuffle.func = deepcopy(func);
del func;
sortingVisualizer.addShuffle(shuffle);
}
new method __eq__(other) {
return this.name == other.name;
}
new method __lt__(other) {
return this.name < other.name;
}
}
@total_ordering;
new class Distribution {
new method __init__(name) {
this.name = name;
this.func = None;
}
new method __call__(func) {
new Distribution distribution = Distribution(this.name);
distribution.func = deepcopy(func);
del func;
sortingVisualizer.addDistribution(distribution);
}
new method __eq__(other) {
return this.name == other.name;
}
new method __lt__(other) {
return this.name < other.name;
}
}
@total_ordering;
abstract: new class Visual {
new dynamic _EXT_ = False;
new method __init__(name, highlightColor = (255, 0, 0), refreshMode = RefreshMode.NOREFRESH, outOfText = False) {
this.name = name;
this.highlightColor = highlightColor;
this.refresh = refreshMode;
this.out = outOfText;
sortingVisualizer.addVisual(this);
}
new method init() {} # gets called whenever the graphics system gets reinitialized. useful to compute data when resolution changes
new method prepare() {} # precomputes data for the visual style based on the array
new method onAuxOn(length) {} # gets called when aux is turned on or constants are to recompute. useful to prepare data
new method onAuxOff() {} # gets called when aux mode is turned off. useful to restore old values
abstract: new method draw(array, indices); # draws the visual style
abstract: new method drawAux(array, indices); # draws the aux array in that visual style
new method selectiveDraw(array, indices) {
return True;
}
new method fastDraw(array, indices) {
this.draw(array, indices);
}
new method fastDrawAux(array, indices) {
this.drawAux(array, indices);
}
new method __eq__(other) {
return this.name == other.name;
}
new method __lt__(other) {
return this.name < other.name;
}
}
@total_ordering;
new class PivotSelection {
new method __init__(name) {
this.name = name;
this.func = None;
}
new method __call__(func) {
new PivotSelection pSel = PivotSelection(this.name);
pSel.func = deepcopy(func);
del func;
sortingVisualizer.addPivotSelection(pSel);
}
new method getFunc() {
return this.func;
}
new method __eq__(other) {
return this.name == other.name;
}
new method __lt__(other) {
return this.name < other.name;
}
}
@total_ordering;
new class Rotation {
new method __init__(name, mode = RotationMode.INDEXED) {
this.name = name;
this.mode = mode;
this.indexedFn = None;
this.lengthFn = None;
}
new method __call__(func) {
new Rotation rot = Rotation(this.name);
match this.mode {
case RotationMode.INDEXED {
rot.indexedFn = deepcopy(func);
del func;
new function __lengthFn(array, a, ll, lr) {
new dynamic tmp = a + ll;
rot.indexedFn(array, a, tmp, tmp + lr);
}
rot.lengthFn = __lengthFn;
}
case RotationMode.LENGTHS {
rot.lengthFn = deepcopy(func);
del func;
new function __indexedFn(array, a, m, b) {
rot.lengthFn(array, a, m - a, b - m);
}
rot.indexedFn = __indexedFn;
}
default {
IO.out(f'Warning: unknown rotation mode "{this.mode}"!\n');
return;
}
}
sortingVisualizer.addRotation(rot);
}
new method __eq__(other) {
return this.name == other.name;
}
new method __lt__(other) {
return this.name < other.name;
}
}
@total_ordering;
abstract: new class Sound {
new dynamic _EXT_ = False;
new method __init__(name) {
this.name = name;
sortingVisualizer.addSound(this);
}
new method prepare() {}
abstract: new method play(value, max_, sample);
new method __eq__(other) {
return this.name == other.name;
}
new method __lt__(other) {
return this.name < other.name;
}
}