| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| const queue_1 = require("./src/queue"); | ||
| exports.default = queue_1.default; | ||
| //# sourceMappingURL=index.js.map |
| {"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":";;AAAA,uCAAgC;AAEhC,kBAAe,eAAK,CAAC"} |
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| const subqueue_1 = require("./subqueue"); | ||
| class Queue { | ||
| constructor() { | ||
| this.top = new subqueue_1.default(); | ||
| this.bottom = this.top; | ||
| this._size = 0; | ||
| } | ||
| get length() { | ||
| return this._size; | ||
| } | ||
| push(...elems) { | ||
| for (let elem of elems) { | ||
| if (this.bottom.full()) { | ||
| this.bottom = this.bottom.next = new subqueue_1.default(); | ||
| } | ||
| this.bottom.enqueue(elem); | ||
| } | ||
| this._size += elems.length; | ||
| } | ||
| shift() { | ||
| if (this._size === 0) { | ||
| return undefined; | ||
| } | ||
| const val = this.top.dequeue(); | ||
| this._size--; | ||
| if (this._size > 0 && this.top.size === 0 && this.top.full()) { | ||
| this.top = this.top.next; | ||
| } | ||
| return val; | ||
| } | ||
| peek() { | ||
| return this.top.peek(); | ||
| } | ||
| last() { | ||
| return this.bottom.last(); | ||
| } | ||
| clear() { | ||
| this.bottom = this.top = new subqueue_1.default(); | ||
| this._size = 0; | ||
| } | ||
| } | ||
| exports.default = Queue; | ||
| //# sourceMappingURL=queue.js.map |
| {"version":3,"file":"queue.js","sourceRoot":"","sources":["../../src/queue.ts"],"names":[],"mappings":";;AAAA,yCAAkC;AAElC;IAAA;QA0CU,QAAG,GAAgB,IAAI,kBAAQ,EAAE,CAAC;QAClC,WAAM,GAAgB,IAAI,CAAC,GAAG,CAAC;QAC/B,UAAK,GAAW,CAAC,CAAC;IAC5B,CAAC;IA5CC,IAAI,MAAM;QACR,OAAO,IAAI,CAAC,KAAK,CAAC;IACpB,CAAC;IAEM,IAAI,CAAC,GAAG,KAAU;QACvB,KAAK,IAAI,IAAI,IAAI,KAAK,EAAE;YACtB,IAAI,IAAI,CAAC,MAAM,CAAC,IAAI,EAAE,EAAE;gBACtB,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC,IAAI,GAAG,IAAI,kBAAQ,EAAK,CAAC;aACpD;YACD,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;SAC3B;QAED,IAAI,CAAC,KAAK,IAAI,KAAK,CAAC,MAAM,CAAC;IAC7B,CAAC;IAEM,KAAK;QACV,IAAI,IAAI,CAAC,KAAK,KAAK,CAAC,EAAE;YACpB,OAAO,SAAS,CAAC;SAClB;QAED,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,OAAO,EAAE,CAAC;QAC/B,IAAI,CAAC,KAAK,EAAE,CAAC;QACb,IAAI,IAAI,CAAC,KAAK,GAAG,CAAC,IAAI,IAAI,CAAC,GAAG,CAAC,IAAI,KAAK,CAAC,IAAI,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,EAAE;YAC5D,IAAI,CAAC,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC;SAC1B;QACD,OAAO,GAAG,CAAC;IACb,CAAC;IAEM,IAAI;QACT,OAAO,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,CAAC;IACzB,CAAC;IAEM,IAAI;QACT,OAAO,IAAI,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC;IAC5B,CAAC;IAEM,KAAK;QACV,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,GAAG,GAAG,IAAI,kBAAQ,EAAE,CAAC;QACxC,IAAI,CAAC,KAAK,GAAG,CAAC,CAAC;IACjB,CAAC;CAKF;AA7CD,wBA6CC"} |
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| const chai_1 = require("chai"); | ||
| const __1 = require(".."); | ||
| describe('Queue', () => { | ||
| describe('shift', () => { | ||
| it('should behave ok with a small amount of data', () => { | ||
| const queue = new __1.default(); | ||
| queue.push(1); | ||
| queue.push(2, 3); | ||
| chai_1.expect(queue.shift()).to.equal(1); | ||
| chai_1.expect(queue.length).to.equal(2); | ||
| chai_1.expect(queue.shift()).to.equal(2); | ||
| chai_1.expect(queue.shift()).to.equal(3); | ||
| chai_1.expect(queue.shift()).to.equal(undefined); | ||
| queue.push(4); | ||
| chai_1.expect(queue.shift()).to.equal(4); | ||
| }); | ||
| }); | ||
| describe('length', () => { | ||
| it('should stay consistent', () => { | ||
| const queue = new __1.default(); | ||
| chai_1.expect(queue.length).to.equal(0); | ||
| queue.push(1); | ||
| queue.push(2, 3); | ||
| chai_1.expect(queue.length).to.equal(3); | ||
| for (let i = 0; i < 3; i++) { | ||
| queue.shift(); | ||
| } | ||
| chai_1.expect(queue.length).to.equal(0); | ||
| queue.shift(); | ||
| chai_1.expect(queue.length).to.equal(0); | ||
| for (let i = 0; i < 503; i++) { | ||
| queue.shift(); | ||
| } | ||
| chai_1.expect(queue.length).to.equal(0); | ||
| }); | ||
| }); | ||
| it('should behave ok with a real example', () => { | ||
| const queue = new __1.default(); | ||
| queue.push(1); | ||
| queue.push(2); | ||
| for (let i = 10; i < 10000; i++) { | ||
| queue.push(i); | ||
| } | ||
| queue.push('Hello', 'World'); | ||
| chai_1.expect(queue.peek()).to.equal(1); | ||
| chai_1.expect(queue.last()).to.equal('World'); | ||
| chai_1.expect(queue.length).to.equal(9994); | ||
| chai_1.expect(queue.shift()).to.equal(1); | ||
| chai_1.expect(queue.peek()).to.equal(2); | ||
| chai_1.expect(queue.length).to.equal(9993); | ||
| queue.clear(); // Empties queue | ||
| chai_1.expect(queue.length).to.equal(0); | ||
| chai_1.expect(queue.shift()).to.equal(undefined); | ||
| chai_1.expect(queue.length).to.equal(0); | ||
| }); | ||
| }); | ||
| //# sourceMappingURL=queue.spec.js.map |
| {"version":3,"file":"queue.spec.js","sourceRoot":"","sources":["../../src/queue.spec.ts"],"names":[],"mappings":";;AAAA,+BAA8B;A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AK,CAAC,CAAC,CAAC,CAAC;QACjC,aAAM,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;QACvC,aAAM,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;QACpC,aAAM,CAAC,KAAK,CAAC,KAAK,EAAE,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;QAClC,aAAM,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;QACjC,aAAM,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;QACpC,KAAK,CAAC,KAAK,EAAE,CAAC,CAAC,gBAAgB;QAC/B,aAAM,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;QACjC,aAAM,CAAC,KAAK,CAAC,KAAK,EAAE,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC;QAC1C,aAAM,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;IACnC,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"} |
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| class Subqueue { | ||
| constructor() { | ||
| this.index = 0; | ||
| this.array = []; | ||
| this.next = null; | ||
| } | ||
| full() { | ||
| return this.array.length >= 1000; | ||
| } | ||
| get size() { | ||
| return this.array.length - this.index; | ||
| } | ||
| peek() { | ||
| return this.array[this.index]; | ||
| } | ||
| last() { | ||
| return this.array[this.array.length - 1]; | ||
| } | ||
| dequeue() { | ||
| return this.array[this.index++]; | ||
| } | ||
| enqueue(elem) { | ||
| this.array.push(elem); | ||
| } | ||
| } | ||
| exports.default = Subqueue; | ||
| //# sourceMappingURL=subqueue.js.map |
| {"version":3,"file":"subqueue.js","sourceRoot":"","sources":["../../src/subqueue.ts"],"names":[],"mappings":";;AAAA;IAAA;QAyBU,UAAK,GAAW,CAAC,CAAC;QAClB,UAAK,GAAS,EAAE,CAAC;QAElB,SAAI,GAAgB,IAAI,CAAC;IAClC,CAAC;IA5BQ,IAAI;QACT,OAAO,IAAI,CAAC,KAAK,CAAC,MAAM,IAAI,IAAI,CAAC;IACnC,CAAC;IAED,IAAW,IAAI;QACb,OAAO,IAAI,CAAC,KAAK,CAAC,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC;IACxC,CAAC;IAEM,IAAI;QACT,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IAChC,CAAC;IAEM,IAAI;QACT,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,GAAC,CAAC,CAAC,CAAC;IACzC,CAAC;IAEM,OAAO;QACZ,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC,CAAC;IAClC,CAAC;IAEM,OAAO,CAAC,IAAO;QACpB,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACxB,CAAC;CAMF;AA7BD,2BA6BC"} |
+1
-1
| { | ||
| "name": "std-queue", | ||
| "version": "0.1.2", | ||
| "version": "0.1.3", | ||
| "description": "Efficient fifo queue for handling large amounts of data in O(1)", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
+12
-11
@@ -7,14 +7,15 @@ import Subqueue from './subqueue'; | ||
| } | ||
| public push(...elems: T[]) { | ||
| for (let elem of elems) { | ||
| for (const elem of elems) { | ||
| this.bottom.enqueue(elem); | ||
| if (this.bottom.full()) { | ||
| this.bottom = this.bottom.next = new Subqueue<T>(); | ||
| } | ||
| this.bottom.enqueue(elem); | ||
| } | ||
| this._size += elems.length; | ||
| } | ||
| public shift(): T { | ||
@@ -24,6 +25,6 @@ if (this._size === 0) { | ||
| } | ||
| const val = this.top.dequeue(); | ||
| this._size--; | ||
| if (this._size > 0 && this.top.size === 0 && this.top.full()) { | ||
| if (this.top.size === 0 && this.top.full() && this._size > 0) { | ||
| this.top = this.top.next; | ||
@@ -33,11 +34,11 @@ } | ||
| } | ||
| public peek(): T { | ||
| return this.top.peek(); | ||
| } | ||
| public last(): T { | ||
| return this.bottom.last(); | ||
| } | ||
| public clear() { | ||
@@ -47,3 +48,3 @@ this.bottom = this.top = new Subqueue(); | ||
| } | ||
| private top: Subqueue<T> = new Subqueue(); | ||
@@ -50,0 +51,0 @@ private bottom: Subqueue<T> = this.top; |
16661
123.46%16
100%278
93.06%