Skip to content

Instantly share code, notes, and snippets.

Created August 21, 2010 22:37
Show Gist options
  • Save anonymous/542959 to your computer and use it in GitHub Desktop.
Save anonymous/542959 to your computer and use it in GitHub Desktop.
function FoldArrayRev (chunkSize) {
this._chunkSize = chunkSize || 1000;
this._data = [[]];
this._tail = 0;
}
FoldArrayRev.prototype = {
push: function (item) {
if (this._data[this._tail].length >= this._chunkSize) {
this._data[this._tail] = this._data[this._tail].reverse();
this._data.push([]);
this._tail++;
}
this._data[this._tail].push(item);
}
, shift: function () {
var retVal = (this._data.length > 1) ? this._data[0].pop() : this._data[0].shift();
if (this._data[0].length < 1) {
this._data.shift();
this._tail--;
}
return(retVal);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment