Skip to content

Commit

Permalink
use binary search to search visible slices
Browse files Browse the repository at this point in the history
  • Loading branch information
dweymouth committed Nov 18, 2023
1 parent 420a945 commit 8afdc18
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions widget/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -752,10 +752,20 @@ func (l *listLayout) updateSeparators() {
}
}

// invariant: visible is in ascending order of IDs
func (l *listLayout) searchVisible(visible []itemAndID, id ListItemID) int {
for i, v := range visible {
if v.id == id {
return i
// binary search
low := 0
high := len(visible) - 1
for low <= high {
mid := (low + high) / 2
if visible[mid].id == id {
return mid
}
if visible[mid].id > id {
high = mid - 1
} else {
low = mid + 1
}
}
return -1
Expand Down

0 comments on commit 8afdc18

Please sign in to comment.