Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by nTu4Ka · Jan 09, 2015 at 09:24 PM · queue

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.

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DanSuperGP · Jan 09, 2015 at 09:39 PM 0
Share

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?

avatar image nTu4Ka · Jan 10, 2015 at 09:57 AM 0
Share

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?

avatar image DanSuperGP · Jan 12, 2015 at 08:51 PM 0
Share

It shouldn't leak memory, if there's no reference to them the GC should clean them up.

avatar image mchts · Mar 06, 2019 at 10:42 AM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
2

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
 
 }







Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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;
         }
     }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Mar 04, 2019 at 12:35 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

30 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Queuing functions then releasing them on command 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges