index.js 700 B

1234567891011121314151617181920212223242526272829303132333435
  1. /* eslint-disable no-var */
  2. function Cyclist (size) {
  3. if (!(this instanceof Cyclist)) return new Cyclist(size)
  4. size = twoify(size)
  5. this.mask = size - 1
  6. this.size = size
  7. this.values = new Array(size)
  8. }
  9. Cyclist.prototype.put = function (index, val) {
  10. var pos = index & this.mask
  11. this.values[pos] = val
  12. return pos
  13. }
  14. Cyclist.prototype.get = function (index) {
  15. return this.values[index & this.mask]
  16. }
  17. Cyclist.prototype.del = function (index) {
  18. var pos = index & this.mask
  19. var val = this.values[pos]
  20. this.values[pos] = undefined
  21. return val
  22. }
  23. module.exports = Cyclist
  24. function twoify (n) {
  25. if (n && !(n & (n - 1))) return n
  26. var p = 1
  27. while (p < n) p <<= 1
  28. return p
  29. }