Skip to content

Commit ee1c3ba

Browse files
committed
simplify
1 parent e2923f5 commit ee1c3ba

File tree

1 file changed

+22
-14
lines changed

1 file changed

+22
-14
lines changed

treap.go

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -361,22 +361,30 @@ func (t Tree[T]) CoverLCP(item T) (result T, ok bool) {
361361

362362
// lcp rec-descent.
363363
func (t *Tree[T]) lcp(n *node[T], item T) (result T, ok bool) {
364-
if n == nil {
365-
return
366-
}
364+
for {
365+
if n == nil {
366+
// stop condition
367+
return
368+
}
367369

368-
// fast exit, node has too small max upper interval value (augmented value)
369-
if t.cmpRR(item, n.maxUpper.item) > 0 {
370-
return
371-
}
370+
// fast exit, node has too small max upper interval value (augmented value)
371+
if t.cmpRR(item, n.maxUpper.item) > 0 {
372+
// stop condition
373+
return
374+
}
372375

373-
switch cmp := t.compare(n.item, item); {
374-
case cmp > 0:
375-
// too big, left rec-descent
376-
return t.lcp(n.left, item)
377-
case cmp == 0:
378-
// equality is always the shortest containing hull
379-
return n.item, true
376+
cmp := t.compare(n.item, item)
377+
if cmp == 0 {
378+
// equality is always the shortest containing hull
379+
return n.item, true
380+
}
381+
382+
if cmp < 0 {
383+
break
384+
}
385+
386+
// item too big, go left
387+
n = n.left
380388
}
381389

382390
// LCP => right backtracking

0 commit comments

Comments
 (0)