• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1var ensureTwoPower = function(n) {
2	if (n && !(n & (n - 1))) return n;
3	var p = 1;
4	while (p < n) p <<= 1;
5	return p;
6};
7
8var Cyclist = function(size) {
9	if (!(this instanceof Cyclist)) return new Cyclist(size);
10	size = ensureTwoPower(size);
11	this.mask = size-1;
12	this.size = size;
13	this.values = new Array(size);
14};
15
16Cyclist.prototype.put = function(index, val) {
17	var pos = index & this.mask;
18	this.values[pos] = val;
19	return pos;
20};
21
22Cyclist.prototype.get = function(index) {
23	return this.values[index & this.mask];
24};
25
26Cyclist.prototype.del = function(index) {
27	var pos = index & this.mask;
28	var val = this.values[pos];
29	this.values[pos] = undefined;
30	return val;
31};
32
33module.exports = Cyclist;