github.com/sandwich-go/boost@v1.3.29/xslice/slice.go (about) 1 // Code generated by tools. DO NOT EDIT. 2 package xslice 3 4 import ( 5 "math/rand" 6 "strings" 7 8 _ "github.com/sandwich-go/boost/xrand" 9 ) 10 11 var ( 12 _ strings.Reader 13 tooManyElement = 1024 14 ) 15 16 // Float32sContain s 中是否含有指定元素 v 17 func Float32sContain(s []float32, v float32) bool { 18 for _, ele := range s { 19 if ele == v { 20 return true 21 } 22 } 23 return false 24 } 25 26 // Float32sSetAdd 如果 s 中不存在给定的元素 v 则添加 27 func Float32sSetAdd(s []float32, v ...float32) []float32 { 28 for _, ele := range v { 29 if !Float32sContain(s, ele) { 30 s = append(s, ele) 31 } 32 } 33 return s 34 } 35 36 // Float32sWalk 遍历 s 将 f 应用到每一个元素,返回更新后的数据 37 func Float32sWalk(s []float32, f func(float32) (float32, bool)) []float32 { 38 out := make([]float32, 0, len(s)) 39 for _, ele := range s { 40 if ret, valid := f(ele); valid { 41 out = append(out, ret) 42 } 43 } 44 return out 45 } 46 47 // Float32sRemoveRepeated 移除重复元素 48 func Float32sRemoveRepeated(s []float32) []float32 { 49 if len(s) == 0 { 50 return s 51 } 52 if len(s) < tooManyElement { 53 return float32RemoveRepeatByLoop(s) 54 } else { 55 return float32RemoveRepeatByMap(s) 56 } 57 } 58 59 // Float32sRemoveEmpty 移除空元素 60 func Float32sRemoveEmpty(s []float32) []float32 { 61 out := make([]float32, 0, len(s)) 62 for _, ele := range s { 63 if ele != 0 { 64 out = append(out, ele) 65 } 66 } 67 return out 68 } 69 70 func float32RemoveRepeatByMap(s []float32) []float32 { 71 out := make([]float32, 0, len(s)) 72 tmp := make(map[float32]struct{}) 73 for _, ele := range s { 74 l := len(tmp) 75 tmp[ele] = struct{}{} 76 if len(tmp) != l { 77 out = append(out, ele) 78 } 79 } 80 return out 81 } 82 83 func float32RemoveRepeatByLoop(s []float32) []float32 { 84 out := make([]float32, 0, len(s)) 85 flag := true 86 for i := range s { 87 flag = true 88 for j := range out { 89 if s[i] == out[j] { 90 flag = false 91 break 92 } 93 } 94 if flag { 95 out = append(out, s[i]) 96 } 97 } 98 return out 99 } 100 101 // Float32sShuffle 数组打乱 102 func Float32sShuffle(s []float32) { 103 for i := range s { 104 j := rand.Intn(i + 1) 105 s[i], s[j] = s[j], s[i] 106 } 107 } 108 109 // Float64sContain s 中是否含有指定元素 v 110 func Float64sContain(s []float64, v float64) bool { 111 for _, ele := range s { 112 if ele == v { 113 return true 114 } 115 } 116 return false 117 } 118 119 // Float64sSetAdd 如果 s 中不存在给定的元素 v 则添加 120 func Float64sSetAdd(s []float64, v ...float64) []float64 { 121 for _, ele := range v { 122 if !Float64sContain(s, ele) { 123 s = append(s, ele) 124 } 125 } 126 return s 127 } 128 129 // Float64sWalk 遍历 s 将 f 应用到每一个元素,返回更新后的数据 130 func Float64sWalk(s []float64, f func(float64) (float64, bool)) []float64 { 131 out := make([]float64, 0, len(s)) 132 for _, ele := range s { 133 if ret, valid := f(ele); valid { 134 out = append(out, ret) 135 } 136 } 137 return out 138 } 139 140 // Float64sRemoveRepeated 移除重复元素 141 func Float64sRemoveRepeated(s []float64) []float64 { 142 if len(s) == 0 { 143 return s 144 } 145 if len(s) < tooManyElement { 146 return float64RemoveRepeatByLoop(s) 147 } else { 148 return float64RemoveRepeatByMap(s) 149 } 150 } 151 152 // Float64sRemoveEmpty 移除空元素 153 func Float64sRemoveEmpty(s []float64) []float64 { 154 out := make([]float64, 0, len(s)) 155 for _, ele := range s { 156 if ele != 0 { 157 out = append(out, ele) 158 } 159 } 160 return out 161 } 162 163 func float64RemoveRepeatByMap(s []float64) []float64 { 164 out := make([]float64, 0, len(s)) 165 tmp := make(map[float64]struct{}) 166 for _, ele := range s { 167 l := len(tmp) 168 tmp[ele] = struct{}{} 169 if len(tmp) != l { 170 out = append(out, ele) 171 } 172 } 173 return out 174 } 175 176 func float64RemoveRepeatByLoop(s []float64) []float64 { 177 out := make([]float64, 0, len(s)) 178 flag := true 179 for i := range s { 180 flag = true 181 for j := range out { 182 if s[i] == out[j] { 183 flag = false 184 break 185 } 186 } 187 if flag { 188 out = append(out, s[i]) 189 } 190 } 191 return out 192 } 193 194 // Float64sShuffle 数组打乱 195 func Float64sShuffle(s []float64) { 196 for i := range s { 197 j := rand.Intn(i + 1) 198 s[i], s[j] = s[j], s[i] 199 } 200 } 201 202 // IntsContain s 中是否含有指定元素 v 203 func IntsContain(s []int, v int) bool { 204 for _, ele := range s { 205 if ele == v { 206 return true 207 } 208 } 209 return false 210 } 211 212 // IntsSetAdd 如果 s 中不存在给定的元素 v 则添加 213 func IntsSetAdd(s []int, v ...int) []int { 214 for _, ele := range v { 215 if !IntsContain(s, ele) { 216 s = append(s, ele) 217 } 218 } 219 return s 220 } 221 222 // IntsWalk 遍历 s 将 f 应用到每一个元素,返回更新后的数据 223 func IntsWalk(s []int, f func(int) (int, bool)) []int { 224 out := make([]int, 0, len(s)) 225 for _, ele := range s { 226 if ret, valid := f(ele); valid { 227 out = append(out, ret) 228 } 229 } 230 return out 231 } 232 233 // IntsRemoveRepeated 移除重复元素 234 func IntsRemoveRepeated(s []int) []int { 235 if len(s) == 0 { 236 return s 237 } 238 if len(s) < tooManyElement { 239 return intRemoveRepeatByLoop(s) 240 } else { 241 return intRemoveRepeatByMap(s) 242 } 243 } 244 245 // IntsRemoveEmpty 移除空元素 246 func IntsRemoveEmpty(s []int) []int { 247 out := make([]int, 0, len(s)) 248 for _, ele := range s { 249 if ele != 0 { 250 out = append(out, ele) 251 } 252 } 253 return out 254 } 255 256 func intRemoveRepeatByMap(s []int) []int { 257 out := make([]int, 0, len(s)) 258 tmp := make(map[int]struct{}) 259 for _, ele := range s { 260 l := len(tmp) 261 tmp[ele] = struct{}{} 262 if len(tmp) != l { 263 out = append(out, ele) 264 } 265 } 266 return out 267 } 268 269 func intRemoveRepeatByLoop(s []int) []int { 270 out := make([]int, 0, len(s)) 271 flag := true 272 for i := range s { 273 flag = true 274 for j := range out { 275 if s[i] == out[j] { 276 flag = false 277 break 278 } 279 } 280 if flag { 281 out = append(out, s[i]) 282 } 283 } 284 return out 285 } 286 287 // IntsShuffle 数组打乱 288 func IntsShuffle(s []int) { 289 for i := range s { 290 j := rand.Intn(i + 1) 291 s[i], s[j] = s[j], s[i] 292 } 293 } 294 295 // Int16sContain s 中是否含有指定元素 v 296 func Int16sContain(s []int16, v int16) bool { 297 for _, ele := range s { 298 if ele == v { 299 return true 300 } 301 } 302 return false 303 } 304 305 // Int16sSetAdd 如果 s 中不存在给定的元素 v 则添加 306 func Int16sSetAdd(s []int16, v ...int16) []int16 { 307 for _, ele := range v { 308 if !Int16sContain(s, ele) { 309 s = append(s, ele) 310 } 311 } 312 return s 313 } 314 315 // Int16sWalk 遍历 s 将 f 应用到每一个元素,返回更新后的数据 316 func Int16sWalk(s []int16, f func(int16) (int16, bool)) []int16 { 317 out := make([]int16, 0, len(s)) 318 for _, ele := range s { 319 if ret, valid := f(ele); valid { 320 out = append(out, ret) 321 } 322 } 323 return out 324 } 325 326 // Int16sRemoveRepeated 移除重复元素 327 func Int16sRemoveRepeated(s []int16) []int16 { 328 if len(s) == 0 { 329 return s 330 } 331 if len(s) < tooManyElement { 332 return int16RemoveRepeatByLoop(s) 333 } else { 334 return int16RemoveRepeatByMap(s) 335 } 336 } 337 338 // Int16sRemoveEmpty 移除空元素 339 func Int16sRemoveEmpty(s []int16) []int16 { 340 out := make([]int16, 0, len(s)) 341 for _, ele := range s { 342 if ele != 0 { 343 out = append(out, ele) 344 } 345 } 346 return out 347 } 348 349 func int16RemoveRepeatByMap(s []int16) []int16 { 350 out := make([]int16, 0, len(s)) 351 tmp := make(map[int16]struct{}) 352 for _, ele := range s { 353 l := len(tmp) 354 tmp[ele] = struct{}{} 355 if len(tmp) != l { 356 out = append(out, ele) 357 } 358 } 359 return out 360 } 361 362 func int16RemoveRepeatByLoop(s []int16) []int16 { 363 out := make([]int16, 0, len(s)) 364 flag := true 365 for i := range s { 366 flag = true 367 for j := range out { 368 if s[i] == out[j] { 369 flag = false 370 break 371 } 372 } 373 if flag { 374 out = append(out, s[i]) 375 } 376 } 377 return out 378 } 379 380 // Int16sShuffle 数组打乱 381 func Int16sShuffle(s []int16) { 382 for i := range s { 383 j := rand.Intn(i + 1) 384 s[i], s[j] = s[j], s[i] 385 } 386 } 387 388 // Int32sContain s 中是否含有指定元素 v 389 func Int32sContain(s []int32, v int32) bool { 390 for _, ele := range s { 391 if ele == v { 392 return true 393 } 394 } 395 return false 396 } 397 398 // Int32sSetAdd 如果 s 中不存在给定的元素 v 则添加 399 func Int32sSetAdd(s []int32, v ...int32) []int32 { 400 for _, ele := range v { 401 if !Int32sContain(s, ele) { 402 s = append(s, ele) 403 } 404 } 405 return s 406 } 407 408 // Int32sWalk 遍历 s 将 f 应用到每一个元素,返回更新后的数据 409 func Int32sWalk(s []int32, f func(int32) (int32, bool)) []int32 { 410 out := make([]int32, 0, len(s)) 411 for _, ele := range s { 412 if ret, valid := f(ele); valid { 413 out = append(out, ret) 414 } 415 } 416 return out 417 } 418 419 // Int32sRemoveRepeated 移除重复元素 420 func Int32sRemoveRepeated(s []int32) []int32 { 421 if len(s) == 0 { 422 return s 423 } 424 if len(s) < tooManyElement { 425 return int32RemoveRepeatByLoop(s) 426 } else { 427 return int32RemoveRepeatByMap(s) 428 } 429 } 430 431 // Int32sRemoveEmpty 移除空元素 432 func Int32sRemoveEmpty(s []int32) []int32 { 433 out := make([]int32, 0, len(s)) 434 for _, ele := range s { 435 if ele != 0 { 436 out = append(out, ele) 437 } 438 } 439 return out 440 } 441 442 func int32RemoveRepeatByMap(s []int32) []int32 { 443 out := make([]int32, 0, len(s)) 444 tmp := make(map[int32]struct{}) 445 for _, ele := range s { 446 l := len(tmp) 447 tmp[ele] = struct{}{} 448 if len(tmp) != l { 449 out = append(out, ele) 450 } 451 } 452 return out 453 } 454 455 func int32RemoveRepeatByLoop(s []int32) []int32 { 456 out := make([]int32, 0, len(s)) 457 flag := true 458 for i := range s { 459 flag = true 460 for j := range out { 461 if s[i] == out[j] { 462 flag = false 463 break 464 } 465 } 466 if flag { 467 out = append(out, s[i]) 468 } 469 } 470 return out 471 } 472 473 // Int32sShuffle 数组打乱 474 func Int32sShuffle(s []int32) { 475 for i := range s { 476 j := rand.Intn(i + 1) 477 s[i], s[j] = s[j], s[i] 478 } 479 } 480 481 // Int64sContain s 中是否含有指定元素 v 482 func Int64sContain(s []int64, v int64) bool { 483 for _, ele := range s { 484 if ele == v { 485 return true 486 } 487 } 488 return false 489 } 490 491 // Int64sSetAdd 如果 s 中不存在给定的元素 v 则添加 492 func Int64sSetAdd(s []int64, v ...int64) []int64 { 493 for _, ele := range v { 494 if !Int64sContain(s, ele) { 495 s = append(s, ele) 496 } 497 } 498 return s 499 } 500 501 // Int64sWalk 遍历 s 将 f 应用到每一个元素,返回更新后的数据 502 func Int64sWalk(s []int64, f func(int64) (int64, bool)) []int64 { 503 out := make([]int64, 0, len(s)) 504 for _, ele := range s { 505 if ret, valid := f(ele); valid { 506 out = append(out, ret) 507 } 508 } 509 return out 510 } 511 512 // Int64sRemoveRepeated 移除重复元素 513 func Int64sRemoveRepeated(s []int64) []int64 { 514 if len(s) == 0 { 515 return s 516 } 517 if len(s) < tooManyElement { 518 return int64RemoveRepeatByLoop(s) 519 } else { 520 return int64RemoveRepeatByMap(s) 521 } 522 } 523 524 // Int64sRemoveEmpty 移除空元素 525 func Int64sRemoveEmpty(s []int64) []int64 { 526 out := make([]int64, 0, len(s)) 527 for _, ele := range s { 528 if ele != 0 { 529 out = append(out, ele) 530 } 531 } 532 return out 533 } 534 535 func int64RemoveRepeatByMap(s []int64) []int64 { 536 out := make([]int64, 0, len(s)) 537 tmp := make(map[int64]struct{}) 538 for _, ele := range s { 539 l := len(tmp) 540 tmp[ele] = struct{}{} 541 if len(tmp) != l { 542 out = append(out, ele) 543 } 544 } 545 return out 546 } 547 548 func int64RemoveRepeatByLoop(s []int64) []int64 { 549 out := make([]int64, 0, len(s)) 550 flag := true 551 for i := range s { 552 flag = true 553 for j := range out { 554 if s[i] == out[j] { 555 flag = false 556 break 557 } 558 } 559 if flag { 560 out = append(out, s[i]) 561 } 562 } 563 return out 564 } 565 566 // Int64sShuffle 数组打乱 567 func Int64sShuffle(s []int64) { 568 for i := range s { 569 j := rand.Intn(i + 1) 570 s[i], s[j] = s[j], s[i] 571 } 572 } 573 574 // Int8sContain s 中是否含有指定元素 v 575 func Int8sContain(s []int8, v int8) bool { 576 for _, ele := range s { 577 if ele == v { 578 return true 579 } 580 } 581 return false 582 } 583 584 // Int8sSetAdd 如果 s 中不存在给定的元素 v 则添加 585 func Int8sSetAdd(s []int8, v ...int8) []int8 { 586 for _, ele := range v { 587 if !Int8sContain(s, ele) { 588 s = append(s, ele) 589 } 590 } 591 return s 592 } 593 594 // Int8sWalk 遍历 s 将 f 应用到每一个元素,返回更新后的数据 595 func Int8sWalk(s []int8, f func(int8) (int8, bool)) []int8 { 596 out := make([]int8, 0, len(s)) 597 for _, ele := range s { 598 if ret, valid := f(ele); valid { 599 out = append(out, ret) 600 } 601 } 602 return out 603 } 604 605 // Int8sRemoveRepeated 移除重复元素 606 func Int8sRemoveRepeated(s []int8) []int8 { 607 if len(s) == 0 { 608 return s 609 } 610 if len(s) < tooManyElement { 611 return int8RemoveRepeatByLoop(s) 612 } else { 613 return int8RemoveRepeatByMap(s) 614 } 615 } 616 617 // Int8sRemoveEmpty 移除空元素 618 func Int8sRemoveEmpty(s []int8) []int8 { 619 out := make([]int8, 0, len(s)) 620 for _, ele := range s { 621 if ele != 0 { 622 out = append(out, ele) 623 } 624 } 625 return out 626 } 627 628 func int8RemoveRepeatByMap(s []int8) []int8 { 629 out := make([]int8, 0, len(s)) 630 tmp := make(map[int8]struct{}) 631 for _, ele := range s { 632 l := len(tmp) 633 tmp[ele] = struct{}{} 634 if len(tmp) != l { 635 out = append(out, ele) 636 } 637 } 638 return out 639 } 640 641 func int8RemoveRepeatByLoop(s []int8) []int8 { 642 out := make([]int8, 0, len(s)) 643 flag := true 644 for i := range s { 645 flag = true 646 for j := range out { 647 if s[i] == out[j] { 648 flag = false 649 break 650 } 651 } 652 if flag { 653 out = append(out, s[i]) 654 } 655 } 656 return out 657 } 658 659 // Int8sShuffle 数组打乱 660 func Int8sShuffle(s []int8) { 661 for i := range s { 662 j := rand.Intn(i + 1) 663 s[i], s[j] = s[j], s[i] 664 } 665 } 666 667 // StringsContain s 中是否含有指定元素 v 668 func StringsContain(s []string, v string) bool { 669 for _, ele := range s { 670 if ele == v { 671 return true 672 } 673 } 674 return false 675 } 676 677 // StringsContainEqualFold s 中是否含有指定元素 v,不区分大小写 678 func StringsContainEqualFold(s []string, v string) bool { 679 for _, ele := range s { 680 if strings.EqualFold(ele, v) { 681 return true 682 } 683 } 684 return false 685 } 686 687 // StringsSetAdd 如果 s 中不存在给定的元素 v 则添加 688 func StringsSetAdd(s []string, v ...string) []string { 689 for _, ele := range v { 690 if !StringsContain(s, ele) { 691 s = append(s, ele) 692 } 693 } 694 return s 695 } 696 697 // StringsWalk 遍历 s 将 f 应用到每一个元素,返回更新后的数据 698 func StringsWalk(s []string, f func(string) (string, bool)) []string { 699 out := make([]string, 0, len(s)) 700 for _, ele := range s { 701 if ret, valid := f(ele); valid { 702 out = append(out, ret) 703 } 704 } 705 return out 706 } 707 708 // StringsAddPrefix 每一个元素添加前缀 709 func StringsAddPrefix(s []string, prefix string) []string { 710 out := make([]string, 0, len(s)) 711 for _, ele := range s { 712 out = append(out, prefix+ele) 713 } 714 return out 715 } 716 717 // StringsAddSuffix 每一个元素添加后缀 718 func StringsAddSuffix(s []string, suffix string) []string { 719 out := make([]string, 0, len(s)) 720 for _, ele := range s { 721 out = append(out, ele+suffix) 722 } 723 return out 724 } 725 726 // StringsRemoveRepeated 移除重复元素 727 func StringsRemoveRepeated(s []string) []string { 728 if len(s) == 0 { 729 return s 730 } 731 if len(s) < tooManyElement { 732 return stringRemoveRepeatByLoop(s) 733 } else { 734 return stringRemoveRepeatByMap(s) 735 } 736 } 737 738 // StringsRemoveEmpty 移除空元素 739 func StringsRemoveEmpty(s []string) []string { 740 out := make([]string, 0, len(s)) 741 for _, ele := range s { 742 if len(ele) > 0 { 743 out = append(out, ele) 744 } 745 } 746 return out 747 } 748 749 func stringRemoveRepeatByMap(s []string) []string { 750 out := make([]string, 0, len(s)) 751 tmp := make(map[string]struct{}) 752 for _, ele := range s { 753 l := len(tmp) 754 tmp[ele] = struct{}{} 755 if len(tmp) != l { 756 out = append(out, ele) 757 } 758 } 759 return out 760 } 761 762 func stringRemoveRepeatByLoop(s []string) []string { 763 out := make([]string, 0, len(s)) 764 flag := true 765 for i := range s { 766 flag = true 767 for j := range out { 768 if s[i] == out[j] { 769 flag = false 770 break 771 } 772 } 773 if flag { 774 out = append(out, s[i]) 775 } 776 } 777 return out 778 } 779 780 // StringsShuffle 数组打乱 781 func StringsShuffle(s []string) { 782 for i := range s { 783 j := rand.Intn(i + 1) 784 s[i], s[j] = s[j], s[i] 785 } 786 } 787 788 // UintsContain s 中是否含有指定元素 v 789 func UintsContain(s []uint, v uint) bool { 790 for _, ele := range s { 791 if ele == v { 792 return true 793 } 794 } 795 return false 796 } 797 798 // UintsSetAdd 如果 s 中不存在给定的元素 v 则添加 799 func UintsSetAdd(s []uint, v ...uint) []uint { 800 for _, ele := range v { 801 if !UintsContain(s, ele) { 802 s = append(s, ele) 803 } 804 } 805 return s 806 } 807 808 // UintsWalk 遍历 s 将 f 应用到每一个元素,返回更新后的数据 809 func UintsWalk(s []uint, f func(uint) (uint, bool)) []uint { 810 out := make([]uint, 0, len(s)) 811 for _, ele := range s { 812 if ret, valid := f(ele); valid { 813 out = append(out, ret) 814 } 815 } 816 return out 817 } 818 819 // UintsRemoveRepeated 移除重复元素 820 func UintsRemoveRepeated(s []uint) []uint { 821 if len(s) == 0 { 822 return s 823 } 824 if len(s) < tooManyElement { 825 return uintRemoveRepeatByLoop(s) 826 } else { 827 return uintRemoveRepeatByMap(s) 828 } 829 } 830 831 // UintsRemoveEmpty 移除空元素 832 func UintsRemoveEmpty(s []uint) []uint { 833 out := make([]uint, 0, len(s)) 834 for _, ele := range s { 835 if ele != 0 { 836 out = append(out, ele) 837 } 838 } 839 return out 840 } 841 842 func uintRemoveRepeatByMap(s []uint) []uint { 843 out := make([]uint, 0, len(s)) 844 tmp := make(map[uint]struct{}) 845 for _, ele := range s { 846 l := len(tmp) 847 tmp[ele] = struct{}{} 848 if len(tmp) != l { 849 out = append(out, ele) 850 } 851 } 852 return out 853 } 854 855 func uintRemoveRepeatByLoop(s []uint) []uint { 856 out := make([]uint, 0, len(s)) 857 flag := true 858 for i := range s { 859 flag = true 860 for j := range out { 861 if s[i] == out[j] { 862 flag = false 863 break 864 } 865 } 866 if flag { 867 out = append(out, s[i]) 868 } 869 } 870 return out 871 } 872 873 // UintsShuffle 数组打乱 874 func UintsShuffle(s []uint) { 875 for i := range s { 876 j := rand.Intn(i + 1) 877 s[i], s[j] = s[j], s[i] 878 } 879 } 880 881 // Uint16sContain s 中是否含有指定元素 v 882 func Uint16sContain(s []uint16, v uint16) bool { 883 for _, ele := range s { 884 if ele == v { 885 return true 886 } 887 } 888 return false 889 } 890 891 // Uint16sSetAdd 如果 s 中不存在给定的元素 v 则添加 892 func Uint16sSetAdd(s []uint16, v ...uint16) []uint16 { 893 for _, ele := range v { 894 if !Uint16sContain(s, ele) { 895 s = append(s, ele) 896 } 897 } 898 return s 899 } 900 901 // Uint16sWalk 遍历 s 将 f 应用到每一个元素,返回更新后的数据 902 func Uint16sWalk(s []uint16, f func(uint16) (uint16, bool)) []uint16 { 903 out := make([]uint16, 0, len(s)) 904 for _, ele := range s { 905 if ret, valid := f(ele); valid { 906 out = append(out, ret) 907 } 908 } 909 return out 910 } 911 912 // Uint16sRemoveRepeated 移除重复元素 913 func Uint16sRemoveRepeated(s []uint16) []uint16 { 914 if len(s) == 0 { 915 return s 916 } 917 if len(s) < tooManyElement { 918 return uint16RemoveRepeatByLoop(s) 919 } else { 920 return uint16RemoveRepeatByMap(s) 921 } 922 } 923 924 // Uint16sRemoveEmpty 移除空元素 925 func Uint16sRemoveEmpty(s []uint16) []uint16 { 926 out := make([]uint16, 0, len(s)) 927 for _, ele := range s { 928 if ele != 0 { 929 out = append(out, ele) 930 } 931 } 932 return out 933 } 934 935 func uint16RemoveRepeatByMap(s []uint16) []uint16 { 936 out := make([]uint16, 0, len(s)) 937 tmp := make(map[uint16]struct{}) 938 for _, ele := range s { 939 l := len(tmp) 940 tmp[ele] = struct{}{} 941 if len(tmp) != l { 942 out = append(out, ele) 943 } 944 } 945 return out 946 } 947 948 func uint16RemoveRepeatByLoop(s []uint16) []uint16 { 949 out := make([]uint16, 0, len(s)) 950 flag := true 951 for i := range s { 952 flag = true 953 for j := range out { 954 if s[i] == out[j] { 955 flag = false 956 break 957 } 958 } 959 if flag { 960 out = append(out, s[i]) 961 } 962 } 963 return out 964 } 965 966 // Uint16sShuffle 数组打乱 967 func Uint16sShuffle(s []uint16) { 968 for i := range s { 969 j := rand.Intn(i + 1) 970 s[i], s[j] = s[j], s[i] 971 } 972 } 973 974 // Uint32sContain s 中是否含有指定元素 v 975 func Uint32sContain(s []uint32, v uint32) bool { 976 for _, ele := range s { 977 if ele == v { 978 return true 979 } 980 } 981 return false 982 } 983 984 // Uint32sSetAdd 如果 s 中不存在给定的元素 v 则添加 985 func Uint32sSetAdd(s []uint32, v ...uint32) []uint32 { 986 for _, ele := range v { 987 if !Uint32sContain(s, ele) { 988 s = append(s, ele) 989 } 990 } 991 return s 992 } 993 994 // Uint32sWalk 遍历 s 将 f 应用到每一个元素,返回更新后的数据 995 func Uint32sWalk(s []uint32, f func(uint32) (uint32, bool)) []uint32 { 996 out := make([]uint32, 0, len(s)) 997 for _, ele := range s { 998 if ret, valid := f(ele); valid { 999 out = append(out, ret) 1000 } 1001 } 1002 return out 1003 } 1004 1005 // Uint32sRemoveRepeated 移除重复元素 1006 func Uint32sRemoveRepeated(s []uint32) []uint32 { 1007 if len(s) == 0 { 1008 return s 1009 } 1010 if len(s) < tooManyElement { 1011 return uint32RemoveRepeatByLoop(s) 1012 } else { 1013 return uint32RemoveRepeatByMap(s) 1014 } 1015 } 1016 1017 // Uint32sRemoveEmpty 移除空元素 1018 func Uint32sRemoveEmpty(s []uint32) []uint32 { 1019 out := make([]uint32, 0, len(s)) 1020 for _, ele := range s { 1021 if ele != 0 { 1022 out = append(out, ele) 1023 } 1024 } 1025 return out 1026 } 1027 1028 func uint32RemoveRepeatByMap(s []uint32) []uint32 { 1029 out := make([]uint32, 0, len(s)) 1030 tmp := make(map[uint32]struct{}) 1031 for _, ele := range s { 1032 l := len(tmp) 1033 tmp[ele] = struct{}{} 1034 if len(tmp) != l { 1035 out = append(out, ele) 1036 } 1037 } 1038 return out 1039 } 1040 1041 func uint32RemoveRepeatByLoop(s []uint32) []uint32 { 1042 out := make([]uint32, 0, len(s)) 1043 flag := true 1044 for i := range s { 1045 flag = true 1046 for j := range out { 1047 if s[i] == out[j] { 1048 flag = false 1049 break 1050 } 1051 } 1052 if flag { 1053 out = append(out, s[i]) 1054 } 1055 } 1056 return out 1057 } 1058 1059 // Uint32sShuffle 数组打乱 1060 func Uint32sShuffle(s []uint32) { 1061 for i := range s { 1062 j := rand.Intn(i + 1) 1063 s[i], s[j] = s[j], s[i] 1064 } 1065 } 1066 1067 // Uint64sContain s 中是否含有指定元素 v 1068 func Uint64sContain(s []uint64, v uint64) bool { 1069 for _, ele := range s { 1070 if ele == v { 1071 return true 1072 } 1073 } 1074 return false 1075 } 1076 1077 // Uint64sSetAdd 如果 s 中不存在给定的元素 v 则添加 1078 func Uint64sSetAdd(s []uint64, v ...uint64) []uint64 { 1079 for _, ele := range v { 1080 if !Uint64sContain(s, ele) { 1081 s = append(s, ele) 1082 } 1083 } 1084 return s 1085 } 1086 1087 // Uint64sWalk 遍历 s 将 f 应用到每一个元素,返回更新后的数据 1088 func Uint64sWalk(s []uint64, f func(uint64) (uint64, bool)) []uint64 { 1089 out := make([]uint64, 0, len(s)) 1090 for _, ele := range s { 1091 if ret, valid := f(ele); valid { 1092 out = append(out, ret) 1093 } 1094 } 1095 return out 1096 } 1097 1098 // Uint64sRemoveRepeated 移除重复元素 1099 func Uint64sRemoveRepeated(s []uint64) []uint64 { 1100 if len(s) == 0 { 1101 return s 1102 } 1103 if len(s) < tooManyElement { 1104 return uint64RemoveRepeatByLoop(s) 1105 } else { 1106 return uint64RemoveRepeatByMap(s) 1107 } 1108 } 1109 1110 // Uint64sRemoveEmpty 移除空元素 1111 func Uint64sRemoveEmpty(s []uint64) []uint64 { 1112 out := make([]uint64, 0, len(s)) 1113 for _, ele := range s { 1114 if ele != 0 { 1115 out = append(out, ele) 1116 } 1117 } 1118 return out 1119 } 1120 1121 func uint64RemoveRepeatByMap(s []uint64) []uint64 { 1122 out := make([]uint64, 0, len(s)) 1123 tmp := make(map[uint64]struct{}) 1124 for _, ele := range s { 1125 l := len(tmp) 1126 tmp[ele] = struct{}{} 1127 if len(tmp) != l { 1128 out = append(out, ele) 1129 } 1130 } 1131 return out 1132 } 1133 1134 func uint64RemoveRepeatByLoop(s []uint64) []uint64 { 1135 out := make([]uint64, 0, len(s)) 1136 flag := true 1137 for i := range s { 1138 flag = true 1139 for j := range out { 1140 if s[i] == out[j] { 1141 flag = false 1142 break 1143 } 1144 } 1145 if flag { 1146 out = append(out, s[i]) 1147 } 1148 } 1149 return out 1150 } 1151 1152 // Uint64sShuffle 数组打乱 1153 func Uint64sShuffle(s []uint64) { 1154 for i := range s { 1155 j := rand.Intn(i + 1) 1156 s[i], s[j] = s[j], s[i] 1157 } 1158 } 1159 1160 // Uint8sContain s 中是否含有指定元素 v 1161 func Uint8sContain(s []uint8, v uint8) bool { 1162 for _, ele := range s { 1163 if ele == v { 1164 return true 1165 } 1166 } 1167 return false 1168 } 1169 1170 // Uint8sSetAdd 如果 s 中不存在给定的元素 v 则添加 1171 func Uint8sSetAdd(s []uint8, v ...uint8) []uint8 { 1172 for _, ele := range v { 1173 if !Uint8sContain(s, ele) { 1174 s = append(s, ele) 1175 } 1176 } 1177 return s 1178 } 1179 1180 // Uint8sWalk 遍历 s 将 f 应用到每一个元素,返回更新后的数据 1181 func Uint8sWalk(s []uint8, f func(uint8) (uint8, bool)) []uint8 { 1182 out := make([]uint8, 0, len(s)) 1183 for _, ele := range s { 1184 if ret, valid := f(ele); valid { 1185 out = append(out, ret) 1186 } 1187 } 1188 return out 1189 } 1190 1191 // Uint8sRemoveRepeated 移除重复元素 1192 func Uint8sRemoveRepeated(s []uint8) []uint8 { 1193 if len(s) == 0 { 1194 return s 1195 } 1196 if len(s) < tooManyElement { 1197 return uint8RemoveRepeatByLoop(s) 1198 } else { 1199 return uint8RemoveRepeatByMap(s) 1200 } 1201 } 1202 1203 // Uint8sRemoveEmpty 移除空元素 1204 func Uint8sRemoveEmpty(s []uint8) []uint8 { 1205 out := make([]uint8, 0, len(s)) 1206 for _, ele := range s { 1207 if ele != 0 { 1208 out = append(out, ele) 1209 } 1210 } 1211 return out 1212 } 1213 1214 func uint8RemoveRepeatByMap(s []uint8) []uint8 { 1215 out := make([]uint8, 0, len(s)) 1216 tmp := make(map[uint8]struct{}) 1217 for _, ele := range s { 1218 l := len(tmp) 1219 tmp[ele] = struct{}{} 1220 if len(tmp) != l { 1221 out = append(out, ele) 1222 } 1223 } 1224 return out 1225 } 1226 1227 func uint8RemoveRepeatByLoop(s []uint8) []uint8 { 1228 out := make([]uint8, 0, len(s)) 1229 flag := true 1230 for i := range s { 1231 flag = true 1232 for j := range out { 1233 if s[i] == out[j] { 1234 flag = false 1235 break 1236 } 1237 } 1238 if flag { 1239 out = append(out, s[i]) 1240 } 1241 } 1242 return out 1243 } 1244 1245 // Uint8sShuffle 数组打乱 1246 func Uint8sShuffle(s []uint8) { 1247 for i := range s { 1248 j := rand.Intn(i + 1) 1249 s[i], s[j] = s[j], s[i] 1250 } 1251 }