Port calls to slices.Sort functions since they now need a cmp() function rather than a less() function

Also rename os.SEEK_* to io.Seek* as the former has been deprecated
This commit is contained in:
Kovid Goyal
2023-08-04 22:50:13 +05:30
parent 18d48c8dcd
commit 341d845b9a
18 changed files with 60 additions and 34 deletions

View File

@@ -308,12 +308,12 @@ func (self *Command) GetVisibleOptions() ([]string, map[string][]*Option) {
}
func sort_levenshtein_matches(q string, matches []string) {
utils.StableSort(matches, func(a, b string) bool {
utils.StableSort(matches, func(a, b string) int {
la, lb := utils.LevenshteinDistance(a, q, true), utils.LevenshteinDistance(b, q, true)
if la != lb {
return la < lb
return la - lb
}
return a < b
return strings.Compare(a, b)
})
}

View File

@@ -15,7 +15,6 @@ import (
"fmt"
"hash"
"io"
"os"
"strconv"
"github.com/zeebo/xxh3"
@@ -291,7 +290,7 @@ func (r *rsync) ApplyDelta(output io.Writer, target io.ReadSeeker, op Operation)
return err
}
write_block := func(op Operation) (err error) {
if _, err = target.Seek(int64(r.BlockSize*int(op.BlockIndex)), os.SEEK_SET); err != nil {
if _, err = target.Seek(int64(r.BlockSize*int(op.BlockIndex)), io.SeekStart); err != nil {
return err
}
n, err = io.ReadAtLeast(target, buffer, r.BlockSize)

View File

@@ -909,7 +909,15 @@ func (self *Themes) ThemeByName(name string) *Theme {
func match(expression string, items []string) []*subseq.Match {
matches := subseq.ScoreItems(expression, items, subseq.Options{Level1: " "})
matches = utils.StableSort(matches, func(a, b *subseq.Match) bool { return a.Score > b.Score })
matches = utils.StableSort(matches, func(a, b *subseq.Match) int {
if b.Score < a.Score {
return -1
}
if b.Score > a.Score {
return 1
}
return 0
})
return matches
}

View File

@@ -81,5 +81,5 @@ func (self *Loop) dispatch_timers(now time.Time) error {
}
func (self *Loop) sort_timers() {
slices.SortStableFunc(self.timers, func(a, b *timer) bool { return a.deadline.Before(b.deadline) })
slices.SortStableFunc(self.timers, func(a, b *timer) int { return a.deadline.Compare(b.deadline) })
}

View File

@@ -90,8 +90,8 @@ func (self *History) merge_items(items ...HistoryItem) {
if !changed {
return
}
self.items = utils.StableSort(self.items, func(a, b HistoryItem) bool {
return a.Timestamp.Before(b.Timestamp)
self.items = utils.StableSort(self.items, func(a, b HistoryItem) int {
return a.Timestamp.Compare(b.Timestamp)
})
if len(self.items) > self.max_items {
self.items = self.items[len(self.items)-self.max_items:]

View File

@@ -447,7 +447,7 @@ func InsertFormatting(text string, spans ...*Span) string {
var in_span *Span
ans := make([]byte, 0, 2*len(text))
var overall_sgr_state SGR
slices.SortFunc(spans, func(a, b *Span) bool { return a.Offset < b.Offset })
slices.SortFunc(spans, func(a, b *Span) int { return a.Offset - b.Offset })
text_len := 0
var ep *wcswidth.EscapeCodeParser

View File

@@ -21,7 +21,15 @@ func TestSubseq(t *testing.T) {
simple := func(items, query string, expected ...string) {
matches := ScoreItems(query, utils.Splitlines(items), Options{})
if sort_by_score {
matches = utils.StableSort(matches, func(a, b *Match) bool { return a.Score > b.Score })
matches = utils.StableSort(matches, func(a, b *Match) int {
if b.Score < a.Score {
return -1
}
if b.Score > a.Score {
return 1
}
return 0
})
}
actual := make([]string, 0, len(matches))
actual_positions := make([][]int, 0, len(matches))

View File

@@ -18,9 +18,9 @@ func TestUnicodeInputQueries(t *testing.T) {
if expected == nil {
expected = make([]rune, 0)
}
expected = utils.Sort(expected, func(a, b rune) bool { return a < b })
expected = utils.Sort(expected, func(a, b rune) int { return int(a) - int(b) })
actual := CodePointsForQuery(query)
actual = utils.Sort(actual, func(a, b rune) bool { return a < b })
actual = utils.Sort(actual, func(a, b rune) int { return int(a) - int(b) })
diff := cmp.Diff(expected, actual)
if diff != "" {
t.Fatalf("Failed query: %#v\n%s", query, diff)

View File

@@ -244,7 +244,7 @@ func OpenNativeImageFromReader(f io.ReadSeeker) (ans *ImageData, err error) {
if err != nil {
return nil, err
}
f.Seek(0, os.SEEK_SET)
f.Seek(0, io.SeekStart)
ans = &ImageData{Width: c.Width, Height: c.Height, Format_uppercase: strings.ToUpper(fmt)}
if ans.Format_uppercase == "GIF" {
@@ -563,7 +563,7 @@ func RenderWithMagick(path string, ro *RenderOptions, frames []IdentifyRecord) (
err = fmt.Errorf("Failed to render %d out of %d frames", len(frames)-len(ans), len(frames))
return
}
slices.SortFunc(ans, func(a, b *ImageFrame) bool { return a.Number < b.Number })
slices.SortFunc(ans, func(a, b *ImageFrame) int { return a.Number - b.Number })
anchor_frame := 1
for i, frame := range ans {
anchor_frame, frame.Compose_onto = SetGIFFrameDisposal(frame.Number, anchor_frame, byte(frames[i].Disposal))

View File

@@ -79,13 +79,13 @@ func Repeat[T any](x T, n int) []T {
return ans
}
func Sort[T any](s []T, less func(a, b T) bool) []T {
slices.SortFunc(s, less)
func Sort[T any](s []T, cmp func(a, b T) int) []T {
slices.SortFunc(s, cmp)
return s
}
func StableSort[T any](s []T, less func(a, b T) bool) []T {
slices.SortStableFunc(s, less)
func StableSort[T any](s []T, cmp func(a, b T) int) []T {
slices.SortStableFunc(s, cmp)
return s
}
@@ -98,10 +98,19 @@ func sort_with_key[T any, C constraints.Ordered](stable bool, s []T, key func(a
for i, x := range s {
temp[i].val, temp[i].key = x, key(x)
}
cmp := func(a, b t) int {
if a.key < b.key {
return -1
}
if a.key > b.key {
return 1
}
return 0
}
if stable {
slices.SortStableFunc(temp, func(a, b t) bool { return a.key < b.key })
slices.SortStableFunc(temp, cmp)
} else {
slices.SortFunc(temp, func(a, b t) bool { return a.key < b.key })
slices.SortFunc(temp, cmp)
}
for i, x := range temp {
s[i] = x.val

View File

@@ -48,9 +48,9 @@ func (self *file_based_mmap) Seek(offset int64, whence int) (ret int64, err erro
switch whence {
case io.SeekStart:
self.pos = offset
case os.SEEK_END:
case io.SeekEnd:
self.pos = int64(len(self.region)) + offset
case os.SEEK_CUR:
case io.SeekCurrent:
self.pos += offset
}
return self.pos, nil

View File

@@ -123,9 +123,9 @@ func (self *syscall_based_mmap) Seek(offset int64, whence int) (ret int64, err e
switch whence {
case io.SeekStart:
self.pos = offset
case os.SEEK_END:
case io.SeekEnd:
self.pos = int64(len(self.region)) + offset
case os.SEEK_CUR:
case io.SeekCurrent:
self.pos += offset
}
return self.pos, nil