/
array.go
645 lines (551 loc) · 14.5 KB
/
array.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
/*
* Package arrayutil provides utility api for array operation
*/
package arrayutil
import (
"fmt"
"math/rand"
"sort"
"time"
"github.com/jhunters/goassist/base"
"github.com/jhunters/goassist/generic"
"github.com/jhunters/goassist/maputil"
"github.com/jhunters/goassist/mathutil"
)
const (
SHUFFLE_THRESHOLD = 5
)
// Sort sort array object, sort order type is decided by cmp function.
// example code:
//
// type Student struct {
// Name string
// }
//
// students := []Student{{"xml"}, {"matthew"}, {"matt"}, {"xiemalin"}}
// Sort(students, func(e1, e2 Student) int {
// return strings.Compare(e1.Name, e2.Name)
// })
func Sort[E any](data []E, cmp base.CMP[E]) {
sortobject := sortable[E]{data: data, cmp: cmp}
sort.Sort(sortobject)
}
// Sort sort array object by order type. for more details pls visit generic.Ordered.
// asc if true means by ascending order
// example code:
// strArray := []string{"xml", "matthew", "matt", "xiemalin"}
// SortOrdered(strArray, true)
func SortOrdered[E generic.Ordered](data []E, asc bool) {
sortobject := sortable[E]{data: data, cmp: func(e1, e2 E) int {
ord := -1
if !asc {
ord *= -1
}
return ord * CompareTo(e1, e2)
}}
sort.Sort(sortobject)
}
type sortable[E any] struct {
data []E
cmp base.CMP[E]
}
func (s sortable[E]) Len() int { return len(s.data) }
func (s sortable[E]) Swap(i, j int) { s.data[i], s.data[j] = s.data[j], s.data[i] }
func (s sortable[E]) Less(i, j int) bool {
return s.cmp(s.data[i], s.data[j]) >= 0
}
// Shuffle randomly permutes the specified list using a default source of
// randomness.
func Shuffle[E any](data []E) {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
ShuffleRandom(data, r)
}
// ShuffleRandom randomly permute the specified array using the specified source of
//
// randomness.
func ShuffleRandom[E any](data []E, r *rand.Rand) {
size := len(data)
for i := 0; i < size; i++ {
j := r.Intn(size)
data[i], data[j] = data[j], data[i]
}
}
// Shuffle read element from slice
func ShuffleRead[E any](data []E, reader func(e E)) {
size := len(data)
eleOrder := rand.Perm(size)
for _, k := range eleOrder {
reader(data[k])
}
}
// Reverse reverses the order of the elements in the specified
func Reverse[E any](data []E) {
size := len(data)
mid := size >> 1
j := size - 1
for i := 0; i < mid; i++ {
data[i], data[j] = data[j], data[i]
j--
}
}
// BinarySearch searches the specified array for the specified value using the
// binary search algorithm. return index of the search key
// note that target array must be ordered
func BinarySearch[E any](data []E, key E, cmp base.CMP[E]) int {
low := 0
high := len(data) - 1
for low <= high {
mid := (low + high) >> 1
midVal := data[mid]
r := cmp(midVal, key)
if r < 0 {
low = mid + 1
} else if r > 0 {
high = mid - 1
} else {
return mid // key found
}
}
return -(low + 1) // key not found.
}
// BinarySearch searches the specified array for the specified value using the
// binary search algorithm. return index of the search key
func BinarySearchOrdered[E generic.Ordered](data []E, key E) int {
return BinarySearch(data, key, CompareTo[E])
}
// Contains returns <tt>true</tt> if this array contains the specified element.
func Contains[E any](data []E, key E, equal base.EQL[E]) bool {
size := len(data)
if size == 0 {
return false
}
for i := 0; i < size; i++ {
if equal(data[i], key) {
return true
}
}
return false
}
// Contains returns <tt>true</tt> if this array contains the specified element.
func ContainsOrdered[E generic.Ordered](data []E, key E) bool {
return Contains(data, key, Equals[E])
}
// ContainsAny returns if any elements from other exist in this array
func ContainsAny[E any](data, other []E, equal base.EQL[E]) bool {
size := len(other)
if size == 0 {
return false
}
for i := 0; i < size; i++ {
if Contains(data, other[i], equal) {
return true
}
}
return false
}
// ContainsAny returns if any elements from other exist in this array
func ContainsAnyOrdered[E generic.Ordered](data, other []E) bool {
return ContainsAny(data, other, Equals[E])
}
// Remove removes the first same element value of the key from this array
func Remove[E any](data []E, key E, equal base.EQL[E]) ([]E, bool) {
return removeContional(data, key, equal, false)
}
// RemoveOrdered removes the first same element value of the key from this array
func RemoveOrdered[E generic.Ordered](data []E, key E) ([]E, bool) {
return removeContional(data, key, Equals[E], false)
}
// Remove removes the all same element value of the key from this array
func RemoveAll[E any](data []E, key E, equal base.EQL[E]) ([]E, bool) {
return removeContional(data, key, equal, true)
}
// Remove Removes the all same element value of the key from this array
func RemoveAllOrdered[E generic.Ordered](data []E, key E) ([]E, bool) {
return removeContional(data, key, Equals[E], true)
}
func removeContional[E any](data []E, key E, equal base.EQL[E], all bool) ([]E, bool) {
size := len(data)
if size == 0 {
return data, false
}
ret := make([]E, 0, size)
for i := 0; i < size; i++ {
if equal(data[i], key) {
if !all {
ret = append(ret, data[i+1:]...)
return ret, true
}
} else {
ret = append(ret, data[i])
}
}
return ret, true
}
// RemoveIndex remove element by index
func RemoveIndex[E any](data []E, i int) []E {
size := len(data)
if i >= size || i < 0 { // out of index, do nothing
return data
}
if i < size-1 {
copy(data[i:], data[i+1:])
}
return data[:len(data)-1]
}
// Min Returns the minimum element and position of the given array
func Min[E any](data []E, cmp base.CMP[E]) (E, int) {
size := len(data)
if size == 1 {
return data[0], 0
}
ret := data[0]
pos := 0
for i := 1; i < size; i++ {
if cmp(ret, data[i]) > 0 {
ret = data[i]
pos = i
}
}
return ret, pos
}
// MinOrdered Returns the minimum element and position of the given array
func MinOrdered[E generic.Ordered](data []E) (E, int) {
return Min(data, func(e1, e2 E) int {
return CompareTo(e1, e2)
})
}
// Max Returns the maximum element and position of the given array
func Max[E any](data []E, cmp base.CMP[E]) (E, int) {
size := len(data)
if size == 1 {
return data[0], 0
}
ret := data[0]
pos := 0
for i := 1; i < size; i++ {
if cmp(ret, data[i]) < 0 {
ret = data[i]
pos = i
}
}
return ret, pos
}
// MaxOrdered Returns the maximum element and position of the given array
func MaxOrdered[E generic.Ordered](data []E) (E, int) {
return Max(data, CompareTo[E])
}
// ReplaceAll Replaces all occurrences of one specified value in a array with another
func ReplaceAll[E any](data []E, oldVal, newVal E, euqal base.EQL[E]) {
if data == nil {
return
}
size := len(data)
for i := 0; i < size; i++ {
if euqal(data[i], oldVal) {
data[i] = newVal
}
}
}
// ReplaceOrderedAll Replaces all occurrences of one specified value in a array with another
func ReplaceOrderedAll[E generic.Ordered](data []E, oldVal, newVal E) {
ReplaceAll(data, oldVal, newVal, Equals[E])
}
// EqualWith to theck all elements of the two array are same
func EqualWith[E any](data, other []E, euqal base.EQL[E]) bool {
s1, s2 := len(data), len(other)
if s1 != s2 {
return false
}
for i := 0; i < s1; i++ {
if !euqal(data[i], other[i]) {
return false
}
}
return true
}
// EqualWithOrdered to theck all elements of the two array are same
func EqualWithOrdered[E generic.Ordered](data, other []E) bool {
return EqualWith(data, other, Equals[E])
}
// Filter to filter target array by specified tester.
func Filter[E any](data []E, evaluate base.Evaluate[E]) []E {
ret := make([]E, 0)
for _, v := range data {
if !evaluate(v) {
ret = append(ret, v)
}
}
return ret
}
// IndexOfSubArrayReturns the starting position of the first occurrence of the specified
//
// target array within the specified source array
func IndexOfSubArray[E any](data, sub []E, euqal base.EQL[E]) int {
s1, s2 := len(data), len(sub)
if s2 == 0 {
return -1
}
if s2 > s1 {
return -1
}
checkSize := s2
beginPos := 0
for beginPos+checkSize <= s1 {
if EqualWith(data[beginPos:beginPos+checkSize], sub, euqal) {
return beginPos
}
beginPos++
}
return -1
}
// IndexOfSubOrderedArray the starting position of the first occurrence of the specified
//
// target array within the specified source array
func IndexOfSubOrderedArray[E generic.Ordered](data, sub []E) int {
return IndexOfSubArray(data, sub, Equals[E])
}
// LastIndexOfSubArray the last starting position of the first occurrence of the specified
//
// target array within the specified source array
func LastIndexOfSubArray[E any](data, sub []E, euqal base.EQL[E]) int {
s1, s2 := len(data), len(sub)
if s2 == 0 {
return -1
}
if s2 > s1 {
return -1
}
checkSize := s2
beginPos := s1 - checkSize
for beginPos >= 0 {
if EqualWith(data[beginPos:beginPos+checkSize], sub, euqal) {
return beginPos
}
beginPos--
}
return -1
}
// LastIndexOfSubArray the last starting position of the first occurrence of the specified
//
// target array within the specified source array
func LastIndexOfSubOrderedArray[E generic.Ordered](data, sub []E) int {
return LastIndexOfSubArray(data, sub, Equals[E])
}
// Disjoint Returns true if the two specified collections have no
// elements in common.
func Disjoint[E any](data []E, other []E, euqal base.EQL[E]) bool {
s1, s2 := len(data), len(other)
if s1 == 0 || s2 == 0 {
return true
}
for i := 0; i < s1; i++ {
if Contains(other, data[i], euqal) {
return false
}
}
return true
}
// Disjoint Returns true if the two specified collections have no
// elements in common.
func DisjointOrdered[E generic.Ordered](data []E, other []E) bool {
return Disjoint(data, other, Equals[E])
}
// Rotate Rotates the elements in the specified array by the specified distance.
// For example, suppose a string array arr := []string{"t", "a", "n", "k", "s"}.
// After invoking arrays.Rotate(arr, 1) (or arrays.Rotate(arr, -4)), output is [s, t, a, n, k].
func Rotate[E any](data []E, distance int) {
size := len(data)
if size == 0 {
return
}
mid := -distance % size
if mid < 0 {
mid += size
}
if mid == 0 {
return
}
Reverse(data[0:mid])
Reverse(data[mid:size])
Reverse(data)
}
func getFreq[E generic.Ordered](key E, mapa map[E]int) int {
v, exist := mapa[key]
if !exist {
return 0
}
return v
}
// UnionOrdered returns a array containing the union
// of the given array.
func UnionOrdered[E generic.Ordered](data, other []E) []E {
ret := make([]E, 0)
mapa := getCardinalityMap(data)
mapb := getCardinalityMap(other)
merged := maputil.AddAll(mapa, mapb)
for k := range merged {
i := 0
for m := mathutil.Max(int(getFreq(k, mapa)), int(getFreq(k, mapb))); i < m; i++ {
ret = append(ret, k)
}
}
return ret
}
// IntersectionOrdered returns a array containing the intersection
// of the given array.
func IntersectionOrdered[E generic.Ordered](data, other []E) []E {
ret := make([]E, 0)
mapa := getCardinalityMap(data)
mapb := getCardinalityMap(other)
merged := maputil.AddAll(mapa, mapb)
for k := range merged {
i := 0
for m := mathutil.Min(int(getFreq(k, mapa)), int(getFreq(k, mapb))); i < m; i++ {
ret = append(ret, k)
}
}
return ret
}
// DisjunctionOrdered returns a array containing the exclusive disjunction
// (symmetric difference) of the given array
func DisjunctionOrdered[E generic.Ordered](data, other []E) []E {
ret := make([]E, 0)
mapa := getCardinalityMap(data)
mapb := getCardinalityMap(other)
merged := maputil.AddAll(mapa, mapb)
for k := range merged {
i := 0
m := mathutil.Max(int(getFreq(k, mapa)), int(getFreq(k, mapb))) - mathutil.Min(int(getFreq(k, mapa)), int(getFreq(k, mapb)))
for ; i < m; i++ {
ret = append(ret, k)
}
}
return ret
}
// Subtract returns a new array containing data - other.
func Substract[E any](data, other []E, equal base.EQL[E]) []E {
ret := Clone(data)
for _, e := range other {
ret, _ = Remove(ret, e, equal)
}
return ret
}
// SubtractOrdered returns a new array containing data - other.
func SubstractOrdered[E generic.Ordered](data, other []E) []E {
ret := Clone(data)
for _, v := range other {
ret, _ = RemoveOrdered(ret, v)
}
return ret
}
func getCardinalityMap[E generic.Ordered](data []E) map[E]int {
ret := make(map[E]int)
for _, e := range data {
_, exist := ret[e]
if exist {
ret[e] = ret[e] + 1
} else {
ret[e] = 1
}
}
return ret
}
// CompareTo Compares this object with the specified object for order. Returns a
//
// negative integer, zero, or a positive integer as this object is less
// than, equal to, or greater than the specified object.
func CompareTo[E generic.Ordered](e1, e2 E) int {
if e1 > e2 {
return 1
} else if e1 < e2 {
return -1
}
return 0
}
// Clone Copies all of the elements and return a new array
func Clone[E any](data []E) []E {
size := len(data)
ret := make([]E, size, cap(data))
copy(ret, data)
return ret
}
// Insert target value in array, if index < 0 or > len(data) will do noting
func Insert[E any](data []E, index int, v E) []E {
if index < 0 || index > len(data) {
return data
}
if index == len(data) {
return append(data, v)
}
var empty E
ret := append(data, empty)
copy(ret[index+1:], ret[index:])
ret[index] = v
return ret
}
// Equals to compare two value is equal
func Equals[E generic.Ordered](e1, e2 E) bool {
return e1 == e2
}
// CreateAndFill create array with the same element t
func CreateAndFill[E any](size int, defaultElementValue E) []E {
ret := make([]E, size)
for i := 0; i < size; i++ {
ret[i] = defaultElementValue
}
return ret
}
// AsList convert to array
func AsList[E any](e ...E) []E {
if e == nil {
return nil
}
ret := make([]E, len(e))
copy(ret, e)
return ret
}
// Join element as string with split
func Join[E any](eles []E, split string) string {
sz := len(eles)
ret := ""
if sz == 0 {
return ret
}
for i := 0; i < sz; i++ {
ret = ret + fmt.Sprintf("%v", eles[i])
if i+1 < sz {
ret = ret + split
}
}
return ret
}
// Swap a series of elements in the given array.
func Swap[E any](array []E, offset1, offset2, l int) {
if len(array) == 0 || offset1 >= len(array) || offset2 >= len(array) {
return
}
if offset1 < 0 {
offset1 = 0
}
if offset2 < 0 {
offset2 = 0
}
if offset1 == offset2 {
return
}
l = mathutil.Min(mathutil.Min(l, len(array)-offset1), len(array)-offset2)
for i := 0; i < l; i++ {
array[offset1], array[offset2] = array[offset2], array[offset1]
offset1++
offset2++
}
}
// Addall add all the elements of the given arrays into a new array.
func Addall[E any](array []E, eles ...E) []E {
ret := make([]E, len(array)+len(eles))
copy(ret[:len(array)], array)
if len(eles) > 0 {
copy(ret[len(array):], eles)
}
return ret
}