Skip to content

Commit

Permalink
Add size() with tests and docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
devinivy committed Nov 4, 2015
1 parent 60ead46 commit a861d01
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
10 changes: 10 additions & 0 deletions lib/graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ exports = module.exports = internals.Graph = function (definition) {
this._vertexLabels = {};
this._edges = {};
this._edgeLabels = {};
this._size = 0;

var vertexDefn;
var vid;
Expand Down Expand Up @@ -275,6 +276,8 @@ internals.Graph.prototype.addEdge = function (u, v, info) {
this._setEdge(v, u, info);
}

this._size++;

return this;
};

Expand Down Expand Up @@ -321,6 +324,8 @@ internals.Graph.prototype.removeEdge = function (u, v) {
this._unsetEdge(v, u);
}

this._size--;

return this;
};

Expand All @@ -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) {
Expand Down
37 changes: 37 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down

0 comments on commit a861d01

Please sign in to comment.