C# - Generic Circular Queue Implementation

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
Apoc
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 7
Joined: Fri Jul 31, 2009 5:52 am

C# - Generic Circular Queue Implementation

Post by Apoc »

Yep, I know. This one is very, -very-, simple for anybody who knows their C#. However, not many developers actually realize how much simple code can help. I've used this code in numerous projects, mostly in places where I didn't think it would be that useful, but happily found out I was wrong.

Anyhow, enough babbling.

Code: Select all

using System.Collections.Generic;

namespace Onyx.Utilities.Collections
{
    /// <summary>
    /// A generic circular queue implementation. If you don't know what it is, or what it's useful for.
    /// Look here: www.google.com
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class CircularQueue<T> : Queue<T>
    {
        /// <summary>
        /// Dequeues the next item in the Queue and automatically pushes it to the back.
        /// This method hides the Queue<T>.Dequeue method on purpose!
        /// </summary>
        /// <returns>An object of type T.</returns>
        public new T Dequeue()
        {
            // For whatever reason, MS decided to not let their collections classes have virtual
            // functions that we can override easily. However, thankfully, they provided us with the
            // various uses of the 'new' keyword! Yay! Or is that fail? Oh well...
            T tmp = base.Dequeue();
            Enqueue(tmp);
            return tmp;
        }

        /// <summary>
        /// Cycles to the next occurance of the specified item. If no occurances are found,
        /// this method does nothing but waste CPU cycles!
        /// </summary>
        /// <param name="item">The item to set the Queue's current position to.</param>
        public void CycleTo(T item)
        {
            // Using a for loop here to avoid and endless cycle if no match is found!
            T tmp = Peek();
            for (int i = 0; i < Count; i++)
            {
                // If the 'item' object implements a custom Equals method, it will be used.
                // Otherwise, this pretty much just checks for reference equality.
                // Hint: IMPLEMENT PROPER EQUALITY OPERATORS!
                if (!tmp.Equals(item))
                {
                    // Toss it to the back.
                    Dequeue();
                    // Check the next one.
                    tmp = Peek();
                }
                else
                {
                    // We've hit our item. Stop here!
                    return;
                }
            }
            // No item was found, so make sure we end up where we started from.
            Dequeue();
        }
    }
}
Comments speak for themselves. I mostly use this for breadcrumb-based navigation systems. (Feed it a list of points to walk, and it'll constantly cycle through that list.)
Post Reply