github.com/jxskiss/gopkg@v0.17.3/easy/sort_gen.go (about) 1 // Code generated by go generate at 2021-05-13T13:03:38+08:00; DO NOT EDIT. 2 3 package easy 4 5 //go:generate go run sort_template.go 6 7 import "sort" 8 9 // Int8sAsc attaches the methods of sort.Interface to []int8, sorting in ascending order. 10 type Int8sAsc []int8 11 12 func (x Int8sAsc) Len() int { return len(x) } 13 func (x Int8sAsc) Less(i, j int) bool { return x[i] < x[j] } 14 func (x Int8sAsc) Swap(i, j int) { x[i], x[j] = x[j], x[i] } 15 16 // Sort is a convenience method which calls sort.Sort(x). 17 func (x Int8sAsc) Sort() { sort.Sort(x) } 18 19 // Int8sDesc attaches the methods of sort.Interface to []int8, sorting in descending order. 20 type Int8sDesc []int8 21 22 func (x Int8sDesc) Len() int { return len(x) } 23 func (x Int8sDesc) Less(i, j int) bool { return x[i] > x[j] } 24 func (x Int8sDesc) Swap(i, j int) { x[i], x[j] = x[j], x[i] } 25 26 // Sort is a convenience method which calls sort.Sort(x). 27 func (x Int8sDesc) Sort() { sort.Sort(x) } 28 29 // SortInt8sAsc is a convenience method which calls sort.Sort(Int8sAsc(x)). 30 func SortInt8sAsc(x []int8) { sort.Sort(Int8sAsc(x)) } 31 32 // SortInt8sDesc is a convenience method which calls sort.Sort(Int8sDesc(x)). 33 func SortInt8sDesc(x []int8) { sort.Sort(Int8sDesc(x)) } 34 35 // Int8sAreSortedAsc is a convenience method which calls sort.IsSorted(Int8sAsc(x)). 36 func Int8sAreSortedAsc(x []int8) bool { return sort.IsSorted(Int8sAsc(x)) } 37 38 // Int8sAreSortedDesc is a convenience method which calls sort.IsSorted(Int8sDesc(x)). 39 func Int8sAreSortedDesc(x []int8) bool { return sort.IsSorted(Int8sDesc(x)) } 40 41 // InSortedInt8s tells whether elem is in a sorted int8 slice. 42 // The given slice must be sorted in either ascending order or 43 // descending, else it may give incorrect result. 44 func InSortedInt8s(slice []int8, elem int8) bool { 45 length := len(slice) 46 if length == 0 { 47 return false 48 } 49 if length == 1 || slice[0] == slice[length-1] { 50 return slice[0] == elem 51 } 52 53 var less func(i int) bool 54 if slice[0] <= slice[length-1] { 55 // ascending order 56 less = func(i int) bool { return slice[i] >= elem } 57 } else { 58 // descending order 59 less = func(i int) bool { return slice[i] <= elem } 60 } 61 i := sort.Search(length, less) 62 return i < len(slice) && slice[i] == elem 63 } 64 65 // Uint8sAsc attaches the methods of sort.Interface to []uint8, sorting in ascending order. 66 type Uint8sAsc []uint8 67 68 func (x Uint8sAsc) Len() int { return len(x) } 69 func (x Uint8sAsc) Less(i, j int) bool { return x[i] < x[j] } 70 func (x Uint8sAsc) Swap(i, j int) { x[i], x[j] = x[j], x[i] } 71 72 // Sort is a convenience method which calls sort.Sort(x). 73 func (x Uint8sAsc) Sort() { sort.Sort(x) } 74 75 // Uint8sDesc attaches the methods of sort.Interface to []uint8, sorting in descending order. 76 type Uint8sDesc []uint8 77 78 func (x Uint8sDesc) Len() int { return len(x) } 79 func (x Uint8sDesc) Less(i, j int) bool { return x[i] > x[j] } 80 func (x Uint8sDesc) Swap(i, j int) { x[i], x[j] = x[j], x[i] } 81 82 // Sort is a convenience method which calls sort.Sort(x). 83 func (x Uint8sDesc) Sort() { sort.Sort(x) } 84 85 // SortUint8sAsc is a convenience method which calls sort.Sort(Uint8sAsc(x)). 86 func SortUint8sAsc(x []uint8) { sort.Sort(Uint8sAsc(x)) } 87 88 // SortUint8sDesc is a convenience method which calls sort.Sort(Uint8sDesc(x)). 89 func SortUint8sDesc(x []uint8) { sort.Sort(Uint8sDesc(x)) } 90 91 // Uint8sAreSortedAsc is a convenience method which calls sort.IsSorted(Uint8sAsc(x)). 92 func Uint8sAreSortedAsc(x []uint8) bool { return sort.IsSorted(Uint8sAsc(x)) } 93 94 // Uint8sAreSortedDesc is a convenience method which calls sort.IsSorted(Uint8sDesc(x)). 95 func Uint8sAreSortedDesc(x []uint8) bool { return sort.IsSorted(Uint8sDesc(x)) } 96 97 // InSortedUint8s tells whether elem is in a sorted uint8 slice. 98 // The given slice must be sorted in either ascending order or 99 // descending, else it may give incorrect result. 100 func InSortedUint8s(slice []uint8, elem uint8) bool { 101 length := len(slice) 102 if length == 0 { 103 return false 104 } 105 if length == 1 || slice[0] == slice[length-1] { 106 return slice[0] == elem 107 } 108 109 var less func(i int) bool 110 if slice[0] <= slice[length-1] { 111 // ascending order 112 less = func(i int) bool { return slice[i] >= elem } 113 } else { 114 // descending order 115 less = func(i int) bool { return slice[i] <= elem } 116 } 117 i := sort.Search(length, less) 118 return i < len(slice) && slice[i] == elem 119 } 120 121 // Int16sAsc attaches the methods of sort.Interface to []int16, sorting in ascending order. 122 type Int16sAsc []int16 123 124 func (x Int16sAsc) Len() int { return len(x) } 125 func (x Int16sAsc) Less(i, j int) bool { return x[i] < x[j] } 126 func (x Int16sAsc) Swap(i, j int) { x[i], x[j] = x[j], x[i] } 127 128 // Sort is a convenience method which calls sort.Sort(x). 129 func (x Int16sAsc) Sort() { sort.Sort(x) } 130 131 // Int16sDesc attaches the methods of sort.Interface to []int16, sorting in descending order. 132 type Int16sDesc []int16 133 134 func (x Int16sDesc) Len() int { return len(x) } 135 func (x Int16sDesc) Less(i, j int) bool { return x[i] > x[j] } 136 func (x Int16sDesc) Swap(i, j int) { x[i], x[j] = x[j], x[i] } 137 138 // Sort is a convenience method which calls sort.Sort(x). 139 func (x Int16sDesc) Sort() { sort.Sort(x) } 140 141 // SortInt16sAsc is a convenience method which calls sort.Sort(Int16sAsc(x)). 142 func SortInt16sAsc(x []int16) { sort.Sort(Int16sAsc(x)) } 143 144 // SortInt16sDesc is a convenience method which calls sort.Sort(Int16sDesc(x)). 145 func SortInt16sDesc(x []int16) { sort.Sort(Int16sDesc(x)) } 146 147 // Int16sAreSortedAsc is a convenience method which calls sort.IsSorted(Int16sAsc(x)). 148 func Int16sAreSortedAsc(x []int16) bool { return sort.IsSorted(Int16sAsc(x)) } 149 150 // Int16sAreSortedDesc is a convenience method which calls sort.IsSorted(Int16sDesc(x)). 151 func Int16sAreSortedDesc(x []int16) bool { return sort.IsSorted(Int16sDesc(x)) } 152 153 // InSortedInt16s tells whether elem is in a sorted int16 slice. 154 // The given slice must be sorted in either ascending order or 155 // descending, else it may give incorrect result. 156 func InSortedInt16s(slice []int16, elem int16) bool { 157 length := len(slice) 158 if length == 0 { 159 return false 160 } 161 if length == 1 || slice[0] == slice[length-1] { 162 return slice[0] == elem 163 } 164 165 var less func(i int) bool 166 if slice[0] <= slice[length-1] { 167 // ascending order 168 less = func(i int) bool { return slice[i] >= elem } 169 } else { 170 // descending order 171 less = func(i int) bool { return slice[i] <= elem } 172 } 173 i := sort.Search(length, less) 174 return i < len(slice) && slice[i] == elem 175 } 176 177 // Uint16sAsc attaches the methods of sort.Interface to []uint16, sorting in ascending order. 178 type Uint16sAsc []uint16 179 180 func (x Uint16sAsc) Len() int { return len(x) } 181 func (x Uint16sAsc) Less(i, j int) bool { return x[i] < x[j] } 182 func (x Uint16sAsc) Swap(i, j int) { x[i], x[j] = x[j], x[i] } 183 184 // Sort is a convenience method which calls sort.Sort(x). 185 func (x Uint16sAsc) Sort() { sort.Sort(x) } 186 187 // Uint16sDesc attaches the methods of sort.Interface to []uint16, sorting in descending order. 188 type Uint16sDesc []uint16 189 190 func (x Uint16sDesc) Len() int { return len(x) } 191 func (x Uint16sDesc) Less(i, j int) bool { return x[i] > x[j] } 192 func (x Uint16sDesc) Swap(i, j int) { x[i], x[j] = x[j], x[i] } 193 194 // Sort is a convenience method which calls sort.Sort(x). 195 func (x Uint16sDesc) Sort() { sort.Sort(x) } 196 197 // SortUint16sAsc is a convenience method which calls sort.Sort(Uint16sAsc(x)). 198 func SortUint16sAsc(x []uint16) { sort.Sort(Uint16sAsc(x)) } 199 200 // SortUint16sDesc is a convenience method which calls sort.Sort(Uint16sDesc(x)). 201 func SortUint16sDesc(x []uint16) { sort.Sort(Uint16sDesc(x)) } 202 203 // Uint16sAreSortedAsc is a convenience method which calls sort.IsSorted(Uint16sAsc(x)). 204 func Uint16sAreSortedAsc(x []uint16) bool { return sort.IsSorted(Uint16sAsc(x)) } 205 206 // Uint16sAreSortedDesc is a convenience method which calls sort.IsSorted(Uint16sDesc(x)). 207 func Uint16sAreSortedDesc(x []uint16) bool { return sort.IsSorted(Uint16sDesc(x)) } 208 209 // InSortedUint16s tells whether elem is in a sorted uint16 slice. 210 // The given slice must be sorted in either ascending order or 211 // descending, else it may give incorrect result. 212 func InSortedUint16s(slice []uint16, elem uint16) bool { 213 length := len(slice) 214 if length == 0 { 215 return false 216 } 217 if length == 1 || slice[0] == slice[length-1] { 218 return slice[0] == elem 219 } 220 221 var less func(i int) bool 222 if slice[0] <= slice[length-1] { 223 // ascending order 224 less = func(i int) bool { return slice[i] >= elem } 225 } else { 226 // descending order 227 less = func(i int) bool { return slice[i] <= elem } 228 } 229 i := sort.Search(length, less) 230 return i < len(slice) && slice[i] == elem 231 } 232 233 // Int32sAsc attaches the methods of sort.Interface to []int32, sorting in ascending order. 234 type Int32sAsc []int32 235 236 func (x Int32sAsc) Len() int { return len(x) } 237 func (x Int32sAsc) Less(i, j int) bool { return x[i] < x[j] } 238 func (x Int32sAsc) Swap(i, j int) { x[i], x[j] = x[j], x[i] } 239 240 // Sort is a convenience method which calls sort.Sort(x). 241 func (x Int32sAsc) Sort() { sort.Sort(x) } 242 243 // Int32sDesc attaches the methods of sort.Interface to []int32, sorting in descending order. 244 type Int32sDesc []int32 245 246 func (x Int32sDesc) Len() int { return len(x) } 247 func (x Int32sDesc) Less(i, j int) bool { return x[i] > x[j] } 248 func (x Int32sDesc) Swap(i, j int) { x[i], x[j] = x[j], x[i] } 249 250 // Sort is a convenience method which calls sort.Sort(x). 251 func (x Int32sDesc) Sort() { sort.Sort(x) } 252 253 // SortInt32sAsc is a convenience method which calls sort.Sort(Int32sAsc(x)). 254 func SortInt32sAsc(x []int32) { sort.Sort(Int32sAsc(x)) } 255 256 // SortInt32sDesc is a convenience method which calls sort.Sort(Int32sDesc(x)). 257 func SortInt32sDesc(x []int32) { sort.Sort(Int32sDesc(x)) } 258 259 // Int32sAreSortedAsc is a convenience method which calls sort.IsSorted(Int32sAsc(x)). 260 func Int32sAreSortedAsc(x []int32) bool { return sort.IsSorted(Int32sAsc(x)) } 261 262 // Int32sAreSortedDesc is a convenience method which calls sort.IsSorted(Int32sDesc(x)). 263 func Int32sAreSortedDesc(x []int32) bool { return sort.IsSorted(Int32sDesc(x)) } 264 265 // InSortedInt32s tells whether elem is in a sorted int32 slice. 266 // The given slice must be sorted in either ascending order or 267 // descending, else it may give incorrect result. 268 func InSortedInt32s(slice []int32, elem int32) bool { 269 length := len(slice) 270 if length == 0 { 271 return false 272 } 273 if length == 1 || slice[0] == slice[length-1] { 274 return slice[0] == elem 275 } 276 277 var less func(i int) bool 278 if slice[0] <= slice[length-1] { 279 // ascending order 280 less = func(i int) bool { return slice[i] >= elem } 281 } else { 282 // descending order 283 less = func(i int) bool { return slice[i] <= elem } 284 } 285 i := sort.Search(length, less) 286 return i < len(slice) && slice[i] == elem 287 } 288 289 // Uint32sAsc attaches the methods of sort.Interface to []uint32, sorting in ascending order. 290 type Uint32sAsc []uint32 291 292 func (x Uint32sAsc) Len() int { return len(x) } 293 func (x Uint32sAsc) Less(i, j int) bool { return x[i] < x[j] } 294 func (x Uint32sAsc) Swap(i, j int) { x[i], x[j] = x[j], x[i] } 295 296 // Sort is a convenience method which calls sort.Sort(x). 297 func (x Uint32sAsc) Sort() { sort.Sort(x) } 298 299 // Uint32sDesc attaches the methods of sort.Interface to []uint32, sorting in descending order. 300 type Uint32sDesc []uint32 301 302 func (x Uint32sDesc) Len() int { return len(x) } 303 func (x Uint32sDesc) Less(i, j int) bool { return x[i] > x[j] } 304 func (x Uint32sDesc) Swap(i, j int) { x[i], x[j] = x[j], x[i] } 305 306 // Sort is a convenience method which calls sort.Sort(x). 307 func (x Uint32sDesc) Sort() { sort.Sort(x) } 308 309 // SortUint32sAsc is a convenience method which calls sort.Sort(Uint32sAsc(x)). 310 func SortUint32sAsc(x []uint32) { sort.Sort(Uint32sAsc(x)) } 311 312 // SortUint32sDesc is a convenience method which calls sort.Sort(Uint32sDesc(x)). 313 func SortUint32sDesc(x []uint32) { sort.Sort(Uint32sDesc(x)) } 314 315 // Uint32sAreSortedAsc is a convenience method which calls sort.IsSorted(Uint32sAsc(x)). 316 func Uint32sAreSortedAsc(x []uint32) bool { return sort.IsSorted(Uint32sAsc(x)) } 317 318 // Uint32sAreSortedDesc is a convenience method which calls sort.IsSorted(Uint32sDesc(x)). 319 func Uint32sAreSortedDesc(x []uint32) bool { return sort.IsSorted(Uint32sDesc(x)) } 320 321 // InSortedUint32s tells whether elem is in a sorted uint32 slice. 322 // The given slice must be sorted in either ascending order or 323 // descending, else it may give incorrect result. 324 func InSortedUint32s(slice []uint32, elem uint32) bool { 325 length := len(slice) 326 if length == 0 { 327 return false 328 } 329 if length == 1 || slice[0] == slice[length-1] { 330 return slice[0] == elem 331 } 332 333 var less func(i int) bool 334 if slice[0] <= slice[length-1] { 335 // ascending order 336 less = func(i int) bool { return slice[i] >= elem } 337 } else { 338 // descending order 339 less = func(i int) bool { return slice[i] <= elem } 340 } 341 i := sort.Search(length, less) 342 return i < len(slice) && slice[i] == elem 343 } 344 345 // IntsAsc attaches the methods of sort.Interface to []int, sorting in ascending order. 346 type IntsAsc []int 347 348 func (x IntsAsc) Len() int { return len(x) } 349 func (x IntsAsc) Less(i, j int) bool { return x[i] < x[j] } 350 func (x IntsAsc) Swap(i, j int) { x[i], x[j] = x[j], x[i] } 351 352 // Sort is a convenience method which calls sort.Sort(x). 353 func (x IntsAsc) Sort() { sort.Sort(x) } 354 355 // IntsDesc attaches the methods of sort.Interface to []int, sorting in descending order. 356 type IntsDesc []int 357 358 func (x IntsDesc) Len() int { return len(x) } 359 func (x IntsDesc) Less(i, j int) bool { return x[i] > x[j] } 360 func (x IntsDesc) Swap(i, j int) { x[i], x[j] = x[j], x[i] } 361 362 // Sort is a convenience method which calls sort.Sort(x). 363 func (x IntsDesc) Sort() { sort.Sort(x) } 364 365 // SortIntsAsc is a convenience method which calls sort.Sort(IntsAsc(x)). 366 func SortIntsAsc(x []int) { sort.Sort(IntsAsc(x)) } 367 368 // SortIntsDesc is a convenience method which calls sort.Sort(IntsDesc(x)). 369 func SortIntsDesc(x []int) { sort.Sort(IntsDesc(x)) } 370 371 // IntsAreSortedAsc is a convenience method which calls sort.IsSorted(IntsAsc(x)). 372 func IntsAreSortedAsc(x []int) bool { return sort.IsSorted(IntsAsc(x)) } 373 374 // IntsAreSortedDesc is a convenience method which calls sort.IsSorted(IntsDesc(x)). 375 func IntsAreSortedDesc(x []int) bool { return sort.IsSorted(IntsDesc(x)) } 376 377 // InSortedInts tells whether elem is in a sorted int slice. 378 // The given slice must be sorted in either ascending order or 379 // descending, else it may give incorrect result. 380 func InSortedInts(slice []int, elem int) bool { 381 length := len(slice) 382 if length == 0 { 383 return false 384 } 385 if length == 1 || slice[0] == slice[length-1] { 386 return slice[0] == elem 387 } 388 389 var less func(i int) bool 390 if slice[0] <= slice[length-1] { 391 // ascending order 392 less = func(i int) bool { return slice[i] >= elem } 393 } else { 394 // descending order 395 less = func(i int) bool { return slice[i] <= elem } 396 } 397 i := sort.Search(length, less) 398 return i < len(slice) && slice[i] == elem 399 } 400 401 // UintsAsc attaches the methods of sort.Interface to []uint, sorting in ascending order. 402 type UintsAsc []uint 403 404 func (x UintsAsc) Len() int { return len(x) } 405 func (x UintsAsc) Less(i, j int) bool { return x[i] < x[j] } 406 func (x UintsAsc) Swap(i, j int) { x[i], x[j] = x[j], x[i] } 407 408 // Sort is a convenience method which calls sort.Sort(x). 409 func (x UintsAsc) Sort() { sort.Sort(x) } 410 411 // UintsDesc attaches the methods of sort.Interface to []uint, sorting in descending order. 412 type UintsDesc []uint 413 414 func (x UintsDesc) Len() int { return len(x) } 415 func (x UintsDesc) Less(i, j int) bool { return x[i] > x[j] } 416 func (x UintsDesc) Swap(i, j int) { x[i], x[j] = x[j], x[i] } 417 418 // Sort is a convenience method which calls sort.Sort(x). 419 func (x UintsDesc) Sort() { sort.Sort(x) } 420 421 // SortUintsAsc is a convenience method which calls sort.Sort(UintsAsc(x)). 422 func SortUintsAsc(x []uint) { sort.Sort(UintsAsc(x)) } 423 424 // SortUintsDesc is a convenience method which calls sort.Sort(UintsDesc(x)). 425 func SortUintsDesc(x []uint) { sort.Sort(UintsDesc(x)) } 426 427 // UintsAreSortedAsc is a convenience method which calls sort.IsSorted(UintsAsc(x)). 428 func UintsAreSortedAsc(x []uint) bool { return sort.IsSorted(UintsAsc(x)) } 429 430 // UintsAreSortedDesc is a convenience method which calls sort.IsSorted(UintsDesc(x)). 431 func UintsAreSortedDesc(x []uint) bool { return sort.IsSorted(UintsDesc(x)) } 432 433 // InSortedUints tells whether elem is in a sorted uint slice. 434 // The given slice must be sorted in either ascending order or 435 // descending, else it may give incorrect result. 436 func InSortedUints(slice []uint, elem uint) bool { 437 length := len(slice) 438 if length == 0 { 439 return false 440 } 441 if length == 1 || slice[0] == slice[length-1] { 442 return slice[0] == elem 443 } 444 445 var less func(i int) bool 446 if slice[0] <= slice[length-1] { 447 // ascending order 448 less = func(i int) bool { return slice[i] >= elem } 449 } else { 450 // descending order 451 less = func(i int) bool { return slice[i] <= elem } 452 } 453 i := sort.Search(length, less) 454 return i < len(slice) && slice[i] == elem 455 } 456 457 // UintptrsAsc attaches the methods of sort.Interface to []uintptr, sorting in ascending order. 458 type UintptrsAsc []uintptr 459 460 func (x UintptrsAsc) Len() int { return len(x) } 461 func (x UintptrsAsc) Less(i, j int) bool { return x[i] < x[j] } 462 func (x UintptrsAsc) Swap(i, j int) { x[i], x[j] = x[j], x[i] } 463 464 // Sort is a convenience method which calls sort.Sort(x). 465 func (x UintptrsAsc) Sort() { sort.Sort(x) } 466 467 // UintptrsDesc attaches the methods of sort.Interface to []uintptr, sorting in descending order. 468 type UintptrsDesc []uintptr 469 470 func (x UintptrsDesc) Len() int { return len(x) } 471 func (x UintptrsDesc) Less(i, j int) bool { return x[i] > x[j] } 472 func (x UintptrsDesc) Swap(i, j int) { x[i], x[j] = x[j], x[i] } 473 474 // Sort is a convenience method which calls sort.Sort(x). 475 func (x UintptrsDesc) Sort() { sort.Sort(x) } 476 477 // SortUintptrsAsc is a convenience method which calls sort.Sort(UintptrsAsc(x)). 478 func SortUintptrsAsc(x []uintptr) { sort.Sort(UintptrsAsc(x)) } 479 480 // SortUintptrsDesc is a convenience method which calls sort.Sort(UintptrsDesc(x)). 481 func SortUintptrsDesc(x []uintptr) { sort.Sort(UintptrsDesc(x)) } 482 483 // UintptrsAreSortedAsc is a convenience method which calls sort.IsSorted(UintptrsAsc(x)). 484 func UintptrsAreSortedAsc(x []uintptr) bool { return sort.IsSorted(UintptrsAsc(x)) } 485 486 // UintptrsAreSortedDesc is a convenience method which calls sort.IsSorted(UintptrsDesc(x)). 487 func UintptrsAreSortedDesc(x []uintptr) bool { return sort.IsSorted(UintptrsDesc(x)) } 488 489 // InSortedUintptrs tells whether elem is in a sorted uintptr slice. 490 // The given slice must be sorted in either ascending order or 491 // descending, else it may give incorrect result. 492 func InSortedUintptrs(slice []uintptr, elem uintptr) bool { 493 length := len(slice) 494 if length == 0 { 495 return false 496 } 497 if length == 1 || slice[0] == slice[length-1] { 498 return slice[0] == elem 499 } 500 501 var less func(i int) bool 502 if slice[0] <= slice[length-1] { 503 // ascending order 504 less = func(i int) bool { return slice[i] >= elem } 505 } else { 506 // descending order 507 less = func(i int) bool { return slice[i] <= elem } 508 } 509 i := sort.Search(length, less) 510 return i < len(slice) && slice[i] == elem 511 } 512 513 // Int64sAsc attaches the methods of sort.Interface to []int64, sorting in ascending order. 514 type Int64sAsc []int64 515 516 func (x Int64sAsc) Len() int { return len(x) } 517 func (x Int64sAsc) Less(i, j int) bool { return x[i] < x[j] } 518 func (x Int64sAsc) Swap(i, j int) { x[i], x[j] = x[j], x[i] } 519 520 // Sort is a convenience method which calls sort.Sort(x). 521 func (x Int64sAsc) Sort() { sort.Sort(x) } 522 523 // Int64sDesc attaches the methods of sort.Interface to []int64, sorting in descending order. 524 type Int64sDesc []int64 525 526 func (x Int64sDesc) Len() int { return len(x) } 527 func (x Int64sDesc) Less(i, j int) bool { return x[i] > x[j] } 528 func (x Int64sDesc) Swap(i, j int) { x[i], x[j] = x[j], x[i] } 529 530 // Sort is a convenience method which calls sort.Sort(x). 531 func (x Int64sDesc) Sort() { sort.Sort(x) } 532 533 // SortInt64sAsc is a convenience method which calls sort.Sort(Int64sAsc(x)). 534 func SortInt64sAsc(x []int64) { sort.Sort(Int64sAsc(x)) } 535 536 // SortInt64sDesc is a convenience method which calls sort.Sort(Int64sDesc(x)). 537 func SortInt64sDesc(x []int64) { sort.Sort(Int64sDesc(x)) } 538 539 // Int64sAreSortedAsc is a convenience method which calls sort.IsSorted(Int64sAsc(x)). 540 func Int64sAreSortedAsc(x []int64) bool { return sort.IsSorted(Int64sAsc(x)) } 541 542 // Int64sAreSortedDesc is a convenience method which calls sort.IsSorted(Int64sDesc(x)). 543 func Int64sAreSortedDesc(x []int64) bool { return sort.IsSorted(Int64sDesc(x)) } 544 545 // InSortedInt64s tells whether elem is in a sorted int64 slice. 546 // The given slice must be sorted in either ascending order or 547 // descending, else it may give incorrect result. 548 func InSortedInt64s(slice []int64, elem int64) bool { 549 length := len(slice) 550 if length == 0 { 551 return false 552 } 553 if length == 1 || slice[0] == slice[length-1] { 554 return slice[0] == elem 555 } 556 557 var less func(i int) bool 558 if slice[0] <= slice[length-1] { 559 // ascending order 560 less = func(i int) bool { return slice[i] >= elem } 561 } else { 562 // descending order 563 less = func(i int) bool { return slice[i] <= elem } 564 } 565 i := sort.Search(length, less) 566 return i < len(slice) && slice[i] == elem 567 } 568 569 // Uint64sAsc attaches the methods of sort.Interface to []uint64, sorting in ascending order. 570 type Uint64sAsc []uint64 571 572 func (x Uint64sAsc) Len() int { return len(x) } 573 func (x Uint64sAsc) Less(i, j int) bool { return x[i] < x[j] } 574 func (x Uint64sAsc) Swap(i, j int) { x[i], x[j] = x[j], x[i] } 575 576 // Sort is a convenience method which calls sort.Sort(x). 577 func (x Uint64sAsc) Sort() { sort.Sort(x) } 578 579 // Uint64sDesc attaches the methods of sort.Interface to []uint64, sorting in descending order. 580 type Uint64sDesc []uint64 581 582 func (x Uint64sDesc) Len() int { return len(x) } 583 func (x Uint64sDesc) Less(i, j int) bool { return x[i] > x[j] } 584 func (x Uint64sDesc) Swap(i, j int) { x[i], x[j] = x[j], x[i] } 585 586 // Sort is a convenience method which calls sort.Sort(x). 587 func (x Uint64sDesc) Sort() { sort.Sort(x) } 588 589 // SortUint64sAsc is a convenience method which calls sort.Sort(Uint64sAsc(x)). 590 func SortUint64sAsc(x []uint64) { sort.Sort(Uint64sAsc(x)) } 591 592 // SortUint64sDesc is a convenience method which calls sort.Sort(Uint64sDesc(x)). 593 func SortUint64sDesc(x []uint64) { sort.Sort(Uint64sDesc(x)) } 594 595 // Uint64sAreSortedAsc is a convenience method which calls sort.IsSorted(Uint64sAsc(x)). 596 func Uint64sAreSortedAsc(x []uint64) bool { return sort.IsSorted(Uint64sAsc(x)) } 597 598 // Uint64sAreSortedDesc is a convenience method which calls sort.IsSorted(Uint64sDesc(x)). 599 func Uint64sAreSortedDesc(x []uint64) bool { return sort.IsSorted(Uint64sDesc(x)) } 600 601 // InSortedUint64s tells whether elem is in a sorted uint64 slice. 602 // The given slice must be sorted in either ascending order or 603 // descending, else it may give incorrect result. 604 func InSortedUint64s(slice []uint64, elem uint64) bool { 605 length := len(slice) 606 if length == 0 { 607 return false 608 } 609 if length == 1 || slice[0] == slice[length-1] { 610 return slice[0] == elem 611 } 612 613 var less func(i int) bool 614 if slice[0] <= slice[length-1] { 615 // ascending order 616 less = func(i int) bool { return slice[i] >= elem } 617 } else { 618 // descending order 619 less = func(i int) bool { return slice[i] <= elem } 620 } 621 i := sort.Search(length, less) 622 return i < len(slice) && slice[i] == elem 623 } 624 625 // Float32sAsc attaches the methods of sort.Interface to []float32, sorting in ascending order. 626 type Float32sAsc []float32 627 628 func (x Float32sAsc) Len() int { return len(x) } 629 func (x Float32sAsc) Less(i, j int) bool { return x[i] < x[j] } 630 func (x Float32sAsc) Swap(i, j int) { x[i], x[j] = x[j], x[i] } 631 632 // Sort is a convenience method which calls sort.Sort(x). 633 func (x Float32sAsc) Sort() { sort.Sort(x) } 634 635 // Float32sDesc attaches the methods of sort.Interface to []float32, sorting in descending order. 636 type Float32sDesc []float32 637 638 func (x Float32sDesc) Len() int { return len(x) } 639 func (x Float32sDesc) Less(i, j int) bool { return x[i] > x[j] } 640 func (x Float32sDesc) Swap(i, j int) { x[i], x[j] = x[j], x[i] } 641 642 // Sort is a convenience method which calls sort.Sort(x). 643 func (x Float32sDesc) Sort() { sort.Sort(x) } 644 645 // SortFloat32sAsc is a convenience method which calls sort.Sort(Float32sAsc(x)). 646 func SortFloat32sAsc(x []float32) { sort.Sort(Float32sAsc(x)) } 647 648 // SortFloat32sDesc is a convenience method which calls sort.Sort(Float32sDesc(x)). 649 func SortFloat32sDesc(x []float32) { sort.Sort(Float32sDesc(x)) } 650 651 // Float32sAreSortedAsc is a convenience method which calls sort.IsSorted(Float32sAsc(x)). 652 func Float32sAreSortedAsc(x []float32) bool { return sort.IsSorted(Float32sAsc(x)) } 653 654 // Float32sAreSortedDesc is a convenience method which calls sort.IsSorted(Float32sDesc(x)). 655 func Float32sAreSortedDesc(x []float32) bool { return sort.IsSorted(Float32sDesc(x)) } 656 657 // InSortedFloat32s tells whether elem is in a sorted float32 slice. 658 // The given slice must be sorted in either ascending order or 659 // descending, else it may give incorrect result. 660 func InSortedFloat32s(slice []float32, elem float32) bool { 661 length := len(slice) 662 if length == 0 { 663 return false 664 } 665 if length == 1 || slice[0] == slice[length-1] { 666 return slice[0] == elem 667 } 668 669 var less func(i int) bool 670 if slice[0] <= slice[length-1] { 671 // ascending order 672 less = func(i int) bool { return slice[i] >= elem } 673 } else { 674 // descending order 675 less = func(i int) bool { return slice[i] <= elem } 676 } 677 i := sort.Search(length, less) 678 return i < len(slice) && slice[i] == elem 679 } 680 681 // Float64sAsc attaches the methods of sort.Interface to []float64, sorting in ascending order. 682 type Float64sAsc []float64 683 684 func (x Float64sAsc) Len() int { return len(x) } 685 func (x Float64sAsc) Less(i, j int) bool { return x[i] < x[j] } 686 func (x Float64sAsc) Swap(i, j int) { x[i], x[j] = x[j], x[i] } 687 688 // Sort is a convenience method which calls sort.Sort(x). 689 func (x Float64sAsc) Sort() { sort.Sort(x) } 690 691 // Float64sDesc attaches the methods of sort.Interface to []float64, sorting in descending order. 692 type Float64sDesc []float64 693 694 func (x Float64sDesc) Len() int { return len(x) } 695 func (x Float64sDesc) Less(i, j int) bool { return x[i] > x[j] } 696 func (x Float64sDesc) Swap(i, j int) { x[i], x[j] = x[j], x[i] } 697 698 // Sort is a convenience method which calls sort.Sort(x). 699 func (x Float64sDesc) Sort() { sort.Sort(x) } 700 701 // SortFloat64sAsc is a convenience method which calls sort.Sort(Float64sAsc(x)). 702 func SortFloat64sAsc(x []float64) { sort.Sort(Float64sAsc(x)) } 703 704 // SortFloat64sDesc is a convenience method which calls sort.Sort(Float64sDesc(x)). 705 func SortFloat64sDesc(x []float64) { sort.Sort(Float64sDesc(x)) } 706 707 // Float64sAreSortedAsc is a convenience method which calls sort.IsSorted(Float64sAsc(x)). 708 func Float64sAreSortedAsc(x []float64) bool { return sort.IsSorted(Float64sAsc(x)) } 709 710 // Float64sAreSortedDesc is a convenience method which calls sort.IsSorted(Float64sDesc(x)). 711 func Float64sAreSortedDesc(x []float64) bool { return sort.IsSorted(Float64sDesc(x)) } 712 713 // InSortedFloat64s tells whether elem is in a sorted float64 slice. 714 // The given slice must be sorted in either ascending order or 715 // descending, else it may give incorrect result. 716 func InSortedFloat64s(slice []float64, elem float64) bool { 717 length := len(slice) 718 if length == 0 { 719 return false 720 } 721 if length == 1 || slice[0] == slice[length-1] { 722 return slice[0] == elem 723 } 724 725 var less func(i int) bool 726 if slice[0] <= slice[length-1] { 727 // ascending order 728 less = func(i int) bool { return slice[i] >= elem } 729 } else { 730 // descending order 731 less = func(i int) bool { return slice[i] <= elem } 732 } 733 i := sort.Search(length, less) 734 return i < len(slice) && slice[i] == elem 735 } 736 737 // StringsAsc attaches the methods of sort.Interface to []string, sorting in ascending order. 738 type StringsAsc []string 739 740 func (x StringsAsc) Len() int { return len(x) } 741 func (x StringsAsc) Less(i, j int) bool { return x[i] < x[j] } 742 func (x StringsAsc) Swap(i, j int) { x[i], x[j] = x[j], x[i] } 743 744 // Sort is a convenience method which calls sort.Sort(x). 745 func (x StringsAsc) Sort() { sort.Sort(x) } 746 747 // StringsDesc attaches the methods of sort.Interface to []string, sorting in descending order. 748 type StringsDesc []string 749 750 func (x StringsDesc) Len() int { return len(x) } 751 func (x StringsDesc) Less(i, j int) bool { return x[i] > x[j] } 752 func (x StringsDesc) Swap(i, j int) { x[i], x[j] = x[j], x[i] } 753 754 // Sort is a convenience method which calls sort.Sort(x). 755 func (x StringsDesc) Sort() { sort.Sort(x) } 756 757 // SortStringsAsc is a convenience method which calls sort.Sort(StringsAsc(x)). 758 func SortStringsAsc(x []string) { sort.Sort(StringsAsc(x)) } 759 760 // SortStringsDesc is a convenience method which calls sort.Sort(StringsDesc(x)). 761 func SortStringsDesc(x []string) { sort.Sort(StringsDesc(x)) } 762 763 // StringsAreSortedAsc is a convenience method which calls sort.IsSorted(StringsAsc(x)). 764 func StringsAreSortedAsc(x []string) bool { return sort.IsSorted(StringsAsc(x)) } 765 766 // StringsAreSortedDesc is a convenience method which calls sort.IsSorted(StringsDesc(x)). 767 func StringsAreSortedDesc(x []string) bool { return sort.IsSorted(StringsDesc(x)) } 768 769 // InSortedStrings tells whether elem is in a sorted string slice. 770 // The given slice must be sorted in either ascending order or 771 // descending, else it may give incorrect result. 772 func InSortedStrings(slice []string, elem string) bool { 773 length := len(slice) 774 if length == 0 { 775 return false 776 } 777 if length == 1 || slice[0] == slice[length-1] { 778 return slice[0] == elem 779 } 780 781 var less func(i int) bool 782 if slice[0] <= slice[length-1] { 783 // ascending order 784 less = func(i int) bool { return slice[i] >= elem } 785 } else { 786 // descending order 787 less = func(i int) bool { return slice[i] <= elem } 788 } 789 i := sort.Search(length, less) 790 return i < len(slice) && slice[i] == elem 791 }