Skip to content

Commit fd16e47

Browse files
committed
fix some bugs in _setData method
1 parent fc7258c commit fd16e47

10 files changed

Lines changed: 141 additions & 127 deletions

File tree

algorithm/graph_search/dfs/tree/code.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
function DFS(node, parent) { // node = current node, parent = previous node
2+
tracer._next();
23
tracer._visit(node, parent);
34
for (var i = 0; i < G[node].length; i++) {
45
if (G[node][i]) { // if current node has the i-th node as a child

js/module/array1d.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ function Array1DTracer(module) {
44

55
Array1DTracer.prototype = $.extend(true, Object.create(Array2DTracer.prototype), {
66
constructor: Array1DTracer,
7-
_setData: function (D) {
8-
return Array2DTracer.prototype._setData.call(this, [D]);
9-
},
107
_notify: function (idx1, idx2) {
118
if (idx2 === undefined) {
129
Array2DTracer.prototype._notify.call(this, 0, idx1);
@@ -47,6 +44,9 @@ Array1DTracer.prototype = $.extend(true, Object.create(Array2DTracer.prototype),
4744
});
4845
});
4946
Array2DTracer.prototype._deselectSet.call(this, coords);
47+
},
48+
setData: function (D) {
49+
return Array2DTracer.prototype.setData.call(this, [D]);
5050
}
5151
});
5252

js/module/array2d.js

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -12,47 +12,6 @@ Array2DTracer.prototype = $.extend(true, Object.create(Tracer.prototype), {
1212
this.$table = this.capsule.$table = $('<div class="mtbl-table">');
1313
this.$container.append(this.$table);
1414
},
15-
resize: function () {
16-
Tracer.prototype.resize.call(this);
17-
18-
this.refresh();
19-
},
20-
clear: function () {
21-
Tracer.prototype.clear.call(this);
22-
23-
this.clearColor();
24-
},
25-
_setData: function (D) {
26-
this.D = D;
27-
this.viewX = this.viewY = 0;
28-
this.paddingH = 6;
29-
this.paddingV = 3;
30-
this.fontSize = 16;
31-
32-
if (Tracer.prototype._setData.call(this, arguments)) {
33-
this.$table.find('.mtbl-row').each(function (i) {
34-
$(this).children().each(function (j) {
35-
$(this).text(D[i][j]);
36-
});
37-
});
38-
return true;
39-
}
40-
41-
this.$table.empty();
42-
for (var i = 0; i < D.length; i++) {
43-
var $row = $('<div class="mtbl-row">');
44-
this.$table.append($row);
45-
for (var j = 0; j < D[i].length; j++) {
46-
var $cell = $('<div class="mtbl-cell">')
47-
.css(this.getCellCss())
48-
.text(D[i][j]);
49-
$row.append($cell);
50-
}
51-
}
52-
this.resize();
53-
54-
return false;
55-
},
5615
_notify: function (x1, y1, x2, y2) {
5716
var second = x2 !== undefined && y2 !== undefined;
5817
tm.pushStep(this.capsule, {
@@ -179,8 +138,51 @@ Array2DTracer.prototype = $.extend(true, Object.create(Tracer.prototype), {
179138
this.paintColor(sx, sy, ex, ey, colorClass, addClass);
180139
}
181140
break;
141+
default:
142+
Tracer.prototype.processStep.call(this, step, options);
182143
}
183144
},
145+
setData: function (D) {
146+
this.D = D;
147+
this.viewX = this.viewY = 0;
148+
this.paddingH = 6;
149+
this.paddingV = 3;
150+
this.fontSize = 16;
151+
152+
if (Tracer.prototype.setData.apply(this, arguments)) {
153+
this.$table.find('.mtbl-row').each(function (i) {
154+
$(this).children().each(function (j) {
155+
$(this).text(D[i][j]);
156+
});
157+
});
158+
return true;
159+
}
160+
161+
this.$table.empty();
162+
for (var i = 0; i < D.length; i++) {
163+
var $row = $('<div class="mtbl-row">');
164+
this.$table.append($row);
165+
for (var j = 0; j < D[i].length; j++) {
166+
var $cell = $('<div class="mtbl-cell">')
167+
.css(this.getCellCss())
168+
.text(D[i][j]);
169+
$row.append($cell);
170+
}
171+
}
172+
this.resize();
173+
174+
return false;
175+
},
176+
resize: function () {
177+
Tracer.prototype.resize.call(this);
178+
179+
this.refresh();
180+
},
181+
clear: function () {
182+
Tracer.prototype.clear.call(this);
183+
184+
this.clearColor();
185+
},
184186
getCellCss: function () {
185187
return {
186188
padding: this.paddingV.toFixed(1) + 'px ' + this.paddingH.toFixed(1) + 'px',

js/module/directed_graph.js

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,39 @@ DirectedGraphTracer.prototype = $.extend(true, Object.create(Tracer.prototype),
4646
this.graph = this.capsule.graph = this.s.graph;
4747
},
4848
_setTreeData: function (G, root) {
49+
tm.pushStep(this.capsule, {type: 'setTreeData', arguments: arguments});
50+
},
51+
_visit: function (target, source) {
52+
tm.pushStep(this.capsule, {type: 'visit', target: target, source: source});
53+
},
54+
_leave: function (target, source) {
55+
tm.pushStep(this.capsule, {type: 'leave', target: target, source: source});
56+
},
57+
processStep: function (step, options) {
58+
switch (step.type) {
59+
case 'setTreeData':
60+
this.setTreeData.apply(this, step.arguments);
61+
break;
62+
case 'visit':
63+
case 'leave':
64+
var visit = step.type == 'visit';
65+
var targetNode = this.graph.nodes(this.n(step.target));
66+
var color = visit ? this.color.visited : this.color.left;
67+
targetNode.color = color;
68+
if (step.source !== undefined) {
69+
var edgeId = this.e(step.source, step.target);
70+
var edge = this.graph.edges(edgeId);
71+
edge.color = color;
72+
this.graph.dropEdge(edgeId).addEdge(edge);
73+
}
74+
var source = step.source;
75+
if (source === undefined) source = '';
76+
break;
77+
default:
78+
Tracer.prototype.processStep.call(this, step, options);
79+
}
80+
},
81+
setTreeData: function (G, root) {
4982
var tracer = this;
5083

5184
root = root || 0;
@@ -62,7 +95,7 @@ DirectedGraphTracer.prototype = $.extend(true, Object.create(Tracer.prototype),
6295
};
6396
getDepth(root, 1);
6497

65-
if (this._setData(G, root)) return true;
98+
if (this.setData.apply(this, arguments)) return true;
6699

67100
var place = function (node, x, y) {
68101
var temp = tracer.graph.nodes(tracer.n(node));
@@ -87,8 +120,8 @@ DirectedGraphTracer.prototype = $.extend(true, Object.create(Tracer.prototype),
87120

88121
this.refresh();
89122
},
90-
_setData: function (G) {
91-
if (Tracer.prototype._setData.call(this, arguments)) return true;
123+
setData: function (G) {
124+
if (Tracer.prototype.setData.apply(this, arguments)) return true;
92125

93126
this.graph.clear();
94127
var nodes = [];
@@ -132,31 +165,6 @@ DirectedGraphTracer.prototype = $.extend(true, Object.create(Tracer.prototype),
132165

133166
return false;
134167
},
135-
_visit: function (target, source) {
136-
tm.pushStep(this.capsule, {type: 'visit', target: target, source: source});
137-
},
138-
_leave: function (target, source) {
139-
tm.pushStep(this.capsule, {type: 'leave', target: target, source: source});
140-
},
141-
processStep: function (step, options) {
142-
switch (step.type) {
143-
case 'visit':
144-
case 'leave':
145-
var visit = step.type == 'visit';
146-
var targetNode = this.graph.nodes(this.n(step.target));
147-
var color = visit ? this.color.visited : this.color.left;
148-
targetNode.color = color;
149-
if (step.source !== undefined) {
150-
var edgeId = this.e(step.source, step.target);
151-
var edge = this.graph.edges(edgeId);
152-
edge.color = color;
153-
this.graph.dropEdge(edgeId).addEdge(edge);
154-
}
155-
var source = step.source;
156-
if (source === undefined) source = '';
157-
break;
158-
}
159-
},
160168
resize: function () {
161169
Tracer.prototype.resize.call(this);
162170

js/module/tracer.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,8 @@ var Tracer = function (module) {
77
};
88

99
Tracer.prototype = {
10-
_setData: function (arguments) {
11-
var data = JSON.stringify(arguments);
12-
if (!this.new && this.lastData == data) return true;
13-
this.capsule.lastData = data;
14-
return false;
10+
_setData: function () {
11+
tm.pushStep(this.capsule, {type: 'setData', arguments: arguments});
1512
},
1613
_clear: function () {
1714
tm.pushStep(this.capsule, {type: 'clear'});
@@ -21,11 +18,21 @@ Tracer.prototype = {
2118
},
2219
processStep: function (step, options) {
2320
switch (step.type) {
21+
case 'setData':
22+
this.setData.apply(this, step.arguments);
23+
break;
2424
case 'clear':
2525
this.clear();
2626
break;
2727
}
2828
},
29+
setData: function () {
30+
var data = JSON.stringify(arguments);
31+
if (!this.new && this.lastData == data) return true;
32+
this.new = this.capsule.new = false;
33+
this.lastData = this.capsule.lastData = data;
34+
return false;
35+
},
2936
resize: function () {
3037
},
3138
refresh: function () {

js/module/tracer_manager.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var stepLimit = 1e6;
44
var TracerManager = function () {
55
this.pause = false;
66
this.capsules = [];
7+
this.interval = 500;
78
};
89

910
TracerManager.prototype = {
@@ -37,10 +38,7 @@ TracerManager.prototype = {
3738
return selectedCapsule;
3839
},
3940
deallocateAll: function () {
40-
this.interval = 500;
41-
this.traces = [];
42-
this.traceIndex = -1;
43-
this.stepCnt = 0;
41+
this.reset();
4442
$.each(this.capsules, function (i, capsule) {
4543
capsule.allocated = false;
4644
});
@@ -82,9 +80,9 @@ TracerManager.prototype = {
8280
},
8381
reset: function () {
8482
this.traces = [];
83+
this.traceIndex = -1;
8584
this.stepCnt = 0;
8685
if (timer) clearTimeout(timer);
87-
$('#tab_trace .wrapper').empty();
8886
this.command('clear');
8987
},
9088
pushStep: function (capsule, step) {
@@ -132,12 +130,11 @@ TracerManager.prototype = {
132130
}, this.interval);
133131
},
134132
prevStep: function () {
135-
$('#tab_trace .wrapper').empty();
136133
this.command('clear');
137134
var finalIndex = this.traceIndex - 1;
138135
if (finalIndex < 0) {
139136
this.traceIndex = -1;
140-
this.refresh();
137+
this.command('refresh');
141138
return;
142139
}
143140
for (var i = 0; i < finalIndex; i++) {

js/module/undirected_graph.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ UndirectedGraphTracer.prototype = $.extend(true, Object.create(DirectedGraphTrac
1919
}
2020
});
2121
},
22-
_setData: function (G) {
23-
if (Tracer.prototype._setData.call(this, arguments)) return true;
22+
setData: function (G) {
23+
if (Tracer.prototype.setData.apply(this, arguments)) return true;
2424

2525
this.graph.clear();
2626
var nodes = [];

js/module/weighted_directed_graph.js

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,43 @@ WeightedDirectedGraphTracer.prototype = $.extend(true, Object.create(DirectedGra
2929
}
3030
});
3131
},
32-
_setData: function (G) {
33-
if (Tracer.prototype._setData.call(this, arguments)) return true;
32+
_weight: function (target, weight, delay) {
33+
tm.pushStep(this.capsule, {type: 'weight', target: target, weight: weight}, delay);
34+
},
35+
_visit: function (target, source, weight) {
36+
tm.pushStep(this.capsule, {type: 'visit', target: target, source: source, weight: weight});
37+
},
38+
_leave: function (target, source, weight) {
39+
tm.pushStep(this.capsule, {type: 'leave', target: target, source: source, weight: weight});
40+
},
41+
processStep: function (step, options) {
42+
switch (step.type) {
43+
case 'weight':
44+
var targetNode = this.graph.nodes(this.n(step.target));
45+
if (step.weight !== undefined) targetNode.weight = step.weight;
46+
break;
47+
case 'visit':
48+
case 'leave':
49+
var visit = step.type == 'visit';
50+
var targetNode = this.graph.nodes(this.n(step.target));
51+
var color = visit ? this.color.visited : this.color.left;
52+
targetNode.color = color;
53+
if (step.weight !== undefined) targetNode.weight = step.weight;
54+
if (step.source !== undefined) {
55+
var edgeId = this.e(step.source, step.target);
56+
var edge = this.graph.edges(edgeId);
57+
edge.color = color;
58+
this.graph.dropEdge(edgeId).addEdge(edge);
59+
}
60+
var source = step.source;
61+
if (source === undefined) source = '';
62+
break;
63+
default:
64+
DirectedGraphTracer.prototype.processStep.call(this, step, options);
65+
}
66+
},
67+
setData: function (G) {
68+
if (Tracer.prototype.setData.apply(this, arguments)) return true;
3469

3570
this.graph.clear();
3671
var nodes = [];
@@ -76,41 +111,6 @@ WeightedDirectedGraphTracer.prototype = $.extend(true, Object.create(DirectedGra
76111

77112
return false;
78113
},
79-
_weight: function (target, weight, delay) {
80-
tm.pushStep(this.capsule, {type: 'weight', target: target, weight: weight}, delay);
81-
},
82-
_visit: function (target, source, weight) {
83-
tm.pushStep(this.capsule, {type: 'visit', target: target, source: source, weight: weight});
84-
},
85-
_leave: function (target, source, weight) {
86-
tm.pushStep(this.capsule, {type: 'leave', target: target, source: source, weight: weight});
87-
},
88-
processStep: function (step, options) {
89-
switch (step.type) {
90-
case 'weight':
91-
var targetNode = this.graph.nodes(this.n(step.target));
92-
if (step.weight !== undefined) targetNode.weight = step.weight;
93-
break;
94-
case 'visit':
95-
case 'leave':
96-
var visit = step.type == 'visit';
97-
var targetNode = this.graph.nodes(this.n(step.target));
98-
var color = visit ? this.color.visited : this.color.left;
99-
targetNode.color = color;
100-
if (step.weight !== undefined) targetNode.weight = step.weight;
101-
if (step.source !== undefined) {
102-
var edgeId = this.e(step.source, step.target);
103-
var edge = this.graph.edges(edgeId);
104-
edge.color = color;
105-
this.graph.dropEdge(edgeId).addEdge(edge);
106-
}
107-
var source = step.source;
108-
if (source === undefined) source = '';
109-
break;
110-
default:
111-
DirectedGraphTracer.prototype.processStep.call(this, step, options);
112-
}
113-
},
114114
clear: function () {
115115
DirectedGraphTracer.prototype.clear.call(this);
116116

0 commit comments

Comments
 (0)