fix: Iterator supports function length or function size

This commit is contained in:
Revone 2023-11-19 09:04:48 +08:00
parent 5c6803b1a9
commit 6f74397f8d
2 changed files with 6 additions and 6 deletions

View file

@ -119,11 +119,11 @@ export class Deque<E> {
*/
constructor(elements: IterableWithSizeOrLength<E> = [], bucketSize = (1 << 12)) {
let _size;
let _size: number;
if ('length' in elements) {
_size = elements.length;
if (elements.length instanceof Function) _size = elements.length(); else _size = elements.length;
} else {
_size = elements.size;
if (elements.size instanceof Function) _size = elements.size();else _size = elements.size;
}
this._bucketSize = bucketSize;
@ -991,7 +991,7 @@ export class Deque<E> {
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)

View file

@ -16,11 +16,11 @@ export const enum IterateDirection {
}
export interface IterableWithSize<T> extends Iterable<T> {
size: number;
size: number | ((...args: any[]) => number);
}
export interface IterableWithLength<T> extends Iterable<T> {
length: number;
length: number | ((...args: any[]) => number);
}
export type IterableWithSizeOrLength<T> = IterableWithSize<T> | IterableWithLength<T>