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