Created
March 6, 2015 17:09
-
-
Save monolithed/4bfc7853498e41f9be90 to your computer and use it in GitHub Desktop.
This file contains 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
'use strict'; | |
/** | |
* @class | |
* @param {number} size | |
*/ | |
export default class FixedList { | |
constructor (size) { | |
this.__list = []; | |
this.resize(size); | |
} | |
/** | |
* Checks if the underlying container has no elements | |
* | |
* @return boolean | |
*/ | |
empty () { | |
return this.size() > 0; | |
} | |
/** | |
* Clears the queue | |
*/ | |
clear () { | |
this.__list.length = 0; | |
} | |
/** | |
* Returns a size the queue | |
* | |
* @return boolean | |
*/ | |
size () { | |
return this.__list.length; | |
} | |
/** | |
* Specifies a size the queue | |
* | |
* @param {number} size | |
*/ | |
resize (size) { | |
this.__length = size | 0; | |
} | |
/** | |
* Adds new element to the end of the queue | |
* | |
* @param {*} item | |
* @return item | |
*/ | |
push (item) { | |
this.__list.push(item); | |
if (this.size() > this.__length) { | |
this.__list.shift(); | |
} | |
return item; | |
} | |
/** | |
* Remove the element | |
* | |
* @param {number} index | |
* @return boolean | |
*/ | |
remove (index) { | |
this.__list.splice(index, 1); | |
} | |
/** | |
* Iterates over the list elements | |
*/ | |
iterator () { | |
var list = this.__list; | |
return { | |
[Symbol.iterator] () { | |
return this; | |
}, | |
index: -1, | |
next () { | |
if (++this.index < list.length) { | |
return { | |
value: list[this.index], | |
done : false | |
}; | |
} | |
else { | |
return { | |
done: true | |
}; | |
} | |
} | |
} | |
} | |
toString () { | |
return '[object Fixedlist]'; | |
} | |
valueOf () { | |
return this.__list; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment