digraphasmatrix.cs

来自「Data Structures and Algorithms with Obj」· CS 代码 · 共 107 行

CS
107
字号
namespace Opus6
{
    using System;
    using System.Collections;

    [Copyright("Copyright (c) 2001 by Bruno R. Preiss, P.Eng."), Version("$Id: DigraphAsMatrix.cs,v 1.5 2001/10/28 19:50:09 brpreiss Exp $")]
    public class DigraphAsMatrix : GraphAsMatrix, Digraph, Graph, Container, IComparable, IEnumerable
    {
        public DigraphAsMatrix(int size) : base(size)
        {
        }

        protected override void AddEdge(Edge edge)
        {
            int num1 = edge.V0.Number;
            int num2 = edge.V1.Number;
            if (base.matrix[num1, num2] != null)
            {
                throw new ArgumentException("duplicate edge");
            }
            base.matrix[num1, num2] = edge;
            base.numberOfEdges++;
        }

        public static void Main()
        {
            Digraph digraph1 = new DigraphAsMatrix(0x20);
            AbstractGraph.TestDigraph(digraph1);
            digraph1.Purge();
            AbstractGraph.TestWeightedDigraph(digraph1);
        }


        public override IEnumerable Edges
        {
            get
            {
                return new Enumerable(new Opus6.DigraphAsMatrix.EdgeEnumerator(this));
            }
        }



        private class EdgeEnumerator : IEnumerator
        {
            internal EdgeEnumerator(DigraphAsMatrix graph)
            {
                this.v = -1;
                this.w = -1;
                this.graph = graph;
            }

            public bool MoveNext()
            {
                while (true)
                {
                    if (this.w == -1)
                    {
                        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 DigraphAsMatrix graph;
            private int v;
            private int w;
        }
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?