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