Files
2026-03-04 09:37:33 +08:00

172 lines
3.2 KiB
C#

using System;
using UnityEngine;
namespace DebuggingEssentials
{
public class FastQueue<T> : FastListBase
{
protected int head;
protected int tail;
protected T[] items;
public FastQueue(int capacity)
{
if (capacity < 1)
{
capacity = 1;
}
items = new T[capacity];
}
protected void DoubleCapacity()
{
int num = items.Length;
T[] destinationArray = new T[num * 2];
if (_count > 0)
{
if (head < tail)
{
Array.Copy(items, head, destinationArray, 0, _count);
}
else
{
Array.Copy(items, head, destinationArray, 0, num - head);
Array.Copy(items, 0, destinationArray, num - head, tail);
}
}
items = destinationArray;
head = 0;
tail = _count;
}
public int ArrayLength()
{
return items.Length;
}
public virtual void Clear()
{
if (head < tail)
{
Array.Clear(items, head, _count);
}
else
{
Array.Clear(items, head, items.Length - head);
Array.Clear(items, 0, tail);
}
tail = (head = 0);
Count = (_count = 0);
}
public virtual void FastClear()
{
tail = (head = 0);
Count = (_count = 0);
}
public virtual void Enqueue(T item)
{
if (_count == items.Length)
{
DoubleCapacity();
}
items[tail] = item;
tail = (tail + 1) % items.Length;
Count = ++_count;
}
public void EnqueueRange(T[] items, int startIndex = 0, int count = -1)
{
if (count == -1)
{
count = items.Length;
}
int num = startIndex + count;
for (int i = startIndex; i < num; i++)
{
Enqueue(items[i]);
}
}
public T Peek(int index)
{
if (index >= _count)
{
Debug.LogError("index " + index + " is bigger than Count " + _count);
return default(T);
}
return items[(head + index) % items.Length];
}
public virtual T Dequeue()
{
if (_count == 0)
{
Debug.LogError("FastQueue is empty!");
return default(T);
}
T result = items[head];
items[head] = default(T);
head = (head + 1) % items.Length;
Count = --_count;
return result;
}
public virtual void Dequeue(T item)
{
if (_count == 0)
{
Debug.LogError("FastQueue is empty!");
return;
}
int num = -1;
int i;
for (i = 0; i < _count; i++)
{
num = (i + head) % items.Length;
if (items[num].Equals(item))
{
break;
}
}
if (i == _count)
{
Debug.LogError("Item not found in FastQueue");
return;
}
Count = --_count;
for (int num2 = i; num2 >= 1; num2--)
{
num = (num2 + head) % items.Length;
int num3 = (num2 + (head - 1)) % items.Length;
items[num] = items[num3];
}
items[head] = default(T);
head = (head + 1) % items.Length;
}
public void Report()
{
string text = "";
for (int i = 0; i < _count; i++)
{
int num = (i + head) % items.Length;
text = text + items[num].ToString() + ", ";
}
string text2 = "";
for (int j = 0; j < items.Length; j++)
{
text2 = text2 + items[j].ToString() + ", ";
}
Debug.Log("==============================================");
Debug.Log(text);
Debug.Log(text2);
Debug.Log("Tail " + tail + " Head " + head + " Count " + Count + "(" + _count + ")");
Debug.Log("==============================================");
}
}
}