Skip to content

Commit

Permalink
fix(api): max recursion depth hit unexpectedly
Browse files Browse the repository at this point in the history
switched tree.ts from dfs to bfs-based traversal; fixes #10
  • Loading branch information
mxsdev committed Nov 7, 2022
1 parent 3d0dd77 commit b97d4de
Show file tree
Hide file tree
Showing 15 changed files with 11,233 additions and 585 deletions.
3 changes: 2 additions & 1 deletion .mocharc.json
Expand Up @@ -2,5 +2,6 @@
"require": "ts-node/register",
"extensions": "ts",
"spec": ["tests/**/*.spec.ts"],
"watch-files": ["src"]
"watch-files": ["src"],
"timeout": 4000
}
1 change: 1 addition & 0 deletions packages/api/src/index.ts
Expand Up @@ -16,6 +16,7 @@ export {
getTypeInfoChildren,
getTypeInfoSymbols,
getTypeInfoAtRange,
getTypeInfoOfNode,
} from "./tree"
export {
TypeInfo,
Expand Down
86 changes: 86 additions & 0 deletions packages/api/src/queue.ts
@@ -0,0 +1,86 @@
/**
* https://stackoverflow.com/a/49902604/14978493
*/
export class Queue<T> {
get length() {
return this._size
}

public isEmpty() {
return this.length === 0
}

public enqueue(...elems: T[]) {
for (const elem of elems) {
if (this.bottom.full()) {
this.bottom = this.bottom.next = new Subqueue<T>()
}
this.bottom.enqueue(elem)
}

this._size += elems.length
}

public dequeue(): T | undefined {
if (this._size === 0) {
return undefined
}

const val = this.top.dequeue()
this._size--
if (this._size > 0 && this.top.size === 0 && this.top.full()) {
// Discard current subqueue and point top to the one after
this.top = this.top.next!
}
return val
}

public peek(): T | undefined {
return this.top.peek()
}

public last(): T | undefined {
return this.bottom.last()
}

public clear() {
this.bottom = this.top = new Subqueue()
this._size = 0
}

private top: Subqueue<T> = new Subqueue()
private bottom: Subqueue<T> = this.top
private _size = 0
}

/** Queue contains a linked list of Subqueue */
class Subqueue<T> {
public full() {
return this.array.length >= 1000
}

public get size() {
return this.array.length - this.index
}

public peek(): T | undefined {
return this.array[this.index]
}

public last(): T | undefined {
return this.array[this.array.length - 1]
}

public dequeue(): T | undefined {
return this.array[this.index++]
}

public enqueue(elem: T) {
this.array.push(elem)
}

private index = 0
private array: T[] = []

public next: Subqueue<T> | undefined = undefined
}

0 comments on commit b97d4de

Please sign in to comment.