func (t *Tree) All() []interface{} {
	t.mutex.RLock()
	defer t.mutex.RUnlock()
	return t.clean(t.all([]interface{}{}, t.root))
}
All will return all stored values in the tree.
package topic
import (
	"fmt"
	"strings"
	"sync"
)
type node struct {
	children map[string]*node
	values   []interface{}
}
func newNode() *node {
	return &node{
		children: make(map[string]*node),
	}
}
func (n *node) removeValue(value interface{}) {
	for i, v := range n.values {
		if v == value {
			n.values[i] = n.values[len(n.values)-1]
			n.values[len(n.values)-1] = nil
			n.values = n.values[:len(n.values)-1]
			break
		}
	}
}
func (n *node) clearValues() {
	n.values = []interface{}{}
}
func (n *node) string(level int) string {
	str := ""
	if level != 0 {
		str = fmt.Sprintf("%d", len(n.values))
	}
	for key, node := range n.children {
		str += fmt.Sprintf("\n| %s'%s' => %s", strings.Repeat(" ", level*2), key, node.string(level+1))
	}
	return str
}
type Tree struct {
	separator    string
	wildcardOne  string
	wildcardSome string
	root         *node
	mutex        sync.RWMutex
}
func NewTree(separator, wildcardOne, wildcardSome string) *Tree {
	return &Tree{
		separator:    separator,
		wildcardOne:  wildcardOne,
		wildcardSome: wildcardSome,
		root:         newNode(),
	}
}
func NewStandardTree() *Tree {
	return NewTree("/", "+", "#")
}
func (t *Tree) Add(topic string, value interface{}) {
	t.mutex.Lock()
	defer t.mutex.Unlock()
	t.add(value, topic, t.root)
}
func (t *Tree) add(value interface{}, topic string, node *node) {
	if topic == topicEnd {
		for _, v := range node.values {
			if v == value {
				return
			}
		}
		node.values = append(node.values, value)
		return
	}
	segment := topicSegment(topic, t.separator)
	child, ok := node.children[segment]
	if !ok {
		child = newNode()
		node.children[segment] = child
	}
	t.add(value, topicShorten(topic, t.separator), child)
}
func (t *Tree) Set(topic string, value interface{}) {
	t.mutex.Lock()
	defer t.mutex.Unlock()
	t.set(value, topic, t.root)
}
func (t *Tree) set(value interface{}, topic string, node *node) {
	if topic == topicEnd {
		node.values = []interface{}{value}
		return
	}
	segment := topicSegment(topic, t.separator)
	child, ok := node.children[segment]
	if !ok {
		child = newNode()
		node.children[segment] = child
	}
	t.set(value, topicShorten(topic, t.separator), child)
}
func (t *Tree) Get(topic string) []interface{} {
	t.mutex.Lock()
	defer t.mutex.Unlock()
	return t.get(topic, t.root)
}
func (t *Tree) get(topic string, node *node) []interface{} {
	if topic == topicEnd {
		return node.values
	}
	segment := topicSegment(topic, t.separator)
	child, ok := node.children[segment]
	if !ok {
		return nil
	}
	return t.get(topicShorten(topic, t.separator), child)
}
func (t *Tree) Remove(topic string, value interface{}) {
	t.mutex.Lock()
	defer t.mutex.Unlock()
	t.remove(value, topic, t.root)
}
func (t *Tree) Empty(topic string) {
	t.mutex.Lock()
	defer t.mutex.Unlock()
	t.remove(nil, topic, t.root)
}
func (t *Tree) remove(value interface{}, topic string, node *node) bool {
	if topic == topicEnd {
		if value == nil {
			node.clearValues()
		} else {
			node.removeValue(value)
		}
		return len(node.values) == 0 && len(node.children) == 0
	}
	segment := topicSegment(topic, t.separator)
	child, ok := node.children[segment]
	if !ok {
		return false
	}
	if t.remove(value, topicShorten(topic, t.separator), child) {
		delete(node.children, segment)
	}
	return len(node.values) == 0 && len(node.children) == 0
}
func (t *Tree) Clear(value interface{}) {
	t.mutex.Lock()
	defer t.mutex.Unlock()
	t.clear(value, t.root)
}
func (t *Tree) clear(value interface{}, node *node) bool {
	node.removeValue(value)
	for segment, child := range node.children {
		if t.clear(value, child) {
			delete(node.children, segment)
		}
	}
	return len(node.values) == 0 && len(node.children) == 0
}
func (t *Tree) Match(topic string) []interface{} {
	t.mutex.RLock()
	defer t.mutex.RUnlock()
	var list []interface{}
	t.match(topic, t.root, func(values []interface{}) bool {
		list = append(list, values...)
		return true
	})
	return t.clean(list)
}
func (t *Tree) MatchFirst(topic string) interface{} {
	t.mutex.RLock()
	defer t.mutex.RUnlock()
	var value interface{}
	t.match(topic, t.root, func(values []interface{}) bool {
		value = values[0]
		return false
	})
	return value
}
func (t *Tree) match(topic string, node *node, fn func([]interface{}) bool) {
	if child, ok := node.children[t.wildcardSome]; ok && len(child.values) > 0 {
		if !fn(child.values) {
			return
		}
	}
	if topic == topicEnd {
		if len(node.values) > 0 {
			fn(node.values)
		}
		return
	}
	if child, ok := node.children[t.wildcardOne]; ok {
		t.match(topicShorten(topic, t.separator), child, fn)
	}
	segment := topicSegment(topic, t.separator)
	if segment != t.wildcardOne && segment != t.wildcardSome {
		if child, ok := node.children[segment]; ok {
			t.match(topicShorten(topic, t.separator), child, fn)
		}
	}
}
func (t *Tree) Search(topic string) []interface{} {
	t.mutex.RLock()
	defer t.mutex.RUnlock()
	var list []interface{}
	t.search(topic, t.root, func(values []interface{}) bool {
		list = append(list, values...)
		return true
	})
	return t.clean(list)
}
func (t *Tree) SearchFirst(topic string) interface{} {
	t.mutex.RLock()
	defer t.mutex.RUnlock()
	var value interface{}
	t.search(topic, t.root, func(values []interface{}) bool {
		value = values[0]
		return false
	})
	return value
}
func (t *Tree) search(topic string, node *node, fn func([]interface{}) bool) {
	if topic == topicEnd {
		if len(node.values) > 0 {
			fn(node.values)
		}
		return
	}
	segment := topicSegment(topic, t.separator)
	if segment == t.wildcardSome {
		if len(node.values) > 0 {
			if !fn(node.values) {
				return
			}
		}
		for _, child := range node.children {
			t.search(topic, child, fn)
		}
	}
	if segment == t.wildcardOne {
		if len(node.values) > 0 {
			if !fn(node.values) {
				return
			}
		}
		for _, child := range node.children {
			t.search(topicShorten(topic, t.separator), child, fn)
		}
	}
	if segment != t.wildcardOne && segment != t.wildcardSome {
		if child, ok := node.children[segment]; ok {
			t.search(topicShorten(topic, t.separator), child, fn)
		}
	}
}
func (t *Tree) clean(values []interface{}) []interface{} {
	result := values[:0]
	for _, v := range values {
		if contains(result, v) {
			continue
		}
		result = append(result, v)
	}
	return result
}
func (t *Tree) Count() int {
	t.mutex.RLock()
	defer t.mutex.RUnlock()
	return t.count(t.root)
}
func (t *Tree) count(node *node) int {
	total := 0
	for _, child := range node.children {
		total += t.count(child)
	}
	return total + len(node.values)
}