mirror of
https://github.com/zrwusa/data-structure-typed.git
synced 2024-11-23 12:54:04 +00:00
fix: #89. Supports maxLen for max length of the Deque
This commit is contained in:
parent
c7dbfd54f1
commit
3e91a97987
|
@ -32,8 +32,9 @@ export class Deque<E = any, R = any> extends IterableElementBase<E, R, Deque<E,
|
|||
super(options);
|
||||
|
||||
if (options) {
|
||||
const { bucketSize } = options;
|
||||
const { bucketSize, maxLen } = options;
|
||||
if (typeof bucketSize === 'number') this._bucketSize = bucketSize;
|
||||
if (typeof maxLen === 'number' && maxLen > 0 && maxLen % 1 === 0) this._maxLen = maxLen;
|
||||
}
|
||||
|
||||
let _size: number;
|
||||
|
@ -73,6 +74,17 @@ export class Deque<E = any, R = any> extends IterableElementBase<E, R, Deque<E,
|
|||
return this._bucketSize;
|
||||
}
|
||||
|
||||
protected _maxLen: number = -1;
|
||||
|
||||
/**
|
||||
* The maxLen function returns the max length of the deque.
|
||||
*
|
||||
* @return The max length of the deque
|
||||
*/
|
||||
get maxLen() {
|
||||
return this._maxLen;
|
||||
}
|
||||
|
||||
protected _bucketFirst = 0;
|
||||
|
||||
/**
|
||||
|
@ -193,6 +205,7 @@ export class Deque<E = any, R = any> extends IterableElementBase<E, R, Deque<E,
|
|||
}
|
||||
this._size += 1;
|
||||
this._buckets[this._bucketLast][this._lastInBucket] = element;
|
||||
if (this._maxLen > 0 && this._size > this._maxLen) this.shift();
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -257,6 +270,7 @@ export class Deque<E = any, R = any> extends IterableElementBase<E, R, Deque<E,
|
|||
}
|
||||
this._size += 1;
|
||||
this._buckets[this._bucketFirst][this._firstInBucket] = element;
|
||||
if (this._maxLen > 0 && this._size > this._maxLen) this.pop();
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -100,7 +100,6 @@ export class Queue<E = any, R = any> extends IterableElementBase<E, R, Queue<E,
|
|||
*
|
||||
* The function "fromArray" creates a new Queue object from an array of elements.Creates a queue from an existing array.
|
||||
* @public
|
||||
* @static
|
||||
* @param {E[]} elements - The "elements" parameter is an array of elements of type E.
|
||||
* @returns The method is returning a new instance of the Queue class, initialized with the elements from the input
|
||||
* array.
|
||||
|
|
|
@ -1,3 +1,6 @@
|
|||
import { IterableElementBaseOptions } from '../base';
|
||||
|
||||
export type DequeOptions<E, R> = { bucketSize?: number } & IterableElementBaseOptions<E, R>;
|
||||
export type DequeOptions<E, R> = {
|
||||
bucketSize?: number,
|
||||
maxLen?: number
|
||||
} & IterableElementBaseOptions<E, R>;
|
||||
|
|
|
@ -260,6 +260,25 @@ describe('Deque - Utility Operations', () => {
|
|||
// deque.print();
|
||||
// expect(consoleSpy).toHaveBeenCalledWith([1, 2]);
|
||||
});
|
||||
|
||||
test('should maxLen work well', () => {
|
||||
const dequeMaxLen = new Deque([3, 4, 5, 6, 7], { maxLen: 3 });
|
||||
expect(dequeMaxLen.size).toBe(3);
|
||||
expect(dequeMaxLen.toArray()).toEqual([5, 6 ,7]);
|
||||
dequeMaxLen.unshift(4);
|
||||
dequeMaxLen.unshift(3);
|
||||
expect(dequeMaxLen.size).toBe(3);
|
||||
expect(dequeMaxLen.toArray()).toEqual([3, 4, 5]);
|
||||
|
||||
const dequeNoMaxLen = new Deque([3, 4, 5, 6, 7]);
|
||||
expect(dequeNoMaxLen.size).toBe(5);
|
||||
expect(dequeNoMaxLen.toArray()).toEqual([3, 4, 5, 6, 7]);
|
||||
dequeNoMaxLen.unshift(4);
|
||||
dequeNoMaxLen.unshift(3);
|
||||
expect(dequeNoMaxLen.size).toBe(7);
|
||||
expect(dequeNoMaxLen.toArray()).toEqual([3, 4, 3, 4, 5, 6, 7]);
|
||||
|
||||
});
|
||||
});
|
||||
|
||||
describe('Deque - Additional Operations', () => {
|
||||
|
|
Loading…
Reference in a new issue