ngraph.graph
Advanced tools
Comparing version 0.0.13 to 0.0.14
44
index.js
@@ -30,9 +30,19 @@ /** | ||
options = options || {}; | ||
if (options.uniqueLinkId === undefined) { | ||
// Request each link id to be unique between same nodes. This negatively | ||
// impacts `addLink()` performance (O(n), where n - number of edges of each | ||
// vertex), but makes operations with multigraphs more accessible. | ||
options.uniqueLinkId = true; | ||
if ('uniqueLinkId' in options) { | ||
console.warn( | ||
'ngraph.graph: Starting from version 0.14 `uniqueLinkId` is deprecated.\n' + | ||
'Use `multigraph` option instead\n', | ||
'\n', | ||
'Note: there is also change in default behavior: From now own each graph\n'+ | ||
'is considered to be not a multigraph by default (each edge is unique).' | ||
); | ||
options.multigraph = options.uniqueLinkId; | ||
} | ||
// Dear reader, the non-multigraphs do not guarantee that there is only | ||
// one link for a given pair of node. When this option is set to false | ||
// we can save some memory and CPU (18% faster for non-multigraph); | ||
if (options.multigraph === undefined) options.multigraph = false; | ||
var nodes = typeof Object.create === 'function' ? Object.create(null) : {}, | ||
@@ -46,3 +56,3 @@ links = [], | ||
forEachNode = createNodeIterator(), | ||
createLink = options.uniqueLinkId ? createUniqueLink : createSingleLink, | ||
createLink = options.multigraph ? createUniqueLink : createSingleLink, | ||
@@ -208,3 +218,3 @@ // Our graph API provides means to listen to graph changes. Users can subscribe | ||
* | ||
* Operation complexity is O(n1 | ||
* Operation complexity is O(1) | ||
* NOTE: this function is synonim for getNode() | ||
@@ -279,11 +289,10 @@ * | ||
if (!node) { | ||
node = new Node(nodeId); | ||
node = new Node(nodeId, data); | ||
nodesCount++; | ||
recordNodeChange(node, 'add'); | ||
} else { | ||
node.data = data; | ||
recordNodeChange(node, 'update'); | ||
} | ||
node.data = data; | ||
nodes[nodeId] = node; | ||
@@ -307,6 +316,7 @@ | ||
if (node.links) { | ||
while (node.links.length) { | ||
var link = node.links[0]; | ||
removeLink(link); | ||
var prevLinks = node.links; | ||
if (prevLinks) { | ||
node.links = null; | ||
for(var i = 0; i < prevLinks.length; ++i) { | ||
removeLink(prevLinks[i]); | ||
} | ||
@@ -559,6 +569,6 @@ } | ||
*/ | ||
function Node(id) { | ||
function Node(id, data) { | ||
this.id = id; | ||
this.links = null; | ||
this.data = null; | ||
this.data = data; | ||
} | ||
@@ -596,3 +606,3 @@ | ||
function makeLinkId(fromId, toId) { | ||
return hashCode(fromId.toString() + '👉 ' + toId.toString()); | ||
return fromId.toString() + '👉 ' + toId.toString(); | ||
} |
{ | ||
"name": "ngraph.graph", | ||
"version": "0.0.13", | ||
"version": "0.0.14", | ||
"description": "Base graph structure in ngraph.*", | ||
@@ -33,3 +33,3 @@ "main": "index.js", | ||
"plato": "^1.3.0", | ||
"tap": "~0.4.4" | ||
"tap": "^10.7.2" | ||
}, | ||
@@ -36,0 +36,0 @@ "dependencies": { |
@@ -112,3 +112,3 @@ var test = require('tap').test, | ||
t.plan(5); | ||
var graph = createGraph(); | ||
var graph = createGraph({multigraph: true}); | ||
graph.addLink(1, 2, 'first'); | ||
@@ -128,21 +128,23 @@ graph.addLink(1, 2, 'second'); | ||
test('it can produce unque link ids', function (t) { | ||
t.test('by default links are unique', function (t) { | ||
test('it can produce unique link ids', function (t) { | ||
t.test('by default links are not unique', function (t) { | ||
var seen = {}; | ||
var graph = createGraph(); | ||
var seen = {}; | ||
graph.addLink(1, 2, 'first'); | ||
graph.addLink(1, 2, 'second'); | ||
graph.addLink(1, 2, 'third'); | ||
graph.forEachLink(verifyLinkIsUnique); | ||
t.end(); | ||
graph.forEachLink(verifyLinksAreNotUnique); | ||
function verifyLinkIsUnique(link) { | ||
t.notOk(seen[link.id], link.id + ' is unique'); | ||
seen[link.id] = true; | ||
var link = graph.getLink(1, 2); | ||
t.equals(seen[link.id], 3, 'Link 1->2 seen 3 times') | ||
function verifyLinksAreNotUnique(link) { | ||
seen[link.id] = (seen[link.id] || 0) + 1; | ||
} | ||
t.end(); | ||
}); | ||
t.test('You can also explicitly request unique links', function (t) { | ||
t.test('You can create multigraph', function (t) { | ||
var graph = createGraph({ | ||
uniqueLinkId: true | ||
multigraph: true | ||
}); | ||
@@ -155,2 +157,3 @@ | ||
graph.forEachLink(verifyLinkIsUnique); | ||
t.equals(graph.getLinksCount(), 3, 'All three links are here'); | ||
t.end(); | ||
@@ -165,5 +168,3 @@ | ||
t.test('you can sacrifice uniqueness in favor of performance', function (t) { | ||
var graph = createGraph({ | ||
uniqueLinkId: false | ||
}); | ||
var graph = createGraph({ }); | ||
@@ -170,0 +171,0 @@ var seen = {}; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
42698
11
1045