x


Circular Buffer Js

I'm attempting to create a BuiltIn Array wrapper in order to make a Circular Buffer, I basically want a class that acts just like an array... although when you Add it checks the maximum length and Shifts the first item if the length is exceeded.

What would be the best approach to this?

more ▼

asked Jun 14 '12 at 08:05 AM

Caiuse gravatar image

Caiuse
787 83 102 121

this is probably more a general programming question than Unity specific.

if you used say StackOverflow you'd get heaps of idiots offering your general pseudocode within a few minutes - maybe that is better for you?

Jun 14 '12 at 03:30 PM Fattie

Okay will do, might post me answer from there :D Cheers

Jun 14 '12 at 04:25 PM Caiuse
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

It's a slow day, thought I'd write you one. You can use it from JS but I wrote it in C# as I wanted to create a generic version. Hence you should put this in a CS file and place it in a Plugins folder to access it from JS.

public class CircularBuffer<T> : List<T>
{
    public int Capacity = 1;

    public CircularBuffer(int capacity)
    {
       Capacity = capacity;
    }

    public CircularBuffer()
    {
    }

    public void Add (T item)
    {
       base.Add(item);
       if(Count > Capacity)
         RemoveAt(0);

    }
    public void Insert (int index, T item)
    {
       base.Insert(index,item);
       if(Count > Capacity)
         RemoveAt(0);
    }
}

To use in Javascript:

    var buffer = new CircularBuffer.<object>(10);  //Use your base class
    buffer.Add("2");

If you choose to use insert, be aware that inserting at 0 will cause it to be immediately deleted if the list is at capacity.

more ▼

answered Jun 14 '12 at 04:49 PM

whydoidoit gravatar image

whydoidoit
33k 11 23 98

That's brilliant, haven't had a chance to test it but it's looks great. Might translate it into js for the benefit of others! Thanks for putting the time aside to answers!

Jun 15 '12 at 12:41 AM Caiuse

This is not really a circular buffer ;) It's more like a mixture of a list and a queue :D Also two more points:

  • List has already a Capacity property which will be hidden by your variable.
  • List also has a special constructor that takes the initial capacity. I think it would make sense to use this one to create the internal array with the max capacity.
  • Since the List functions aren't virtual it doesn't make much sense to inherit your class from List. This can even cause more problems when you assign it to a List variable (which is possible since it's the baseclass). I would use a composition here. So your class would have a List as member and wrap the functions.

As a quick an dirty solution this works of course ;)

Final note: Depending on the usage, Circular buffers usually have a read and write pointer. Especially to detect buffer-overruns.

I guess the OP doesn't really want a circular buffer, since it's usually just a queue without random access.

@caius-1: It would be great if you could go a bit more in detail for what purpose you need the buffer.

Jun 15 '12 at 01:35 AM Bunny83

You are quite right of course :) It was a slow day, but not that slow...

Actually as I hacked it out I typed override into MonoDevelop and it gave me Add and Insert - it wasn't until I'd actually posted it that I realised it had just happily dropped the override as they weren't virtual.

Jun 15 '12 at 01:37 AM whydoidoit
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1358
x1

asked: Jun 14 '12 at 08:05 AM

Seen: 402 times

Last Updated: Jun 15 '12 at 01:41 AM