itertools: Added bfs iterator
This commit is contained in:
parent
d854218c9d
commit
d3e4a25ae0
1 changed files with 28 additions and 0 deletions
|
@ -22,6 +22,34 @@ export function * MakeIterator(root: IContainerModel): Generator<IContainerModel
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface ContainerAndDepth {
|
||||||
|
container: IContainerModel
|
||||||
|
depth: number
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Returns a Generator iterating of over the children depth-first
|
||||||
|
*/
|
||||||
|
export function * MakeBFSIterator(root: IContainerModel): Generator<ContainerAndDepth, void, unknown> {
|
||||||
|
const queue: IContainerModel[] = [root];
|
||||||
|
let depth = 0;
|
||||||
|
while (queue.length > 0) {
|
||||||
|
let levelSize = queue.length;
|
||||||
|
while (levelSize-- !== 0) {
|
||||||
|
const container = queue.shift() as IContainerModel;
|
||||||
|
yield {
|
||||||
|
container,
|
||||||
|
depth
|
||||||
|
};
|
||||||
|
|
||||||
|
for (let i = container.children.length - 1; i >= 0; i--) {
|
||||||
|
const child = container.children[i];
|
||||||
|
queue.push(child);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
depth++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the depth of the container
|
* Returns the depth of the container
|
||||||
* @returns The depth of the container
|
* @returns The depth of the container
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue