github.com/puellanivis/breton@v0.2.16/lib/sort/uint.go (about) 1 package sort 2 3 import ( 4 "math/bits" 5 "sort" 6 ) 7 8 // UintSlice attaches the methods of sort.Interface to []uint, sorting in increasing order. 9 type UintSlice []uint 10 11 // Len implements sort.Interface. 12 func (p UintSlice) Len() int { return len(p) } 13 14 // Less implements sort.Interface. 15 func (p UintSlice) Less(i, j int) bool { return p[i] < p[j] } 16 17 // Swap implements sort.Interface. 18 func (p UintSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] } 19 20 func cmpUint(x, y uint) int { 21 if x == y { 22 return 0 23 } 24 25 if x < y { 26 return -1 27 } 28 29 return +1 30 } 31 32 // Compare implements Comparer. 33 func (p UintSlice) Compare(i, j int) int { 34 return cmpUint(p[i], p[j]) 35 } 36 37 // CompareFunc implements Comparer. 38 func (p UintSlice) CompareFunc(x interface{}) func(int) int { 39 e := x.(uint) 40 return func(i int) int { 41 return cmpUint(p[i], e) 42 } 43 } 44 45 // RadixRange implements RadixInterface. 46 func (p UintSlice) RadixRange() (int, int) { 47 allBits := uint((1 << uint(uintMSB)) - 1) 48 var anyBits uint 49 for _, v := range p { 50 anyBits |= v 51 allBits &= v 52 } 53 bitMask := anyBits &^ allBits 54 55 end := uintMSB - bits.TrailingZeros(uint(bitMask)) 56 57 return bits.LeadingZeros(uint(bitMask)), end 58 } 59 60 // RadixFunc implements RadixInterface. 61 func (p UintSlice) RadixFunc(r int) RadixTest { 62 mask := uint(1) << uint(uintMSB-r) 63 return func(i int) bool { 64 return p[i]&mask != 0 65 } 66 } 67 68 // Sort is a convenience method. 69 func (p UintSlice) Sort() { radix(p) } 70 71 // Radix is a convenience method. 72 func (p UintSlice) Radix() { radix(p) } 73 74 // Search is a convenience method. 75 func (p UintSlice) Search(x uint) int { return SearchUints(p, x) } 76 77 // SearchFor is a convenience method. 78 func (p UintSlice) SearchFor(x interface{}) int { return SearchUints(p, x.(uint)) } 79 80 // Uints sorts a slice of uints in increasing order. 81 func Uints(a []uint) { radix(UintSlice(a)) } 82 83 // SearchUints searches for x in a sorted slice of uints and returns the index as specified by sort.Search. 84 // The return value is the index to insert x if x is not present (it could be len(a)). 85 // The slice must be sorted in ascending order. 86 func SearchUints(a []uint, x uint) int { 87 return sort.Search(len(a), func(i int) bool { return a[i] >= x }) 88 } 89 90 // UintsAreSorted tests whether a slice of uints is sorted in increasing order. 91 func UintsAreSorted(a []uint) bool { return sort.IsSorted(UintSlice(a)) } 92 93 // Uint64Slice attaches the methods of sort.Interface to []uint64, sorting in increasing order. 94 type Uint64Slice []uint64 95 96 // Len implements sort.Interface. 97 func (p Uint64Slice) Len() int { return len(p) } 98 99 // Less implements sort.Interface. 100 func (p Uint64Slice) Less(i, j int) bool { return p[i] < p[j] } 101 102 // Swap implements sort.Interface. 103 func (p Uint64Slice) Swap(i, j int) { p[i], p[j] = p[j], p[i] } 104 105 func cmpUint64(x, y uint64) int { 106 if x == y { 107 return 0 108 } 109 110 if x < y { 111 return -1 112 } 113 114 return +1 115 } 116 117 // Compare implements Comparer. 118 func (p Uint64Slice) Compare(i, j int) int { 119 return cmpUint64(p[i], p[j]) 120 } 121 122 // CompareFunc implements Comparer. 123 func (p Uint64Slice) CompareFunc(x interface{}) func(int) int { 124 e := x.(uint64) 125 return func(i int) int { 126 return cmpUint64(p[i], e) 127 } 128 } 129 130 // RadixRange implements RadixInterface. 131 func (p Uint64Slice) RadixRange() (int, int) { 132 allBits := uint64((1 << uint(63)) - 1) 133 var anyBits uint64 134 for _, v := range p { 135 anyBits |= v 136 allBits &= v 137 } 138 bitMask := anyBits &^ allBits 139 140 end := 63 - bits.TrailingZeros64(uint64(bitMask)) 141 142 return bits.LeadingZeros64(uint64(bitMask)), end 143 } 144 145 // RadixFunc implements RadixInterface. 146 func (p Uint64Slice) RadixFunc(r int) RadixTest { 147 mask := uint64(1) << uint(63-r) 148 return func(i int) bool { 149 return p[i]&mask != 0 150 } 151 } 152 153 // Sort is a convenience method. 154 func (p Uint64Slice) Sort() { radix(p) } 155 156 // Radix is a convenience method. 157 func (p Uint64Slice) Radix() { radix(p) } 158 159 // Search is a convenience method. 160 func (p Uint64Slice) Search(x uint64) int { return SearchUint64s(p, x) } 161 162 // SearchFor is a convenience method. 163 func (p Uint64Slice) SearchFor(x interface{}) int { return SearchUint64s(p, x.(uint64)) } 164 165 // Uint64s sorts a slice of uint64s in increasing order. 166 func Uint64s(a []uint64) { radix(Uint64Slice(a)) } 167 168 // SearchUint64s searches for x in a sorted slice of uint64s and returns the index as specified by sort.Search. 169 // The return value is the index to insert x if x is not present (it could be len(a)). 170 // The slice must be sorted in ascending order. 171 func SearchUint64s(a []uint64, x uint64) int { 172 return sort.Search(len(a), func(i int) bool { return a[i] >= x }) 173 } 174 175 // Uint64sAreSorted tests whether a slice of uint64s is sorted in increasing order. 176 func Uint64sAreSorted(a []uint64) bool { return sort.IsSorted(Uint64Slice(a)) } 177 178 // Uint32Slice attaches the methods of sort.Interface to []uint32, sorting in increasing order. 179 type Uint32Slice []uint32 180 181 // Len implements sort.Interface. 182 func (p Uint32Slice) Len() int { return len(p) } 183 184 // Less implements sort.Interface. 185 func (p Uint32Slice) Less(i, j int) bool { return p[i] < p[j] } 186 187 // Swap implements sort.Interface. 188 func (p Uint32Slice) Swap(i, j int) { p[i], p[j] = p[j], p[i] } 189 190 func cmpUint32(x, y uint32) int { 191 if x == y { 192 return 0 193 } 194 195 if x < y { 196 return -1 197 } 198 199 return +1 200 } 201 202 // Compare implements Comparer. 203 func (p Uint32Slice) Compare(i, j int) int { 204 return cmpUint32(p[i], p[j]) 205 } 206 207 // CompareFunc implements Comparer. 208 func (p Uint32Slice) CompareFunc(x interface{}) func(int) int { 209 e := x.(uint32) 210 return func(i int) int { 211 return cmpUint32(p[i], e) 212 } 213 } 214 215 // RadixRange implements RadixInterface. 216 func (p Uint32Slice) RadixRange() (int, int) { 217 allBits := uint32((1 << uint(31)) - 1) 218 var anyBits uint32 219 for _, v := range p { 220 anyBits |= v 221 allBits &= v 222 } 223 bitMask := anyBits &^ allBits 224 225 end := 31 - bits.TrailingZeros32(uint32(bitMask)) 226 227 return bits.LeadingZeros32(uint32(bitMask)), end 228 } 229 230 // RadixFunc implements RadixInterface. 231 func (p Uint32Slice) RadixFunc(r int) RadixTest { 232 mask := uint32(1) << uint(31-r) 233 return func(i int) bool { 234 return p[i]&mask != 0 235 } 236 } 237 238 // Sort is a convenience method. 239 func (p Uint32Slice) Sort() { radix(p) } 240 241 // Radix is a convenience method. 242 func (p Uint32Slice) Radix() { radix(p) } 243 244 // Search is a convenience method. 245 func (p Uint32Slice) Search(x uint32) int { return SearchUint32s(p, x) } 246 247 // SearchFor is a convenience method. 248 func (p Uint32Slice) SearchFor(x interface{}) int { return SearchUint32s(p, x.(uint32)) } 249 250 // Uint32s sorts a slice of uint32s in increasing order. 251 func Uint32s(a []uint32) { radix(Uint32Slice(a)) } 252 253 // SearchUint32s searches for x in a sorted slice of uint32s and returns the index as specified by sort.Search. 254 // The return value is the index to insert x if x is not present (it could be len(a)). 255 // The slice must be sorted in ascending order. 256 func SearchUint32s(a []uint32, x uint32) int { 257 return sort.Search(len(a), func(i int) bool { return a[i] >= x }) 258 } 259 260 // Uint32sAreSorted tests whether a slice of uint32s is sorted in increasing order. 261 func Uint32sAreSorted(a []uint32) bool { return sort.IsSorted(Uint32Slice(a)) } 262 263 // Uint16Slice attaches the methods of sort.Interface to []uint16, sorting in increasing order. 264 type Uint16Slice []uint16 265 266 // Len implements sort.Interface. 267 func (p Uint16Slice) Len() int { return len(p) } 268 269 // Less implements sort.Interface. 270 func (p Uint16Slice) Less(i, j int) bool { return p[i] < p[j] } 271 272 // Swap implements sort.Interface. 273 func (p Uint16Slice) Swap(i, j int) { p[i], p[j] = p[j], p[i] } 274 275 func cmpUint16(x, y uint16) int { 276 if x == y { 277 return 0 278 } 279 280 if x < y { 281 return -1 282 } 283 284 return +1 285 } 286 287 // Compare implements Comparer. 288 func (p Uint16Slice) Compare(i, j int) int { 289 return cmpUint16(p[i], p[j]) 290 } 291 292 // CompareFunc implements Comparer. 293 func (p Uint16Slice) CompareFunc(x interface{}) func(int) int { 294 e := x.(uint16) 295 return func(i int) int { 296 return cmpUint16(p[i], e) 297 } 298 } 299 300 // RadixRange implements RadixInterface. 301 func (p Uint16Slice) RadixRange() (int, int) { 302 allBits := uint16((1 << uint(15)) - 1) 303 var anyBits uint16 304 for _, v := range p { 305 anyBits |= v 306 allBits &= v 307 } 308 bitMask := anyBits &^ allBits 309 310 end := 15 - bits.TrailingZeros16(uint16(bitMask)) 311 312 return bits.LeadingZeros16(uint16(bitMask)), end 313 } 314 315 // RadixFunc implements RadixInterface. 316 func (p Uint16Slice) RadixFunc(r int) RadixTest { 317 mask := uint16(1) << uint(15-r) 318 return func(i int) bool { 319 return p[i]&mask != 0 320 } 321 } 322 323 // Sort is a convenience method. 324 func (p Uint16Slice) Sort() { radix(p) } 325 326 // Radix is a convenience method. 327 func (p Uint16Slice) Radix() { radix(p) } 328 329 // Search is a convenience method. 330 func (p Uint16Slice) Search(x uint16) int { return SearchUint16s(p, x) } 331 332 // SearchFor is a convenience method. 333 func (p Uint16Slice) SearchFor(x interface{}) int { return SearchUint16s(p, x.(uint16)) } 334 335 // Uint16s sorts a slice of uint16s in increasing order. 336 func Uint16s(a []uint16) { radix(Uint16Slice(a)) } 337 338 // SearchUint16s searches for x in a sorted slice of uint16s and returns the index as specified by sort.Search. 339 // The return value is the index to insert x if x is not present (it could be len(a)). 340 // The slice must be sorted in ascending order. 341 func SearchUint16s(a []uint16, x uint16) int { 342 return sort.Search(len(a), func(i int) bool { return a[i] >= x }) 343 } 344 345 // Uint16sAreSorted tests whether a slice of uint16s is sorted in increasing order. 346 func Uint16sAreSorted(a []uint16) bool { return sort.IsSorted(Uint16Slice(a)) } 347 348 // Uint8Slice attaches the methods of sort.Interface to []uint8, sorting in increasing order. 349 type Uint8Slice []uint8 350 351 // Len implements sort.Interface. 352 func (p Uint8Slice) Len() int { return len(p) } 353 354 // Less implements sort.Interface. 355 func (p Uint8Slice) Less(i, j int) bool { return p[i] < p[j] } 356 357 // Swap implements sort.Interface. 358 func (p Uint8Slice) Swap(i, j int) { p[i], p[j] = p[j], p[i] } 359 360 func cmpUint8(x, y uint8) int { 361 if x == y { 362 return 0 363 } 364 365 if x < y { 366 return -1 367 } 368 369 return +1 370 } 371 372 // Compare implements Comparer. 373 func (p Uint8Slice) Compare(i, j int) int { 374 return cmpUint8(p[i], p[j]) 375 } 376 377 // CompareFunc implements Comparer. 378 func (p Uint8Slice) CompareFunc(x interface{}) func(int) int { 379 e := x.(uint8) 380 return func(i int) int { 381 return cmpUint8(p[i], e) 382 } 383 } 384 385 // RadixRange implements RadixInterface. 386 func (p Uint8Slice) RadixRange() (int, int) { 387 allBits := uint8((1 << uint(7)) - 1) 388 var anyBits uint8 389 for _, v := range p { 390 anyBits |= v 391 allBits &= v 392 } 393 bitMask := anyBits &^ allBits 394 395 end := 7 - bits.TrailingZeros8(uint8(bitMask)) 396 397 return bits.LeadingZeros8(uint8(bitMask)), end 398 } 399 400 // RadixFunc implements RadixInterface. 401 func (p Uint8Slice) RadixFunc(r int) RadixTest { 402 mask := uint8(1) << uint(7-r) 403 return func(i int) bool { 404 return p[i]&mask != 0 405 } 406 } 407 408 // Sort is a convenience method. 409 func (p Uint8Slice) Sort() { radix(p) } 410 411 // Radix is a convenience method. 412 func (p Uint8Slice) Radix() { radix(p) } 413 414 // Search is a convenience method. 415 func (p Uint8Slice) Search(x uint8) int { return SearchUint8s(p, x) } 416 417 // SearchFor is a convenience method. 418 func (p Uint8Slice) SearchFor(x interface{}) int { return SearchUint8s(p, x.(uint8)) } 419 420 // Uint8s sorts a slice of uint8s in increasing order. 421 func Uint8s(a []uint8) { radix(Uint8Slice(a)) } 422 423 // SearchUint8s searches for x in a sorted slice of uint8s and returns the index as specified by sort.Search. 424 // The return value is the index to insert x if x is not present (it could be len(a)). 425 // The slice must be sorted in ascending order. 426 func SearchUint8s(a []uint8, x uint8) int { 427 return sort.Search(len(a), func(i int) bool { return a[i] >= x }) 428 } 429 430 // Uint8sAreSorted tests whether a slice of uint8s is sorted in increasing order. 431 func Uint8sAreSorted(a []uint8) bool { return sort.IsSorted(Uint8Slice(a)) }