[corefx註釋說]-System.Collections.Generic.Queue
為了節省時間,只寫一些關注的方法好了,剩下的可以MSDN嘛XD
首先是宣告部分,表示為佇列是一個可用於列舉的只讀集合
[DebuggerTypeProxy(typeof(QueueDebugView<>))]
[DebuggerDisplay("Count = {Count}")]
public class Queue<T> : IEnumerable<T>,
System.Collections.ICollection,
IReadOnlyCollection<T>
欄位
private T[] _array;
private int _head; // First valid element in the queue
private int _tail; // Last valid element in the queue
private int _size; // Number of elements.
private int _version;
private Object _syncRoot;
private const int MinimumGrow = 4 ;
private const int GrowFactor = 200; // double each time
private const int DefaultCapacity = 4;
這裡有“遞增因子”的概念存在,因此可以從中學習到對於佇列這樣的資料結構,通過使用陣列,如何在“滿隊”情況下擴充空間。
構造方法
// Creates a queue with room for capacity objects. The default initial
// capacity and grow factor are used.
/// <include file='doc\Queue.uex' path='docs/doc[@for="Queue.Queue"]/*' />
public Queue()
{
_array = Array.Empty<T>();
}
// Creates a queue with room for capacity objects. The default grow factor
// is used.
//
/// <include file='doc\Queue.uex' path='docs/doc[@for="Queue.Queue1"]/*' />
public Queue(int capacity)
{
if (capacity < 0)
throw new ArgumentOutOfRangeException("capacity", SR.ArgumentOutOfRange_NeedNonNegNumRequired);
_array = new T[capacity];
}
// Fills a Queue with the elements of an ICollection. Uses the enumerator
// to get each of the elements.
//
/// <include file='doc\Queue.uex' path='docs/doc[@for="Queue.Queue3"]/*' />
public Queue(IEnumerable<T> collection)
{
if (collection == null)
throw new ArgumentNullException("collection");
_array = new T[DefaultCapacity];
using (IEnumerator<T> en = collection.GetEnumerator())
{
while (en.MoveNext())
{
Enqueue(en.Current);
}
}
}
提供三種構造方法:
- 構造空佇列
- 構造指定空間容量的佇列
- 複製已有佇列(特別注意這個,很有意思)
屬性
/// <include file='doc\Queue.uex' path='docs/doc[@for="Queue.Count"]/*' />
public int Count
{
get { return _size; }
}
/// <include file='doc\Queue.uex' path='docs/doc[@for="Queue.IsSynchronized"]/*' />
bool System.Collections.ICollection.IsSynchronized
{
get { return false; }
}
Object System.Collections.ICollection.SyncRoot
{
get
{
if (_syncRoot == null)
{
System.Threading.Interlocked.CompareExchange<Object>(ref _syncRoot, new Object(), null);
}
return _syncRoot;
}
}
這裡沒什麼好說的,第一個是返回當前佇列內的元素數,第二個是返回同步物件。
方法
void Clear() //清除佇列內所有元素
// Removes all Objects from the queue.
/// <include file='doc\Queue.uex' path='docs/doc[@for="Queue.Clear"]/*' />
public void Clear()
{
if (_head < _tail)
Array.Clear(_array, _head, _size);
else
{
Array.Clear(_array, _head, _array.Length - _head);
Array.Clear(_array, 0, _tail);
}
_head = 0;
_tail = 0;
_size = 0;
_version++;
}
這裡可以看出來,這裡的佇列使用的是“迴圈佇列”的概念(_tail入 _head 出)。這裡需要再次明確的是:_size是指的元素個數而非真實的陣列長度(Array.Length)
void CopyTo(T[], int) / void Copy(Array, int) 複製到某一陣列中
// CopyTo copies a collection into an Array, starting at a particular
// index into the array.
//
/// <include file='doc\Queue.uex' path='docs/doc[@for="Queue.CopyTo"]/*' />
public void CopyTo(T[] array, int arrayIndex)
{
if (array == null)
{
throw new ArgumentNullException("array");
}
if (arrayIndex < 0 || arrayIndex > array.Length)
{
throw new ArgumentOutOfRangeException("arrayIndex", SR.ArgumentOutOfRange_Index);
}
int arrayLen = array.Length;
if (arrayLen - arrayIndex < _size)
{
throw new ArgumentException(SR.Argument_InvalidOffLen);
}
int numToCopy = (arrayLen - arrayIndex < _size) ? (arrayLen - arrayIndex) : _size;
if (numToCopy == 0) return;
int firstPart = (_array.Length - _head < numToCopy) ? _array.Length - _head : numToCopy;
Array.Copy(_array, _head, array, arrayIndex, firstPart);
numToCopy -= firstPart;
if (numToCopy > 0)
{
Array.Copy(_array, 0, array, arrayIndex + _array.Length - _head, numToCopy);
}
}
void System.Collections.ICollection.CopyTo(Array array, int index)
{
if (array == null)
{
throw new ArgumentNullException("array");
}
if (array.Rank != 1)
{
throw new ArgumentException(SR.Arg_RankMultiDimNotSupported);
}
if (array.GetLowerBound(0) != 0)
{
throw new ArgumentException(SR.Arg_NonZeroLowerBound);
}
int arrayLen = array.Length;
if (index < 0 || index > arrayLen)
{
throw new ArgumentOutOfRangeException("index", SR.ArgumentOutOfRange_Index);
}
if (arrayLen - index < _size)
{
throw new ArgumentException(SR.Argument_InvalidOffLen);
}
int numToCopy = (arrayLen - index < _size) ? arrayLen - index : _size;
if (numToCopy == 0) return;
try
{
int firstPart = (_array.Length - _head < numToCopy) ? _array.Length - _head : numToCopy;
Array.Copy(_array, _head, array, index, firstPart);
numToCopy -= firstPart;
if (numToCopy > 0)
{
Array.Copy(_array, 0, array, index + _array.Length - _head, numToCopy);
}
}
catch (ArrayTypeMismatchException)
{
throw new ArgumentException(SR.Argument_InvalidArrayType);
}
複製到從某一索引起的指定的一個數組中。當然中間做了一些容量是否足夠等等的判斷
void Enqueue(T) 入佇列
// Adds item to the tail of the queue.
//
/// <include file='doc\Queue.uex' path='docs/doc[@for="Queue.Enqueue"]/*' />
public void Enqueue(T item)
{
if (_size == _array.Length)
{
int newcapacity = (int)((long)_array.Length * (long)GrowFactor / 100);
if (newcapacity < _array.Length + MinimumGrow)
{
newcapacity = _array.Length + MinimumGrow;
}
SetCapacity(newcapacity);
}
_array[_tail] = item;
_tail = (_tail + 1) % _array.Length;
_size++;
_version++;
}
這裡看一下“生長因子”的作用:就是個生長比例。。程式碼裡跟Stack一樣是兩倍這樣子往上加。這個強型別轉換弄得整個人濛濛噠。
這裡有個SetCapacity這個方法,這個方法的看點是如何解決從“環形佇列”裡抽取掉沒用的陣列單元。
T Dequeue() //出佇列
public T Dequeue()
{
if (_size == 0)
throw new InvalidOperationException(SR.InvalidOperation_EmptyQueue);
T removed = _array[_head];
_array[_head] = default(T);
_head = (_head + 1) % _array.Length;
_size--;
_version++;
return removed;
}
這裡很奇怪,退出佇列時並沒有檢查是否空間過分冗餘而作空間節省上的優化(實用階段這一塊不做是明智的嗎?看來空間不值錢是趨勢)
T Peek() //隊尾元素
public T Peek()
{
if (_size == 0)
throw new InvalidOperationException(SR.InvalidOperation_EmptyQueue);
return _array[_head];
}
bool Contains(T) 集合中是否存在某元素
public bool Contains(T item)
{
int index = _head;
int count = _size;
EqualityComparer<T> c = EqualityComparer<T>.Default;
while (count-- > 0)
{
if (((Object)item) == null)
{
if (((Object)_array[index]) == null)
return true;
}
else if (_array[index] != null && c.Equals(_array[index], item))
{
return true;
}
index = (index + 1) % _array.Length;
}
return false;
}
void SetCapacity(int) 設定佇列容量
// PRIVATE Grows or shrinks the buffer to hold capacity objects. Capacity
// must be >= _size.
private void SetCapacity(int capacity)
{
T[] newarray = new T[capacity];
if (_size > 0)
{
if (_head < _tail)
{
Array.Copy(_array, _head, newarray, 0, _size);
}
else
{
Array.Copy(_array, _head, newarray, 0, _array.Length - _head);
Array.Copy(_array, 0, newarray, _array.Length - _head, _tail);
}
}
_array = newarray;
_head = 0;
_tail = (_size == capacity) ? 0 : _size;
_version++;
}
方法是這樣的:把原來在_array中的所有元素,規規矩矩排好順序(因為是迴圈佇列,可能下標和隊內順序不是偏序嘛)然後再把_array 指向 newarray
但是這裡並沒有判斷capacity和size的關係!(其實Copy會報異常的)
這裡特意興沖沖的跑去VS13實驗一下:會引發目標長度不夠。
異常發生點:
在 System.Array.Copy(Array sourceArray, Int32 sourceIndex, Array destinationArray, Int32 destinationIndex, Int32 length, Boolean reliable)
在 System.Collections.Generic.Queue`1.SetCapacity(Int32 capacity)
(看來還真的是還沒開完,或者說是有新改進)
測試程式碼:
Queue<Int32> que = new Queue<int>(1024);
for (int i = 0; i < 1023; i++)
{
que.Enqueue(i);
}
var dd = que.GetType().GetMethod("SetCapacity", BindingFlags.NonPublic | BindingFlags.Instance);
dd.Invoke(que, new Object[] { 100 });
Console.ReadKey();