From a861d012c56d8e07a82fa78e00544aa014c2ee9d Mon Sep 17 00:00:00 2001 From: Devin Ivy Date: Tue, 3 Nov 2015 23:46:28 -0500 Subject: [PATCH] Add size() with tests and docs. --- README.md | 3 +++ lib/graph.js | 10 ++++++++++ test/index.js | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+) diff --git a/README.md b/README.md index a3f63b8..61bc021 100644 --- a/README.md +++ b/README.md @@ -157,6 +157,9 @@ Removes the edge that runs from vertex `u` to vertex `v`. If the graph is undir #### `graph.removeEdges(edgePairsOrLabel)` Removes a collection of edges from the graph. If `edgePairsOrLabel` is an array of edge-pairs (each edge-pair an array of two vertex ids), those edges will be removed. If `edgePairsOrLabel` is a label, all edges with that label will be removed. +#### `graph.size()` +Returns the number of edges in the graph. + #### `graph.equals(anotherGraph, [matchWeights])` Returns `true` when `anotherGraph` has the same graph structure and vertex ids, and returns `false` otherwise. This ignores all labels and vertex data, but takes into account if the graphs are directed or not. When `matchWeights` is `true`, it will require that edge weights also correspond for the two graphs to be considered equal. diff --git a/lib/graph.js b/lib/graph.js index 90bf87a..cd41adf 100644 --- a/lib/graph.js +++ b/lib/graph.js @@ -19,6 +19,7 @@ exports = module.exports = internals.Graph = function (definition) { this._vertexLabels = {}; this._edges = {}; this._edgeLabels = {}; + this._size = 0; var vertexDefn; var vid; @@ -275,6 +276,8 @@ internals.Graph.prototype.addEdge = function (u, v, info) { this._setEdge(v, u, info); } + this._size++; + return this; }; @@ -321,6 +324,8 @@ internals.Graph.prototype.removeEdge = function (u, v) { this._unsetEdge(v, u); } + this._size--; + return this; }; @@ -337,6 +342,11 @@ internals.Graph.prototype.removeEdges = function (pairs /* or a label */) { return this; }; +internals.Graph.prototype.size = function () { + + return this._size; +}; + internals.Graph.prototype.equals = function (graph, matchWeights) { if (this.directed !== graph.directed) { diff --git a/test/index.js b/test/index.js index ed1779d..22b036b 100644 --- a/test/index.js +++ b/test/index.js @@ -1638,6 +1638,43 @@ describe('Gert', function () { done(); }); + it('size() returns the size of a graph or digraph.', function (done) { + + var digraph = new Graph({ + directed: true, + edges: [ + ['a', 'b'], + ['b', 'a'], + ['b', 'c'], + ['b', 'b'] + ] + }); + + digraph.addEdge('c', 'a'); + digraph.addEdge('c', 'b'); + digraph.removeEdge('a', 'b'); + digraph.updateEdge('c', 'a', {}); + + var nondigraph = new Graph({ + directed: false, + edges: [ + ['a', 'b'], + ['b', 'c'], + ['b', 'b'] + ] + }); + + nondigraph.removeEdge('a', 'b'); + nondigraph.removeEdge('b', 'c'); + nondigraph.addEdge('c', 'a'); + nondigraph.updateEdge('b', 'b', {}); + + expect(digraph.size()).to.equal(5); + expect(nondigraph.size()).to.equal(2); + + done(); + }); + it('equals(graph) says digraphs and non-digraphs are not equal.', function (done) { var graph = new Graph({ directed: false });