📄 graphaslists.cs
字号:
namespace Opus6
{
using System;
using System.Collections;
[Copyright("Copyright (c) 2001 by Bruno R. Preiss, P.Eng."), Version("$Id: GraphAsLists.cs,v 1.5 2001/10/28 19:50:09 brpreiss Exp $")]
public class GraphAsLists : AbstractGraph
{
public GraphAsLists(int size) : base(size)
{
this.adjacencyList = new LinkedList[size];
for (int num1 = 0; num1 < size; num1++)
{
this.adjacencyList[num1] = new LinkedList();
}
}
protected override void AddEdge(Edge edge)
{
int num1 = edge.V0.Number;
this.adjacencyList[num1].Append(edge);
base.numberOfEdges++;
}
public override int CompareTo(object arg)
{
throw new MethodNotImplementedException();
}
public override Edge GetEdge(int v, int w)
{
Bounds.Check(v, 0, base.numberOfVertices);
Bounds.Check(w, 0, base.numberOfVertices);
for (LinkedList.Element element1 = this.adjacencyList[v].Head; element1 != null; element1 = element1.Next)
{
Edge edge1 = (Edge) element1.Datum;
if (edge1.V1.Number == w)
{
return edge1;
}
}
throw new ArgumentException("edge not found");
}
protected override IEnumerable GetEmanatingEdges(int v)
{
return new Enumerable(new Opus6.GraphAsLists.EmanatingEdgeEnumerator(this, v));
}
protected override IEnumerable GetIncidentEdges(int v)
{
throw new MethodNotImplementedException();
}
public override bool IsEdge(int v, int w)
{
Bounds.Check(v, 0, base.numberOfVertices);
Bounds.Check(w, 0, base.numberOfVertices);
for (LinkedList.Element element1 = this.adjacencyList[v].Head; element1 != null; element1 = element1.Next)
{
Edge edge1 = (Edge) element1.Datum;
if (edge1.V1.Number == w)
{
return true;
}
}
return false;
}
public static void Main()
{
Graph graph1 = new GraphAsLists(0x20);
AbstractGraph.TestGraph(graph1);
graph1.Purge();
AbstractGraph.TestWeightedGraph(graph1);
}
public override void Purge()
{
for (int num1 = 0; num1 < base.numberOfVertices; num1++)
{
this.adjacencyList[num1].Purge();
}
base.Purge();
}
public override IEnumerable Edges
{
get
{
return new Enumerable(new Opus6.GraphAsLists.EdgeEnumerator(this));
}
}
protected LinkedList[] adjacencyList;
private class EdgeEnumerator : IEnumerator
{
internal EdgeEnumerator(GraphAsLists graph)
{
this.v = -1;
this.ptr = null;
this.graph = graph;
}
public bool MoveNext()
{
if (this.ptr != null)
{
this.ptr = this.ptr.Next;
}
if (this.ptr == null)
{
this.v++;
while (this.v < this.graph.numberOfVertices)
{
this.ptr = this.graph.adjacencyList[this.v].Head;
if (this.ptr != null)
{
return true;
}
this.v++;
}
this.v = -1;
return false;
}
return true;
}
public void Reset()
{
this.v = -1;
this.ptr = null;
}
public object Current
{
get
{
if (this.ptr == null)
{
throw new InvalidOperationException();
}
return this.ptr.Datum;
}
}
private GraphAsLists graph;
private LinkedList.Element ptr;
private int v;
}
private class EmanatingEdgeEnumerator : IEnumerator
{
internal EmanatingEdgeEnumerator(GraphAsLists graph, int v)
{
this.ptr = null;
this.graph = graph;
this.v = v;
}
public bool MoveNext()
{
if (this.ptr == null)
{
this.ptr = this.graph.adjacencyList[this.v].Head;
}
else
{
this.ptr = this.ptr.Next;
}
return (this.ptr != null);
}
public void Reset()
{
this.ptr = null;
}
public object Current
{
get
{
if (this.ptr == null)
{
throw new InvalidOperationException();
}
return this.ptr.Datum;
}
}
private GraphAsLists graph;
private LinkedList.Element ptr;
private int v;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -