Skip to content

Commit

Permalink
change exp slices pkg to std pkg and replace exp ordered with cmp ord…
Browse files Browse the repository at this point in the history
…ered
  • Loading branch information
poopoothegorilla committed Dec 1, 2023
1 parent 62df19c commit d4e8a30
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions utils/sorting.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ package utils

import (
"bytes"

"golang.org/x/exp/constraints"
"golang.org/x/exp/slices"
"cmp"

Check failure on line 8 in utils/sorting.go

View workflow job for this annotation

GitHub Actions / test_upgrade

package cmp is not in GOROOT (/opt/hostedtoolcache/go/1.20.11/x64/src/cmp)

Check failure on line 8 in utils/sorting.go

View workflow job for this annotation

GitHub Actions / test_e2e

package cmp is not in GOROOT (/opt/hostedtoolcache/go/1.20.11/x64/src/cmp)

Check failure on line 8 in utils/sorting.go

View workflow job for this annotation

GitHub Actions / test_e2e_persistent

package cmp is not in GOROOT (/opt/hostedtoolcache/go/1.20.11/x64/src/cmp)

Check failure on line 8 in utils/sorting.go

View workflow job for this annotation

GitHub Actions / unit_tests (ubuntu-20.04)

package cmp is not in GOROOT (/opt/hostedtoolcache/go/1.20.11/x64/src/cmp)

Check failure on line 8 in utils/sorting.go

View workflow job for this annotation

GitHub Actions / unit_tests (ubuntu-22.04)

package cmp is not in GOROOT (/opt/hostedtoolcache/go/1.20.11/x64/src/cmp)

Check failure on line 8 in utils/sorting.go

View workflow job for this annotation

GitHub Actions / unit_tests (self-hosted, linux, ARM64, focal)

package cmp is not in GOROOT (/opt/github-actions2/_work/_tool/go/1.20.10/arm64/src/cmp)

Check failure on line 8 in utils/sorting.go

View workflow job for this annotation

GitHub Actions / unit_tests (self-hosted, linux, ARM64, jammy)

package cmp is not in GOROOT (/opt/github-actions2/_work/_tool/go/1.20.10/arm64/src/cmp)
"slices"

Check failure on line 9 in utils/sorting.go

View workflow job for this annotation

GitHub Actions / test_upgrade

package slices is not in GOROOT (/opt/hostedtoolcache/go/1.20.11/x64/src/slices)

Check failure on line 9 in utils/sorting.go

View workflow job for this annotation

GitHub Actions / test_e2e

package slices is not in GOROOT (/opt/hostedtoolcache/go/1.20.11/x64/src/slices)

Check failure on line 9 in utils/sorting.go

View workflow job for this annotation

GitHub Actions / test_e2e_persistent

package slices is not in GOROOT (/opt/hostedtoolcache/go/1.20.11/x64/src/slices)

Check failure on line 9 in utils/sorting.go

View workflow job for this annotation

GitHub Actions / unit_tests (ubuntu-20.04)

package slices is not in GOROOT (/opt/hostedtoolcache/go/1.20.11/x64/src/slices)

Check failure on line 9 in utils/sorting.go

View workflow job for this annotation

GitHub Actions / unit_tests (ubuntu-22.04)

package slices is not in GOROOT (/opt/hostedtoolcache/go/1.20.11/x64/src/slices)

Check failure on line 9 in utils/sorting.go

View workflow job for this annotation

GitHub Actions / unit_tests (self-hosted, linux, ARM64, focal)

package slices is not in GOROOT (/opt/github-actions2/_work/_tool/go/1.20.10/arm64/src/slices)

Check failure on line 9 in utils/sorting.go

View workflow job for this annotation

GitHub Actions / unit_tests (self-hosted, linux, ARM64, jammy)

package slices is not in GOROOT (/opt/github-actions2/_work/_tool/go/1.20.10/arm64/src/slices)

"github.com/ava-labs/avalanchego/utils/hashing"
)
Expand All @@ -20,24 +19,29 @@ type Sortable[T any] interface {

// Sorts the elements of [s].
func Sort[T Sortable[T]](s []T) {
slices.SortFunc(s, T.Less)
slices.SortFunc(s, func(a, b T) int {
if a.Less(b) {
return -1
}
return 1
})
}

// Sorts the elements of [s] based on their hashes.
func SortByHash[T ~[]byte](s []T) {
slices.SortFunc(s, func(i, j T) bool {
slices.SortFunc(s, func(i, j T) int {
iHash := hashing.ComputeHash256(i)
jHash := hashing.ComputeHash256(j)
return bytes.Compare(iHash, jHash) == -1
return bytes.Compare(iHash, jHash)
})
}

// Sorts a 2D byte slice.
// Each byte slice is not sorted internally; the byte slices are sorted relative
// to one another.
func SortBytes[T ~[]byte](s []T) {
slices.SortFunc(s, func(i, j T) bool {
return bytes.Compare(i, j) == -1
slices.SortFunc(s, func(i, j T) int {
return bytes.Compare(i, j)
})
}

Expand All @@ -62,7 +66,7 @@ func IsSortedAndUnique[T Sortable[T]](s []T) bool {
}

// Returns true iff the elements in [s] are unique and sorted.
func IsSortedAndUniqueOrdered[T constraints.Ordered](s []T) bool {
func IsSortedAndUniqueOrdered[T cmp.Ordered](s []T) bool {
for i := 0; i < len(s)-1; i++ {
if s[i] >= s[i+1] {
return false
Expand Down

0 comments on commit d4e8a30

Please sign in to comment.