57 lines
998 B
Go
57 lines
998 B
Go
package core
|
|
|
|
import "container/list"
|
|
|
|
type BufferNode struct {
|
|
name string
|
|
children []*BufferNode
|
|
messages []*Message
|
|
}
|
|
|
|
func NewBufferNode(name string) *BufferNode {
|
|
return &BufferNode{
|
|
name: name,
|
|
children: make([]*BufferNode, 0),
|
|
}
|
|
}
|
|
|
|
const (
|
|
WALK_DEPTH_FIRST = 0
|
|
WALK_BREADTH_FIRST = 1
|
|
)
|
|
|
|
type WalkEntry struct {
|
|
Node *BufferNode
|
|
Depth int
|
|
}
|
|
|
|
func (n*BufferNode) AddChild(child*BufferNode) {
|
|
n.children = append(n.children, child)
|
|
}
|
|
|
|
func (n *BufferNode) Walk(cb func(*WalkEntry), mode int) {
|
|
queue := list.New()
|
|
queue.PushBack(&WalkEntry{
|
|
Node: n,
|
|
Depth: 0,
|
|
})
|
|
|
|
for queue.Len() > 0 {
|
|
next := queue.Front()
|
|
queue.Remove(next)
|
|
entry := next.Value.(*WalkEntry)
|
|
cb(entry)
|
|
|
|
for _, child := range entry.Node.children {
|
|
childEntry := &WalkEntry{
|
|
Node: child,
|
|
Depth: entry.Depth + 1,
|
|
}
|
|
if mode == WALK_DEPTH_FIRST {
|
|
queue.PushFront(childEntry)
|
|
} else if mode == WALK_BREADTH_FIRST {
|
|
queue.PushBack(childEntry)
|
|
}
|
|
}
|
|
}
|
|
}
|