diff --git a/index.js b/index.js index 95c5dc4..a5da9c7 100644 --- a/index.js +++ b/index.js @@ -30,10 +30,10 @@ function createGraph(options) { if ('uniqueLinkId' in options) { console.warn( 'ngraph.graph: Starting from version 0.14 `uniqueLinkId` is deprecated.\n' + - 'Use `multigraph` option instead\n', + 'Use `multigraph` option instead\n', '\n', - 'Note: there is also change in default behavior: From now on each graph\n'+ - 'is considered to be not a multigraph by default (each edge is unique).' + 'Note: there is also change in default behavior: From now on each graph\n' + + 'is considered to be not a multigraph by default (each edge is unique).' ); options.multigraph = options.uniqueLinkId; @@ -46,17 +46,18 @@ function createGraph(options) { if (typeof Map !== 'function') { // TODO: Should we polyfill it ourselves? We don't use much operations there.. - throw new Error('ngraph.graph requires `Map` to be defined. Please polyfill it before using ngraph'); - } + throw new Error( + 'ngraph.graph requires `Map` to be defined. Please polyfill it before using ngraph' + ); + } var nodes = new Map(); // nodeId => Node var links = new Map(); // linkId => Link - // Hash of multi-edges. Used to track ids of edges between same nodes + // Hash of multi-edges. Used to track ids of edges between same nodes var multiEdges = {}; var suspendEvents = 0; var createLink = options.multigraph ? createUniqueLink : createSingleLink, - // Our graph API provides means to listen to graph changes. Users can subscribe // to be notified about changes in the graph by using `on` method. However // in some cases they don't use it. To avoid unnecessary memory consumption @@ -156,7 +157,7 @@ function createGraph(options) { * Synonym for `getLinkCount()` */ getLinksCount: getLinkCount, - + /** * Synonym for `getNodeCount()` */ @@ -232,7 +233,7 @@ function createGraph(options) { /** * Detects whether there is a node with given id - * + * * Operation complexity is O(1) * NOTE: this function is synonym for getNode() * @@ -343,7 +344,6 @@ function createGraph(options) { return true; } - function addLink(fromId, toId, data) { enterModification(); @@ -355,11 +355,13 @@ function createGraph(options) { links.set(link.id, link); - // TODO: this is not cool. On large graphs potentially would consume more memory. - addLinkToNode(fromNode, link); - if (fromId !== toId) { - // make sure we are not duplicating links for self-loops - addLinkToNode(toNode, link); + if (isUpdate === false) { + // TODO: this is not cool. On large graphs potentially would consume more memory. + addLinkToNode(fromNode, link); + if (fromId !== toId) { + // make sure we are not duplicating links for self-loops + addLinkToNode(toNode, link); + } } recordLinkChange(link, isUpdate ? 'update' : 'add'); @@ -371,12 +373,6 @@ function createGraph(options) { function createSingleLink(fromId, toId, data) { var linkId = makeLinkId(fromId, toId); - var prevLink = links.get(linkId); - if (prevLink) { - prevLink.data = data; - return prevLink; - } - return new Link(fromId, toId, data, linkId); } @@ -388,7 +384,7 @@ function createGraph(options) { if (!isMultiEdge) { multiEdges[linkId] = 0; } - var suffix = '@' + (++multiEdges[linkId]); + var suffix = '@' + ++multiEdges[linkId]; linkId = makeLinkId(fromId + suffix, toId + suffix); } @@ -429,11 +425,11 @@ function createGraph(options) { var toNode = getNode(link.toId); if (fromNode) { - fromNode.links.delete(link); + fromNode.links.delete(link.id); } if (toNode) { - toNode.links.delete(link); + toNode.links.delete(link.id); } recordLinkChange(link, 'remove'); @@ -450,7 +446,7 @@ function createGraph(options) { function clear() { enterModification(); - forEachNode(function(node) { + forEachNode(function (node) { removeNode(node.id); }); exitModification(); @@ -534,7 +530,10 @@ function createGraph(options) { function forEachNode(callback) { if (typeof callback !== 'function') { - throw new Error('Function is expected to iterate over graph nodes. You passed ' + callback); + throw new Error( + 'Function is expected to iterate over graph nodes. You passed ' + + callback + ); } var valuesIterator = nodes.values(); @@ -558,11 +557,11 @@ function Node(id, data) { } function addLinkToNode(node, link) { - if (node.links) { - node.links.add(link); - } else { - node.links = new Set([link]); + if (!node.links) { + node.links = new Map(); } + + node.links.set(link.id, link); } /** diff --git a/package-lock.json b/package-lock.json index 0fac2bb..0259a3e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,11 +1,11 @@ { "name": "ngraph.graph", - "version": "20.0.0", + "version": "21.0.1", "lockfileVersion": 2, "requires": true, "packages": { "": { - "version": "20.0.0", + "version": "21.0.1", "license": "BSD-3-Clause", "dependencies": { "ngraph.events": "^1.2.1" diff --git a/package.json b/package.json index 6735318..c225e96 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { - "name": "ngraph.graph", - "version": "20.0.0", + "name": "ngraph.graph2", + "version": "21.0.1", "description": "graph data structure", "main": "index.js", "jsdelivr": "dist/ngraph.graph.min.js", @@ -12,7 +12,7 @@ }, "repository": { "type": "git", - "url": "https://github.com/anvaka/ngraph.graph.git" + "url": "https://github.com/parweb/ngraph.graph.git" }, "keywords": [ "graph",