Created
May 12, 2013 19:34
-
-
Save fernandojsg/5564624 to your computer and use it in GitHub Desktop.
JS: RingBuffer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var createRingBuffer = function(length){ | |
var start = 0, buffer = []; | |
for (var i=0; i<length; i++) | |
buffer[i] = -10; | |
return { | |
get : function(key){return buffer[(start+key) % length];}, | |
push : function(item){ | |
buffer[start] = item; // insert value at start | |
start = (length + start +1) % length; // advance start | |
} | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment