github.com/andeya/ameda@v1.5.3/uint64s.go (about) 1 package ameda 2 3 // OneUint64 try to return the first element, otherwise return zero value. 4 func OneUint64(u []uint64) uint64 { 5 if len(u) > 0 { 6 return u[0] 7 } 8 return 0 9 } 10 11 // Uint64sCopy creates a copy of the uint64 slice. 12 func Uint64sCopy(u []uint64) []uint64 { 13 b := make([]uint64, len(u)) 14 copy(b, u) 15 return b 16 } 17 18 // Uint64sToInterfaces converts uint64 slice to interface slice. 19 func Uint64sToInterfaces(u []uint64) []interface{} { 20 r := make([]interface{}, len(u)) 21 for k, v := range u { 22 r[k] = Uint64ToInterface(v) 23 } 24 return r 25 } 26 27 // Uint64sToStrings converts uint64 slice to string slice. 28 func Uint64sToStrings(u []uint64) []string { 29 r := make([]string, len(u)) 30 for k, v := range u { 31 r[k] = Uint64ToString(v) 32 } 33 return r 34 } 35 36 // Uint64sToBools converts uint64 slice to bool slice. 37 // NOTE: 38 // 39 // 0 is false, everything else is true 40 func Uint64sToBools(u []uint64) []bool { 41 r := make([]bool, len(u)) 42 for k, v := range u { 43 r[k] = Uint64ToBool(v) 44 } 45 return r 46 } 47 48 // Uint64sToFloat32s converts uint64 slice to float32 slice. 49 func Uint64sToFloat32s(u []uint64) []float32 { 50 r := make([]float32, len(u)) 51 for k, v := range u { 52 r[k] = Uint64ToFloat32(v) 53 } 54 return r 55 } 56 57 // Uint64sToFloat64s converts uint64 slice to float64 slice. 58 func Uint64sToFloat64s(u []uint64) []float64 { 59 r := make([]float64, len(u)) 60 for k, v := range u { 61 r[k] = Uint64ToFloat64(v) 62 } 63 return r 64 } 65 66 // Uint64sToInts converts uint64 slice to int slice. 67 func Uint64sToInts(u []uint64) []int { 68 r := make([]int, len(u)) 69 for k, v := range u { 70 r[k] = Uint64ToInt(v) 71 } 72 return r 73 } 74 75 // Uint64sToInt8s converts uint64 slice to int8 slice. 76 func Uint64sToInt8s(u []uint64) ([]int8, error) { 77 var err error 78 r := make([]int8, len(u)) 79 for k, v := range u { 80 r[k], err = Uint64ToInt8(v) 81 if err != nil { 82 return r, err 83 } 84 } 85 return r, nil 86 } 87 88 // Uint64sToInt16s converts uint64 slice to int16 slice. 89 func Uint64sToInt16s(u []uint64) ([]int16, error) { 90 var err error 91 r := make([]int16, len(u)) 92 for k, v := range u { 93 r[k], err = Uint64ToInt16(v) 94 if err != nil { 95 return r, err 96 } 97 } 98 return r, nil 99 } 100 101 // Uint64sToInt32s converts uint64 slice to int32 slice. 102 func Uint64sToInt32s(u []uint64) ([]int32, error) { 103 var err error 104 r := make([]int32, len(u)) 105 for k, v := range u { 106 r[k], err = Uint64ToInt32(v) 107 if err != nil { 108 return r, err 109 } 110 } 111 return r, nil 112 } 113 114 // Uint64sToInt64s converts uint64 slice to int64 slice. 115 func Uint64sToInt64s(u []uint64) ([]int64, error) { 116 var err error 117 r := make([]int64, len(u)) 118 for k, v := range u { 119 r[k], err = Uint64ToInt64(v) 120 if err != nil { 121 return r, err 122 } 123 } 124 return r, nil 125 } 126 127 // Uint64sToUints converts uint64 slice to uint slice. 128 func Uint64sToUints(u []uint64) ([]uint, error) { 129 var err error 130 r := make([]uint, len(u)) 131 for k, v := range u { 132 r[k], err = Uint64ToUint(v) 133 if err != nil { 134 return r, err 135 } 136 } 137 return r, nil 138 } 139 140 // Uint64sToUint8s converts uint64 slice to uint8 slice. 141 func Uint64sToUint8s(u []uint64) ([]uint8, error) { 142 var err error 143 r := make([]uint8, len(u)) 144 for k, v := range u { 145 r[k], err = Uint64ToUint8(v) 146 if err != nil { 147 return r, err 148 } 149 } 150 return r, nil 151 } 152 153 // Uint64sToUint16s converts uint64 slice to uint16 slice. 154 func Uint64sToUint16s(u []uint64) ([]uint16, error) { 155 var err error 156 r := make([]uint16, len(u)) 157 for k, v := range u { 158 r[k], err = Uint64ToUint16(v) 159 if err != nil { 160 return r, err 161 } 162 } 163 return r, nil 164 } 165 166 // Uint64sToUint32s converts uint64 slice to uint32 slice. 167 func Uint64sToUint32s(u []uint64) ([]uint32, error) { 168 var err error 169 r := make([]uint32, len(u)) 170 for k, v := range u { 171 r[k], err = Uint64ToUint32(v) 172 if err != nil { 173 return r, err 174 } 175 } 176 return r, nil 177 } 178 179 // Uint64sCopyWithin copies part of an slice to another location in the current slice. 180 // @target 181 // 182 // Zero-based index at which to copy the sequence to. If negative, target will be counted from the end. 183 // 184 // @start 185 // 186 // Zero-based index at which to start copying elements from. If negative, start will be counted from the end. 187 // 188 // @end 189 // 190 // Zero-based index at which to end copying elements from. CopyWithin copies up to but not including end. 191 // If negative, end will be counted from the end. 192 // If end is omitted, CopyWithin will copy until the last index (default to len(s)). 193 func Uint64sCopyWithin(u []uint64, target, start int, end ...int) { 194 target = fixIndex(len(u), target, true) 195 if target == len(u) { 196 return 197 } 198 sub := Uint64sSlice(u, start, end...) 199 for k, v := range sub { 200 u[target+k] = v 201 } 202 } 203 204 // Uint64sEvery tests whether all elements in the slice pass the test implemented by the provided function. 205 // NOTE: 206 // 207 // Calling this method on an empty slice will return true for any condition! 208 func Uint64sEvery(u []uint64, fn func(u []uint64, k int, v uint64) bool) bool { 209 for k, v := range u { 210 if !fn(u, k, v) { 211 return false 212 } 213 } 214 return true 215 } 216 217 // Uint64sFill changes all elements in the current slice to a value, from a start index to an end index. 218 // @value 219 // 220 // Zero-based index at which to copy the sequence to. If negative, target will be counted from the end. 221 // 222 // @start 223 // 224 // Zero-based index at which to start copying elements from. If negative, start will be counted from the end. 225 // 226 // @end 227 // 228 // Zero-based index at which to end copying elements from. CopyWithin copies up to but not including end. 229 // If negative, end will be counted from the end. 230 // If end is omitted, CopyWithin will copy until the last index (default to len(s)). 231 func Uint64sFill(u []uint64, value uint64, start int, end ...int) { 232 fixedStart, fixedEnd, ok := fixRange(len(u), start, end...) 233 if !ok { 234 return 235 } 236 for k := fixedStart; k < fixedEnd; k++ { 237 u[k] = value 238 } 239 } 240 241 // Uint64sFilter creates a new slice with all elements that pass the test implemented by the provided function. 242 func Uint64sFilter(u []uint64, fn func(u []uint64, k int, v uint64) bool) []uint64 { 243 ret := make([]uint64, 0) 244 for k, v := range u { 245 if fn(u, k, v) { 246 ret = append(ret, v) 247 } 248 } 249 return ret 250 } 251 252 // Uint64sFind returns the key-value of the first element in the provided slice that satisfies the provided testing function. 253 // NOTE: 254 // 255 // If not found, k = -1 256 func Uint64sFind(u []uint64, fn func(u []uint64, k int, v uint64) bool) (k int, v uint64) { 257 for k, v := range u { 258 if fn(u, k, v) { 259 return k, v 260 } 261 } 262 return -1, 0 263 } 264 265 // Uint64sIncludes determines whether an slice includes a certain value among its entries. 266 // @fromIndex 267 // 268 // The index to start the search at. Defaults to 0. 269 func Uint64sIncludes(u []uint64, valueToFind uint64, fromIndex ...int) bool { 270 return Uint64sIndexOf(u, valueToFind, fromIndex...) > -1 271 } 272 273 // Uint64sIndexOf returns the first index at which a given element can be found in the slice, or -1 if it is not present. 274 // @fromIndex 275 // 276 // The index to start the search at. Defaults to 0. 277 func Uint64sIndexOf(u []uint64, searchElement uint64, fromIndex ...int) int { 278 idx := getFromIndex(len(u), fromIndex...) 279 for k, v := range u[idx:] { 280 if searchElement == v { 281 return k + idx 282 } 283 } 284 return -1 285 } 286 287 // Uint64sLastIndexOf returns the last index at which a given element can be found in the slice, or -1 if it is not present. 288 // @fromIndex 289 // 290 // The index to start the search at. Defaults to 0. 291 func Uint64sLastIndexOf(u []uint64, searchElement uint64, fromIndex ...int) int { 292 idx := getFromIndex(len(u), fromIndex...) 293 for k := len(u) - 1; k >= idx; k-- { 294 if searchElement == u[k] { 295 return k 296 } 297 } 298 return -1 299 } 300 301 // Uint64sMap creates a new slice populated with the results of calling a provided function 302 // on every element in the calling slice. 303 func Uint64sMap(u []uint64, fn func(u []uint64, k int, v uint64) uint64) []uint64 { 304 ret := make([]uint64, len(u)) 305 for k, v := range u { 306 ret[k] = fn(u, k, v) 307 } 308 return ret 309 } 310 311 // Uint64sPop removes the last element from an slice and returns that element. 312 // This method changes the length of the slice. 313 func Uint64sPop(u *[]uint64) (uint64, bool) { 314 a := *u 315 if len(a) == 0 { 316 return 0, false 317 } 318 lastIndex := len(a) - 1 319 last := a[lastIndex] 320 a = a[:lastIndex] 321 *u = a[:len(a):len(a)] 322 return last, true 323 } 324 325 // Uint64sPush adds one or more elements to the end of an slice and returns the new length of the slice. 326 func Uint64sPush(u *[]uint64, element ...uint64) int { 327 *u = append(*u, element...) 328 return len(*u) 329 } 330 331 // Uint64sPushDistinct adds one or more new elements that do not exist in the current slice at the end. 332 func Uint64sPushDistinct(u []uint64, element ...uint64) []uint64 { 333 L: 334 for _, v := range element { 335 for _, vv := range u { 336 if vv == v { 337 continue L 338 } 339 } 340 u = append(u, v) 341 } 342 return u 343 } 344 345 // Uint64sReduce executes a reducer function (that you provide) on each element of the slice, 346 // resulting in a single output value. 347 // @accumulator 348 // 349 // The accumulator accumulates callback's return values. 350 // It is the accumulated value previously returned in the last invocation of the callback—or initialValue, 351 // if it was supplied (see below). 352 // 353 // @initialValue 354 // 355 // A value to use as the first argument to the first call of the callback. 356 // If no initialValue is supplied, the first element in the slice will be used and skipped. 357 func Uint64sReduce( 358 u []uint64, 359 fn func(u []uint64, k int, v, accumulator uint64) uint64, initialValue ...uint64, 360 ) uint64 { 361 if len(u) == 0 { 362 return 0 363 } 364 start := 0 365 acc := u[start] 366 if len(initialValue) > 0 { 367 acc = initialValue[0] 368 } else { 369 start += 1 370 } 371 for k := start; k < len(u); k++ { 372 acc = fn(u, k, u[k], acc) 373 } 374 return acc 375 } 376 377 // Uint64sReduceRight applies a function against an accumulator and each value of the slice (from right-to-left) 378 // to reduce it to a single value. 379 // @accumulator 380 // 381 // The accumulator accumulates callback's return values. 382 // It is the accumulated value previously returned in the last invocation of the callback—or initialValue, 383 // if it was supplied (see below). 384 // 385 // @initialValue 386 // 387 // A value to use as the first argument to the first call of the callback. 388 // If no initialValue is supplied, the first element in the slice will be used and skipped. 389 func Uint64sReduceRight( 390 u []uint64, 391 fn func(u []uint64, k int, v, accumulator uint64) uint64, initialValue ...uint64, 392 ) uint64 { 393 if len(u) == 0 { 394 return 0 395 } 396 end := len(u) - 1 397 acc := u[end] 398 if len(initialValue) > 0 { 399 acc = initialValue[0] 400 } else { 401 end -= 1 402 } 403 for k := end; k >= 0; k-- { 404 acc = fn(u, k, u[k], acc) 405 } 406 return acc 407 } 408 409 // Uint64sReverse reverses an slice in place. 410 func Uint64sReverse(u []uint64) { 411 first := 0 412 last := len(u) - 1 413 for first < last { 414 u[first], u[last] = u[last], u[first] 415 first++ 416 last-- 417 } 418 } 419 420 // Uint64sShift removes the first element from an slice and returns that removed element. 421 // This method changes the length of the slice. 422 func Uint64sShift(u *[]uint64) (uint64, bool) { 423 a := *u 424 if len(a) == 0 { 425 return 0, false 426 } 427 first := a[0] 428 a = a[1:] 429 *u = a[:len(a):len(a)] 430 return first, true 431 } 432 433 // Uint64sSlice returns a copy of a portion of an slice into a new slice object selected 434 // from begin to end (end not included) where begin and end represent the index of items in that slice. 435 // The original slice will not be modified. 436 func Uint64sSlice(u []uint64, begin int, end ...int) []uint64 { 437 fixedStart, fixedEnd, ok := fixRange(len(u), begin, end...) 438 if !ok { 439 return []uint64{} 440 } 441 return Uint64sCopy(u[fixedStart:fixedEnd]) 442 } 443 444 // Uint64sSome tests whether at least one element in the slice passes the test implemented by the provided function. 445 // NOTE: 446 // 447 // Calling this method on an empty slice returns false for any condition! 448 func Uint64sSome(u []uint64, fn func(u []uint64, k int, v uint64) bool) bool { 449 for k, v := range u { 450 if fn(u, k, v) { 451 return true 452 } 453 } 454 return false 455 } 456 457 // Uint64sSplice changes the contents of an slice by removing or replacing 458 // existing elements and/or adding new elements in place. 459 func Uint64sSplice(u *[]uint64, start, deleteCount int, items ...uint64) { 460 a := *u 461 if deleteCount < 0 { 462 deleteCount = 0 463 } 464 start, end, _ := fixRange(len(a), start, start+1+deleteCount) 465 deleteCount = end - start - 1 466 for k := 0; k < len(items); k++ { 467 if deleteCount > 0 { 468 // replace 469 a[start] = items[k] 470 deleteCount-- 471 start++ 472 } else { 473 // insert 474 lastSlice := Uint64sCopy(a[start:]) 475 items = items[k:] 476 a = append(a[:start], items...) 477 a = append(a[:start+len(items)], lastSlice...) 478 *u = a[:len(a):len(a)] 479 return 480 } 481 } 482 if deleteCount > 0 { 483 a = append(a[:start], a[start+1+deleteCount:]...) 484 } 485 *u = a[:len(a):len(a)] 486 } 487 488 // Uint64sUnshift adds one or more elements to the beginning of an slice and returns the new length of the slice. 489 func Uint64sUnshift(u *[]uint64, element ...uint64) int { 490 *u = append(element, *u...) 491 return len(*u) 492 } 493 494 // Uint64sUnshiftDistinct adds one or more new elements that do not exist in the current slice to the beginning 495 // and returns the new length of the slice. 496 func Uint64sUnshiftDistinct(u *[]uint64, element ...uint64) int { 497 a := *u 498 if len(element) == 0 { 499 return len(a) 500 } 501 m := make(map[uint64]bool, len(element)) 502 r := make([]uint64, 0, len(a)+len(element)) 503 L: 504 for _, v := range element { 505 if m[v] { 506 continue 507 } 508 m[v] = true 509 for _, vv := range a { 510 if vv == v { 511 continue L 512 } 513 } 514 r = append(r, v) 515 } 516 r = append(r, a...) 517 *u = r[:len(r):len(r)] 518 return len(r) 519 } 520 521 // Uint64sRemoveFirst removes the first matched elements from the slice, 522 // and returns the new length of the slice. 523 func Uint64sRemoveFirst(p *[]uint64, elements ...uint64) int { 524 a := *p 525 m := make(map[interface{}]struct{}, len(elements)) 526 for _, element := range elements { 527 if _, ok := m[element]; ok { 528 continue 529 } 530 m[element] = struct{}{} 531 for k, v := range a { 532 if v == element { 533 a = append(a[:k], a[k+1:]...) 534 break 535 } 536 } 537 } 538 n := len(a) 539 *p = a[:n:n] 540 return n 541 } 542 543 // Uint64sRemoveEvery removes all the elements from the slice, 544 // and returns the new length of the slice. 545 func Uint64sRemoveEvery(p *[]uint64, elements ...uint64) int { 546 a := *p 547 m := make(map[interface{}]struct{}, len(elements)) 548 for _, element := range elements { 549 if _, ok := m[element]; ok { 550 continue 551 } 552 m[element] = struct{}{} 553 for i := 0; i < len(a); i++ { 554 if a[i] == element { 555 a = append(a[:i], a[i+1:]...) 556 i-- 557 } 558 } 559 } 560 n := len(a) 561 *p = a[:n:n] 562 return n 563 } 564 565 // Uint64sConcat is used to merge two or more slices. 566 // This method does not change the existing slices, but instead returns a new slice. 567 func Uint64sConcat(u ...[]uint64) []uint64 { 568 var totalLen int 569 for _, v := range u { 570 totalLen += len(v) 571 } 572 ret := make([]uint64, totalLen) 573 dst := ret 574 for _, v := range u { 575 n := copy(dst, v) 576 dst = dst[n:] 577 } 578 return ret 579 } 580 581 // Uint64sIntersect calculates intersection of two or more slices, 582 // and returns the count of each element. 583 func Uint64sIntersect(u ...[]uint64) (intersectCount map[uint64]int) { 584 if len(u) == 0 { 585 return nil 586 } 587 for _, v := range u { 588 if len(v) == 0 { 589 return nil 590 } 591 } 592 counts := make([]map[uint64]int, len(u)) 593 for k, v := range u { 594 counts[k] = uint64sDistinct(v, nil) 595 } 596 intersectCount = counts[0] 597 L: 598 for k, v := range intersectCount { 599 for _, c := range counts[1:] { 600 v2 := c[k] 601 if v2 == 0 { 602 delete(intersectCount, k) 603 continue L 604 } 605 if v > v2 { 606 v = v2 607 } 608 } 609 intersectCount[k] = v 610 } 611 return intersectCount 612 } 613 614 // Uint64sDistinct calculates the count of each different element, 615 // and only saves these different elements in place if changeSlice is true. 616 func Uint64sDistinct(i *[]uint64, changeSlice bool) (distinctCount map[uint64]int) { 617 if !changeSlice { 618 return uint64sDistinct(*i, nil) 619 } 620 a := (*i)[:0] 621 distinctCount = uint64sDistinct(*i, &a) 622 n := len(distinctCount) 623 *i = a[:n:n] 624 return distinctCount 625 } 626 627 func uint64sDistinct(src []uint64, dst *[]uint64) map[uint64]int { 628 m := make(map[uint64]int, len(src)) 629 if dst == nil { 630 for _, v := range src { 631 n := m[v] 632 m[v] = n + 1 633 } 634 } else { 635 a := *dst 636 for _, v := range src { 637 n := m[v] 638 m[v] = n + 1 639 if n == 0 { 640 a = append(a, v) 641 } 642 } 643 *dst = a 644 } 645 return m 646 } 647 648 // Uint64SetUnion calculates between multiple collections: set1 ∪ set2 ∪ others... 649 // This method does not change the existing slices, but instead returns a new slice. 650 func Uint64SetUnion(set1, set2 []uint64, others ...[]uint64) []uint64 { 651 m := make(map[uint64]struct{}, len(set1)+len(set2)) 652 r := make([]uint64, 0, len(m)) 653 for _, set := range append([][]uint64{set1, set2}, others...) { 654 for _, v := range set { 655 _, ok := m[v] 656 if ok { 657 continue 658 } 659 r = append(r, v) 660 m[v] = struct{}{} 661 } 662 } 663 return r 664 } 665 666 // Uint64SetIntersect calculates between multiple collections: set1 ∩ set2 ∩ others... 667 // This method does not change the existing slices, but instead returns a new slice. 668 func Uint64SetIntersect(set1, set2 []uint64, others ...[]uint64) []uint64 { 669 sets := append([][]uint64{set2}, others...) 670 setsCount := make([]map[uint64]int, len(sets)) 671 for k, v := range sets { 672 setsCount[k] = uint64sDistinct(v, nil) 673 } 674 m := make(map[uint64]struct{}, len(set1)) 675 r := make([]uint64, 0, len(m)) 676 L: 677 for _, v := range set1 { 678 if _, ok := m[v]; ok { 679 continue 680 } 681 m[v] = struct{}{} 682 for _, m2 := range setsCount { 683 if m2[v] == 0 { 684 continue L 685 } 686 } 687 r = append(r, v) 688 } 689 return r 690 } 691 692 // Uint64SetDifference calculates between multiple collections: set1 - set2 - others... 693 // This method does not change the existing slices, but instead returns a new slice. 694 func Uint64SetDifference(set1, set2 []uint64, others ...[]uint64) []uint64 { 695 m := make(map[uint64]struct{}, len(set1)) 696 r := make([]uint64, 0, len(set1)) 697 sets := append([][]uint64{set2}, others...) 698 for _, v := range sets { 699 inter := Uint64SetIntersect(set1, v) 700 for _, v := range inter { 701 m[v] = struct{}{} 702 } 703 } 704 for _, v := range set1 { 705 if _, ok := m[v]; !ok { 706 r = append(r, v) 707 m[v] = struct{}{} 708 } 709 } 710 return r 711 }