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

Method WalkDown

tree/nodebase.go:489–542  ·  view source on GitHub ↗

WalkDown strategy: https://stackoverflow.com/questions/5278580/non-recursive-depth-first-search-algorithm WalkDown calls the given function on the node and all of its children in a depth-first manner over all of the children, sequentially in the current goroutine. It stops walking the current branch

(fun func(n Node) bool)

Source from the content-addressed store, hash-verified

487// method is called for every node after the given function, which enables nodes
488// to also traverse additional nodes, like widget parts.
489func (n *NodeBase) WalkDown(fun func(n Node) bool) {
490 if n.This == nil {
491 return
492 }
493 tm := map[Node]int{} // traversal map
494 start := n.This
495 cur := start
496 tm[cur] = -1
497outer:
498 for {
499 cb := cur.AsTree()
500 if cb.This != nil && fun(cur) { // false return means stop
501 cb.This.NodeWalkDown(fun)
502 if cb.HasChildren() {
503 tm[cur] = 0 // 0 for no fields
504 nxt := cb.Child(0)
505 if nxt != nil && nxt.AsTree().This != nil {
506 cur = nxt.AsTree().This
507 tm[cur] = -1
508 continue
509 }
510 }
511 } else {
512 tm[cur] = cb.NumChildren()
513 }
514 // if we get here, we're in the ascent branch -- move to the right and then up
515 for {
516 cb := cur.AsTree() // may have changed, so must get again
517 curChild := tm[cur]
518 if (curChild + 1) < cb.NumChildren() {
519 curChild++
520 tm[cur] = curChild
521 nxt := cb.Child(curChild)
522 if nxt != nil && nxt.AsTree().This != nil {
523 cur = nxt.AsTree().This
524 tm[cur] = -1
525 continue outer
526 }
527 continue
528 }
529 delete(tm, cur)
530 // couldn't go right, move up..
531 if cur == start {
532 break outer // done!
533 }
534 parent := cb.Parent
535 if parent == nil || parent == cur { // shouldn't happen, but does..
536 // fmt.Printf("nil / cur parent %v\n", par)
537 break outer
538 }
539 cur = parent
540 }
541 }
542}
543
544// NodeWalkDown is a placeholder implementation of [Node.NodeWalkDown]
545// that does nothing.

Callers 15

TestNodeWalkPathFunction · 0.95
SearchFunction · 0.80
OpenURLMethod · 0.80
NodeWalkDownMethod · 0.80
WidgetWalkDownMethod · 0.80
RunMainWindowMethod · 0.80
CompileAllMethod · 0.80
FindMethod · 0.80
SetRuleMapMethod · 0.80
CompileAllMethod · 0.80
FindMethod · 0.80
RaySolidIntersectionsMethod · 0.80

Calls 5

ChildMethod · 0.80
NumChildrenMethod · 0.80
AsTreeMethod · 0.65
NodeWalkDownMethod · 0.65
HasChildrenMethod · 0.45

Tested by 1

TestNodeWalkPathFunction · 0.76