Feed Data Structure

Monkey Forums/Monkey Code/Feed Data Structure

Shinkiro1(Posted 2016) [#1]
I didn't know what else to call this. You basically push items to it and it and the newest one will be on top. It has a fixed size though so everything that's pushed out will be lost.

Strict

Class Feed<T>

    Method New(size:Int)
        data = New T[size]
    End

    Method Push:Void(val:T)
        For Local i:Int = count To 1 Step -1
            data[i] = data[i-1]
        Next
        data[0] = val
        count += 1
        count = Min(count, data.Length-1)
    End

    Method Get:T(i:Int)
        Return data[i]
    End

    Method Length:Int() Property
        Return count+1
    End

    Private
    Field data:T[]
    Field count:Int
End


I use it for a debug overlay where I constantly push variables that change over time (position, etc.).
For drawing just loop through it like you would with a monkey stack.


diemar(Posted 2016) [#2]
Probably a FIFO Stack with a business end ^^ First in first out, kind of. Thx for sharing.

For speed optimation you could instead cicle the list top index instead, like: write latest var to index n, then read the list beginning at index n down to index zero, continuing with index max down to n+1. then inc n...


Shinkiro1(Posted 2016) [#3]
Yes, it's FIFO style with a limit.
This optimization would only take effect if the feed is not full right? Once it hits maxSize it would be the same.