MCPcopy Create free account
hub / github.com/cogentcore/core / WalkDownBreadth

Method WalkDownBreadth

tree/nodebase.go:620–645  ·  view source on GitHub ↗

Note: it does not appear that there is a good recursive breadth-first-search strategy: https://herringtondarkholme.github.io/2014/02/17/generator/ https://stackoverflow.com/questions/2549541/performing-breadth-first-search-recursively/2549825#2549825 WalkDownBreadth calls the given function on the n

(fun func(n Node) bool)

Source from the content-addressed store, hash-verified

618// function returns [Break] and keeps walking if it returns [Continue]. It is
619// non-recursive, but not safe for concurrent calling.
620func (n *NodeBase) WalkDownBreadth(fun func(n Node) bool) {
621 start := n.This
622
623 level := 0
624 start.AsTree().depth = level
625 queue := make([]Node, 1)
626 queue[0] = start
627
628 for {
629 if len(queue) == 0 {
630 break
631 }
632 cur := queue[0]
633 depth := cur.AsTree().depth
634 queue = queue[1:]
635
636 if cur.AsTree().This != nil && fun(cur) { // false return means don't proceed
637 for _, cn := range cur.AsTree().Children {
638 if cn != nil && cn.AsTree().This != nil {
639 cn.AsTree().depth = depth + 1
640 queue = append(queue, cn)
641 }
642 }
643 }
644 }
645}
646
647// Deep Copy:
648

Callers 2

TestNodeWalkFunction · 0.95
childByLabelCanFocusFunction · 0.80

Calls 1

AsTreeMethod · 0.65

Tested by 1

TestNodeWalkFunction · 0.76