diff options
| author | Jeff Carr <[email protected]> | 2025-03-05 03:00:21 -0600 |
|---|---|---|
| committer | Jeff Carr <[email protected]> | 2025-03-05 03:00:21 -0600 |
| commit | 15100aad3ee4294fc22bbeb697e6c6b88228a0ca (patch) | |
| tree | 1b0c8f73b86e9d28b0bc682d35b5e5e6bde00265 /find.go | |
| parent | 975c2d31020540dd4a67fd049aab565bb168ae21 (diff) | |
func shouldn't have been globalv0.22.22
Diffstat (limited to 'find.go')
| -rw-r--r-- | find.go | 59 |
1 files changed, 59 insertions, 0 deletions
@@ -0,0 +1,59 @@ +package tree + +import "log" + +// find things in the tree +// also verify parent <-> child mappings aren't a lie + +// searches the binary tree for a WidgetId +func FindWidgetId(id int) *Node { + return treeRoot.FindWidgetId(id) +} + +// searches the binary tree for a WidgetId +func (n *Node) FindWidgetId(id int) *Node { + if n == nil { + return nil + } + + if n.WidgetId == id { + return n + } + + for _, child := range n.children { + newN := child.FindWidgetId(id) + if newN != nil { + return newN + } + } + return nil +} + +// used for debugging the 'gui' package +// to make sure things are valid +// fixes any errors along the way +func (me *Node) VerifyParentId() bool { + return me.verifyParentId(0) +} + +func (n *Node) verifyParentId(parentId int) bool { + if n == nil { + return false + } + var ok bool = true + + if n.ParentId != parentId { + log.Printf("fixed widgetId %d from %d to %d", n.WidgetId, n.ParentId, parentId) + n.ParentId = parentId + ok = false + } + + for _, child := range n.children { + if child.verifyParentId(n.WidgetId) { + // everything is ok + } else { + ok = false + } + } + return ok +} |
