.NET の Queue<T> のサイズを制限しますか?
-
08-06-2019 - |
質問
容量 2 に初期化した Queue<T> オブジェクトがありますが、明らかにこれは単なる容量であり、項目を追加すると拡張し続けます。制限に達したときに項目を自動的にデキューするオブジェクトはすでに存在しますか、それとも独自の継承クラスを作成するのが最善の解決策ですか?
解決
探しているものの基本バージョンを見つけました。完璧ではありませんが、より良いものが登場するまでは機能します。
public class LimitedQueue<T> : Queue<T>
{
public int Limit { get; set; }
public LimitedQueue(int limit) : base(limit)
{
Limit = limit;
}
public new void Enqueue(T item)
{
while (Count >= Limit)
{
Dequeue();
}
base.Enqueue(item);
}
}
他のヒント
引き上げることをお勧めします C5ライブラリ. 。SCG (System.Collections.Generic) とは異なり、C5 はインターフェイス用にプログラムされており、サブクラス化されるように設計されています。ほとんどのパブリック メソッドは仮想メソッドであり、どのクラスもシールされていません。こうすることで、次の場合にトリガーされない不快な「新しい」キーワードを使用する必要がなくなります。 LimitedQueue<T>
にキャストされました SCG.Queue<T>
. 。C5 を使用し、以前とほぼ同じコードを使用すると、次のようになります。 CircularQueue<T>
. 。の CircularQueue<T>
実際にはスタックとキューの両方を実装しているため、制限付きの両方のオプションをほぼ無料で入手できます。いくつかの 3.5 構造を使用して以下に書き直しました。
using C5;
public class LimitedQueue<T> : CircularQueue<T>
{
public int Limit { get; set; }
public LimitedQueue(int limit) : base(limit)
{
this.Limit = limit;
}
public override void Push(T item)
{
CheckLimit(false);
base.Push(item);
}
public override void Enqueue(T item)
{
CheckLimit(true);
base.Enqueue(item);
}
protected virtual void CheckLimit(bool enqueue)
{
while (this.Count >= this.Limit)
{
if (enqueue)
{
this.Dequeue();
}
else
{
this.Pop();
}
}
}
}
このコードはまさにあなたが探していたものを実行するはずだと思います。
独自のクラスを作成する必要があります。おそらくリングバッファがニーズに合うでしょう。
容量を指定できる .NET のデータ構造 (配列を除く) は、これを使用して、内部データを保持するために使用される内部データ構造を構築します。
たとえば、リストの場合、容量は内部配列のサイズを設定するために使用されます。リストに要素を追加し始めると、この配列のインデックス 0 以降からの入力が開始され、容量に達すると、容量をさらに大きな容量に増やして埋め続けます。
なぜサイズ 2 の配列を使用しないのでしょうか?キューは動的に拡大および縮小できることになっています。
または、インスタンスの周囲にラッパー クラスを作成します。 Queue<T>
インスタンスをキューに入れるたびに、 <T>
オブジェクトの場合は、キューのサイズを確認してください。2 より大きい場合は、最初の項目をキューから取り出します。
このクラスがあなたのお役に立てれば幸いです。
内部的には、循環 FIFO バッファーは指定されたサイズの Queue<T> を使用します。バッファーのサイズに達すると、古い項目が新しい項目に置き換えられます。
注記:アイテムをランダムに削除することはできません。Remove(T item) メソッドが false を返すように設定しました。必要に応じて、アイテムをランダムに削除するように変更できます
public class CircularFIFO<T> : ICollection<T> , IDisposable
{
public Queue<T> CircularBuffer;
/// <summary>
/// The default initial capacity.
/// </summary>
private int capacity = 32;
/// <summary>
/// Gets the actual capacity of the FIFO.
/// </summary>
public int Capacity
{
get { return capacity; }
}
/// <summary>
/// Initialize a new instance of FIFO class that is empty and has the default initial capacity.
/// </summary>
public CircularFIFO()
{
CircularBuffer = new Queue<T>();
}
/// <summary>
/// Initialize a new instance of FIFO class that is empty and has the specified initial capacity.
/// </summary>
/// <param name="size"> Initial capacity of the FIFO. </param>
public CircularFIFO(int size)
{
capacity = size;
CircularBuffer = new Queue<T>(capacity);
}
/// <summary>
/// Adds an item to the end of the FIFO.
/// </summary>
/// <param name="item"> The item to add to the end of the FIFO. </param>
public void Add(T item)
{
if (this.Count >= this.Capacity)
Remove();
CircularBuffer.Enqueue(item);
}
/// <summary>
/// Adds array of items to the end of the FIFO.
/// </summary>
/// <param name="item"> The array of items to add to the end of the FIFO. </param>
public void Add(T[] item)
{
int enqueuedSize = 0;
int remainEnqueueSize = this.Capacity - this.Count;
for (; (enqueuedSize < item.Length && enqueuedSize < remainEnqueueSize); enqueuedSize++)
CircularBuffer.Enqueue(item[enqueuedSize]);
if ((item.Length - enqueuedSize) != 0)
{
Remove((item.Length - enqueuedSize));//remaining item size
for (; enqueuedSize < item.Length; enqueuedSize++)
CircularBuffer.Enqueue(item[enqueuedSize]);
}
}
/// <summary>
/// Removes and Returns an item from the FIFO.
/// </summary>
/// <returns> Item removed. </returns>
public T Remove()
{
T removedItem = CircularBuffer.Peek();
CircularBuffer.Dequeue();
return removedItem;
}
/// <summary>
/// Removes and Returns the array of items form the FIFO.
/// </summary>
/// <param name="size"> The size of item to be removed from the FIFO. </param>
/// <returns> Removed array of items </returns>
public T[] Remove(int size)
{
if (size > CircularBuffer.Count)
size = CircularBuffer.Count;
T[] removedItems = new T[size];
for (int i = 0; i < size; i++)
{
removedItems[i] = CircularBuffer.Peek();
CircularBuffer.Dequeue();
}
return removedItems;
}
/// <summary>
/// Returns the item at the beginning of the FIFO with out removing it.
/// </summary>
/// <returns> Item Peeked. </returns>
public T Peek()
{
return CircularBuffer.Peek();
}
/// <summary>
/// Returns the array of item at the beginning of the FIFO with out removing it.
/// </summary>
/// <param name="size"> The size of the array items. </param>
/// <returns> Array of peeked items. </returns>
public T[] Peek(int size)
{
T[] arrayItems = new T[CircularBuffer.Count];
CircularBuffer.CopyTo(arrayItems, 0);
if (size > CircularBuffer.Count)
size = CircularBuffer.Count;
T[] peekedItems = new T[size];
Array.Copy(arrayItems, 0, peekedItems, 0, size);
return peekedItems;
}
/// <summary>
/// Gets the actual number of items presented in the FIFO.
/// </summary>
public int Count
{
get
{
return CircularBuffer.Count;
}
}
/// <summary>
/// Removes all the contents of the FIFO.
/// </summary>
public void Clear()
{
CircularBuffer.Clear();
}
/// <summary>
/// Resets and Initialize the instance of FIFO class that is empty and has the default initial capacity.
/// </summary>
public void Reset()
{
Dispose();
CircularBuffer = new Queue<T>(capacity);
}
#region ICollection<T> Members
/// <summary>
/// Determines whether an element is in the FIFO.
/// </summary>
/// <param name="item"> The item to locate in the FIFO. </param>
/// <returns></returns>
public bool Contains(T item)
{
return CircularBuffer.Contains(item);
}
/// <summary>
/// Copies the FIFO elements to an existing one-dimensional array.
/// </summary>
/// <param name="array"> The one-dimensional array that have at list a size of the FIFO </param>
/// <param name="arrayIndex"></param>
public void CopyTo(T[] array, int arrayIndex)
{
if (array.Length >= CircularBuffer.Count)
CircularBuffer.CopyTo(array, 0);
}
public bool IsReadOnly
{
get { return false; }
}
public bool Remove(T item)
{
return false;
}
#endregion
#region IEnumerable<T> Members
public IEnumerator<T> GetEnumerator()
{
return CircularBuffer.GetEnumerator();
}
#endregion
#region IEnumerable Members
IEnumerator IEnumerable.GetEnumerator()
{
return CircularBuffer.GetEnumerator();
}
#endregion
#region IDisposable Members
/// <summary>
/// Releases all the resource used by the FIFO.
/// </summary>
public void Dispose()
{
CircularBuffer.Clear();
CircularBuffer = null;
GC.Collect();
}
#endregion
}
誰かの役に立てば、私は LimitedStack<T>
.
public class LimitedStack<T>
{
public readonly int Limit;
private readonly List<T> _stack;
public LimitedStack(int limit = 32)
{
Limit = limit;
_stack = new List<T>(limit);
}
public void Push(T item)
{
if (_stack.Count == Limit) _stack.RemoveAt(0);
_stack.Add(item);
}
public T Peek()
{
return _stack[_stack.Count - 1];
}
public void Pop()
{
_stack.RemoveAt(_stack.Count - 1);
}
public int Count
{
get { return _stack.Count; }
}
}
サイズが大きくなりすぎると、最も古いアイテム (スタックの一番下) が削除されます。
(この質問は、「C# スタック サイズの制限」に関する Google の検索結果のトップでした)
同時実行ソリューション
public class LimitedConcurrentQueue<ELEMENT> : ConcurrentQueue<ELEMENT>
{
public readonly int Limit;
public LimitedConcurrentQueue(int limit)
{
Limit = limit;
}
public new void Enqueue(ELEMENT element)
{
base.Enqueue(element);
if (Count > Limit)
{
TryDequeue(out ELEMENT discard);
}
}
}
注記:以来 Enqueue
要素の追加を制御し、一度に 1 つずつ実行するため、 while
のために TryDequeue
.