Click here to Skip to main content
Licence CPOL
First Posted 10 Dec 2008
Views 21,942
Downloads 174
Bookmarked 24 times

A Generic Circular Array

By | 10 Dec 2008 | Article
An efficient circular array - fixed length first in, last out.

Introduction

CircularArray<T> is a class that implements a fixed length first in last out 'queue' or buffer. This is useful, for example, if you want to keep, say, the last 30 values in a real-time system. The next value gets put into the array, and then the last value in the array gets pushed out. It does this by using a fixed array, and uses the DivRem math operator to calculate the position of the head and the tail in the array. It's like the array is circular, with a pointer moving around the circle. The only downside is I had to use an internal facade array to get an array in the sequence I wanted, copying out the values; obviously, this isn't great. I can't think of anything else unless I decide to implement something that appears as an array but is actually a linked list or something.

Background

I needed to keep the last X values in a real-time stock tracking application, efficiently, to work out various things. I couldn't find anything like this at all in Generics (I might be wrong. If I am, can someone please tell me about the built-in class available for this?).

Using the code

The constructor specifies the number of elements to hold. Here, I am holding 'Bar' objects:

CircularArray<Bar> _circularArray = new CircularArray<Bar>(periods);

Push a value onto the array using the Push method:

_circularArray.Push(value);

The Get method gets the element X from the head, e.g., Get(0) gets the last value you pushed on, Get(1) gets the second last value you pushed on etc.

_circularArray.Get(i)

The Array method returns an array sequenced from tail to head, e.g., from the oldest value to the newest values that were pushed on to the queue. One use of this may be to use this as input into a TA-LIB technical analysis library, or anything where you will need an array of the last X values in a real-time system. Here is a snippet of the Unit Test of Array.

[Test]

public void Push()
{

    CircularArray<int> cq = new CircularArray<int>(5);

    cq.Push(1);
    cq.Push(2);
    cq.Push(3);
    cq.Push(4);
    cq.Push(5);
    cq.Push(6);
    cq.Push(7);
    cq.Push(8);
    cq.Push(9);
    cq.Push(10);
    cq.Push(11);

    Assert.AreEqual(7, cq.Array[0]);
    Assert.AreEqual(8, cq.Array[1]);
    Assert.AreEqual(9, cq.Array[2]);
    Assert.AreEqual(10, cq.Array[3]);
    Assert.AreEqual(11, cq.Array[4]);

}

I've included the code and Unit Tests in the solution Zip file; everything is there.

Points of interest

As mentioned, I couldn't find anything like this already available. I've probably missed out on some sort of built-in generic Microsoft class. If anyone knows about such a thing, please let me know!

Update: Note, after I posted this, I did read Marc Clifton's article 'A Circular List'. After plugging it into an NUnit test equivalent to the one above:

[Test]
public void CicularList()
{

    CircularList<int> cq = new CircularList<int>(5);

    cq.Value = 1;
    cq.Next();

    cq.Value = 2;
    cq.Next();

    cq.Value = 3;
    cq.Next();

    cq.Value = 4;
    cq.Next();

    cq.Value = 5;
    cq.Next();

    cq.Value = 6;
    cq.Next();

    cq.Value = 7;
    cq.Next();

    cq.Value = 8;
    cq.Next();

    cq.Value = 9;
    cq.Next();

    cq.Value = 10;
    cq.Next();

    cq.Value = 11;
    cq.Next();

    Assert.AreEqual(7, cq[0]); // Failed its 11
    Assert.AreEqual(8, cq[1]); // Failed its 7
    Assert.AreEqual(9, cq[2]); // Failed its 8
    Assert.AreEqual(10, cq[3]); // Failed its 9
    Assert.AreEqual(11, cq[4]); // Failed its 10

}

The array cq would be 11,7,8,9,10, which is the raw circular array. I was expecting 7,8,9,10,11 - e.g., the last 5 values, oldest to newest. Maybe, I'm using it wrong. 'Failed' above is just against what I expected, so don't read into it. I'm sure CircularList is useful for some, just not for my purposes. I totally respect and value Marc Clifton's contributions.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Jean-marc Lai

Web Developer

United Kingdom United Kingdom

Member

Jean-marc is a IT consultant specializing on the microsoft platform. Jean-marc lives in London, UK.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generalcircular buffers with contiguous storage of values Pinmembersuralis23:40 15 Dec '08  
GeneralRe: circular buffers with contiguous storage of values PinmemberJean-marc Lai10:50 17 Dec '08  
GeneralThoughts PinmemberPIEBALDconsult9:29 10 Dec '08  
GeneralRe: Thoughts PinmemberJean-marc Lai23:02 10 Dec '08  
GeneralRe: Thoughts [modified] PinmemberPIEBALDconsult8:44 11 Dec '08  
GeneralRe: Thoughts PinmemberJean-marc Lai15:20 12 Dec '08  
GeneralRe: Thoughts PinmemberPIEBALDconsult18:11 12 Dec '08  
General_facadeArray problems PinmvpLuc Pattyn4:37 10 Dec '08  
GeneralRe: _facadeArray problems PinmemberJean-marc Lai5:12 10 Dec '08  
GeneralRe: _facadeArray problems PinmvpLuc Pattyn5:51 10 Dec '08  
GeneralRe: _facadeArray problems PinmemberJean-marc Lai23:06 10 Dec '08  
GeneralRe: _facadeArray problems PinmvpLuc Pattyn0:16 11 Dec '08  
GeneralRe: _facadeArray problems PinmemberJean-marc Lai0:35 11 Dec '08  
GeneralRe: _facadeArray problems PinmvpLuc Pattyn1:36 11 Dec '08  
GeneralGood Article PinmemberDonsw4:31 10 Dec '08  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120517.1 | Last Updated 10 Dec 2008
Article Copyright 2008 by Jean-marc Lai
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid