Question

I have an array of arrays and every time I have a new array made available, I want to add it to the back of the array. However, once the array of arrays reaches 30 elements, I want the oldest array to be discarded and the newest one to be added. I cant seem to figure this out. Below is an example of what I'm talking about in attempt to clarify.

int[30][] jagged;
int[] updatingDataFeed;

//updatingDataFeed is different at different times and updating the jagged array can 
//occur in an event handler that fires whenever the new data is ready

*This is within the event handler
jagged[0] = updatingDataFeed

I cant figure out what to do once you completely fill the 30 elements of jagged with updatingDataFeed arrays. When updatingDataFeed has a new element for the 31st time, I want jagged to look like:

jagged[0] = updatingDataFeed31
jagged[1] = updatingDataFeed2
jagged[2] = updatingDataFeed3
...
jagged[28] = updatingDataFeed29
jagged[29] = updatingDataFeed30
Was it helpful?

Solution 2

int nextIndex = 0;

int count = 0;


//When you make Initializations.

count = count+1;
if (count%30) = 0
   nextIndex = 0;

jagged[nextIndex++] = updatingDataFeed;

Since you are updating the values in order (0,1,2..) you always know that at the end of the 30 updates you need to go back to 0 index. So just use Module % operator to find if you have made 30 updates, if so you just reset the array index back to 0 which is the oldest else just increment the index.`

OTHER TIPS

Do you really need to use an Array? why cant you just use a Queueinside a class you define and expose the methods you want and keep the logic internally inside that class.

Something like this answer here maybe?

EDIT:

I didn't realise you were looking for a circular queue with a fixed size. I made an answer here where you can also test it by pressing run.

Here is the implementation i wrote for the answer in the link.

public class FixedSizedCircularQueue<T>
{
    private int _maxSize;
    private int _currentIndex;
    private T[] _q;

    public FixedSizedCircularQueue(int maxSize)
    {
        _maxSize = maxSize;
        _currentIndex = 0;
        _q = new T[_maxSize];
    }

    public void Enqueue(T obj)
    {
        ResetIndexIfMaxSizeExceeded();
        _q[_currentIndex] = obj;
        _currentIndex++;

        //for debug
        Console.WriteLine("index: " + _currentIndex + " object: " + obj);
    }

    private void ResetIndexIfMaxSizeExceeded()
    {
        if((_currentIndex % _maxSize) == 0) _currentIndex = 0;
    }
}

And here is how you would use it

var testQueue = new FixedSizedCircularQueue<int>(10);

for(var i = 0; i < 20; i++)
{
    testQueue.Enqueue(i + 1);
}

which would output this with the debug console.WriteLine() function

index: 1 object: 1
index: 2 object: 2
index: 3 object: 3
index: 4 object: 4
index: 5 object: 5
index: 6 object: 6
index: 7 object: 7
index: 8 object: 8
index: 9 object: 9
index: 10 object: 10
index: 1 object: 11
index: 2 object: 12
index: 3 object: 13
index: 4 object: 14
index: 5 object: 15
index: 6 object: 16
index: 7 object: 17
index: 8 object: 18
index: 9 object: 19
index: 10 object: 20

First of all, you need to keep track of the "next" array index to be filled. As you fill the array, increment that index, wrapping back around to 0.

Second, this is enough to take care of your other problem. If there was something else already in an index, your new value will overwrite it.

Try

int[30][] jagged;
int[] updatingDataFeed;
int nextIndex = 0;

private void UpdateDataFeed()
{
    jagged[nextIndex++] = updatingDataFeed;
    if (nextIndex == 30) nextIndex = 0;
}

You obviously have a way to keep track of which element you are on, so you can just keep incrementing that value and then use the modulus operator to always keep the value below 30. Here is an example of the modulus operator in use:

for (int i =0; i<100; i++)
{
    Console.WriteLine(i % 30);
}

You can maintain a variable to hold the index where you add the new available data feed. You go on incrementing this variable and once you cross 30, you reset it back to 0. This can be done easily with the modulus operator. So, if your mod is p and for any given range of integers, you can map them to the range {0..p-1} by taking modulus with p.

int[30][] jagged;
int[] updatingDataFeed;
int indexToAddNewFeedTo = 0;

void addNewDataFeed()
{
    jagged[indexToAddNewFeed] = updatingDataFeed;
    indexToaddNewFeedTo = (indexToAddNewFeedTo + 1)%30;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top