ngraph.graph
Advanced tools
Comparing version 0.0.8 to 0.0.9
46
index.js
@@ -408,22 +408,34 @@ /** | ||
function forEachLinkedNode(nodeId, callback, oriented) { | ||
var node = getNode(nodeId), | ||
i, | ||
link, | ||
linkedNodeId; | ||
var node = getNode(nodeId); | ||
if (node && node.links && typeof callback === 'function') { | ||
// Extracted orientation check out of the loop to increase performance | ||
if (oriented) { | ||
for (i = 0; i < node.links.length; ++i) { | ||
link = node.links[i]; | ||
if (link.fromId === nodeId) { | ||
callback(nodes[link.toId], link); | ||
} | ||
} | ||
return forEachOrientedLink(node.links, nodeId, callback); | ||
} else { | ||
for (i = 0; i < node.links.length; ++i) { | ||
link = node.links[i]; | ||
linkedNodeId = link.fromId === nodeId ? link.toId : link.fromId; | ||
return forEachNonOrientedLink(node.links, nodeId, callback); | ||
} | ||
} | ||
} | ||
callback(nodes[linkedNodeId], link); | ||
function forEachNonOrientedLink(links, nodeId, callback) { | ||
var quitFast; | ||
for (var i = 0; i < links.length; ++i) { | ||
var link = links[i]; | ||
var linkedNodeId = link.fromId === nodeId ? link.toId : link.fromId; | ||
quitFast = callback(nodes[linkedNodeId], link); | ||
if (quitFast) { | ||
return true; // Client does not need more iterations. Break now. | ||
} | ||
} | ||
} | ||
function forEachOrientedLink(links, nodeId, callback) { | ||
var quitFast; | ||
for (var i = 0; i < links.length; ++i) { | ||
var link = links[i]; | ||
if (link.fromId === nodeId) { | ||
quitFast = callback(nodes[link.toId], link); | ||
if (quitFast) { | ||
return true; // Client does not need more iterations. Break now. | ||
} | ||
@@ -466,3 +478,3 @@ } | ||
if (callback(nodes[keys[i]])) { | ||
return; // client doesn't want to proceed. Return. | ||
return true; // client doesn't want to proceed. Return. | ||
} | ||
@@ -480,3 +492,3 @@ } | ||
if (callback(nodes[node])) { | ||
return; // client doesn't want to proceed. Return. | ||
return true; // client doesn't want to proceed. Return. | ||
} | ||
@@ -483,0 +495,0 @@ } |
{ | ||
"name": "ngraph.graph", | ||
"version": "0.0.8", | ||
"version": "0.0.9", | ||
"description": "Base graph structure in ngraph.*", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -102,5 +102,12 @@ ngraph.graph | ||
Finally to remove a node or a link from a graph use `removeNode()` or `removeLink()` correspondingly: | ||
TO get a particular link object use `getLink()` method: | ||
``` js | ||
var helloWorldLink = g.getLink('hello', 'world'); // returns a link from 'hello' to 'world' | ||
console.log(helloWorldLink); | ||
``` | ||
To remove a node or a link from a graph use `removeNode()` or `removeLink()` correspondingly: | ||
``` js | ||
g.removeNode('space'); | ||
@@ -113,2 +120,9 @@ // Removing link is a bit harder, since method requires actual link object: | ||
You can also remove all nodes and links by calling | ||
``` js | ||
g.clear(); | ||
``` | ||
## Listening to Events | ||
@@ -115,0 +129,0 @@ Whenever someone changes your graph you can listen to notifications: |
@@ -44,2 +44,44 @@ var test = require('tap').test, | ||
test('forEachLinkedNode can quit fast for oriented graphs', function(t) { | ||
t.plan(1); | ||
var graph = createGraph(); | ||
var oriented = true; | ||
graph.addLink(1, 2); | ||
graph.addLink(1, 3); | ||
graph.forEachLinkedNode(1, function() { | ||
t.ok(true, 'Visited first link'); | ||
return true; // We want to stop right now! | ||
}, oriented); | ||
}); | ||
test('forEachLinkedNode can quit fast for non-oriented graphs', function(t) { | ||
t.plan(1); | ||
var graph = createGraph(); | ||
var oriented = false; | ||
graph.addLink(1, 2); | ||
graph.addLink(1, 3); | ||
graph.forEachLinkedNode(1, function() { | ||
t.ok(true, 'Visited first link'); | ||
return true; // We want to stop right now! | ||
}, oriented); | ||
}); | ||
test('forEachLinkedNode returns quitFast flag', function(t) { | ||
var graph = createGraph(); | ||
graph.addLink(1, 2); | ||
graph.addLink(1, 3); | ||
var fastQuit = graph.forEachLinkedNode(1, function() { | ||
return true; // Stop right now. | ||
}); | ||
t.ok(fastQuit, 'Fast quit triggered when callback opted out'); | ||
var notSoFast = graph.forEachLinkedNode(1, function() { }); | ||
t.notOk(notSoFast, 'Fast quit is not triggered when all elements visited'); | ||
t.end(); | ||
}); | ||
test('forEachLink visits each link', function(t) { | ||
@@ -62,6 +104,10 @@ t.plan(1); | ||
test('forEachNode will not crash on empty callback', function(t) { | ||
test('forEachNode will stop when requested', function(t) { | ||
t.plan(1); | ||
var graph = createGraph(); | ||
graph.addLink(1, 2); | ||
graph.forEachNode(); // no callback? No worries | ||
graph.forEachNode(function(node) { | ||
t.equals(node.id, 1, 'We visit only one node'); | ||
return true; // we want to stop now! | ||
}); | ||
@@ -71,7 +117,6 @@ t.end(); | ||
test('forEachNode will stop when requested', function(t) { | ||
t.plan(1); | ||
test('forEachNode returns fastQuit', function(t) { | ||
var graph = createGraph(); | ||
graph.addLink(1, 2); | ||
graph.forEachNode(function(node) { | ||
var fastQuit = graph.forEachNode(function(node) { | ||
t.equals(node.id, 1, 'We visit only one node'); | ||
@@ -81,3 +126,7 @@ return true; // we want to stop now! | ||
t.ok(fastQuit, 'fastQuit is set when callback opted out'); | ||
var notSoFast = graph.forEachNode(function() { }); | ||
t.notOk(notSoFast, 'fastQuit is not set when all nodes visited'); | ||
t.end(); | ||
}); |
33053
767
176