Debian Codegraph @

utils/utils.go

raw symbols SCIP
1 //Some additional utilities that are useful when processing CSV headers and data.
2 package utils
3
4 type Index map[string]int
5
6 // Return a map that maps each string in the input slice to its index in the slice.
7 func NewIndex(a []string) Index {
8 index := make(map[string]int)
9 for i, v := range a {
10 index[v] = i
11 }
12 return index
13 }
14
15 // Answer true if the index contains the specified string.
16 func (i Index) Contains(k string) bool {
17 _, ok := i[k]
18 return ok
19 }
20
21 // Calculate the intersection between two string slices. The first returned slice
22 // is the intersection between the two slices. The second returned slice is
23 // a slice of elements in the first slice but not the second. The third returned
24 // slice is a slice of elements in the second slice but not the first.
25 func Intersect(a []string, b []string) ([]string, []string, []string) {
26 index := NewIndex(a)
27 result := make([]string, 0, len(b))
28 aNotB := make([]string, len(a), len(a))
29 copy(aNotB, a)
30 bNotA := make([]string, 0, len(b))
31 for _, v := range b {
32 if i, ok := index[v]; ok {
33 result = append(result, v)
34 aNotB[i] = ""
35 } else {
36 bNotA = append(bNotA, v)
37 }
38 }
39 i := 0
40 for j := range a {
41 present := (aNotB[j] == a[j])
42 aNotB[i] = a[j]
43 if present {
44 i++
45 }
46 }
47 aNotB = aNotB[0:i]
48 return result, aNotB, bNotA
49 }