* Breadth first traverse * Return true to stop traversing
(
cb: (this: Ctx, node: GraphNode, fromNode: GraphNode) => boolean | void,
startNode: GraphNode | string,
direction: 'none' | 'in' | 'out',
context?: Ctx
)
| 216 | * Return true to stop traversing |
| 217 | */ |
| 218 | breadthFirstTraverse<Ctx>( |
| 219 | cb: (this: Ctx, node: GraphNode, fromNode: GraphNode) => boolean | void, |
| 220 | startNode: GraphNode | string, |
| 221 | direction: 'none' | 'in' | 'out', |
| 222 | context?: Ctx |
| 223 | ) { |
| 224 | if (!(startNode instanceof GraphNode)) { |
| 225 | startNode = this._nodesMap[generateNodeKey(startNode)]; |
| 226 | } |
| 227 | if (!startNode) { |
| 228 | return; |
| 229 | } |
| 230 | |
| 231 | const edgeType: 'inEdges' | 'outEdges' | 'edges' = direction === 'out' |
| 232 | ? 'outEdges' : (direction === 'in' ? 'inEdges' : 'edges'); |
| 233 | |
| 234 | for (let i = 0; i < this.nodes.length; i++) { |
| 235 | this.nodes[i].__visited = false; |
| 236 | } |
| 237 | |
| 238 | if (cb.call(context, startNode, null)) { |
| 239 | return; |
| 240 | } |
| 241 | |
| 242 | const queue = [startNode]; |
| 243 | while (queue.length) { |
| 244 | const currentNode = queue.shift(); |
| 245 | const edges = currentNode[edgeType]; |
| 246 | |
| 247 | for (let i = 0; i < edges.length; i++) { |
| 248 | const e = edges[i]; |
| 249 | const otherNode = e.node1 === currentNode |
| 250 | ? e.node2 : e.node1; |
| 251 | if (!otherNode.__visited) { |
| 252 | if (cb.call(context, otherNode, currentNode)) { |
| 253 | // Stop traversing |
| 254 | return; |
| 255 | } |
| 256 | queue.push(otherNode); |
| 257 | otherNode.__visited = true; |
| 258 | } |
| 259 | } |
| 260 | } |
| 261 | }; |
| 262 | |
| 263 | // TODO |
| 264 | // depthFirstTraverse( |
nothing calls this directly
no test coverage detected