-
Notifications
You must be signed in to change notification settings - Fork 660
/
sorting.go
98 lines (85 loc) · 2.02 KB
/
sorting.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package utils
import (
"bytes"
"golang.org/x/exp/constraints"
"golang.org/x/exp/slices"
"github.com/ava-labs/avalanchego/utils/hashing"
)
// TODO can we handle sorting where the Compare function relies on a codec?
type Sortable[T any] interface {
Compare(T) int
}
// Sorts the elements of [s].
func Sort[T Sortable[T]](s []T) {
slices.SortFunc(s, T.Compare)
}
// Sorts the elements of [s] based on their hashes.
func SortByHash[T ~[]byte](s []T) {
slices.SortFunc(s, func(i, j T) int {
iHash := hashing.ComputeHash256(i)
jHash := hashing.ComputeHash256(j)
return bytes.Compare(iHash, jHash)
})
}
// Returns true iff the elements in [s] are sorted.
func IsSortedBytes[T ~[]byte](s []T) bool {
for i := 0; i < len(s)-1; i++ {
if bytes.Compare(s[i], s[i+1]) == 1 {
return false
}
}
return true
}
// Returns true iff the elements in [s] are unique and sorted.
func IsSortedAndUnique[T Sortable[T]](s []T) bool {
for i := 0; i < len(s)-1; i++ {
if s[i].Compare(s[i+1]) >= 0 {
return false
}
}
return true
}
// Returns true iff the elements in [s] are unique and sorted.
func IsSortedAndUniqueOrdered[T constraints.Ordered](s []T) bool {
for i := 0; i < len(s)-1; i++ {
if s[i] >= s[i+1] {
return false
}
}
return true
}
// Returns true iff the elements in [s] are unique and sorted
// based by their hashes.
func IsSortedAndUniqueByHash[T ~[]byte](s []T) bool {
if len(s) <= 1 {
return true
}
rightHash := hashing.ComputeHash256(s[0])
for i := 1; i < len(s); i++ {
leftHash := rightHash
rightHash = hashing.ComputeHash256(s[i])
if bytes.Compare(leftHash, rightHash) != -1 {
return false
}
}
return true
}
// Compare returns
//
// -1 if x is less than y,
// 0 if x equals y,
// 1 if x is greater than y.
//
// TODO: Remove after updating to go1.21.
func Compare[T constraints.Ordered](x, y T) int {
switch {
case x < y:
return -1
case x > y:
return 1
default:
return 0
}
}