forked from algorithm-visualizer/algorithm-visualizer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtracer_manager.js
201 lines (195 loc) · 5.88 KB
/
tracer_manager.js
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
const stepLimit = 1e6;
var TracerManager = function () {
this.timer = null;
this.pause = false;
this.capsules = [];
this.interval = 500;
};
TracerManager.prototype = {
add: function (tracer) {
var $container = $('<section class="module_wrapper">');
$('.module_container').append($container);
var capsule = {
module: tracer.module,
tracer: tracer,
allocated: true,
defaultName: null,
$container: $container,
new: true
};
this.capsules.push(capsule);
return capsule;
},
allocate: function (newTracer) {
var selectedCapsule = null;
var count = 0;
$.each(this.capsules, function (i, capsule) {
if (capsule.module == newTracer.module) {
count++;
if (!capsule.allocated) {
capsule.tracer = newTracer;
capsule.allocated = true;
capsule.new = false;
selectedCapsule = capsule;
return false;
}
}
});
if (selectedCapsule == null) {
count++;
selectedCapsule = this.add(newTracer);
}
selectedCapsule.defaultName = newTracer.constructor.name + ' ' + count;
return selectedCapsule;
},
deallocateAll: function () {
this.reset();
$.each(this.capsules, function (i, capsule) {
capsule.allocated = false;
});
},
removeUnallocated: function () {
var changed = false;
this.capsules = $.grep(this.capsules, function (capsule) {
var removed = !capsule.allocated;
if (capsule.new || removed) changed = true;
if (removed) {
capsule.$container.remove();
}
return !removed;
});
if (changed) this.place();
},
place: function () {
var capsules = this.capsules;
$.each(capsules, function (i, capsule) {
var width = 100;
var height = (100 / capsules.length);
var top = height * i;
capsule.$container.css({
top: top + '%',
width: width + '%',
height: height + '%'
});
capsule.tracer.resize();
});
},
resize: function () {
this.command('resize');
},
isPause: function () {
return this.pause;
},
setInterval: function (interval) {
$('#interval').val(interval);
},
reset: function () {
this.traces = [];
this.traceIndex = -1;
this.stepCnt = 0;
if (this.timer) clearTimeout(this.timer);
this.command('clear');
},
pushStep: function (capsule, step) {
if (this.stepCnt++ > stepLimit) throw "Tracer's stack overflow";
var len = this.traces.length;
var last = [];
if (len == 0) {
this.traces.push(last);
} else {
last = this.traces[len - 1];
}
last.push($.extend(step, {capsule: capsule}));
},
newStep: function () {
this.traces.push([]);
},
pauseStep: function () {
if (this.traceIndex < 0) return;
this.pause = true;
if (this.timer) clearTimeout(this.timer);
$('#btn_pause').addClass('active');
},
resumeStep: function () {
this.pause = false;
this.step(this.traceIndex + 1);
$('#btn_pause').removeClass('active');
},
step: function (i, options) {
var tracer = this;
if (isNaN(i) || i >= this.traces.length || i < 0) return;
options = options || {};
this.traceIndex = i;
var trace = this.traces[i];
trace.forEach(function (step) {
step.capsule.tracer.processStep(step, options);
});
if (!options.virtual) {
this.command('refresh');
}
if (this.pause) return;
this.timer = setTimeout(function () {
tracer.step(i + 1, options);
}, this.interval);
},
prevStep: function () {
this.command('clear');
var finalIndex = this.traceIndex - 1;
if (finalIndex < 0) {
this.traceIndex = -1;
this.command('refresh');
return;
}
for (var i = 0; i < finalIndex; i++) {
this.step(i, {virtual: true});
}
this.step(finalIndex);
},
nextStep: function () {
this.step(this.traceIndex + 1);
},
visualize: function () {
this.traceIndex = -1;
this.resumeStep();
},
command: function () {
var args = Array.prototype.slice.call(arguments);
var functionName = args.shift();
$.each(this.capsules, function (i, capsule) {
if (capsule.allocated) {
capsule.tracer.module.prototype[functionName].apply(capsule.tracer, args);
}
});
},
findOwner: function (container) {
var selectedCapsule = null;
$.each(this.capsules, function (i, capsule) {
if (capsule.$container[0] == container) {
selectedCapsule = capsule;
return false;
}
});
return selectedCapsule.tracer;
}
};
var TracerUtil = {
toJSON: function (obj) {
return JSON.stringify(obj, function (key, value) {
return value === Infinity ? "Infinity" : value;
});
},
fromJSON: function (obj) {
return JSON.parse(obj, function (key, value) {
return value === "Infinity" ? Infinity : value;
});
},
refineByType: function (item) {
return typeof(item) === 'number' ? this.refineNumber(item) : this.refineString(item);
},
refineString: function (str) {
return str === '' ? ' ' : str;
},
refineNumber: function (num) {
return num === Infinity ? '∞' : num;
}
};