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