60 lines
1.1 KiB
Go
60 lines
1.1 KiB
Go
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
|
|
}
|