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