Skip to content

Commit

Permalink
use sort.Search instead of own binary search impl
Browse files Browse the repository at this point in the history
  • Loading branch information
dweymouth committed Nov 18, 2023
1 parent e7e3628 commit 913e656
Showing 1 changed file with 5 additions and 13 deletions.
18 changes: 5 additions & 13 deletions widget/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package widget
import (
"fmt"
"math"
"sort"
"sync"

"fyne.io/fyne/v2"
Expand Down Expand Up @@ -746,19 +747,10 @@ func (l *listLayout) updateSeparators() {

// invariant: visible is in ascending order of IDs
func (l *listLayout) searchVisible(visible []itemAndID, id ListItemID) (*listItem, bool) {
// binary search
low := 0
high := len(visible) - 1
for low <= high {
mid := (low + high) / 2
if visible[mid].id == id {
return visible[mid].item, true
}
if visible[mid].id > id {
high = mid - 1
} else {
low = mid + 1
}
ln := len(visible)
idx := sort.Search(ln, func(i int) bool { return visible[i].id >= id })
if idx < ln && visible[idx].id == id {
return visible[idx].item, true
}
return nil, false
}
Expand Down

0 comments on commit 913e656

Please sign in to comment.