// ... everybody needs good neighbours ... import std.datetime, std.stdio; import dgraph.graph, dgraph.test.samplegraph50; size_t checkNeighbours(bool directed)(ref Graph!directed g) { size_t neighbourSum = 0; foreach(vertex; 0 .. g.vertexCount) { static if (directed) { foreach(n; g.neighboursIn(vertex)) { neighbourSum += n; } foreach(n; g.neighboursOut(vertex)) { neighbourSum += n; } } else { foreach(n; g.neighbours(vertex)) { neighbourSum += n; } } } return neighbourSum; } void main() { auto g = new Graph!false; g.addVertices(50); // g.addVertices(10_000); size_t neighbourSum = 0; foreach(i; 0 .. sampleGraph50.length / 2) { g.addEdge(sampleGraph50[2*i], sampleGraph50[2 * i + 1]); } /* foreach(i; 0 .. sampleGraph10k.length / 2) { g.addEdge(sampleGraph10k[2*i], sampleGraph10k[2 * i + 1]); }*/ writeln("Vertex count: ", g.vertexCount); writeln("Edge count: ", g.edgeCount); StopWatch watch; watch.start; foreach(i; 0 .. 500_000) { neighbourSum += checkNeighbours(g); } watch.stop; writeln("Done in ", watch.peek.msecs, " ms."); writeln("Neighbour sum = ", neighbourSum); }