Skip to content

Commit 5673047

Browse files
committed
#33 compatibility: eachNode,eachEdge,filterNodes
Fixed issue #33 Graph.filterNodes is not available anymore to be compatible with previous releases
1 parent cbf5e07 commit 5673047

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

lib/graph.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,27 @@ Graph.prototype.neighbors = function(v) {
268268
}
269269
};
270270

271+
Graph.prototype.eachNode = function(func) {
272+
for (var id in this._nodes) {
273+
func(id, this._nodes[id]);
274+
}
275+
};
276+
277+
Graph.prototype.filterNodes = function(filter) {
278+
var copy = new this.constructor();
279+
copy.graph(this.graph());
280+
this.eachNode(function(u, value) {
281+
if (filter(u)) {
282+
copy.setNode(u, value);
283+
}
284+
});
285+
this.eachEdge(function(id, v, w, value) {
286+
if (copy.hasNode(v) && copy.hasNode(w)) {
287+
copy.setEdge(v, w, value);
288+
}
289+
});
290+
return copy;
291+
};
271292
/* === Edge functions ========== */
272293

273294
Graph.prototype.setDefaultEdgeLabel = function(newDefault) {
@@ -428,6 +449,14 @@ Graph.prototype.nodeEdges = function(v, w) {
428449
}
429450
};
430451

452+
Graph.prototype.eachEdge = function(func) {
453+
var id, edge;
454+
for (id in this._edgeObjs) {
455+
edge=this._edgeObjs[id];
456+
func(id, edge.v, edge.w, this._edgeLabels[id]);
457+
}
458+
};
459+
431460
function incrementOrInitEntry(map, k) {
432461
if (_.has(map, k)) {
433462
map[k]++;

0 commit comments

Comments
 (0)