Exception in Put()

Feb 3, 2011 at 11:48 PM

OK, maybe I'm not using CircularBuffer the right way. I want to fill the buffer, then take out an element from the front and add an element to the back. My test code below fills the buffer with 3,4,5,6,7. I then Get() an element and try to put in another element with Put(). When the code hits Put(), I get an exception in Put() because the "tail" index is too large. The exception happens on Put(11).

Any suggestions on how to get the behavior I want? I tried AllowOverflow both true and false.

 

 

static void Main(string[] args)
        {
            CircularBuffer<int> indexCircularBuffer = new CircularBuffer<int>(5,true);

            for (int i = 0; i < indexCircularBuffer.Capacity; i++)
            {
                indexCircularBuffer.Put(i + 3);
            }

            int index = indexCircularBuffer.Get();

            indexCircularBuffer.Put(11);

            index = indexCircularBuffer.Get();
        }

 
Jun 17, 2011 at 2:59 PM

Have a look under the "Issue Tracker" tab. I attached a fixed CircularBuffer.cs for this problem.