summaryrefslogtreecommitdiff
path: root/find.go
blob: a109271278645e6d044e9cba7228202cc63bc4e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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)
}

func FindWidgetById(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
}