- Home /
How to build fixed size queue with C#?
Hi,
Is there a straightforward way to build a queue of fixed size: when adding excessive elements they pop elements that were added first - much like queue works.
I tried to use C# Queue but looks like Enqueue method extends set size.
Thank you.
You could write an EnqueueWithFixedSize extension method for the existing Queue ins$$anonymous$$d of writing a whole new Generic
http://msdn.microsoft.com/en-us/library/bb383977.aspx
Just curious, what happens to the popped elements in the kind of queue you are referring to?
There won't be a reference to them in the memory any longer and will be removed by garbage collector. But it is an assumption based on nothing mostly. :)
Or you think it can lead to memory leak?
It shouldn't leak memory, if there's no reference to them the GC should clean them up.
Here is a custom class i wrote for limiting the number of values that queue can have. It only has one constructor to force the user to declare a limit on initialization.
using System.Collections.Generic;
public class LimitedQueue<T> {
public int Limit {get; private set;}
public int Count {get { return queue.Count;}}
private Queue<T> queue;
public LimitedQueue(int limit){
queue = new Queue<T>();
Limit = limit;
}
public void Enqueue(T item){
if (queue.Count == Limit)
return;
queue.Enqueue(item);
}
public void Dequeue() {
if (queue.Count == 0)
return;
queue.Dequeue();
}
public bool Contains(T item) {
return queue.Contains(item);
}
public void CopyTo(T[] array, int index) {
queue.CopyTo(array, index);
}
public Queue<T>.Enumerator GetEnumerator() {
return queue.GetEnumerator();
}
public void Clear() {
queue.Clear();
}
}
If necessary you may also implement other queue methods as well.
Answer by sam2k13 · Jan 09, 2015 at 10:11 PM
The simplest way to do this may be by dequeueing the object at the front after a specified size is reached. This will maintain a queue of any size.
int counter = 0;
int size = 10;//size of queue
if(counter < size){
//Enqueue
counter += 1
}
else{
//Enqueue
//Dequeue
}
Answer by tomachinz · Mar 04, 2019 at 12:18 PM
Here's my one:
void HandleLog(string logString, string stackTrace, LogType type)
{
myLog = logString;
string newString = "\n [" + type + "] : " + myLog;
myLogQueue.Enqueue(newString);
count++;
if (count > logLength)
{
myLogQueue.Dequeue();
}
// beef it up
if (type == LogType.Exception)
{
newString = "\n" + stackTrace;
myLogQueue.Enqueue(newString);
}
// print it
myLog = string.Empty;
foreach (string mylog in myLogQueue)
{
myLog += mylog;
}
}
Your code is a bit confusing and doesn't ensure the queue doesn't exceed the limit if exceptions are thrown. You always enqueue the log string in line 5. If the count exceeds your set limit you dequeue the oldest. However if the type is exception you again add another string (the stacktrace). That means if many exceptions are thrown the queue is getting longer and longer since you're adding two entries while only removing one each time.
Also your foreach loop to build the final string is extremely inefficient and produces tons of garbage.
It's also strange that you use your own count variable which you only count up but it never goes down again.
This would make much more sense:
StringBuilder sb = new StringBuilder();
void HandleLog(string logString, string stackTrace, LogType type)
{
string newString = "\n [" + type + "] : " + myLog;
if (type == LogType.Exception)
{
newString = "\n" + stackTrace;
}
myLogQueue.Enqueue(newString);
while (myLogQueue.Count > logLength)
{
myLogQueue.Dequeue();
}
sb.Length = 0;
foreach (string line in myLogQueue)
{
sb.Append(line);
}
myLog = sb.ToString();
}
Your answer
Follow this Question
Related Questions
Rendering Order 4 Answers
moving an object trough a queue of points 0 Answers
C# - Is it possible to sync a Queue to a List, or is there a better option? 1 Answer
Priority Queue Code Problems 1 Answer