SHARE
TWEET

Untitled

a guest Aug 18th, 2019 73 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class CircularQueue {
  2.     constructor (size) {
  3.         this._max = size;
  4.         this._start = 0;
  5.         this._end = 0;
  6.         this._length = 0;
  7.         this._queue = new Array(size);
  8.     }
  9.  
  10.     enq (item) {
  11.         this._queue[this._end] = item;
  12.         this._end = (this._end + 1) % this._max;
  13.  
  14.         if (this._length === this._max) {
  15.             this._start = this._end;
  16.         } else {
  17.             this._length++;
  18.         }
  19.     }
  20.  
  21.     deq () {
  22.         if (this._length) {
  23.             const item = this._queue[this._start];
  24.             this._start = (this._start + 1) % this._max;
  25.             this._length--;
  26.  
  27.             return item;
  28.         }
  29.     }
  30.  
  31.     each (lambda) {
  32.         for (let i = 0; i < this._length; i++) {
  33.             const result = lambda(this._queue[(this._start + i) % this._max]);
  34.  
  35.             if (typeof result != 'undefined' && !result) // Stop iterating if "false" is returned
  36.                 break;
  37.         }
  38.     }
  39. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top