-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogic.go
88 lines (78 loc) · 2.06 KB
/
logic.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
package simplecsv
// countIndexesInSlices counts how many times an integrer shows up in one or more slices
func countIndexesInSlices(indexes [][]int) map[int]int {
valuesMap := map[int]int{}
var valueExists bool
for _, toCount := range indexes {
for _, v := range toCount {
_, valueExists = valuesMap[v]
if valueExists != true {
valuesMap[v] = 1
} else {
valuesMap[v]++
}
}
}
return valuesMap
}
// showsMoreThanTimes returns an array of ints they show more than the min time
func showsMoreThanTimes(valuesMap map[int]int, min int) []int {
result := []int{}
for k, v := range valuesMap {
if v >= min {
result = append(result, k)
}
}
return result
}
// contains checks if a number exits in a slice
func contains(s []int, e int) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}
// OrIndex operates an OR operator in the indexes
func OrIndex(indexes ...[]int) []int {
minTimes := 1
base := [][]int{}
for _, index := range indexes {
base = append(base, index)
}
valuesMap := countIndexesInSlices(base)
result := showsMoreThanTimes(valuesMap, minTimes)
return result
}
// AndIndex operates an AND operator in the indexes
func AndIndex(indexes ...[]int) []int {
minTimes := len(indexes)
base := [][]int{}
for _, index := range indexes {
base = append(base, index)
}
valuesMap := countIndexesInSlices(base)
result := showsMoreThanTimes(valuesMap, minTimes)
return result
}
// NotIndex negates the index between a min value and a max value
// The min value tipically is either 0 or 1 (without and with headers)
// The max value tipically is the csv length - 1 (number of rows -1).
// A table with a header and 2 rows (length = 3) can have indexes with values of 1 and 2
func NotIndex(index []int, min, max int) []int {
counter := make(map[int]bool)
for i := min; i <= max; i++ {
counter[i] = false
}
for _, v := range index {
counter[v] = true
}
negativeIndex := []int{}
for i := min; i <= max; i++ {
if counter[i] == false {
negativeIndex = append(negativeIndex, i)
}
}
return negativeIndex
}