public bool TryGetSuccessor(IImplicitGraph <TVertex, TEdge> g, TVertex u, out TEdge successor) { Contract.Requires(g != null); Contract.Requires(u != null); int outDegree = g.OutDegree(u); if (outDegree > 0) { int index; if (!outEdgeIndices.TryGetValue(u, out index)) { index = 0; outEdgeIndices.Add(u, index); } TEdge e = g.OutEdge(u, index); this.outEdgeIndices[u] = (++index) % outDegree; successor = e; return(true); } successor = default(TEdge); return(false); }
protected static void OutEdge_Test( [NotNull] GraphData <int, Edge <int> > data, [NotNull] IImplicitGraph <int, Edge <int> > graph) { var edge11 = new Edge <int>(1, 1); var edge12 = new Edge <int>(1, 2); var edge13 = new Edge <int>(1, 3); data.CheckCalls(0); data.ShouldReturnValue = true; data.ShouldReturnEdges = new[] { edge11, edge12, edge13 }; Assert.AreSame(edge11, graph.OutEdge(1, 0)); data.CheckCalls(1); Assert.AreSame(edge13, graph.OutEdge(1, 2)); data.CheckCalls(1); }
protected static void OutEdge_NullThrows_Test <TVertex, TEdge>( IImplicitGraph <TVertex, TEdge> graph) where TVertex : class where TEdge : IEdge <TVertex> { // ReSharper disable once ReturnValueOfPureMethodIsNotUsed // ReSharper disable once AssignNullToNotNullAttribute Assert.Throws <ArgumentNullException>(() => graph.OutEdge(null, 0)); }
protected static void OutEdge_Test( IImplicitGraph <int, Edge <int> > graph, Action <IEnumerable <Edge <int> > > addVerticesAndEdgeRange) { var edge11 = new Edge <int>(1, 1); var edge12 = new Edge <int>(1, 2); var edge13 = new Edge <int>(1, 3); var edge24 = new Edge <int>(2, 4); var edge33 = new Edge <int>(3, 3); var edge41 = new Edge <int>(4, 1); addVerticesAndEdgeRange(new[] { edge11, edge12, edge13, edge24, edge33, edge41 }); Assert.AreSame(edge11, graph.OutEdge(1, 0)); Assert.AreSame(edge13, graph.OutEdge(1, 2)); Assert.AreSame(edge24, graph.OutEdge(2, 0)); Assert.AreSame(edge33, graph.OutEdge(3, 0)); Assert.AreSame(edge41, graph.OutEdge(4, 0)); }
protected static void OutEdge_Throws_Test( IImplicitGraph <int, Edge <int> > graph, Action <int> addVertex, Action <Edge <int> > addEdge) { const int vertex1 = 1; const int vertex2 = 2; // ReSharper disable ReturnValueOfPureMethodIsNotUsed Assert.Throws <VertexNotFoundException>(() => graph.OutEdge(vertex1, 0)); addVertex(vertex1); addVertex(vertex2); AssertIndexOutOfRange(() => graph.OutEdge(vertex1, 0)); addEdge(new Edge <int>(1, 2)); AssertIndexOutOfRange(() => graph.OutEdge(vertex1, 5)); // ReSharper restore ReturnValueOfPureMethodIsNotUsed }
protected static void OutEdge_Throws_Test( [NotNull] GraphData <int, Edge <int> > data, [NotNull] IImplicitGraph <int, Edge <int> > graph) { // ReSharper disable ReturnValueOfPureMethodIsNotUsed data.CheckCalls(0); data.ShouldReturnValue = false; Assert.Throws <VertexNotFoundException>(() => graph.OutEdge(1, 0)); data.CheckCalls(1); data.ShouldReturnValue = true; AssertIndexOutOfRange(() => graph.OutEdge(1, 0)); data.CheckCalls(1); data.ShouldReturnEdges = new[] { new Edge <int>(1, 2) }; AssertIndexOutOfRange(() => graph.OutEdge(1, 1)); data.CheckCalls(1); // ReSharper restore ReturnValueOfPureMethodIsNotUsed }
protected static void OutEdge_ImmutableGraph_Test( IMutableVertexAndEdgeSet <int, Edge <int> > wrappedGraph, Func <IImplicitGraph <int, SEquatableEdge <int> > > createGraph) { var edge11 = new Edge <int>(1, 1); var edge12 = new Edge <int>(1, 2); var edge13 = new Edge <int>(1, 3); var edge24 = new Edge <int>(2, 4); var edge33 = new Edge <int>(3, 3); var edge41 = new Edge <int>(4, 1); wrappedGraph.AddVerticesAndEdgeRange(new[] { edge11, edge12, edge13, edge24, edge33, edge41 }); IImplicitGraph <int, SEquatableEdge <int> > graph = createGraph(); Assert.AreEqual(new SEquatableEdge <int>(1, 1), graph.OutEdge(1, 0)); Assert.AreEqual(new SEquatableEdge <int>(1, 3), graph.OutEdge(1, 2)); Assert.AreEqual(new SEquatableEdge <int>(2, 4), graph.OutEdge(2, 0)); Assert.AreEqual(new SEquatableEdge <int>(3, 3), graph.OutEdge(3, 0)); Assert.AreEqual(new SEquatableEdge <int>(4, 1), graph.OutEdge(4, 0)); }
protected static void OutEdge_ImmutableGraph_Test( [NotNull] IMutableVertexAndEdgeSet <int, Edge <int> > wrappedGraph, [NotNull, InstantHandle] Func <IImplicitGraph <int, Edge <int> > > createGraph) { var edge11 = new Edge <int>(1, 1); var edge12 = new Edge <int>(1, 2); var edge13 = new Edge <int>(1, 3); var edge24 = new Edge <int>(2, 4); var edge33 = new Edge <int>(3, 3); var edge41 = new Edge <int>(4, 1); wrappedGraph.AddVerticesAndEdgeRange(new[] { edge11, edge12, edge13, edge24, edge33, edge41 }); IImplicitGraph <int, Edge <int> > graph = createGraph(); Assert.AreSame(edge11, graph.OutEdge(1, 0)); Assert.AreSame(edge13, graph.OutEdge(1, 2)); Assert.AreSame(edge24, graph.OutEdge(2, 0)); Assert.AreSame(edge33, graph.OutEdge(3, 0)); Assert.AreSame(edge41, graph.OutEdge(4, 0)); }
protected static void OutEdge_Throws_ImmutableGraph_ReversedTest( IMutableVertexAndEdgeSet <int, Edge <int> > wrappedGraph, Func <IImplicitGraph <int, SReversedEdge <int, Edge <int> > > > createGraph) { IImplicitGraph <int, SReversedEdge <int, Edge <int> > > graph = createGraph(); const int vertex1 = 1; const int vertex2 = 2; // ReSharper disable ReturnValueOfPureMethodIsNotUsed Assert.Throws <VertexNotFoundException>(() => graph.OutEdge(vertex1, 0)); wrappedGraph.AddVertex(vertex1); wrappedGraph.AddVertex(vertex2); graph = createGraph(); AssertIndexOutOfRange(() => graph.OutEdge(vertex1, 0)); wrappedGraph.AddEdge(new Edge <int>(1, 2)); graph = createGraph(); AssertIndexOutOfRange(() => graph.OutEdge(vertex1, 5)); // ReSharper restore ReturnValueOfPureMethodIsNotUsed }
public override TEdge Successor(IImplicitGraph <TVertex, TEdge> g, TVertex u) { int outDegree = g.OutDegree(u); if (outDegree == 0) { return(default(TEdge)); } int index = this.Rand.Next(0, outDegree); return(g.OutEdge(u, index)); }
/// <inheritdoc /> public override bool TryGetSuccessor(IImplicitGraph<TVertex, TEdge> graph, TVertex vertex, out TEdge successor) { int outDegree = graph.OutDegree(vertex); if (outDegree > 0) { int index = Rand.Next(0, outDegree); successor = graph.OutEdge(vertex, index); return true; } successor = default(TEdge); return false; }
public override bool TryGetSuccessor(IImplicitGraph <TVertex, TEdge> g, TVertex u, out TEdge successor) { int outDegree = g.OutDegree(u); if (outDegree > 0) { int index = this.Rand.Next(0, outDegree); successor = g.OutEdge(u, index); return(true); } successor = default(TEdge); return(false); }
protected static void OutEdge_Throws_ImmutableGraph_Test <TEdge>( [NotNull] IMutableVertexAndEdgeSet <int, Edge <int> > wrappedGraph, [NotNull, InstantHandle] Func <IImplicitGraph <int, TEdge> > createGraph) where TEdge : IEdge <int> { IImplicitGraph <int, TEdge> graph = createGraph(); const int vertex1 = 1; const int vertex2 = 2; // ReSharper disable ReturnValueOfPureMethodIsNotUsed Assert.Throws <VertexNotFoundException>(() => graph.OutEdge(vertex1, 0)); wrappedGraph.AddVertex(vertex1); wrappedGraph.AddVertex(vertex2); graph = createGraph(); AssertIndexOutOfRange(() => graph.OutEdge(vertex1, 0)); wrappedGraph.AddEdge(new Edge <int>(1, 2)); graph = createGraph(); AssertIndexOutOfRange(() => graph.OutEdge(vertex1, 5)); // ReSharper restore ReturnValueOfPureMethodIsNotUsed }
/// <inheritdoc /> public bool TryGetSuccessor(IImplicitGraph <TVertex, TEdge> graph, TVertex vertex, out TEdge successor) { int outDegree = graph.OutDegree(vertex); if (outDegree > 0) { if (!_outEdgeIndices.TryGetValue(vertex, out int index)) { index = 0; _outEdgeIndices.Add(vertex, index); } TEdge edge = graph.OutEdge(vertex, index); _outEdgeIndices[vertex] = ++index % outDegree; successor = edge; return(true); } successor = default(TEdge); return(false); }
public TEdge Successor(IImplicitGraph <TVertex, TEdge> g, TVertex u) { int outDegree = g.OutDegree(u); if (outDegree == 0) { return(default(TEdge)); } int index; if (!outEdgeIndices.TryGetValue(u, out index)) { index = 0; outEdgeIndices.Add(u, index); } TEdge e = g.OutEdge(u, index); this.outEdgeIndices[u] = (++index) % outDegree; return(e); }
public Edge OutEdge(Vertex v, int index) { return(_impl.OutEdge(v, index)); }