Skip to content

Commit eecf242

Browse files
committed
refresh module only if data changed
1 parent 1ef17da commit eecf242

File tree

5 files changed

+114
-16
lines changed

5 files changed

+114
-16
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
var D; // D[i] indicates whether the i-th node is discovered or not
2+
3+
function DFS(v, p) { // v = current node, p = previous node
4+
tracer.visit(v, p);
5+
D[v] = true; // label v as discovered
6+
G[v].forEach(function (w) { // G[v] contains edges starting from v
7+
if (!D[w]) { // if w is not labeled as discovered
8+
DFS(w, v); // recursively call DFS
9+
}
10+
});
11+
tracer.leave(v, p);
12+
}
13+
14+
for (var i = 0; i < G.length; i++) { // start from every node
15+
tracer.print('start from ' + i);
16+
D = new Array(G.length);
17+
DFS(i);
18+
tracer.clear();
19+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
var tracer = new WeightedGraphTracer();
2+
var G = [
3+
{3: 1, 4: 2}, // connected nodes from node 0
4+
{2: 1, 4: 3},
5+
{1: 4},
6+
{1: 1},
7+
{2: 2}
8+
];
9+
tracer.setData(G);

js/module/graph.js

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
var s = null, graph = null, graphMode = "default";
1+
var s = null, graph = null, graphMode = null;
22

3-
function GraphTracer() {
4-
Tracer.call(this, GraphTracer);
3+
GraphTracer.graphMode = "default";
54

6-
if (s && graph && graphMode == "default") return;
7-
initSigma();
5+
function GraphTracer(module) {
6+
Tracer.call(this, module || GraphTracer);
7+
return initGraph(this.module);
88
}
99

1010
GraphTracer.prototype = Object.create(Tracer.prototype);
@@ -26,9 +26,8 @@ GraphTracer.prototype.reset = function () {
2626

2727
// Override
2828
GraphTracer.prototype.setData = function (G) {
29-
Tracer.prototype.setData.call(this, G);
29+
if (Tracer.prototype.setData.call(this, arguments)) return;
3030

31-
this.G = G;
3231
graph.clear();
3332
var nodes = [];
3433
var edges = [];
@@ -73,11 +72,11 @@ Tracer.prototype.clear = function () {
7372
};
7473

7574
Tracer.prototype.visit = function (targetNode, sourceNode) {
76-
this.pushStep({type: 'visit', node: targetNode, parent: sourceNode}, true);
75+
this.pushStep({type: 'visit', node: targetNode, Tracer: sourceNode}, true);
7776
};
7877

7978
Tracer.prototype.leave = function (targetNode, sourceNode) {
80-
this.pushStep({type: 'leave', node: targetNode, parent: sourceNode}, true);
79+
this.pushStep({type: 'leave', node: targetNode, Tracer: sourceNode}, true);
8180
};
8281

8382
GraphTracer.prototype.processStep = function (step, options) {
@@ -92,15 +91,15 @@ GraphTracer.prototype.processStep = function (step, options) {
9291
var node = graph.nodes(n(step.node));
9392
var color = visit ? graphColor.visited : graphColor.left;
9493
node.color = color;
95-
if (step.parent !== undefined) {
96-
var edgeId = e(step.parent, step.node);
94+
if (step.Tracer !== undefined) {
95+
var edgeId = e(step.Tracer, step.node);
9796
var edge = graph.edges(edgeId);
9897
edge.color = color;
9998
graph.dropEdge(edgeId).addEdge(edge);
10099
}
101-
var parent = step.parent;
102-
if (parent === undefined) parent = '';
103-
printTrace(visit ? parent + ' -> ' + step.node : parent + ' <- ' + step.node);
100+
var Tracer = step.Tracer;
101+
if (Tracer === undefined) Tracer = '';
102+
printTrace(visit ? Tracer + ' -> ' + step.node : Tracer + ' <- ' + step.node);
104103
break;
105104
}
106105
};
@@ -187,7 +186,11 @@ var e = function (v1, v2) {
187186
return 'e' + v1 + '_' + v2;
188187
};
189188

190-
var initSigma = function () {
189+
var initGraph = function (module) {
190+
if (s && graph && graphMode == module.graphMode) return false;
191+
graphMode = module.graphMode;
192+
193+
$('.visualize_container').empty();
191194
s = new sigma({
192195
renderer: {
193196
container: $('.visualize_container')[0],
@@ -256,4 +259,6 @@ var initSigma = function () {
256259
});
257260
};
258261
sigma.plugins.dragNodes(s, s.renderers[0]);
262+
263+
return true;
259264
};

js/module/weighted_graph.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
WeightedGraphTracer.graphMode = "weighted";
2+
3+
function WeightedGraphTracer(module) {
4+
if (GraphTracer.call(this, module || WeightedGraphTracer)) {
5+
initWeightedGraph();
6+
return true;
7+
}
8+
return false;
9+
}
10+
11+
WeightedGraphTracer.prototype = Object.create(GraphTracer.prototype);
12+
WeightedGraphTracer.prototype.constructor = WeightedGraphTracer;
13+
14+
// Override
15+
WeightedGraphTracer.prototype.setData = function (G) {
16+
if (Tracer.prototype.setData.call(this, arguments)) return;
17+
18+
graph.clear();
19+
var nodes = [];
20+
var edges = [];
21+
var unitAngle = 2 * Math.PI / G.length;
22+
var currentAngle = 0;
23+
for (var i = 0; i < G.length; i++) {
24+
currentAngle += unitAngle;
25+
nodes.push({
26+
id: n(i),
27+
label: '' + i,
28+
x: .5 + Math.sin(currentAngle) / 2,
29+
y: .5 + Math.cos(currentAngle) / 2,
30+
size: 1,
31+
color: graphColor.default
32+
});
33+
for (var j = 0; j < G[i].length; j++) {
34+
edges.push({
35+
id: e(i, G[i][j]),
36+
source: n(i),
37+
target: n(G[i][j]),
38+
color: graphColor.default,
39+
size: 1
40+
})
41+
}
42+
}
43+
44+
graph.read({
45+
nodes: nodes,
46+
edges: edges
47+
});
48+
s.camera.goTo({
49+
x: 0,
50+
y: 0,
51+
angle: 0,
52+
ratio: 1
53+
});
54+
this.refresh();
55+
};
56+
57+
var initWeightedGraph = function () {
58+
};

js/tracer.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
var timer = null;
2+
var lastModule = null, lastData = null;
23

34
var Tracer = function (module) {
45
this.module = module;
56
this.traces = [];
67
this.pause = false;
78
this.traceOptions = null;
89
this.traceIndex = -1;
10+
this.lastData = null;
911
};
1012

1113
Tracer.prototype.resize = function () {
@@ -17,7 +19,12 @@ Tracer.prototype.reset = function () {
1719
$('#tab_trace .wrapper').empty();
1820
};
1921

20-
Tracer.prototype.setData = function () {
22+
Tracer.prototype.setData = function (arguments) {
23+
var data = JSON.stringify(arguments);
24+
if (lastModule == this.module && lastData == data) return true;
25+
lastModule = this.module;
26+
lastData = data;
27+
return false;
2128
};
2229

2330
Tracer.prototype.pushStep = function (step, delay) {

0 commit comments

Comments
 (0)