From d3e4a25ae029403adb8410151ffda00a4298d0f9 Mon Sep 17 00:00:00 2001 From: Siklos Date: Thu, 18 Aug 2022 13:06:01 +0200 Subject: [PATCH] itertools: Added bfs iterator --- src/utils/itertools.ts | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/utils/itertools.ts b/src/utils/itertools.ts index 221d6c0..e52e034 100644 --- a/src/utils/itertools.ts +++ b/src/utils/itertools.ts @@ -22,6 +22,34 @@ export function * MakeIterator(root: IContainerModel): Generator { + 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