Skip to content

Commit

Permalink
Remove more old code + add more functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
cpettitt committed Sep 24, 2014
1 parent df6f95b commit 13c2644
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 479 deletions.
119 changes: 40 additions & 79 deletions lib/graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ var EDGE_KEY_DELIM = "\x00",
function Graph(opts) {
this.isMultigraph = opts && opts.multigraph;

this.isDirected = !opts || opts.directed;

// Attributes for the graph itself
this.attrs = {};

Expand Down Expand Up @@ -39,16 +41,6 @@ Graph.prototype._edgeCount = 0;

/* === Graph functions ========== */

Graph.prototype.copy = function() {
var copy = new Graph();
copy._nodeCount = this._nodeCount;
copy._edgeCount = this._edgeCount;
copy._nodes = _.clone(this._nodes);
copy._inEdges = _.mapValues(this._inEdges, _.clone);
copy._outEdges = _.mapValues(this._outEdges, _.clone);
return copy;
};


/* === Node functions ========== */

Expand Down Expand Up @@ -76,57 +68,20 @@ Graph.prototype.hasNode = function(v) {
return _.has(this._nodes, v);
};

Graph.prototype.setNodes = function(vs, label) {
var args = arguments;
_.each(vs, function(v) {
if (args.length > 1) {
this.setNode(v, label);
} else {
this.setNode(v);
}
}, this);
return this;
};

Graph.prototype.removeNode = function(v) {
var self = this;
if (_.has(this._nodes, v)) {
--this._nodeCount;
var removeEdge = function(e) { self.removeEdge(e); };
delete this._nodes[v];

this._onRemoveNode(v);

delete this._inEdges[v];
delete this._outEdges[v];
_.each(_.keys(this._in[v]), removeEdge);
delete this._in[v];
_.each(_.keys(this._out[v]), removeEdge);
delete this._out[v];
--this._nodeCount;
}
return this;
};

Graph.prototype.successors = function(v) {
var outEdges = this._outEdges[v];
return outEdges && _.keys(outEdges);
};

Graph.prototype.predecessors = function(v) {
var inEdges = this._inEdges[v];
return inEdges && _.keys(inEdges);
};

Graph.prototype.inEdges = function(v) {
var inEdges = this._inEdges[v];
if (inEdges) {
return _.values(inEdges);
}
};

Graph.prototype.outEdges = function(v) {
var outEdges = this._outEdges[v];
if (outEdges) {
return _.values(outEdges);
}
};

Graph.prototype._onRemoveNode = _.noop;


/* === Edge functions ========== */

Expand All @@ -139,7 +94,7 @@ Graph.prototype.edges = function() {
};

Graph.prototype.edge = function(v, w, name) {
var e = arguments.length === 1 ? v : createEdgeKey(v, w, name),
var e = arguments.length === 1 ? v : this.edgeKey(v, w, name),
edge;

edge = this._edges[e];
Expand All @@ -150,10 +105,10 @@ Graph.prototype.edge = function(v, w, name) {
if (_.isUndefined(name) || this.isMultigraph) {
// We need to create the edge
if (arguments.length === 1) {
var splitKey = splitEdgeKey(e);
v = splitKey[0];
w = splitKey[1];
name = splitKey[2];
var splitKey = this.edgeKeyParts(e);
v = splitKey.v;
w = splitKey.w;
name = splitKey.name;
}

// Ensure the nodes are created
Expand All @@ -170,31 +125,37 @@ Graph.prototype.edge = function(v, w, name) {
};

Graph.prototype.hasEdge = function(v, w, name) {
var e = arguments.length === 1 ? v : createEdgeKey(v, w, name);
var e = arguments.length === 1 ? v : this.edgeKey(v, w, name);
return _.has(this._edges, e);
};

Graph.prototype.edgeKey = createEdgeKey;
Graph.prototype.removeEdge = function(v, w, name) {
var e = arguments.length === 1 ? v : this.edgeKey(v, w, name);
if (_.has(this._edges, e)) {
if (arguments.length === 1) {
var splitKey = this.edgeKeyParts(e);
v = splitKey.v;
w = splitKey.w;
name = splitKey.name;
}
delete this._out[v][e];
delete this._in[w][e];
delete this._edges[e];
this._edgeCount--;
}
};

function createEdgeKey(v, w, name) {
Graph.prototype.edgeKey = function(v, w, name) {
if (!this.isDirected && v > w) {
var tmp = v;
v = w;
w = tmp;
}
return v + EDGE_KEY_DELIM + w + EDGE_KEY_DELIM +
(_.isUndefined(name) ? DEFAULT_EDGE_NAME : name);
}

function splitEdgeKey(e) {
return e.split(EDGE_KEY_DELIM);
}
};

Graph.prototype.setPath = function(vs, label) {
var self = this,
args = arguments;
_.reduce(vs, function(v, w) {
if (args.length > 1) {
self.setEdge(v, w, label);
} else {
self.setEdge(v, w);
}
return w;
});
return this;
Graph.prototype.edgeKeyParts = function splitEdgeKey(e) {
var parts = e.split(EDGE_KEY_DELIM);
return { v: parts[0], w: parts[1], name: parts[2] };
};
Loading

0 comments on commit 13c2644

Please sign in to comment.