Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix update link #44

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 29 additions & 30 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -156,7 +157,7 @@ function createGraph(options) {
* Synonym for `getLinkCount()`
*/
getLinksCount: getLinkCount,

/**
* Synonym for `getNodeCount()`
*/
Expand Down Expand Up @@ -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()
*
Expand Down Expand Up @@ -343,7 +344,6 @@ function createGraph(options) {
return true;
}


function addLink(fromId, toId, data) {
enterModification();

Expand All @@ -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');
Expand All @@ -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);
}

Expand All @@ -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);
}

Expand Down Expand Up @@ -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');
Expand All @@ -450,7 +446,7 @@ function createGraph(options) {

function clear() {
enterModification();
forEachNode(function(node) {
forEachNode(function (node) {
removeNode(node.id);
});
exitModification();
Expand Down Expand Up @@ -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();
Expand All @@ -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);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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",
Expand Down