Skip to content

Commit

Permalink
Merge a254a04 into ebe332b
Browse files Browse the repository at this point in the history
  • Loading branch information
gillesdemey committed Sep 26, 2020
2 parents ebe332b + a254a04 commit e7b33da
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
5 changes: 5 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
declare class Denque<T = any> {
constructor();
constructor(array: T[]);
constructor(array: T[], options: IDenqueOptions);

push(item: T): number;
unshift(item: T): number;
Expand All @@ -23,4 +24,8 @@ declare class Denque<T = any> {
length: number;
}

interface IDenqueOptions {
capacity?: number
}

export = Denque;
10 changes: 8 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
/**
* Custom implementation of a double ended queue.
*/
function Denque(array) {
function Denque(array, options) {
var options = options || { capacity: Infinity };

this._head = 0;
this._tail = 0;
this._capacity = options.capacity;
this._capacityMask = 0x3;
this._list = new Array(4);
if (Array.isArray(array)) {
Expand Down Expand Up @@ -104,6 +107,7 @@ Denque.prototype.unshift = function unshift(item) {
this._head = (this._head - 1 + len) & this._capacityMask;
this._list[this._head] = item;
if (this._tail === this._head) this._growArray();
if (this.size() > this._capacity) this.pop();
if (this._head < this._tail) return this._tail - this._head;
else return this._capacityMask + 1 - (this._head - this._tail);
};
Expand Down Expand Up @@ -135,7 +139,9 @@ Denque.prototype.push = function push(item) {
if (this._tail === this._head) {
this._growArray();
}

if (this.size() > this._capacity) {
this.shift();
}
if (this._head < this._tail) return this._tail - this._head;
else return this._capacityMask + 1 - (this._head - this._tail);
};
Expand Down
20 changes: 20 additions & 0 deletions test/denque.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,16 @@ describe('Denque.prototype.push', function () {
assert.deepEqual(a.toArray(), [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 1]);
});

it('should respect capacity', function () {
var a = new Denque([1, 2, 3], { capacity: 3 });
a.push(4);

assert.equal(a.size(), 3);
assert.equal(a.peekAt(0), 2);
assert.equal(a.peekAt(1), 3);
assert.equal(a.peekAt(2), 4);
});

});

describe('Denque.prototype.unshift', function () {
Expand Down Expand Up @@ -203,6 +213,16 @@ describe('Denque.prototype.unshift', function () {
assert.deepEqual(a.toArray(), [1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]);
});

it('should respect capacity', function () {
var a = new Denque([1, 2, 3], { capacity: 3 });
a.unshift(0);

assert.equal(a.size(), 3);
assert.equal(a.peekAt(0), 0);
assert.equal(a.peekAt(1), 1);
assert.equal(a.peekAt(2), 2);
});

});

describe('Denque.prototype.pop', function () {
Expand Down
1 change: 1 addition & 0 deletions test/type/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Denque = require("../../");

const queue = new Denque(["my", "super", "array"]);
new Denque(); // also work
new Denque<number>([1, 2, 3], { capacity: 3 });

queue.push("awesome");
queue.unshift("typescript");
Expand Down

0 comments on commit e7b33da

Please sign in to comment.