func (n *node) findAll(f func(fn *node) (bool, error)) ([]*node, error) {
	var allFound []*node
	hasFound, err := f(n)
	if err != nil {
		return nil, errors.Annotate(err, ErrNodeFindAll, errorMessages)
	}
	if hasFound {
		allFound = append(allFound, n)
	}
	for _, child := range n.children {
		found, err := child.findAll(f)
		if err != nil {
			return nil, errors.Annotate(err, ErrNodeFindAll, errorMessages)
		}
		if found != nil {
			allFound = append(allFound, found...)
		}
	}
	return allFound, nil
}
findAll returns all nodes for which the passed function
returns true.
package collections
import (
	"fmt"
	"github.com/tideland/golib/errors"
)
type nodeContent interface {
	key() interface{}
	value() interface{}
	deepCopy() nodeContent
}
type justValue struct {
	v interface{}
}
func (v justValue) key() interface{} {
	return v.v
}
func (v justValue) value() interface{} {
	return v.v
}
func (v justValue) deepCopy() nodeContent {
	return justValue{v.v}
}
func (v justValue) String() string {
	return fmt.Sprintf("%v", v.v)
}
type keyValue struct {
	k interface{}
	v interface{}
}
func (v keyValue) key() interface{} {
	return v.k
}
func (v keyValue) value() interface{} {
	return v.v
}
func (v keyValue) deepCopy() nodeContent {
	return keyValue{v.k, v.v}
}
func (v keyValue) String() string {
	return fmt.Sprintf("%v = '%v'", v.k, v.v)
}
type nodeContainer struct {
	root       *node
	duplicates bool
}
func newNodeContainer(c nodeContent, duplicates bool) *nodeContainer {
	nc := &nodeContainer{
		root: &node{
			content: c,
		},
		duplicates: duplicates,
	}
	nc.root.container = nc
	return nc
}
func (nc *nodeContainer) deepCopy() *nodeContainer {
	cnc := &nodeContainer{
		duplicates: nc.duplicates,
	}
	cnc.root = nc.root.deepCopy(cnc, nil)
	return cnc
}
type node struct {
	container *nodeContainer
	parent    *node
	content   nodeContent
	children  []*node
}
func (n *node) isAllowed(c nodeContent, here bool) bool {
	if n.container.duplicates {
		return true
	}
	checkNode := n
	if here {
		checkNode = n.parent
	}
	for _, child := range checkNode.children {
		if child.content.key() == c.key() {
			return false
		}
	}
	return true
}
func (n *node) hasDuplicateSibling(key interface{}) bool {
	if n.parent == nil {
		return false
	}
	for _, sibling := range n.parent.children {
		if sibling == n {
			continue
		}
		if sibling.content.key() == key {
			return true
		}
	}
	return false
}
func (n *node) addChild(c nodeContent) (*node, error) {
	if !n.isAllowed(c, false) {
		return nil, errors.New(ErrDuplicate, errorMessages)
	}
	child := &node{
		container: n.container,
		parent:    n,
		content:   c,
	}
	n.children = append(n.children, child)
	return child, nil
}
func (n *node) remove() error {
	if n.parent == nil {
		return errors.New(ErrCannotRemoveRoot, errorMessages)
	}
	for i, child := range n.parent.children {
		if child == n {
			n.parent.children = append(n.parent.children[:i], n.parent.children[i+1:]...)
			return nil
		}
	}
	panic("cannot find node to remove at parent")
}
func (n *node) at(path ...nodeContent) (*node, error) {
	if len(path) == 0 || path[0].key() != n.content.key() {
		return nil, errors.New(ErrNodeNotFound, errorMessages)
	}
	if len(path) == 1 {
		return n, nil
	}
	for _, child := range n.children {
		found, err := child.at(path[1:]...)
		if err != nil && !IsNodeNotFoundError(err) {
			return nil, errors.Annotate(err, ErrIllegalPath, errorMessages)
		}
		if found != nil {
			return found, nil
		}
	}
	return nil, errors.New(ErrNodeNotFound, errorMessages)
}
func (n *node) create(path ...nodeContent) (*node, error) {
	if len(path) == 0 || path[0].key() != n.content.key() {
		return nil, errors.New(ErrNodeNotFound, errorMessages)
	}
	if len(path) == 1 {
		return n, nil
	}
	var found *node
	for _, child := range n.children {
		if path[1].key() == child.content.key() {
			found = child
			break
		}
	}
	if found == nil {
		child, err := n.addChild(path[1])
		if err != nil {
			return nil, errors.Annotate(err, ErrNodeAddChild, errorMessages)
		}
		return child.create(path[1:]...)
	}
	return found.create(path[1:]...)
}
func (n *node) findFirst(f func(fn *node) (bool, error)) (*node, error) {
	hasFound, err := f(n)
	if err != nil {
		return nil, errors.Annotate(err, ErrNodeFindFirst, errorMessages)
	}
	if hasFound {
		return n, nil
	}
	for _, child := range n.children {
		found, err := child.findFirst(f)
		if err != nil && !IsNodeNotFoundError(err) {
			return nil, errors.Annotate(err, ErrNodeFindFirst, errorMessages)
		}
		if found != nil {
			return found, nil
		}
	}
	return nil, errors.New(ErrNodeNotFound, errorMessages)
}