From ee1c3ba53285b27506c1e3bf1744dfe4a63a0a4d Mon Sep 17 00:00:00 2001 From: Karl Gaissmaier Date: Wed, 1 Feb 2023 14:43:45 +0100 Subject: [PATCH] simplify --- treap.go | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/treap.go b/treap.go index ba92feb..40bda0c 100644 --- a/treap.go +++ b/treap.go @@ -361,22 +361,30 @@ func (t Tree[T]) CoverLCP(item T) (result T, ok bool) { // lcp rec-descent. func (t *Tree[T]) lcp(n *node[T], item T) (result T, ok bool) { - if n == nil { - return - } + for { + if n == nil { + // stop condition + return + } - // fast exit, node has too small max upper interval value (augmented value) - if t.cmpRR(item, n.maxUpper.item) > 0 { - return - } + // fast exit, node has too small max upper interval value (augmented value) + if t.cmpRR(item, n.maxUpper.item) > 0 { + // stop condition + return + } - switch cmp := t.compare(n.item, item); { - case cmp > 0: - // too big, left rec-descent - return t.lcp(n.left, item) - case cmp == 0: - // equality is always the shortest containing hull - return n.item, true + cmp := t.compare(n.item, item) + if cmp == 0 { + // equality is always the shortest containing hull + return n.item, true + } + + if cmp < 0 { + break + } + + // item too big, go left + n = n.left } // LCP => right backtracking