all: use cmp.Compare (#30958)

This commit is contained in:
gitglorythegreat 2025-01-02 21:06:47 +08:00 committed by GitHub
parent 0feb999d3f
commit 85ffbde427
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 10 additions and 35 deletions

View File

@ -17,6 +17,7 @@
package main package main
import ( import (
"cmp"
"context" "context"
"errors" "errors"
"fmt" "fmt"
@ -292,13 +293,7 @@ func sortChanges(changes []types.Change) {
if a.Action == b.Action { if a.Action == b.Action {
return strings.Compare(*a.ResourceRecordSet.Name, *b.ResourceRecordSet.Name) return strings.Compare(*a.ResourceRecordSet.Name, *b.ResourceRecordSet.Name)
} }
if score[string(a.Action)] < score[string(b.Action)] { return cmp.Compare(score[string(a.Action)], score[string(b.Action)])
return -1
}
if score[string(a.Action)] > score[string(b.Action)] {
return 1
}
return 0
}) })
} }

View File

@ -18,6 +18,7 @@ package main
import ( import (
"bytes" "bytes"
"cmp"
"encoding/json" "encoding/json"
"fmt" "fmt"
"os" "os"
@ -104,13 +105,7 @@ func (ns nodeSet) topN(n int) nodeSet {
byscore = append(byscore, v) byscore = append(byscore, v)
} }
slices.SortFunc(byscore, func(a, b nodeJSON) int { slices.SortFunc(byscore, func(a, b nodeJSON) int {
if a.Score > b.Score { return cmp.Compare(b.Score, a.Score)
return -1
}
if a.Score < b.Score {
return 1
}
return 0
}) })
result := make(nodeSet, n) result := make(nodeSet, n)
for _, v := range byscore[:n] { for _, v := range byscore[:n] {

View File

@ -18,6 +18,7 @@ package snapshot
import ( import (
"bytes" "bytes"
"cmp"
"fmt" "fmt"
"slices" "slices"
"sort" "sort"
@ -45,13 +46,7 @@ func (it *weightedIterator) Cmp(other *weightedIterator) int {
return 1 return 1
} }
// Same account/storage-slot in multiple layers, split by priority // Same account/storage-slot in multiple layers, split by priority
if it.priority < other.priority { return cmp.Compare(it.priority, other.priority)
return -1
}
if it.priority > other.priority {
return 1
}
return 0
} }
// fastIterator is a more optimized multi-layer iterator which maintains a // fastIterator is a more optimized multi-layer iterator which maintains a

View File

@ -17,6 +17,7 @@
package p2p package p2p
import ( import (
"cmp"
"fmt" "fmt"
"strings" "strings"
@ -81,13 +82,7 @@ func (cap Cap) String() string {
// Cmp defines the canonical sorting order of capabilities. // Cmp defines the canonical sorting order of capabilities.
func (cap Cap) Cmp(other Cap) int { func (cap Cap) Cmp(other Cap) int {
if cap.Name == other.Name { if cap.Name == other.Name {
if cap.Version < other.Version { return cmp.Compare(cap.Version, other.Version)
return -1
}
if cap.Version > other.Version {
return 1
}
return 0
} }
return strings.Compare(cap.Name, other.Name) return strings.Compare(cap.Name, other.Name)
} }

View File

@ -18,6 +18,7 @@ package pathdb
import ( import (
"bytes" "bytes"
"cmp"
"fmt" "fmt"
"slices" "slices"
"sort" "sort"
@ -45,13 +46,7 @@ func (it *weightedIterator) Cmp(other *weightedIterator) int {
return 1 return 1
} }
// Same account/storage-slot in multiple layers, split by priority // Same account/storage-slot in multiple layers, split by priority
if it.priority < other.priority { return cmp.Compare(it.priority, other.priority)
return -1
}
if it.priority > other.priority {
return 1
}
return 0
} }
// fastIterator is a more optimized multi-layer iterator which maintains a // fastIterator is a more optimized multi-layer iterator which maintains a