Skip to content

Commit

Permalink
fix: Iterator supports function length or function size
Browse files Browse the repository at this point in the history
  • Loading branch information
zrwusa committed Nov 19, 2023
1 parent 5c6803b commit 6f74397
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
8 changes: 4 additions & 4 deletions src/data-structures/queue/deque.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -991,7 +991,7 @@ export class Deque<E> {
* Time Complexity: O(1)
* Space Complexity: O(1)
*/

/**
* Time Complexity: O(1)
* Space Complexity: O(1)
Expand Down
4 changes: 2 additions & 2 deletions src/types/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>

0 comments on commit 6f74397

Please sign in to comment.