Repository navigation
Expand file tree
/
Copy pathlogic.go
More file actions
91 lines (81 loc) · 2.44 KB
/
Copy pathlogic.go
File metadata and controls
91 lines (81 loc) · 2.44 KB
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
package simplecsv
import "sort"
// countIndexesInSlices counts in how many slices each integer shows up.
// Duplicates inside one slice are counted only once, so each slice is
// treated as a set.
func countIndexesInSlices(indexes [][]int) map[int]int {
valuesMap := map[int]int{}
for _, toCount := range indexes {
seen := map[int]bool{}
for _, v := range toCount {
if !seen[v] {
seen[v] = true
valuesMap[v]++
}
}
}
return valuesMap
}
// showsMoreThanTimes returns, sorted in ascending order, the ints that show
// at least min times
func showsMoreThanTimes(valuesMap map[int]int, min int) []int {
result := []int{}
for k, v := range valuesMap {
if v >= min {
result = append(result, k)
}
}
sort.Ints(result)
return result
}
// OrIndex operates an OR operator in the indexes
// With no operands it returns an empty slice; with one operand it returns a
// sorted, de-duplicated copy. The result is sorted in ascending order.
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: it returns the values
// present in every index, treating each index as a set (duplicates inside
// one index do not count as presence in the others)
// With no operands it returns an empty slice; with one operand it returns a
// sorted, de-duplicated copy.
// The result is sorted in ascending order
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 {
if v >= min && v <= max {
counter[v] = true
}
}
negativeIndex := []int{}
for i := min; i <= max; i++ {
if counter[i] == false {
negativeIndex = append(negativeIndex, i)
}
}
return negativeIndex
}