Skip to content

Commit 8905ce8

Browse files
Merge branch 'master' of https://github.com/ilyakharlamov/graphlib into filterNodes
2 parents bdbe857 + 5673047 commit 8905ce8

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
@@ -270,6 +270,27 @@ Graph.prototype.neighbors = function(v) {
270270
}
271271
};
272272

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

275296
Graph.prototype.setDefaultEdgeLabel = function(newDefault) {
@@ -430,6 +451,14 @@ Graph.prototype.nodeEdges = function(v, w) {
430451
}
431452
};
432453

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

0 commit comments

Comments
 (0)