From 851ff1c735b2066771379cc6252465f38652409f Mon Sep 17 00:00:00 2001 From: chris Date: Thu, 16 Jun 2022 10:25:53 +0200 Subject: [PATCH 1/6] create a new link and return even if a previous link exist prevent weird data assignation behavior --- index.js | 6 ------ 1 file changed, 6 deletions(-) diff --git a/index.js b/index.js index 95c5dc4..7b62b41 100644 --- a/index.js +++ b/index.js @@ -371,12 +371,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); } From 62f77eea3c7c52c3cea42b78e86079faca265239 Mon Sep 17 00:00:00 2001 From: chris Date: Thu, 16 Jun 2022 10:26:19 +0200 Subject: [PATCH 2/6] when we update a link we don't need to "addLinkToNode" because juste link.data changed --- index.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 7b62b41..d3b0e22 100644 --- a/index.js +++ b/index.js @@ -355,11 +355,14 @@ 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); + // when we update a link we don't need to "addLinkToNode" because juste link.data changed + 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'); From 81f12c98683722292ed1e261f604e3d7b2f70916 Mon Sep 17 00:00:00 2001 From: chris Date: Thu, 16 Jun 2022 11:17:40 +0200 Subject: [PATCH 3/6] 20.1.0 --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 0fac2bb..1dc34ba 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,11 +1,11 @@ { "name": "ngraph.graph", - "version": "20.0.0", + "version": "20.1.0", "lockfileVersion": 2, "requires": true, "packages": { "": { - "version": "20.0.0", + "version": "20.1.0", "license": "BSD-3-Clause", "dependencies": { "ngraph.events": "^1.2.1" diff --git a/package.json b/package.json index 6735318..72605ac 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ngraph.graph", - "version": "20.0.0", + "version": "20.1.0", "description": "graph data structure", "main": "index.js", "jsdelivr": "dist/ngraph.graph.min.js", From 105d19ddc7ac272ae0ea402fb4e3251bb5a2bfbe Mon Sep 17 00:00:00 2001 From: chris Date: Thu, 16 Jun 2022 11:44:45 +0200 Subject: [PATCH 4/6] replace Set links in node with Map --- index.js | 54 ++++++++++++++++++++++++++-------------------------- package.json | 6 +++--- 2 files changed, 30 insertions(+), 30 deletions(-) diff --git a/index.js b/index.js index d3b0e22..4518dca 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,14 +355,11 @@ function createGraph(options) { links.set(link.id, link); - // when we update a link we don't need to "addLinkToNode" because juste link.data changed - 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); - } + // 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'); @@ -385,7 +382,7 @@ function createGraph(options) { if (!isMultiEdge) { multiEdges[linkId] = 0; } - var suffix = '@' + (++multiEdges[linkId]); + var suffix = '@' + ++multiEdges[linkId]; linkId = makeLinkId(fromId + suffix, toId + suffix); } @@ -426,11 +423,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'); @@ -447,7 +444,7 @@ function createGraph(options) { function clear() { enterModification(); - forEachNode(function(node) { + forEachNode(function (node) { removeNode(node.id); }); exitModification(); @@ -531,7 +528,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(); @@ -555,11 +555,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.json b/package.json index 72605ac..204e577 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { - "name": "ngraph.graph", - "version": "20.1.0", + "name": "ngraph.graph2", + "version": "21.0.0", "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", From 61205ed1861f9f9bc07e1a8d4d6377c8cff4222e Mon Sep 17 00:00:00 2001 From: chris Date: Thu, 16 Jun 2022 11:48:13 +0200 Subject: [PATCH 5/6] try condition on isUpdate --- index.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 4518dca..a5da9c7 100644 --- a/index.js +++ b/index.js @@ -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'); From e483a17a014a7366622674f66f7e9a35a5ccca3d Mon Sep 17 00:00:00 2001 From: chris Date: Thu, 16 Jun 2022 11:48:21 +0200 Subject: [PATCH 6/6] 21.0.1 --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 1dc34ba..0259a3e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,11 +1,11 @@ { "name": "ngraph.graph", - "version": "20.1.0", + "version": "21.0.1", "lockfileVersion": 2, "requires": true, "packages": { "": { - "version": "20.1.0", + "version": "21.0.1", "license": "BSD-3-Clause", "dependencies": { "ngraph.events": "^1.2.1" diff --git a/package.json b/package.json index 204e577..c225e96 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ngraph.graph2", - "version": "21.0.0", + "version": "21.0.1", "description": "graph data structure", "main": "index.js", "jsdelivr": "dist/ngraph.graph.min.js",