1
0
Fork 0

Graph Module: Even more in Graph Common

This commit is contained in:
Lucas Dohmen 2013-06-19 17:06:30 +02:00
parent 6a84d4726b
commit f64c227b16
3 changed files with 55 additions and 165 deletions

View File

@ -326,38 +326,10 @@ Vertex.prototype.setProperty = function (name, value) {
return name;
};
////////////////////////////////////////////////////////////////////////////////
/// @brief returns the number of edges
////////////////////////////////////////////////////////////////////////////////
Vertex.prototype.degree = function () {
return this.getEdges().length;
};
////////////////////////////////////////////////////////////////////////////////
/// @brief returns the number of in-edges
////////////////////////////////////////////////////////////////////////////////
Vertex.prototype.inDegree = function () {
return this.getInEdges().length;
};
////////////////////////////////////////////////////////////////////////////////
/// @brief returns the number of out-edges
////////////////////////////////////////////////////////////////////////////////
Vertex.prototype.outDegree = function () {
return this.getOutEdges().length;
};
////////////////////////////////////////////////////////////////////////////////
/// @}
////////////////////////////////////////////////////////////////////////////////
// -----------------------------------------------------------------------------
// --SECTION-- Graph
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// --SECTION-- constructors and destructors
// -----------------------------------------------------------------------------
@ -512,26 +484,6 @@ Graph.prototype.removeEdge = function (edge) {
edge._properties = undefined;
};
////////////////////////////////////////////////////////////////////////////////
/// @brief returns the number of vertices
///
/// @FUN{@FA{graph}.order()}
////////////////////////////////////////////////////////////////////////////////
Graph.prototype.order = function () {
return this._vertices.count();
};
////////////////////////////////////////////////////////////////////////////////
/// @brief returns the number of edges
///
/// @FUN{@FA{graph}.size()}
////////////////////////////////////////////////////////////////////////////////
Graph.prototype.size = function () {
return this._edges.count();
};
////////////////////////////////////////////////////////////////////////////////
/// @}
////////////////////////////////////////////////////////////////////////////////

View File

@ -759,6 +759,26 @@ Graph.prototype.addVertex = function (id, data, waitForSync) {
return this._saveVertex(id, this._prepareVertexData(data), waitForSync);
};
////////////////////////////////////////////////////////////////////////////////
/// @brief returns the number of vertices
///
/// @FUN{@FA{graph}.order()}
////////////////////////////////////////////////////////////////////////////////
Graph.prototype.order = function () {
return this._vertices.count();
};
////////////////////////////////////////////////////////////////////////////////
/// @brief returns the number of edges
///
/// @FUN{@FA{graph}.size()}
////////////////////////////////////////////////////////////////////////////////
Graph.prototype.size = function () {
return this._edges.count();
};
////////////////////////////////////////////////////////////////////////////////
/// @}
////////////////////////////////////////////////////////////////////////////////

View File

@ -36,7 +36,8 @@ var arangodb = require("org/arangodb"),
Edge = common.Edge,
Graph = common.Graph,
Vertex = common.Vertex,
GraphArray = common.GraphArray;
GraphArray = common.GraphArray,
Iterator = common.Iterator;
// -----------------------------------------------------------------------------
// --SECTION-- module "org/arangodb/graph"
@ -326,51 +327,32 @@ Vertex.prototype.setProperty = function (name, value) {
////////////////////////////////////////////////////////////////////////////////
/// @brief returns the number of edges
///
/// @FUN{@FA{vertex}.degree()}
///
/// Returns the number of edges of the @FA{vertex}.
///
////////////////////////////////////////////////////////////////////////////////
Vertex.prototype.degree = function () {
return this._graph._edges.edges(this._properties._id).length;
return this.getEdges().length;
};
////////////////////////////////////////////////////////////////////////////////
/// @brief returns the number of in-edges
///
/// @FUN{@FA{vertex}.inDegree()}
///
/// Returns the number of inbound edges of the @FA{vertex}.
///
////////////////////////////////////////////////////////////////////////////////
Vertex.prototype.inDegree = function () {
return this._graph._edges.inEdges(this._properties._id).length;
return this.getInEdges().length;
};
////////////////////////////////////////////////////////////////////////////////
/// @brief returns the number of out-edges
///
/// @FUN{@FA{vertex}.outDegree()}
///
/// Returns the number of outbound edges of the @FA{vertex}.
///
////////////////////////////////////////////////////////////////////////////////
Vertex.prototype.outDegree = function () {
return this._graph._edges.outEdges(this._properties._id).length;
return this.getOutEdges().length;
};
////////////////////////////////////////////////////////////////////////////////
/// @}
////////////////////////////////////////////////////////////////////////////////
// -----------------------------------------------------------------------------
// --SECTION-- Graph
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// --SECTION-- constructors and destructors
// -----------------------------------------------------------------------------
@ -558,7 +540,10 @@ Graph.prototype._saveEdge = function(id, out_vertex, in_vertex, shallow, waitFor
}
var ref = this._edges.save(out_vertex._properties._id,
in_vertex._properties._id, shallow, waitForSync);
in_vertex._properties._id,
shallow,
waitForSync);
return this.constructEdge(ref._id);
};
@ -591,24 +576,20 @@ Graph.prototype._saveVertex = function (id, shallow, waitForSync) {
////////////////////////////////////////////////////////////////////////////////
Graph.prototype.getVertex = function (id) {
var ref;
var vertex;
var ref, vertex;
try {
ref = this._vertices.document(id);
}
catch (e) {
} catch (e) {
ref = null;
}
if (ref !== null) {
vertex = this.constructVertex(ref);
}
else {
} else {
try {
vertex = this.constructVertex(id);
}
catch (e1) {
} catch (e1) {
vertex = null;
}
}
@ -630,30 +611,12 @@ Graph.prototype.getVertex = function (id) {
////////////////////////////////////////////////////////////////////////////////
Graph.prototype.getVertices = function () {
var that = this;
var all = this._vertices.all();
var v;
var Iterator;
Iterator = function () {
this.next = function next() {
if (all.hasNext()) {
return that.constructVertex(all.next());
}
return undefined;
var all = this._vertices.all(),
graph = this,
wrapper = function(object) {
return graph.constructVertex(object);
};
this.hasNext = function hasNext() {
return all.hasNext();
};
this._PRINT = function (context) {
context.output += "[vertex iterator]";
};
};
return new Iterator();
return new Iterator(wrapper, graph.constructVertex, "[edge iterator]");
};
////////////////////////////////////////////////////////////////////////////////
@ -669,24 +632,20 @@ Graph.prototype.getVertices = function () {
////////////////////////////////////////////////////////////////////////////////
Graph.prototype.getEdge = function (id) {
var ref;
var edge;
var ref, edge;
try {
ref = this._edges.document(id);
}
catch (e) {
} catch (e) {
ref = null;
}
if (ref !== null) {
edge = this.constructEdge(ref);
}
else {
} else {
try {
edge = this.constructEdge(id);
}
catch (e1) {
} catch (e1) {
edge = null;
}
}
@ -708,30 +667,12 @@ Graph.prototype.getEdge = function (id) {
////////////////////////////////////////////////////////////////////////////////
Graph.prototype.getEdges = function () {
var that = this;
var all = this._edges.all();
var v;
var Iterator;
Iterator = function () {
this.next = function next() {
if (all.hasNext()) {
return that.constructEdge(all.next());
}
return undefined;
var all = this._edges.all(),
graph = this,
wrapper = function(object) {
return graph.constructEdge(object);
};
this.hasNext = function hasNext() {
return all.hasNext();
};
this._PRINT = function (context) {
context.output += "[edge iterator]";
};
};
return new Iterator();
return new Iterator(wrapper, all, "[edge iterator]");
};
////////////////////////////////////////////////////////////////////////////////
@ -747,15 +688,14 @@ Graph.prototype.getEdges = function () {
////////////////////////////////////////////////////////////////////////////////
Graph.prototype.removeVertex = function (vertex, waitForSync) {
var result;
var graph = this;
var result, graph = this;
this.emptyCachedPredecessors();
if (vertex._properties) {
result = this._vertices.remove(vertex._properties, true, waitForSync);
if (! result) {
if (!result) {
throw "cannot delete vertex";
}
@ -796,26 +736,6 @@ Graph.prototype.removeEdge = function (edge, waitForSync) {
}
};
////////////////////////////////////////////////////////////////////////////////
/// @brief returns the number of vertices
///
/// @FUN{@FA{graph}.order()}
////////////////////////////////////////////////////////////////////////////////
Graph.prototype.order = function () {
return this._vertices.count();
};
////////////////////////////////////////////////////////////////////////////////
/// @brief returns the number of edges
///
/// @FUN{@FA{graph}.size()}
////////////////////////////////////////////////////////////////////////////////
Graph.prototype.size = function () {
return this._edges.count();
};
////////////////////////////////////////////////////////////////////////////////
/// @}
////////////////////////////////////////////////////////////////////////////////
@ -878,8 +798,7 @@ Graph.prototype.constructVertex = function (data) {
if (typeof data === "string") {
id = data;
}
else {
} else {
id = data._id;
}
@ -903,21 +822,20 @@ Graph.prototype.constructVertex = function (data) {
////////////////////////////////////////////////////////////////////////////////
Graph.prototype.constructEdge = function (data) {
var id;
var id, edge, properties;
if (typeof data === "string") {
id = data;
}
else {
} else {
id = data._id;
}
var edge = this._edgesCache[id];
edge = this._edgesCache[id];
if (edge === undefined) {
var properties = this._edges.document(id);
properties = this._edges.document(id);
if (! properties) {
if (!properties) {
throw "accessing a deleted edge";
}