Queue类的简化实现

针对特殊需要时修改比较方便。

 

using  System;
public   class  FIFO
{
    
private object[] _Buffer;
    
private float _Grow;
    
private int _Head;
    
private int _Tail;
    
private int _ItemCount;
    
public int Count get return this._ItemCount; } }

    
public FIFO(int BufferSize, float Grow)
    
{
        
if (BufferSize < 0throw new ArgumentOutOfRangeException(); }
        
if (Grow < 1 || Grow > 10throw new ArgumentOutOfRangeException(); }
        
this._Buffer = new object[BufferSize];
        
this._Grow = Grow;
    }

    
public FIFO() : this(322{ }

    
void ChangeBufferSize(int Size)
    
{
        
object[] newbuf = new object[Size];
        
if (this._ItemCount > 0)
        
{
            
if (this._Head < this._Tail)
            
{
                Array.Copy(
this._Buffer, this._Head, newbuf, 0this._ItemCount);
            }

            
else
            
{
                Array.Copy(
this._Buffer, this._Head, newbuf, 0this._Buffer.Length - this._Head);
                Array.Copy(
this._Buffer, 0, newbuf, this._Buffer.Length - this._Head, this._Tail);
            }

        }

        
this._Buffer = newbuf;
        
this._Head = 0;
        
this._Tail = this._ItemCount;
    }

    
public void PUT(object obj)
    
{
        
if (this._ItemCount == this._Buffer.Length)   //空间不足
        {
            
int newSize = (int)(this._Buffer.Length * this._Grow);
            
if (newSize < (this._Buffer.Length + 4)) newSize = this._Buffer.Length + 4;
            
this.ChangeBufferSize(newSize);
        }

        
this._Buffer[this._Tail] = obj;
        
this._Tail = (this._Tail + 1% this._Buffer.Length;
        
this._ItemCount++;
    }

    
public object GET()
    
{
        
if (this._ItemCount == 0throw new InvalidOperationException(); }
        
object ret = this._Buffer[this._Head];
        
this._Buffer[this._Head] = null;
        
this._Head = (this._Head + 1% this._Buffer.Length;
        
this._ItemCount--;
        
return ret;
    }

}

技术讨论的QQ群: 2514097 或 10987609

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值