Skip to content

Commit

Permalink
[TASK] TRK-496 Improve gofp package. (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
Skandalik authored Sep 1, 2022
1 parent 4bb2f15 commit a3bc3c4
Show file tree
Hide file tree
Showing 9 changed files with 792 additions and 13 deletions.
12 changes: 0 additions & 12 deletions gofp/any.go

This file was deleted.

59 changes: 59 additions & 0 deletions gofp/contains.go
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
}
157 changes: 157 additions & 0 deletions gofp/contains_test.go
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)
}
}
52 changes: 52 additions & 0 deletions gofp/diff.go
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)
}
Loading

0 comments on commit a3bc3c4

Please sign in to comment.