| 1 |
// Copyright 2009 The Go Authors. |
| 2 |
// Copyright 2015 Randall Farmer. |
| 3 |
// All rights reserved. |
| 4 |
|
| 5 |
// Use of this source code is governed by a BSD-style |
| 6 |
// license that can be found in the LICENSE file. |
| 7 |
|
| 8 |
// Package sortutil sorts and searches common slice types (and helps sort |
| 9 |
// floats). |
| 10 |
package sortutil |
| 11 |
|
| 12 |
import ( |
| 13 |
"bytes" |
| 14 |
"math" |
| 15 |
"sort" |
| 16 |
|
| 17 |
"github.com/twotwotwo/sorts" |
| 18 |
) |
| 19 |
|
| 20 |
// Float32Key generates a uint64 key from a float32. Use with Float32Less. |
| 21 |
func Float32Key(f float32) uint64 { |
| 22 |
b := uint64(math.Float32bits(f)) << 32 |
| 23 |
b ^= ^(b>>63 - 1) | (1 << 63) |
| 24 |
return b |
| 25 |
} |
| 26 |
|
| 27 |
// Float32Less compares float32s, treating NaN as greater than all numbers. |
| 28 |
func Float32Less(f, g float32) bool { |
| 29 |
return Float32Key(f) < Float32Key(g) |
| 30 |
} |
| 31 |
|
| 32 |
// Float64Key generates a uint64 key from a float64. Use with Float64Less. |
| 33 |
func Float64Key(f float64) uint64 { |
| 34 |
b := math.Float64bits(f) |
| 35 |
b ^= ^(b>>63 - 1) | (1 << 63) |
| 36 |
return b |
| 37 |
} |
| 38 |
|
| 39 |
// Float64Less compares float64s, treating NaN as greater than all numbers. |
| 40 |
func Float64Less(f, g float64) bool { |
| 41 |
return Float64Key(f) < Float64Key(g) |
| 42 |
} |
| 43 |
|
| 44 |
// IntSlice attaches the methods of Int64Interface to []int, sorting in increasing order. |
| 45 |
type IntSlice []int |
| 46 |
|
| 47 |
func (p IntSlice) Len() int { return len(p) } |
| 48 |
func (p IntSlice) Less(i, j int) bool { return p[i] < p[j] } |
| 49 |
func (p IntSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] } |
| 50 |
|
| 51 |
// Key produces a radix sort key for an integer item. |
| 52 |
func (p IntSlice) Key(i int) int64 { return int64(p[i]) } |
| 53 |
|
| 54 |
// Sort is a convenience method. |
| 55 |
func (p IntSlice) Sort() { sorts.ByInt64(p) } |
| 56 |
|
| 57 |
// Int32Slice attaches the methods of Uint64Interface to []int32, sorting in increasing order. |
| 58 |
type Int32Slice []int32 |
| 59 |
|
| 60 |
func (p Int32Slice) Len() int { return len(p) } |
| 61 |
func (p Int32Slice) Less(i, j int) bool { return p[i] < p[j] } |
| 62 |
func (p Int32Slice) Swap(i, j int) { p[i], p[j] = p[j], p[i] } |
| 63 |
|
| 64 |
// Key produces a radix sort key for an integer item. |
| 65 |
func (p Int32Slice) Key(i int) int64 { return int64(p[i]) } |
| 66 |
|
| 67 |
// Sort is a convenience method. |
| 68 |
func (p Int32Slice) Sort() { sorts.ByInt64(p) } |
| 69 |
|
| 70 |
// Int64Slice attaches the methods of Uint64Interface to []int64, sorting in increasing order. |
| 71 |
type Int64Slice []int64 |
| 72 |
|
| 73 |
func (p Int64Slice) Len() int { return len(p) } |
| 74 |
func (p Int64Slice) Less(i, j int) bool { return p[i] < p[j] } |
| 75 |
func (p Int64Slice) Swap(i, j int) { p[i], p[j] = p[j], p[i] } |
| 76 |
|
| 77 |
// Key produces a radix sort key for an integer item. |
| 78 |
func (p Int64Slice) Key(i int) int64 { return p[i] } |
| 79 |
|
| 80 |
// Sort is a convenience method. |
| 81 |
func (p Int64Slice) Sort() { sorts.ByInt64(p) } |
| 82 |
|
| 83 |
// UintSlice attaches the methods of Uint64Interface to []uint, sorting in increasing order. |
| 84 |
type UintSlice []uint |
| 85 |
|
| 86 |
func (p UintSlice) Len() int { return len(p) } |
| 87 |
func (p UintSlice) Less(i, j int) bool { return p[i] < p[j] } |
| 88 |
func (p UintSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] } |
| 89 |
|
| 90 |
// Key produces a radix sort key for an unsigned integer item. |
| 91 |
func (p UintSlice) Key(i int) uint64 { return uint64(p[i]) } |
| 92 |
|
| 93 |
// Sort is a convenience method. |
| 94 |
func (p UintSlice) Sort() { sorts.ByUint64(p) } |
| 95 |
|
| 96 |
// Uint32Slice attaches the methods of Uint64Interface to []int32, sorting in increasing order. |
| 97 |
type Uint32Slice []uint32 |
| 98 |
|
| 99 |
func (p Uint32Slice) Len() int { return len(p) } |
| 100 |
func (p Uint32Slice) Less(i, j int) bool { return p[i] < p[j] } |
| 101 |
func (p Uint32Slice) Swap(i, j int) { p[i], p[j] = p[j], p[i] } |
| 102 |
|
| 103 |
// Key produces a radix sort key for an unsigned integer item. |
| 104 |
func (p Uint32Slice) Key(i int) uint64 { return uint64(p[i]) } |
| 105 |
|
| 106 |
// Sort is a convenience method. |
| 107 |
func (p Uint32Slice) Sort() { sorts.ByUint64(p) } |
| 108 |
|
| 109 |
// Uint64Slice attaches the methods of Uint64Interface to []uint64, sorting in increasing order. |
| 110 |
type Uint64Slice []uint64 |
| 111 |
|
| 112 |
func (p Uint64Slice) Len() int { return len(p) } |
| 113 |
func (p Uint64Slice) Less(i, j int) bool { return p[i] < p[j] } |
| 114 |
func (p Uint64Slice) Swap(i, j int) { p[i], p[j] = p[j], p[i] } |
| 115 |
|
| 116 |
// Key produces a radix sort key for an unsigned integer item. |
| 117 |
func (p Uint64Slice) Key(i int) uint64 { return p[i] } |
| 118 |
|
| 119 |
// Sort is a convenience method. |
| 120 |
func (p Uint64Slice) Sort() { sorts.ByUint64(p) } |
| 121 |
|
| 122 |
// Float32Slice attaches the methods of Uint64Interface to []uint32, sorting in increasing order, NaNs last. |
| 123 |
type Float32Slice []float32 |
| 124 |
|
| 125 |
func (p Float32Slice) Len() int { return len(p) } |
| 126 |
func (p Float32Slice) Less(i, j int) bool { return Float32Less(p[i], p[j]) } |
| 127 |
func (p Float32Slice) Swap(i, j int) { p[i], p[j] = p[j], p[i] } |
| 128 |
|
| 129 |
// Key produces a radix sort key for a floating-point value. |
| 130 |
func (p Float32Slice) Key(i int) uint64 { return Float32Key(p[i]) } |
| 131 |
|
| 132 |
// Sort is a convenience method. |
| 133 |
func (p Float32Slice) Sort() { sorts.ByUint64(p) } |
| 134 |
|
| 135 |
// Float64Slice attaches the methods of Uint64Interface to []float64, sorting in increasing order, NaNs last. |
| 136 |
type Float64Slice []float64 |
| 137 |
|
| 138 |
func (p Float64Slice) Len() int { return len(p) } |
| 139 |
func (p Float64Slice) Less(i, j int) bool { return Float64Less(p[i], p[j]) } |
| 140 |
func (p Float64Slice) Swap(i, j int) { p[i], p[j] = p[j], p[i] } |
| 141 |
|
| 142 |
// Key produces a radix sort key for a floating-point value. |
| 143 |
func (p Float64Slice) Key(i int) uint64 { return Float64Key(p[i]) } |
| 144 |
|
| 145 |
// Sort is a convenience method. |
| 146 |
func (p Float64Slice) Sort() { sorts.ByUint64(p) } |
| 147 |
|
| 148 |
// StringSlice attaches the methods of StringInterface to []string, sorting in increasing order. |
| 149 |
type StringSlice []string |
| 150 |
|
| 151 |
func (p StringSlice) Len() int { return len(p) } |
| 152 |
func (p StringSlice) Less(i, j int) bool { return p[i] < p[j] } |
| 153 |
func (p StringSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] } |
| 154 |
|
| 155 |
// Key returns a string item in a collection. |
| 156 |
func (p StringSlice) Key(i int) string { return p[i] } |
| 157 |
|
| 158 |
// Sort is a convenience method. |
| 159 |
func (p StringSlice) Sort() { sorts.ByString(p) } |
| 160 |
|
| 161 |
// BytesSlice attaches the methods of BytesInterface to [][]byte, sorting in increasing order. |
| 162 |
type BytesSlice [][]byte |
| 163 |
|
| 164 |
func (p BytesSlice) Len() int { return len(p) } |
| 165 |
func (p BytesSlice) Less(i, j int) bool { return bytes.Compare(p[i], p[j]) == -1 } |
| 166 |
func (p BytesSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] } |
| 167 |
|
| 168 |
// Key returns a []byte item in a collection. |
| 169 |
func (p BytesSlice) Key(i int) []byte { return p[i] } |
| 170 |
|
| 171 |
// Sort is a convenience method. |
| 172 |
func (p BytesSlice) Sort() { sorts.ByBytes(p) } |
| 173 |
|
| 174 |
// Ints sorts a slice of ints in increasing order. |
| 175 |
func Ints(a []int) { IntSlice(a).Sort() } |
| 176 |
|
| 177 |
// Int32s sorts a slice of int32s in increasing order. |
| 178 |
func Int32s(a []int32) { Int32Slice(a).Sort() } |
| 179 |
|
| 180 |
// Int64s sorts a slice of int64s in increasing order. |
| 181 |
func Int64s(a []int64) { Int64Slice(a).Sort() } |
| 182 |
|
| 183 |
// Uints sorts a slice of ints in increasing order. |
| 184 |
func Uints(a []uint) { UintSlice(a).Sort() } |
| 185 |
|
| 186 |
// Uint32s sorts a slice of uint64s in increasing order. |
| 187 |
func Uint32s(a []uint32) { Uint32Slice(a).Sort() } |
| 188 |
|
| 189 |
// Uint64s sorts a slice of uint64s in increasing order. |
| 190 |
func Uint64s(a []uint64) { Uint64Slice(a).Sort() } |
| 191 |
|
| 192 |
// Float32s sorts a slice of uint64s in increasing order, NaNs last. |
| 193 |
func Float32s(a []float32) { Float32Slice(a).Sort() } |
| 194 |
|
| 195 |
// Float64s sorts a slice of uint64s in increasing order, NaNs last. |
| 196 |
func Float64s(a []float64) { Float64Slice(a).Sort() } |
| 197 |
|
| 198 |
// Strings sorts a slice of strings in increasing order. |
| 199 |
func Strings(a []string) { StringSlice(a).Sort() } |
| 200 |
|
| 201 |
// Bytes sorts a slice of byte slices in increasing order. |
| 202 |
func Bytes(a [][]byte) { BytesSlice(a).Sort() } |
| 203 |
|
| 204 |
// IntsAreSorted tests whether a slice of ints is sorted in increasing order. |
| 205 |
func IntsAreSorted(a []int) bool { return sort.IsSorted(IntSlice(a)) } |
| 206 |
|
| 207 |
// Int32sAreSorted tests whether a slice of int32s is sorted in increasing order. |
| 208 |
func Int32sAreSorted(a []int32) bool { return sort.IsSorted(Int32Slice(a)) } |
| 209 |
|
| 210 |
// Int64sAreSorted tests whether a slice of int64s is sorted in increasing order. |
| 211 |
func Int64sAreSorted(a []int64) bool { return sort.IsSorted(Int64Slice(a)) } |
| 212 |
|
| 213 |
// UintsAreSorted tests whether a slice of ints is sorted in increasing order. |
| 214 |
func UintsAreSorted(a []uint) bool { return sort.IsSorted(UintSlice(a)) } |
| 215 |
|
| 216 |
// Uint32sAreSorted tests whether a slice of uint32s is sorted in increasing order. |
| 217 |
func Uint32sAreSorted(a []uint32) bool { return sort.IsSorted(Uint32Slice(a)) } |
| 218 |
|
| 219 |
// Uint64sAreSorted tests whether a slice of uint64s is sorted in increasing order. |
| 220 |
func Uint64sAreSorted(a []uint64) bool { return sort.IsSorted(Uint64Slice(a)) } |
| 221 |
|
| 222 |
// Float32sAreSorted tests whether a slice of float32s is sorted in increasing order, NaNs last. |
| 223 |
func Float32sAreSorted(a []float32) bool { return sort.IsSorted(Float32Slice(a)) } |
| 224 |
|
| 225 |
// Float64sAreSorted tests whether a slice of float64s is sorted in increasing order, NaNs last. |
| 226 |
func Float64sAreSorted(a []float64) bool { return sort.IsSorted(Float64Slice(a)) } |
| 227 |
|
| 228 |
// StringsAreSorted tests whether a slice of strings is sorted in increasing order. |
| 229 |
func StringsAreSorted(a []string) bool { return sort.IsSorted(StringSlice(a)) } |
| 230 |
|
| 231 |
// BytesAreSorted tests whether a slice of byte slices is sorted in increasing order. |
| 232 |
func BytesAreSorted(a [][]byte) bool { return sort.IsSorted(BytesSlice(a)) } |
| 233 |
|
| 234 |
// SearchInts searches ints; read about sort.Search for more. |
| 235 |
func SearchInts(a []int, x int) int { |
| 236 |
return sort.Search(len(a), func(i int) bool { return a[i] >= x }) |
| 237 |
} |
| 238 |
|
| 239 |
// Search returns the result of applying SearchInts to the receiver and x. |
| 240 |
func (p IntSlice) Search(x int) int { return SearchInts(p, x) } |
| 241 |
|
| 242 |
// SearchInt32s searches int32s; read about sort.Search for more. |
| 243 |
func SearchInt32s(a []int32, x int32) int { |
| 244 |
return sort.Search(len(a), func(i int) bool { return a[i] >= x }) |
| 245 |
} |
| 246 |
|
| 247 |
// Search returns the result of applying SearchInt32s to the receiver and x. |
| 248 |
func (p Int32Slice) Search(x int32) int { return SearchInt32s(p, x) } |
| 249 |
|
| 250 |
// SearchInt64s searches int64s; read about sort.Search for more. |
| 251 |
func SearchInt64s(a []int64, x int64) int { |
| 252 |
return sort.Search(len(a), func(i int) bool { return a[i] >= x }) |
| 253 |
} |
| 254 |
|
| 255 |
// Search returns the result of applying SearchInt64s to the receiver and x. |
| 256 |
func (p Int64Slice) Search(x int64) int { return SearchInt64s(p, x) } |
| 257 |
|
| 258 |
// SearchUints searches uints; read about sort.Search for more. |
| 259 |
func SearchUints(a []uint, x uint) int { |
| 260 |
return sort.Search(len(a), func(i int) bool { return a[i] >= x }) |
| 261 |
} |
| 262 |
|
| 263 |
// Search returns the result of applying SearchUints to the receiver and x. |
| 264 |
func (p UintSlice) Search(x uint) int { return SearchUints(p, x) } |
| 265 |
|
| 266 |
// SearchUint32s searches uint32s; read about sort.Search for more. |
| 267 |
func SearchUint32s(a []uint32, x uint32) int { |
| 268 |
return sort.Search(len(a), func(i int) bool { return a[i] >= x }) |
| 269 |
} |
| 270 |
|
| 271 |
// Search returns the result of applying SearchUint32s to the receiver and x. |
| 272 |
func (p Uint32Slice) Search(x uint32) int { return SearchUint32s(p, x) } |
| 273 |
|
| 274 |
// SearchUint64s searches uint64s; read about sort.Search for more. |
| 275 |
func SearchUint64s(a []uint64, x uint64) int { |
| 276 |
return sort.Search(len(a), func(i int) bool { return a[i] >= x }) |
| 277 |
} |
| 278 |
|
| 279 |
// Search returns the result of applying SearchUint64s to the receiver and x. |
| 280 |
func (p Uint64Slice) Search(x uint64) int { return SearchUint64s(p, x) } |
| 281 |
|
| 282 |
// SearchFloat32s searches float32s; read about sort.Search for more. |
| 283 |
func SearchFloat32s(a []float32, x float32) int { |
| 284 |
return sort.Search(len(a), func(i int) bool { return Float32Key(a[i]) >= Float32Key(x) }) |
| 285 |
} |
| 286 |
|
| 287 |
// Search returns the result of applying SearchFloat32s to the receiver and x. |
| 288 |
func (p Float32Slice) Search(x float32) int { return SearchFloat32s(p, x) } |
| 289 |
|
| 290 |
// SearchFloat64s searches float64s; read about sort.Search for more. |
| 291 |
func SearchFloat64s(a []float64, x float64) int { |
| 292 |
return sort.Search(len(a), func(i int) bool { return Float64Key(a[i]) >= Float64Key(x) }) |
| 293 |
} |
| 294 |
|
| 295 |
// Search returns the result of applying SearchFloat64s to the receiver and x. |
| 296 |
func (p Float64Slice) Search(x float64) int { return SearchFloat64s(p, x) } |
| 297 |
|
| 298 |
// SearchStrings searches strings; read about sort.Search for more. |
| 299 |
func SearchStrings(a []string, x string) int { |
| 300 |
return sort.Search(len(a), func(i int) bool { return a[i] >= x }) |
| 301 |
} |
| 302 |
|
| 303 |
// Search returns the result of applying SearchStrings to the receiver and x. |
| 304 |
func (p StringSlice) Search(x string) int { return SearchStrings(p, x) } |
| 305 |
|
| 306 |
// SearchBytes searches []bytes; read about sort.Search for more. |
| 307 |
func SearchBytes(a [][]byte, x []byte) int { |
| 308 |
return sort.Search(len(a), func(i int) bool { return bytes.Compare(a[i], x) >= 0 }) |
| 309 |
} |
| 310 |
|
| 311 |
// Search returns the result of applying SearchBytes to the receiver and x. |
| 312 |
func (p BytesSlice) Search(x []byte) int { return SearchBytes(p, x) } |