Skip to content

Commit

Permalink
#28. Add ArrayIntersect + test
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurkushman committed Mar 28, 2019
1 parent c2182e3 commit ce3794e
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,4 +211,11 @@ pgo.ArrayKeys(map[string]int{"foo": 1, "bar": 8, "fizz": 12, "baz": 0}) // []str
pgo.ArrayKeys(map[interface{}]int{3.45: 32, "foo": 33, 8: 53, "bar": 1, 9: 1}) // []interface{}{3.45, "foo", 8, "bar", 9}
```

#### ArrayIntersect
computes the intersection of arrays
```go
pgo.ArrayIntersect([]int{12, 54, 32, 12, 33}, []int{3, 12, 54, 9}, []int{12, 33, 9}) // []int{12, 54}

pgo.ArrayIntersect([]string{"foo", "bar", "baz", "fizz", "bazz", "fizz", "fizz"}, []string{"bar", "fizz"}, []string{"foo", "bar", "hey"}) // []string{"foo", "bar", "fizz"}
```
See more examples in *_test.go files.
39 changes: 39 additions & 0 deletions array.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,42 @@ func getFloat(unk interface{}) (float64, error) {
fv := v.Convert(floatType)
return fv.Float(), nil
}

// ArrayIntersect computes the intersection of arrays
func ArrayIntersect(arrays ...interface{}) []interface{} {
s := reflect.ValueOf(arrays[0])
len := s.Len()

var result []interface{}
isFound := false

intersected := make(map[interface{}]bool)
for i := 0; i < len; i++ {
needle := s.Index(i).Interface()

for _, v := range arrays[1:] {
switch reflect.TypeOf(v).Kind() {
case reflect.Slice:
ss := reflect.ValueOf(v)
sLen := ss.Len()

for j := 0; j < sLen; j++ {
if needle == ss.Index(j).Interface() && !intersected[needle] {
isFound = true
intersected[needle] = true // del op is more expensive for slices
goto out // it is stupid to iterate O(n^2) if found
}
}
}
}

out:
if isFound {
result = append(result, needle)
}

isFound = false
}

return result
}
28 changes: 28 additions & 0 deletions array_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,3 +249,31 @@ func TestArraySum(t *testing.T) {
}
}
}

var testArrayIntersect = []struct {
values interface{}
diff interface{}
diff2 interface{}
result interface{}
}{
{[]int{12, 54, 32, 12, 33}, []int{3, 12, 54, 9}, []int{12, 33, 9}, []int{12, 54}},
{[]string{"foo", "bar", "baz", "fizz", "bazz", "fizz", "fizz"}, []string{"bar", "fizz"},
[]string{"foo", "bar", "hey"}, []string{"foo", "bar", "fizz"}},
}

func TestArrayIntersect(t *testing.T) {
for _, object := range testArrayIntersect {
res := pgo.ArrayIntersect(object.values, object.diff, object.diff2)

resVal := reflect.ValueOf(object.result)
resLen := resVal.Len()

s := reflect.ValueOf(object.result)

for i := 0; i < resLen; i++ {
if resVal.Index(i).Interface() != s.Index(i).Interface() {
t.Fatalf("want %v, got %v", s.Index(i).Interface(), res[i])
}
}
}
}

0 comments on commit ce3794e

Please sign in to comment.