This repository has been archived by the owner on Dec 8, 2020. It is now read-only.
forked from shenghui0779/yiigo
/
slice.go
914 lines (717 loc) · 17.3 KB
/
slice.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
package yiigo
import (
"reflect"
"sort"
)
const (
numberUniqueThreshold = 1024
stringUniqueThreshold = 256
)
// UintSlice attaches the methods of Interface to []uint, sorting a increasing order.
type UintSlice []uint
func (p UintSlice) Len() int { return len(p) }
func (p UintSlice) Less(i, j int) bool { return p[i] < p[j] }
func (p UintSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
// SortUints sorts []uints in increasing order.
func SortUints(a []uint) {
sort.Sort(UintSlice(a))
}
// SearchUints searches for x in a sorted slice of uints and returns the index
// as specified by Search. The return value is the index to insert x if x is
// not present (it could be len(a)).
// The slice must be sorted in ascending order.
func SearchUints(a []uint, x uint) int {
return sort.Search(len(a), func(i int) bool { return a[i] >= x })
}
// Int8Slice attaches the methods of Interface to []int8, sorting a increasing order.
type Int8Slice []int8
func (p Int8Slice) Len() int { return len(p) }
func (p Int8Slice) Less(i, j int) bool { return p[i] < p[j] }
func (p Int8Slice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
// SortInt8s sorts []int8s in increasing order.
func SortInt8s(a []int8) {
sort.Sort(Int8Slice(a))
}
// SearchInt8s searches for x in a sorted slice of int8s and returns the index
// as specified by Search. The return value is the index to insert x if x is
// not present (it could be len(a)).
// The slice must be sorted in ascending order.
func SearchInt8s(a []int8, x int8) int {
return sort.Search(len(a), func(i int) bool { return a[i] >= x })
}
// Uint8Slice attaches the methods of Interface to []uint8, sorting a increasing order.
type Uint8Slice []uint8
func (p Uint8Slice) Len() int { return len(p) }
func (p Uint8Slice) Less(i, j int) bool { return p[i] < p[j] }
func (p Uint8Slice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
// SortUint8s sorts []uint8s in increasing order.
func SortUint8s(a []uint8) {
sort.Sort(Uint8Slice(a))
}
// SearchUint8s searches for x in a sorted slice of uint8s and returns the index
// as specified by Search. The return value is the index to insert x if x is
// not present (it could be len(a)).
// The slice must be sorted in ascending order.
func SearchUint8s(a []uint8, x uint8) int {
return sort.Search(len(a), func(i int) bool { return a[i] >= x })
}
// Int16Slice attaches the methods of Interface to []int16, sorting a increasing order.
type Int16Slice []int16
func (p Int16Slice) Len() int { return len(p) }
func (p Int16Slice) Less(i, j int) bool { return p[i] < p[j] }
func (p Int16Slice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
// SortInt16s sorts []int16s in increasing order.
func SortInt16s(a []int16) {
sort.Sort(Int16Slice(a))
}
// SearchInt16s searches for x in a sorted slice of int16s and returns the index
// as specified by Search. The return value is the index to insert x if x is
// not present (it could be len(a)).
// The slice must be sorted in ascending order.
func SearchInt16s(a []int16, x int16) int {
return sort.Search(len(a), func(i int) bool { return a[i] >= x })
}
// Uint16Slice attaches the methods of Interface to []uint16, sorting a increasing order.
type Uint16Slice []uint16
func (p Uint16Slice) Len() int { return len(p) }
func (p Uint16Slice) Less(i, j int) bool { return p[i] < p[j] }
func (p Uint16Slice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
// SortUint16s sorts []uint16s in increasing order.
func SortUint16s(a []uint16) {
sort.Sort(Uint16Slice(a))
}
// SearchUints searches for x in a sorted slice of uint16s and returns the index
// as specified by Search. The return value is the index to insert x if x is
// not present (it could be len(a)).
// The slice must be sorted in ascending order.
func SearchUint16s(a []uint16, x uint16) int {
return sort.Search(len(a), func(i int) bool { return a[i] >= x })
}
// Int32Slice attaches the methods of Interface to []int32, sorting a increasing order.
type Int32Slice []int32
func (p Int32Slice) Len() int { return len(p) }
func (p Int32Slice) Less(i, j int) bool { return p[i] < p[j] }
func (p Int32Slice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
// SortInt32s sorts []int32s in increasing order.
func SortInt32s(a []int32) {
sort.Sort(Int32Slice(a))
}
// SearchInt32s searches for x in a sorted slice of int32s and returns the index
// as specified by Search. The return value is the index to insert x if x is
// not present (it could be len(a)).
// The slice must be sorted in ascending order.
func SearchInt32s(a []int32, x int32) int {
return sort.Search(len(a), func(i int) bool { return a[i] >= x })
}
// Uint32Slice attaches the methods of Interface to []uint, sorting a increasing order.
type Uint32Slice []uint32
func (p Uint32Slice) Len() int { return len(p) }
func (p Uint32Slice) Less(i, j int) bool { return p[i] < p[j] }
func (p Uint32Slice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
// SortUint32s sorts []uint32s in increasing order.
func SortUint32s(a []uint32) {
sort.Sort(Uint32Slice(a))
}
// SearchUint32s searches for x in a sorted slice of uint32s and returns the index
// as specified by Search. The return value is the index to insert x if x is
// not present (it could be len(a)).
// The slice must be sorted in ascending order.
func SearchUint32s(a []uint32, x uint32) int {
return sort.Search(len(a), func(i int) bool { return a[i] >= x })
}
// Int64Slice attaches the methods of Interface to []int64, sorting a increasing order.
type Int64Slice []int64
func (p Int64Slice) Len() int { return len(p) }
func (p Int64Slice) Less(i, j int) bool { return p[i] < p[j] }
func (p Int64Slice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
// SortInt64s sorts []int64s in increasing order.
func SortInt64s(a []int64) {
sort.Sort(Int64Slice(a))
}
// SearchInt64s searches for x in a sorted slice of int64s and returns the index
// as specified by Search. The return value is the index to insert x if x is
// not present (it could be len(a)).
// The slice must be sorted in ascending order.
func SearchInt64s(a []int64, x int64) int {
return sort.Search(len(a), func(i int) bool { return a[i] >= x })
}
// Uint64Slice attaches the methods of Interface to []uint64, sorting a increasing order.
type Uint64Slice []uint64
func (p Uint64Slice) Len() int { return len(p) }
func (p Uint64Slice) Less(i, j int) bool { return p[i] < p[j] }
func (p Uint64Slice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
// SortUint64s sorts []uint64s in increasing order.
func SortUint64s(a []uint64) {
sort.Sort(Uint64Slice(a))
}
// SearchUint64s searches for x in a sorted slice of uint64s and returns the index
// as specified by Search. The return value is the index to insert x if x is
// not present (it could be len(a)).
// The slice must be sorted in ascending order.
func SearchUint64s(a []uint64, x uint64) int {
return sort.Search(len(a), func(i int) bool { return a[i] >= x })
}
// InInts checks if x exists in []ints and returns TRUE if x is found.
func InInts(x int, y ...int) bool {
if len(y) == 0 {
return false
}
for _, v := range y {
if x == v {
return true
}
}
return false
}
// InUints checks if x exists in []uints and returns TRUE if x is found.
func InUints(x uint, y ...uint) bool {
if len(y) == 0 {
return false
}
for _, v := range y {
if x == v {
return true
}
}
return false
}
// InInt8s checks if x exists in []int8s and returns TRUE if x is found.
func InInt8s(x int8, y ...int8) bool {
if len(y) == 0 {
return false
}
for _, v := range y {
if x == v {
return true
}
}
return false
}
// InUint8s checks if x exists in []uint8s and returns TRUE if x is found.
func InUint8s(x uint8, y ...uint8) bool {
if len(y) == 0 {
return false
}
for _, v := range y {
if x == v {
return true
}
}
return false
}
// InInt16s checks if x exists in []int16s and returns TRUE if x is found.
func InInt16s(x int16, y ...int16) bool {
if len(y) == 0 {
return false
}
for _, v := range y {
if x == v {
return true
}
}
return false
}
// InUint16s checks if x exists in []uint16s and returns TRUE if x is found.
func InUint16s(x uint16, y ...uint16) bool {
if len(y) == 0 {
return false
}
for _, v := range y {
if x == v {
return true
}
}
return false
}
// InInt32s checks if x exists in []int32s and returns TRUE if x is found.
func InInt32s(x int32, y ...int32) bool {
if len(y) == 0 {
return false
}
for _, v := range y {
if x == v {
return true
}
}
return false
}
// InUint32s checks if x exists in []uint32s and returns TRUE if x is found.
func InUint32s(x uint32, y ...uint32) bool {
if len(y) == 0 {
return false
}
for _, v := range y {
if x == v {
return true
}
}
return false
}
// InInt64s checks if x exists in []int64s and returns TRUE if x is found.
func InInt64s(x int64, y ...int64) bool {
if len(y) == 0 {
return false
}
for _, v := range y {
if x == v {
return true
}
}
return false
}
// InUint64s checks if x exists in []uint64s and returns TRUE if x is found.
func InUint64s(x uint64, y ...uint64) bool {
if len(y) == 0 {
return false
}
for _, v := range y {
if x == v {
return true
}
}
return false
}
// InFloat64s checks if x exists in []float64s and returns TRUE if x is found.
func InFloat64s(x float64, y ...float64) bool {
if len(y) == 0 {
return false
}
for _, v := range y {
if x == v {
return true
}
}
return false
}
// InStrings checks if x exists in []strings and returns TRUE if x is found.
func InStrings(x string, y ...string) bool {
if len(y) == 0 {
return false
}
for _, v := range y {
if x == v {
return true
}
}
return false
}
// InArray checks if x exists in a slice and returns TRUE if x is found.
func InArray(x interface{}, y ...interface{}) bool {
if len(y) == 0 {
return false
}
for _, v := range y {
if reflect.DeepEqual(x, v) {
return true
}
}
return false
}
// IntsUnique takes an input slice of ints and
// returns a new slice of ints without duplicate values.
func IntsUnique(a []int) []int {
l := len(a)
if l <= 1 {
return a
}
r := make([]int, 0, l)
// remove duplicates with loop
if l < numberUniqueThreshold {
for _, v := range a {
exist := false
for _, u := range r {
if v == u {
exist = true
break
}
}
if !exist {
r = append(r, v)
}
}
return r
}
// remove duplicates with map
m := make(map[int]byte, l)
for _, v := range a {
if _, ok := m[v]; !ok {
m[v] = 0
r = append(r, v)
}
}
return r
}
// UintsUnique takes an input slice of uints and
// returns a new slice of uints without duplicate values.
func UintsUnique(a []uint) []uint {
l := len(a)
if l <= 1 {
return a
}
r := make([]uint, 0, l)
// remove duplicates with loop
if l < numberUniqueThreshold {
for _, v := range a {
exist := false
for _, u := range r {
if v == u {
exist = true
break
}
}
if !exist {
r = append(r, v)
}
}
return r
}
// remove duplicates with map
m := make(map[uint]byte, l)
for _, v := range a {
if _, ok := m[v]; !ok {
m[v] = 0
r = append(r, v)
}
}
return r
}
// Int8sUnique takes an input slice of int8s and
// returns a new slice of int8s without duplicate values.
func Int8sUnique(a []int8) []int8 {
l := len(a)
if l <= 1 {
return a
}
r := make([]int8, 0, l)
// remove duplicates with loop
if l < numberUniqueThreshold {
for _, v := range a {
exist := false
for _, u := range r {
if v == u {
exist = true
break
}
}
if !exist {
r = append(r, v)
}
}
return r
}
// remove duplicates with map
m := make(map[int8]byte, l)
for _, v := range a {
if _, ok := m[v]; !ok {
m[v] = 0
r = append(r, v)
}
}
return r
}
// Uint8sUnique takes an input slice of uint8s and
// returns a new slice of uint8s without duplicate values.
func Uint8sUnique(a []uint8) []uint8 {
l := len(a)
if l <= 1 {
return a
}
r := make([]uint8, 0, l)
// remove duplicates with loop
if l < numberUniqueThreshold {
for _, v := range a {
exist := false
for _, u := range r {
if v == u {
exist = true
break
}
}
if !exist {
r = append(r, v)
}
}
return r
}
// remove duplicates with map
m := make(map[uint8]byte, l)
for _, v := range a {
if _, ok := m[v]; !ok {
m[v] = 0
r = append(r, v)
}
}
return r
}
// Int16sUnique takes an input slice of int16s and
// returns a new slice of int16s without duplicate values.
func Int16sUnique(a []int16) []int16 {
l := len(a)
if l <= 1 {
return a
}
r := make([]int16, 0, l)
// remove duplicates with loop
if l < numberUniqueThreshold {
for _, v := range a {
exist := false
for _, u := range r {
if v == u {
exist = true
break
}
}
if !exist {
r = append(r, v)
}
}
return r
}
// remove duplicates with map
m := make(map[int16]byte, l)
for _, v := range a {
if _, ok := m[v]; !ok {
m[v] = 0
r = append(r, v)
}
}
return r
}
// Uint16sUnique takes an input slice of uint16s and
// returns a new slice of uint16s without duplicate values.
func Uint16sUnique(a []uint16) []uint16 {
l := len(a)
if l <= 1 {
return a
}
r := make([]uint16, 0, l)
// remove duplicates with loop
if l < numberUniqueThreshold {
for _, v := range a {
exist := false
for _, u := range r {
if v == u {
exist = true
break
}
}
if !exist {
r = append(r, v)
}
}
return r
}
// remove duplicates with map
m := make(map[uint16]byte, l)
for _, v := range a {
if _, ok := m[v]; !ok {
m[v] = 0
r = append(r, v)
}
}
return r
}
// Int32sUnique takes an input slice of int32s and
// returns a new slice of int32s without duplicate values.
func Int32sUnique(a []int32) []int32 {
l := len(a)
if l <= 1 {
return a
}
r := make([]int32, 0, l)
// remove duplicates with loop
if l < numberUniqueThreshold {
for _, v := range a {
exist := false
for _, u := range r {
if v == u {
exist = true
break
}
}
if !exist {
r = append(r, v)
}
}
return r
}
// remove duplicates with map
m := make(map[int32]byte, l)
for _, v := range a {
if _, ok := m[v]; !ok {
m[v] = 0
r = append(r, v)
}
}
return r
}
// Uint32sUnique takes an input slice of uint32s and
// returns a new slice of uint32s without duplicate values.
func Uint32sUnique(a []uint32) []uint32 {
l := len(a)
if l <= 1 {
return a
}
r := make([]uint32, 0, l)
// remove duplicates with loop
if l < numberUniqueThreshold {
for _, v := range a {
exist := false
for _, u := range r {
if v == u {
exist = true
break
}
}
if !exist {
r = append(r, v)
}
}
return r
}
// remove duplicates with map
m := make(map[uint32]byte, l)
for _, v := range a {
if _, ok := m[v]; !ok {
m[v] = 0
r = append(r, v)
}
}
return r
}
// Int64sUnique takes an input slice of int64s and
// returns a new slice of int64s without duplicate values.
func Int64sUnique(a []int64) []int64 {
l := len(a)
if l <= 1 {
return a
}
r := make([]int64, 0, l)
// remove duplicates with loop
if l < numberUniqueThreshold {
for _, v := range a {
exist := false
for _, u := range r {
if v == u {
exist = true
break
}
}
if !exist {
r = append(r, v)
}
}
return r
}
// remove duplicates with map
m := make(map[int64]byte, l)
for _, v := range a {
if _, ok := m[v]; !ok {
m[v] = 0
r = append(r, v)
}
}
return r
}
// Uint64sUnique takes an input slice of uint64s and
// returns a new slice of uint64s without duplicate values.
func Uint64sUnique(a []uint64) []uint64 {
l := len(a)
if l <= 1 {
return a
}
r := make([]uint64, 0, l)
// remove duplicates with loop
if l < numberUniqueThreshold {
for _, v := range a {
exist := false
for _, u := range r {
if v == u {
exist = true
break
}
}
if !exist {
r = append(r, v)
}
}
return r
}
// remove duplicates with map
m := make(map[uint64]byte, l)
for _, v := range a {
if _, ok := m[v]; !ok {
m[v] = 0
r = append(r, v)
}
}
return r
}
// Float64sUnique takes an input slice of float64s and
// returns a new slice of float64s without duplicate values.
func Float64sUnique(a []float64) []float64 {
l := len(a)
if l <= 1 {
return a
}
r := make([]float64, 0, l)
// remove duplicates with loop
if l < numberUniqueThreshold {
for _, v := range a {
exist := false
for _, u := range r {
if v == u {
exist = true
break
}
}
if !exist {
r = append(r, v)
}
}
return r
}
// remove duplicates with map
m := make(map[float64]byte, l)
for _, v := range a {
if _, ok := m[v]; !ok {
m[v] = 0
r = append(r, v)
}
}
return r
}
// StringsUnique takes an input slice of strings and
// returns a new slice of strings without duplicate values.
func StringsUnique(a []string) []string {
l := len(a)
if l <= 1 {
return a
}
r := make([]string, 0, l)
// remove duplicates with loop
if l < stringUniqueThreshold {
for _, v := range a {
exist := false
for _, u := range r {
if v == u {
exist = true
break
}
}
if !exist {
r = append(r, v)
}
}
return r
}
// remove duplicates with map
m := make(map[string]byte, l)
for _, v := range a {
if _, ok := m[v]; !ok {
m[v] = 0
r = append(r, v)
}
}
return r
}