/
arrayOperations.go
704 lines (616 loc) · 17.2 KB
/
arrayOperations.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
package arrayOperations
import (
"reflect"
)
// Distinct returns the unique vals of a slice
//
// [1, 1, 2, 3] >> [1, 2, 3]
func Distinct(arr interface{}) (reflect.Value, bool) {
// create a slice from our input interface
slice, ok := takeArg(arr, reflect.Slice)
if !ok {
return reflect.Value{}, ok
}
// put the values of our slice into a map
// the key's of the map will be the slice's unique values
c := slice.Len()
m := make(map[interface{}]bool)
for i := 0; i < c; i++ {
m[slice.Index(i).Interface()] = true
}
mapLen := len(m)
// create the output slice and populate it with the map's keys
out := reflect.MakeSlice(reflect.TypeOf(arr), mapLen, mapLen)
i := 0
for k := range m {
v := reflect.ValueOf(k)
o := out.Index(i)
o.Set(v)
i++
}
return out, ok
}
// Intersect returns a slice of values that are present in all of the input slices
//
// [1, 1, 3, 4, 5, 6] & [2, 3, 6] >> [3, 6]
//
// [1, 1, 3, 4, 5, 6] >> [1, 3, 4, 5, 6]
func Intersect(arrs ...interface{}) (reflect.Value, bool) {
// create a map to count all the instances of the slice elems
arrLength := len(arrs)
var kind reflect.Kind
tempMap := make(map[interface{}]int)
for i, arg := range arrs {
tempArr, ok := Distinct(arg)
if !ok {
return reflect.Value{}, ok
}
// check to be sure the type hasn't changed
if i > 0 && tempArr.Index(0).Kind() != kind {
return reflect.Value{}, false
}
kind = tempArr.Index(0).Kind()
c := tempArr.Len()
for idx := 0; idx < c; idx++ {
// how many times have we encountered this elem?
if _, ok := tempMap[tempArr.Index(idx).Interface()]; ok {
tempMap[tempArr.Index(idx).Interface()]++
} else {
tempMap[tempArr.Index(idx).Interface()] = 1
}
}
}
// find the keys equal to the length of the input args
numElems := 0
for _, v := range tempMap {
if v == arrLength {
numElems++
}
}
out := reflect.MakeSlice(reflect.TypeOf(arrs[0]), numElems, numElems)
i := 0
for key, val := range tempMap {
if val == arrLength {
v := reflect.ValueOf(key)
o := out.Index(i)
o.Set(v)
i++
}
}
return out, true
}
// Union returns a slice that contains the unique values of all the input slices
//
// [1, 2, 2, 4, 6] & [2, 4, 5] >> [1, 2, 4, 5, 6]
//
// [1, 1, 3, 4, 5, 6] >> [1, 3, 4, 5, 6]
func Union(arrs ...interface{}) (reflect.Value, bool) {
// create a temporary map to hold the contents of the arrays
tempMap := make(map[interface{}]uint8)
var kind reflect.Kind
// write the contents of the arrays as keys to the map. The map values don't matter
for i, arg := range arrs {
tempArr, ok := Distinct(arg)
if !ok {
return reflect.Value{}, ok
}
// check to be sure the type hasn't changed
if i > 0 && tempArr.Index(0).Kind() != kind {
return reflect.Value{}, false
}
kind = tempArr.Index(0).Kind()
c := tempArr.Len()
for idx := 0; idx < c; idx++ {
tempMap[tempArr.Index(idx).Interface()] = 0
}
}
// the map keys are now unique instances of all of the array contents
mapLen := len(tempMap)
out := reflect.MakeSlice(reflect.TypeOf(arrs[0]), mapLen, mapLen)
i := 0
for key := range tempMap {
v := reflect.ValueOf(key)
o := out.Index(i)
o.Set(v)
i++
}
return out, true
}
// Difference returns a slice of values that are only present in one of the input slices
//
// [1, 2, 2, 4, 6] & [2, 4, 5] >> [5, 6]
//
// [1, 1, 3, 4, 5, 6] >> [1, 3, 4, 5, 6]
func Difference(arrs ...interface{}) (reflect.Value, bool) {
// create a temporary map to hold the contents of the arrays
tempMap := make(map[interface{}]int)
var kind reflect.Kind
for i, arg := range arrs {
tempArr, ok := Distinct(arg)
if !ok {
return reflect.Value{}, ok
}
// check to be sure the type hasn't changed
if i > 0 && tempArr.Index(0).Kind() != kind {
return reflect.Value{}, false
}
kind = tempArr.Index(0).Kind()
c := tempArr.Len()
for idx := 0; idx < c; idx++ {
// how many times have we encountered this elem?
if _, ok := tempMap[tempArr.Index(idx).Interface()]; ok {
tempMap[tempArr.Index(idx).Interface()]++
} else {
tempMap[tempArr.Index(idx).Interface()] = 1
}
}
}
// write the final val of the diffMap to an array and return
numElems := 0
for _, v := range tempMap {
if v == 1 {
numElems++
}
}
out := reflect.MakeSlice(reflect.TypeOf(arrs[0]), numElems, numElems)
i := 0
for key, val := range tempMap {
if val == 1 {
v := reflect.ValueOf(key)
o := out.Index(i)
o.Set(v)
i++
}
}
return out, true
}
func takeArg(arg interface{}, kind reflect.Kind) (val reflect.Value, ok bool) {
val = reflect.ValueOf(arg)
if val.Kind() == kind {
ok = true
}
return
}
/* ***************************************************************
*
* THE SECTIONS BELOW ARE DEPRECATED
*
/* *************************************************************** */
/* ***************************************************************
*
* THIS SECTION IS FOR STRINGS
*
/* *************************************************************** */
// IntersectString finds the intersection of two arrays.
//
// Deprecated: use Intersect instead.
func IntersectString(args ...[]string) []string {
// create a map to count all the instances of the strings
arrLength := len(args)
tempMap := make(map[string]int)
for _, arg := range args {
tempArr := DistinctString(arg)
for idx := range tempArr {
// how many times have we encountered this elem?
if _, ok := tempMap[tempArr[idx]]; ok {
tempMap[tempArr[idx]]++
} else {
tempMap[tempArr[idx]] = 1
}
}
}
// find the keys equal to the length of the input args
tempArray := make([]string, 0)
for key, val := range tempMap {
if val == arrLength {
tempArray = append(tempArray, key)
}
}
return tempArray
}
// IntersectStringArr finds the intersection of two arrays using a multidimensional array as inputs
//
// Deprecated: use Intersect instead.
func IntersectStringArr(arr [][]string) []string {
// create a map to count all the instances of the strings
arrLength := len(arr)
tempMap := make(map[string]int)
for idx1 := range arr {
tempArr := DistinctString(arr[idx1])
for idx2 := range tempArr {
// how many times have we encountered this elem?
if _, ok := tempMap[tempArr[idx2]]; ok {
tempMap[tempArr[idx2]]++
} else {
tempMap[tempArr[idx2]] = 1
}
}
}
// find the keys equal to the length of the input args
tempArray := make([]string, 0)
for key, val := range tempMap {
if val == arrLength {
tempArray = append(tempArray, key)
}
}
return tempArray
}
// UnionString finds the union of two arrays.
//
// Deprecated: use Union instead.
func UnionString(args ...[]string) []string {
// create a temporary map to hold the contents of the arrays
tempMap := make(map[string]uint8)
// write the contents of the arrays as keys to the map. The map values don't matter
for _, arg := range args {
for idx := range arg {
tempMap[arg[idx]] = 0
}
}
// the map keys are now unique instances of all of the array contents
tempArray := make([]string, 0)
for key := range tempMap {
tempArray = append(tempArray, key)
}
return tempArray
}
// UnionStringArr finds the union of two arrays using a multidimensional array as inputs
//
// Deprecated: use Union instead.
func UnionStringArr(arr [][]string) []string {
// create a temporary map to hold the contents of the arrays
tempMap := make(map[string]uint8)
// write the contents of the arrays as keys to the map. The map values don't matter
for idx1 := range arr {
for idx2 := range arr[idx1] {
tempMap[arr[idx1][idx2]] = 0
}
}
// the map keys are now unique instances of all of the array contents
tempArray := make([]string, 0)
for key := range tempMap {
tempArray = append(tempArray, key)
}
return tempArray
}
// DifferenceString finds the difference of two arrays.
//
// Deprecated: use Difference instead.
func DifferenceString(args ...[]string) []string {
// create a temporary map to hold the contents of the arrays
tempMap := make(map[string]int)
for _, arg := range args {
tempArr := DistinctString(arg)
for idx := range tempArr {
// how many times have we encountered this elem?
if _, ok := tempMap[tempArr[idx]]; ok {
tempMap[tempArr[idx]]++
} else {
tempMap[tempArr[idx]] = 1
}
}
}
// write the final val of the diffMap to an array and return
tempArray := make([]string, 0)
for key, val := range tempMap {
if val == 1 {
tempArray = append(tempArray, key)
}
}
return tempArray
}
// DifferenceStringArr finds the difference of two arrays using a multidimensional array as inputs
//
// Deprecated: use Difference instead.
func DifferenceStringArr(arr [][]string) []string {
// create a temporary map to hold the contents of the arrays
tempMap := make(map[string]int)
for idx1 := range arr {
tempArr := DistinctString(arr[idx1])
for idx2 := range tempArr {
// how many times have we encountered this elem?
if _, ok := tempMap[tempArr[idx2]]; ok {
tempMap[tempArr[idx2]]++
} else {
tempMap[tempArr[idx2]] = 1
}
}
}
// write the final val of the diffMap to an array and return
tempArray := make([]string, 0)
for key, val := range tempMap {
if val == 1 {
tempArray = append(tempArray, key)
}
}
return tempArray
}
// DistinctString removes duplicate values from one array.
//
// Deprecated: use Distinct instead.
func DistinctString(arg []string) []string {
tempMap := make(map[string]uint8)
for idx := range arg {
tempMap[arg[idx]] = 0
}
tempArray := make([]string, 0)
for key := range tempMap {
tempArray = append(tempArray, key)
}
return tempArray
}
/* ***************************************************************
*
* THIS SECTION IS FOR uint64's
*
/* *************************************************************** */
// IntersectUint64 finds the intersection of two arrays.
//
// Deprecated: use Intersect instead.
func IntersectUint64(args ...[]uint64) []uint64 {
// create a map to count all the instances of the strings
arrLength := len(args)
tempMap := make(map[uint64]int)
for _, arg := range args {
tempArr := DistinctUint64(arg)
for idx := range tempArr {
// how many times have we encountered this elem?
if _, ok := tempMap[tempArr[idx]]; ok {
tempMap[tempArr[idx]]++
} else {
tempMap[tempArr[idx]] = 1
}
}
}
// find the keys equal to the length of the input args
tempArray := make([]uint64, 0)
for key, val := range tempMap {
if val == arrLength {
tempArray = append(tempArray, key)
}
}
return tempArray
}
// DistinctIntersectUint64 finds the intersection of two arrays of distinct vals.
//
// Deprecated: use Intersect instead.
func DistinctIntersectUint64(args ...[]uint64) []uint64 {
// create a map to count all the instances of the strings
arrLength := len(args)
tempMap := make(map[uint64]int)
for _, arg := range args {
for idx := range arg {
// how many times have we encountered this elem?
if _, ok := tempMap[arg[idx]]; ok {
tempMap[arg[idx]]++
} else {
tempMap[arg[idx]] = 1
}
}
}
// find the keys equal to the length of the input args
tempArray := make([]uint64, 0)
for key, val := range tempMap {
if val == arrLength {
tempArray = append(tempArray, key)
}
}
return tempArray
}
func sortedIntersectUintHelper(a1 []uint64, a2 []uint64) []uint64 {
intersection := make([]uint64, 0)
n1 := len(a1)
n2 := len(a2)
i := 0
j := 0
for i < n1 && j < n2 {
switch {
case a1[i] > a2[j]:
j++
case a2[j] > a1[i]:
i++
default:
intersection = append(intersection, a1[i])
i++
j++
}
}
return intersection
}
// SortedIntersectUint64 finds the intersection of two sorted arrays.
//
// Deprecated: use Intersect instead.
func SortedIntersectUint64(args ...[]uint64) []uint64 {
// create an array to hold the intersection and write the first array to it
tempIntersection := args[0]
argsLen := len(args)
for k := 1; k < argsLen; k++ {
// do we have any intersections?
switch len(tempIntersection) {
case 0:
// nope! Give them an empty array!
return tempIntersection
default:
// yup, keep chugging
tempIntersection = sortedIntersectUintHelper(tempIntersection, args[k])
}
}
return tempIntersection
}
// IntersectUint64Arr finds the intersection of two arrays using a multidimensional array as inputs
//
// Deprecated: use Intersect instead.
func IntersectUint64Arr(arr [][]uint64) []uint64 {
// create a map to count all the instances of the strings
arrLength := len(arr)
tempMap := make(map[uint64]int)
for idx1 := range arr {
tempArr := DistinctUint64(arr[idx1])
for idx2 := range tempArr {
// how many times have we encountered this elem?
if _, ok := tempMap[tempArr[idx2]]; ok {
tempMap[tempArr[idx2]]++
} else {
tempMap[tempArr[idx2]] = 1
}
}
}
// find the keys equal to the length of the input args
tempArray := make([]uint64, 0)
for key, val := range tempMap {
if val == arrLength {
tempArray = append(tempArray, key)
}
}
return tempArray
}
// SortedIntersectUint64Arr finds the intersection of two arrays using a multidimensional array as inputs
//
// Deprecated: use Intersect instead.
func SortedIntersectUint64Arr(arr [][]uint64) []uint64 {
// create an array to hold the intersection and write the first array to it
tempIntersection := arr[0]
argsLen := len(arr)
for k := 1; k < argsLen; k++ {
// do we have any intersections?
switch len(tempIntersection) {
case 0:
// nope! Give them an empty array!
return tempIntersection
default:
// yup, keep chugging
tempIntersection = sortedIntersectUintHelper(tempIntersection, arr[k])
}
}
return tempIntersection
}
// DistinctIntersectUint64Arr finds the intersection of two distinct arrays using a multidimensional array as inputs
//
// Deprecated: use Distinct instead.
func DistinctIntersectUint64Arr(arr [][]uint64) []uint64 {
// create a map to count all the instances of the strings
arrLength := len(arr)
tempMap := make(map[uint64]int)
for idx1 := range arr {
for idx2 := range arr[idx1] {
// how many times have we encountered this elem?
if _, ok := tempMap[arr[idx1][idx2]]; ok {
tempMap[arr[idx1][idx2]]++
} else {
tempMap[arr[idx1][idx2]] = 1
}
}
}
// find the keys equal to the length of the input args
tempArray := make([]uint64, 0)
for key, val := range tempMap {
if val == arrLength {
tempArray = append(tempArray, key)
}
}
return tempArray
}
// UnionUint64 finds the union of two arrays.
//
// Deprecated: use Union instead.
func UnionUint64(args ...[]uint64) []uint64 {
// create a temporary map to hold the contents of the arrays
tempMap := make(map[uint64]uint8)
// write the contents of the arrays as keys to the map. The map values don't matter
for _, arg := range args {
for idx := range arg {
tempMap[arg[idx]] = 0
}
}
// the map keys are now unique instances of all of the array contents
tempArray := make([]uint64, 0)
for key := range tempMap {
tempArray = append(tempArray, key)
}
return tempArray
}
// UnionUint64Arr finds the union of two arrays using a multidimensional array as inputs
//
// Deprecated: use Union instead.
func UnionUint64Arr(arr [][]uint64) []uint64 {
// create a temporary map to hold the contents of the arrays
tempMap := make(map[uint64]uint8)
// write the contents of the arrays as keys to the map. The map values don't matter
for idx1 := range arr {
for idx2 := range arr[idx1] {
tempMap[arr[idx1][idx2]] = 0
}
}
// the map keys are now unique instances of all of the array contents
tempArray := make([]uint64, 0)
for key := range tempMap {
tempArray = append(tempArray, key)
}
return tempArray
}
// DifferenceUint64 finds the difference of two arrays.
//
// Deprecated: use Difference instead.
func DifferenceUint64(args ...[]uint64) []uint64 {
// create a temporary map to hold the contents of the arrays
tempMap := make(map[uint64]int)
for _, arg := range args {
tempArr := DistinctUint64(arg)
for idx := range tempArr {
// how many times have we encountered this elem?
if _, ok := tempMap[tempArr[idx]]; ok {
tempMap[tempArr[idx]]++
} else {
tempMap[tempArr[idx]] = 1
}
}
}
// write the final val of the diffMap to an array and return
tempArray := make([]uint64, 0)
for key, val := range tempMap {
if val == 1 {
tempArray = append(tempArray, key)
}
}
return tempArray
}
// DifferenceUint64Arr finds the difference of two arrays using a multidimensional array as inputs.
//
// Deprecated: use Difference instead.
func DifferenceUint64Arr(arr [][]uint64) []uint64 {
// create a temporary map to hold the contents of the arrays
tempMap := make(map[uint64]int)
for idx1 := range arr {
tempArr := DistinctUint64(arr[idx1])
for idx2 := range tempArr {
// how many times have we encountered this elem?
if _, ok := tempMap[tempArr[idx2]]; ok {
tempMap[tempArr[idx2]]++
} else {
tempMap[tempArr[idx2]] = 1
}
}
}
// write the final val of the diffMap to an array and return
tempArray := make([]uint64, 0)
for key, val := range tempMap {
if val == 1 {
tempArray = append(tempArray, key)
}
}
return tempArray
}
// DistinctUint64 removes duplicate values from one array.
//
// Deprecated: use Distinct instead.
func DistinctUint64(arg []uint64) []uint64 {
tempMap := make(map[uint64]uint8)
for idx := range arg {
tempMap[arg[idx]] = 0
}
tempArray := make([]uint64, 0)
for key := range tempMap {
tempArray = append(tempArray, key)
}
return tempArray
}