1
0
mirror of https://github.com/mum4k/termdash.git synced 2025-04-25 13:48:50 +08:00
termdash/container/traversal.go
2018-04-06 04:56:36 +02:00

45 lines
1.1 KiB
Go

package container
// traversal.go provides functions that navigate the container tree.
// rootCont returns the root container.
func rootCont(c *Container) *Container {
for p := c.parent; p != nil; p = c.parent {
c = p
}
return c
}
// visitFunc is executed during traversals when node is visited.
// If the visit function returns an error, the traversal terminates and the
// errStr is set to the text of the returned error.
type visitFunc func(*Container) error
// preOrder performs pre-order DFS traversal on the container tree.
func preOrder(c *Container, errStr *string, visit visitFunc) {
if c == nil || *errStr != "" {
return
}
if err := visit(c); err != nil {
*errStr = err.Error()
return
}
preOrder(c.first, errStr, visit)
preOrder(c.second, errStr, visit)
}
// postOrder performs post-order DFS traversal on the container tree.
func postOrder(c *Container, errStr *string, visit visitFunc) {
if c == nil || *errStr != "" {
return
}
postOrder(c.first, errStr, visit)
postOrder(c.second, errStr, visit)
if err := visit(c); err != nil {
*errStr = err.Error()
return
}
}