find.go 677 Bytes
Newer Older
1 2 3
package trie

import (
tavit ohanian's avatar
tavit ohanian committed
4
	"gitlab.dms3.io/p2p/go-p2p-xor/key"
5 6
)

Petar Maymounkov's avatar
Petar Maymounkov committed
7 8 9
// Find looks for the key q in the trie.
// It returns the depth of the leaf reached along the path of q, regardless of whether q was found in that leaf.
// It also returns a boolean flag indicating whether the key was found.
10 11 12 13 14
func (trie *Trie) Find(q key.Key) (reachedDepth int, found bool) {
	return trie.FindAtDepth(0, q)
}

func (trie *Trie) FindAtDepth(depth int, q key.Key) (reachedDepth int, found bool) {
15 16 17 18 19 20 21
	switch {
	case trie.IsEmptyLeaf():
		return depth, false
	case trie.IsNonEmptyLeaf():
		return depth, key.Equal(trie.Key, q)
	default:
		return trie.Branch[q.BitAt(depth)].FindAtDepth(depth+1, q)
22 23
	}
}