Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/ilyakharlamov/graphlib in…
Browse files Browse the repository at this point in the history
…to filterNodes
  • Loading branch information
cpettitt-linkedin committed Jul 10, 2015
2 parents bdbe857 + 5673047 commit 8905ce8
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions lib/graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,27 @@ Graph.prototype.neighbors = function(v) {
}
};

Graph.prototype.eachNode = function(func) {
for (var id in this._nodes) {
func(id, this._nodes[id]);
}
};

Graph.prototype.filterNodes = function(filter) {
var copy = new this.constructor();
copy.graph(this.graph());
this.eachNode(function(u, value) {
if (filter(u)) {
copy.setNode(u, value);
}
});
this.eachEdge(function(id, v, w, value) {
if (copy.hasNode(v) && copy.hasNode(w)) {
copy.setEdge(v, w, value);
}
});
return copy;
};
/* === Edge functions ========== */

Graph.prototype.setDefaultEdgeLabel = function(newDefault) {
Expand Down Expand Up @@ -430,6 +451,14 @@ Graph.prototype.nodeEdges = function(v, w) {
}
};

Graph.prototype.eachEdge = function(func) {
var id, edge;
for (id in this._edgeObjs) {
edge=this._edgeObjs[id];
func(id, edge.v, edge.w, this._edgeLabels[id]);
}
};

function incrementOrInitEntry(map, k) {
if (_.has(map, k)) {
map[k]++;
Expand Down

0 comments on commit 8905ce8

Please sign in to comment.