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