package iskra import "git.smesh.lol/iskradb/lattice" // WalkPath returns the node path from root to key in the given branch. // Delegates to iskradb's WalkPath which tracks intermediate nodes. func (t *Tree) WalkPath(branch lattice.Branch, key lattice.Key) []uint32 { return t.db.WalkPath(branch, key) } // TreeDistance returns the hop count between two keys within a branch // via their lowest common ancestor. Returns -1 if either key is not found. func (t *Tree) TreeDistance(branch lattice.Branch, keyA, keyB lattice.Key) int { if keyA == keyB { return 0 } pathA := t.WalkPath(branch, keyA) pathB := t.WalkPath(branch, keyB) if pathA == nil || pathB == nil { return -1 } lcaDepth := 0 minLen := len(pathA) if len(pathB) < minLen { minLen = len(pathB) } for i := 0; i < minLen; i++ { if pathA[i] != pathB[i] { break } lcaDepth = i } return (len(pathA) - 1 - lcaDepth) + (len(pathB) - 1 - lcaDepth) }