diff --git a/src/data-structures/queue/queue.ts b/src/data-structures/queue/queue.ts index 2086017..bb06fcc 100644 --- a/src/data-structures/queue/queue.ts +++ b/src/data-structures/queue/queue.ts @@ -19,6 +19,12 @@ import { SinglyLinkedList } from '../linked-list'; export class Queue extends IterableElementBase> { constructor(elements: Iterable | Iterable = [], options?: QueueOptions) { super(options); + + if (options) { + const { autoCompactRatio = 0.5 } = options; + this._autoCompactRatio = autoCompactRatio; + } + if (elements) { for (const el of elements) { if (this.toElementFn) this.push(this.toElementFn(el as R)); @@ -89,6 +95,25 @@ export class Queue extends IterableElementBase 0 ? this.elements[this.elements.length - 1] : undefined; } + _autoCompactRatio: number = 0.5; + + /** + * This function returns the value of the autoCompactRatio property. + * @returns The `autoCompactRatio` property of the object, which is a number. + */ + get autoCompactRatio(): number { + return this._autoCompactRatio; + } + + /** + * The above function sets the autoCompactRatio property to a specified number in TypeScript. + * @param {number} v - The parameter `v` represents the value that will be assigned to the + * `_autoCompactRatio` property. + */ + set autoCompactRatio(v: number) { + this._autoCompactRatio = v; + } + /** * Time Complexity: O(n) * Space Complexity: O(n) @@ -145,12 +170,7 @@ export class Queue extends IterableElementBase this.autoCompactRatio) this.compact(); return first; } @@ -237,6 +257,17 @@ export class Queue extends IterableElementBase extends IterableElementBase { + protected *_getIterator(): IterableIterator { for (const item of this.elements.slice(this.offset)) { yield item; } diff --git a/src/types/data-structures/queue/queue.ts b/src/types/data-structures/queue/queue.ts index 3058185..eb60615 100644 --- a/src/types/data-structures/queue/queue.ts +++ b/src/types/data-structures/queue/queue.ts @@ -1,3 +1,5 @@ import { IterableElementBaseOptions } from '../base'; -export type QueueOptions = IterableElementBaseOptions & {}; +export type QueueOptions = IterableElementBaseOptions & { + autoCompactRatio?: number; +}; diff --git a/test/unit/data-structures/queue/queue.test.ts b/test/unit/data-structures/queue/queue.test.ts index b912d99..c6cdee8 100644 --- a/test/unit/data-structures/queue/queue.test.ts +++ b/test/unit/data-structures/queue/queue.test.ts @@ -90,6 +90,16 @@ describe('Queue', () => { expect(queue.isEmpty()).toBeTruthy(); }); + test('compact method should work well', () => { + for (let i = 0; i < 1000; i++) queue.push(i); + + for (let i = 0; i < 499; i++) queue.shift(); + + expect(queue.elements.length).toBe(1000); + queue.compact(); + expect(queue.elements.length).toBe(501); + }); + test('should at after shifting', () => { for (let i = 0; i < 100; i++) { queue.push(i); @@ -116,7 +126,10 @@ describe('Queue', () => { }); it('should object queue map & filter', function () { - const queue = new Queue<{ a: string; key: number }>([ + const queue = new Queue<{ + a: string; + key: number; + }>([ { key: 1, a: 'a1' }, { key: 6, a: 'a6' }, { key: 5, a: 'a5' },