📄 queueasarray.cs
字号:
namespace Opus6
{
using System;
using System.Collections;
[Copyright("Copyright (c) 2001 by Bruno R. Preiss, P.Eng."), Version("$Id: QueueAsArray.cs,v 1.5 2001/10/28 19:50:09 brpreiss Exp $")]
public class QueueAsArray : AbstractContainer, Opus6.Queue, Container, IComparable, IEnumerable
{
public QueueAsArray(int size)
{
this.array = new object[size];
this.head = 0;
this.tail = size - 1;
}
public override void Accept(Visitor visitor)
{
int num1 = this.head;
for (int num2 = 0; num2 < base.count; num2++)
{
visitor.Visit(this.array[num1]);
if (visitor.IsDone)
{
return;
}
if (++num1 == this.array.Length)
{
num1 = 0;
}
}
}
public override int CompareTo(object arg)
{
throw new MethodNotImplementedException();
}
public object Dequeue()
{
if (base.count == 0)
{
throw new ContainerEmptyException();
}
object obj1 = this.array[this.head];
this.array[this.head] = null;
if (++this.head == this.array.Length)
{
this.head = 0;
}
base.count--;
return obj1;
}
public void Enqueue(object obj)
{
if (base.count == this.array.Length)
{
throw new ContainerFullException();
}
if (++this.tail == this.array.Length)
{
this.tail = 0;
}
this.array[this.tail] = obj;
base.count++;
}
public override IEnumerator GetEnumerator()
{
return new Opus6.QueueAsArray.Enumerator(this);
}
public static void Main()
{
Opus6.Console.WriteLine("QueueAsArray Test");
Opus6.Queue queue1 = new QueueAsArray(5);
QueueAsArray.TestQueue(queue1);
}
public override void Purge()
{
while (base.count > 0)
{
this.array[this.head] = null;
if (++this.head == this.array.Length)
{
this.head = 0;
}
base.count--;
}
}
internal static void TestQueue(Opus6.Queue queue)
{
for (int num1 = 0; num1 < 6; num1++)
{
if (queue.IsFull)
{
break;
}
queue.Enqueue(new Association(num1, num1));
}
Opus6.Console.WriteLine(queue);
while (!queue.IsEmpty)
{
object obj1 = queue.Dequeue();
Opus6.Console.WriteLine(obj1);
}
}
public object Head
{
get
{
if (base.count == 0)
{
throw new ContainerEmptyException();
}
return this.array[this.head];
}
}
public override bool IsFull
{
get
{
return (base.count == this.array.Length);
}
}
protected object[] array;
protected int head;
protected int tail;
private class Enumerator : IEnumerator
{
internal Enumerator(QueueAsArray queue)
{
this.position = -1;
this.queue = queue;
}
public bool MoveNext()
{
if (++this.position == this.queue.Count)
{
this.position = -1;
}
return (this.position >= 0);
}
public void Reset()
{
this.position = -1;
}
public object Current
{
get
{
if (this.position < 0)
{
throw new InvalidOperationException();
}
return this.queue.array[(this.queue.head + this.position) % this.queue.array.Length];
}
}
protected int position;
private QueueAsArray queue;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -