-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TASK] TRK-496 Improve gofp package. (#19)
- Loading branch information
Showing
9 changed files
with
792 additions
and
13 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package gofp | ||
|
||
// ContainsAll checks if all elements from needles list are in the haystack. | ||
func ContainsAll[T comparable](haystack []T, needles ...T) bool { | ||
// Avoid allocations for a single check. | ||
if len(needles) == 1 { | ||
for _, h := range haystack { | ||
if h == needles[0] { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
checks := make(map[T]struct{}, len(needles)) | ||
for _, n := range needles { | ||
checks[n] = struct{}{} | ||
} | ||
|
||
for _, h := range haystack { | ||
_, ok := checks[h] | ||
if ok { | ||
delete(checks, h) | ||
|
||
if len(checks) == 0 { | ||
return true | ||
} | ||
} | ||
} | ||
|
||
return false | ||
} | ||
|
||
// ContainsAtLeastOne checks if at least one element from needles list is in the haystack. | ||
func ContainsAtLeastOne[T comparable](haystack []T, needles ...T) bool { | ||
// Avoid allocations for a single check. | ||
if len(needles) == 1 { | ||
for _, h := range haystack { | ||
if h == needles[0] { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
checks := make(map[T]struct{}, len(needles)) | ||
for _, n := range needles { | ||
checks[n] = struct{}{} | ||
} | ||
|
||
for _, h := range haystack { | ||
_, ok := checks[h] | ||
if ok { | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
package gofp_test | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/msales/gox/gofp" | ||
) | ||
|
||
func TestContainsAll(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
slice []int64 | ||
contains []int64 | ||
want bool | ||
}{ | ||
{ | ||
name: "found 1 element", | ||
slice: []int64{1, 2, 3, 4, 5, 6}, | ||
contains: []int64{3}, | ||
want: true, | ||
}, | ||
{ | ||
name: "found 3 elements", | ||
slice: []int64{1, 2, 3, 4, 5, 6}, | ||
contains: []int64{3, 5, 6}, | ||
want: true, | ||
}, | ||
{ | ||
name: "found some but not all elements", | ||
slice: []int64{1, 2, 3, 4, 5, 6}, | ||
contains: []int64{3, 5, 6, 7}, | ||
want: false, | ||
}, | ||
{ | ||
name: "found no elements", | ||
slice: []int64{1, 2, 3, 4, 5, 6}, | ||
contains: []int64{7, 9}, | ||
want: false, | ||
}, | ||
{ | ||
name: "found no element", | ||
slice: []int64{1, 2, 3, 4, 5, 6}, | ||
contains: []int64{7}, | ||
want: false, | ||
}, | ||
{ | ||
name: "found all in equal slices", | ||
slice: []int64{1, 2, 3}, | ||
contains: []int64{1, 2, 3}, | ||
want: true, | ||
}, | ||
{ | ||
name: "found some with needles containing all slice elements plus some", | ||
slice: []int64{1, 2, 3}, | ||
contains: []int64{1, 2, 3, 4}, | ||
want: false, | ||
}, | ||
{ | ||
name: "found no element in empty slicee", | ||
slice: []int64{}, | ||
contains: []int64{1, 2, 3, 4}, | ||
want: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got := ContainsAll(tt.slice, tt.contains...) | ||
if tt.want != got { | ||
t.Errorf("Got %+v, want %+v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func BenchmarkContainsAll_Single(b *testing.B) { | ||
haystack := []int64{1, 2, 3, 4, 5, 6} | ||
b.ResetTimer() | ||
|
||
for i := 0; i < b.N; i++ { | ||
ContainsAll(haystack, 3) | ||
} | ||
} | ||
|
||
func BenchmarkContainsAll_Multiple(b *testing.B) { | ||
haystack := []int64{1, 2, 3, 4, 5, 6} | ||
b.ResetTimer() | ||
|
||
for i := 0; i < b.N; i++ { | ||
ContainsAll(haystack, 2, 3) | ||
} | ||
} | ||
|
||
func TestContainsAtLeastOne(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
slice []int64 | ||
contains []int64 | ||
want bool | ||
}{ | ||
{ | ||
name: "found 1 element", | ||
slice: []int64{1, 2, 3, 4, 5, 6}, | ||
contains: []int64{3}, | ||
want: true, | ||
}, | ||
{ | ||
name: "found 3 elements", | ||
slice: []int64{1, 2, 3, 4, 5, 6}, | ||
contains: []int64{3, 5, 6}, | ||
want: true, | ||
}, | ||
{ | ||
name: "found some but not all elements", | ||
slice: []int64{1, 2, 3, 4, 5, 6}, | ||
contains: []int64{3, 5, 6, 7}, | ||
want: true, | ||
}, | ||
{ | ||
name: "found no elements", | ||
slice: []int64{1, 2, 3, 4, 5, 6}, | ||
contains: []int64{7, 9}, | ||
want: false, | ||
}, | ||
{ | ||
name: "found no element", | ||
slice: []int64{1, 2, 3, 4, 5, 6}, | ||
contains: []int64{7}, | ||
want: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got := ContainsAtLeastOne(tt.slice, tt.contains...) | ||
if tt.want != got { | ||
t.Errorf("Got %+v, want %+v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func BenchmarkContainsAtLeastOne_Single(b *testing.B) { | ||
haystack := []int64{1, 2, 3, 4, 5, 6} | ||
b.ResetTimer() | ||
|
||
for i := 0; i < b.N; i++ { | ||
ContainsAtLeastOne(haystack, 3) | ||
} | ||
} | ||
|
||
func BenchmarkContainsAtLeastOne_Multiple(b *testing.B) { | ||
haystack := []int64{1, 2, 3, 4, 5, 6} | ||
b.ResetTimer() | ||
|
||
for i := 0; i < b.N; i++ { | ||
ContainsAtLeastOne(haystack, 2, 3) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package gofp | ||
|
||
// SymDiff returns symmetric difference of 2 slices. | ||
// https://en.wikipedia.org/wiki/Symmetric_difference | ||
func SymDiff[T comparable](slice1, slice2 []T) []T { | ||
checks := make(map[T]int8, len(slice1)+len(slice2)) | ||
idx := make([]T, 0, len(slice1)+len(slice2)) | ||
for _, v := range slice1 { | ||
checks[v]++ | ||
idx = append(idx, v) | ||
} | ||
for _, v := range slice2 { | ||
checks[v]++ | ||
if checks[v] == 1 { | ||
idx = append(idx, v) | ||
} | ||
} | ||
|
||
outer := make([]T, 0, len(slice1)+len(slice2)) | ||
for _, id := range idx { | ||
if checks[id] == 1 { | ||
outer = append(outer, id) | ||
} | ||
} | ||
|
||
return outer | ||
} | ||
|
||
// DiffLeft returns left diff of 2 slices. | ||
func DiffLeft[T comparable](slice1, slice2 []T) []T { | ||
checks := make(map[T]int8, len(slice1)) | ||
for _, v := range slice1 { | ||
checks[v]++ | ||
} | ||
for _, v := range slice2 { | ||
checks[v]++ | ||
} | ||
|
||
outer := make([]T, 0, len(slice1)) | ||
for _, id := range slice1 { | ||
if checks[id] == 1 { | ||
outer = append(outer, id) | ||
} | ||
} | ||
|
||
return outer | ||
} | ||
|
||
// DiffRight returns right diff of 2 slices. | ||
func DiffRight[T comparable](slice1, slice2 []T) []T { | ||
return DiffLeft(slice2, slice1) | ||
} |
Oops, something went wrong.