Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limit size of Queue<T> in .NET?

I have a Queue<T> object that I have initialised to a capacity of 2, but obviously that is just the capacity and it keeps expanding as I add items. Is there already an object that automatically dequeues an item when the limit is reached, or is the best solution to create my own inherited class?

like image 436
tags2k Avatar asked Aug 04 '08 14:08

tags2k


People also ask

What is queue C#?

Queue represents a first-in, first out collection of object. It is used when you need a first-in, first-out access of items. When you add an item in the list, it is called enqueue, and when you remove an item, it is called dequeue . This class comes under System.

What is queue in C# with example?

A Queue in C# represents a first-in, first-out (FIFO) collection of objects. An example of a queue is a line of people waiting. The Queue<T> class in the System. Collection. Generic namespace represents a queue in C#, where T specifies the type of elements in the queue.

Which of the following queue Constructors is used to create an instance of queue class which is empty and having the default initial capacity?

Queue(Int32): This constructor is used to create an instance of Queue class which is empty and having specified initial capacity, and uses the default growth factor.


2 Answers

I would recommend that you pull up the C5 Library. Unlike SCG (System.Collections.Generic), C5 is programmed to interface and designed to be subclassed. Most public methods are virtual and none of the classes are sealed. This way, you won't have to use that icky "new" keyword which wouldn't trigger if your LimitedQueue<T> were cast to a SCG.Queue<T>. With C5 and using close to the same code as you had before, you would derive from the CircularQueue<T>. The CircularQueue<T> actually implements both a stack and a queue, so you can get both options with a limit nearly for free. I've rewritten it below with some 3.5 constructs:

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();             }         }     } } 

I think that this code should do exactly what you were looking for.

like image 22
Marcus Griep Avatar answered Oct 02 '22 09:10

Marcus Griep


I've knocked up a basic version of what I'm looking for, it's not perfect but it'll do the job until something better comes along.

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);     } } 
like image 138
tags2k Avatar answered Oct 02 '22 08:10

tags2k