Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/sorted generic v2 #16

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,21 @@ The average time and allocated bytes are more than halved when using generics, a
| BenchmarkHash/Size_10000-_generics-16 | 1 802 | 652 946 ns/op | 389 466 B/op | 255 allocs/op |


## Sorted Generic V2

`SortedGenericV2` function allows increase performance for the few times, but can be unsafe in some cases cause will change order for elements in the left array.

| **Name** | **Runs** | **Average** | **Allocated** | **Allocations from heap** |
|----------------------------------------------------|------------:|----------------:|--------------:|--------------------------:|
| BenchmarkSortedGeneric/SortedGeneric_-_1-16 | 41 944 108 | 28.25 ns/op | 8 B/op | 1 allocs/op |
| BenchmarkSortedGeneric/SortedGenericV2_-_1-16 | 199 261 370 | 5.992 ns/op | 0 B/op | 0 allocs/op |
| BenchmarkSortedGeneric/SortedGeneric_-_10-16 | 10 445 590 | 111.6 ns/op | 0 B/op | 0 allocs/op |
| BenchmarkSortedGeneric/SortedGenericV2_-_10-16 | 20 496 318 | 57.31 ns/op | 0 B/op | 0 allocs/op |
| BenchmarkSortedGeneric/SortedGeneric_-_100-16 | 745 248 | 1 624 ns/op | 0 B/op | 0 allocs/op |
| BenchmarkSortedGeneric/SortedGenericV2_-_100-16 | 1 909 890 | 626.2 ns/op | 0 B/op | 0 allocs/op |
| BenchmarkSortedGeneric/SortedGeneric_-_1000-16 | 38 613 | 31 097 ns/op | 0 B/op | 0 allocs/op |
| BenchmarkSortedGeneric/SortedGenericV2_-_1000-16 | 188 733 | 6 367 ns/op | 0 B/op | 0 allocs/op |
| BenchmarkSortedGeneric/SortedGeneric_-_10000-16 | 3 799 | 308 313 ns/op | 0 B/op | 0 allocs/op |
| BenchmarkSortedGeneric/SortedGenericV2_-_10000-16 | 9 190 | 134 617 ns/op | 0 B/op | 0 allocs/op |
| BenchmarkSortedGeneric/SortedGeneric_-_100000-16 | 327 | 3 679 350 ns/op | 0 B/op | 0 allocs/op |
| BenchmarkSortedGeneric/SortedGenericV2_-_100000-16 | 1 537 | 783 528 ns/op | 0 B/op | 0 allocs/op |
30 changes: 30 additions & 0 deletions intersect.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,36 @@ func SortedGeneric[T comparable](a []T, b []T) []T {
return set
}

type Comparator func(i, j int) bool

// SortedGenericV2 has complexity: O(n + x) where n is length of the shortest array and x duplicate cases in the longest array.
// Best case complexity: O(n) where n is length of the shortest array (all values unique)
// Worst case complexity: O(n) where n is length of the longest array (all values of the longest array are duplicates of intersect match)
// Warning: Function will change left array order
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general I'm not fan of side effects - if one has a sorted array, then loosing ordering because of a call to a intersect would be suppressing in my humble opinion.

func SortedGenericV2[T comparable](a []T, b []T, leftGreater Comparator) []T {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If one always should use a "leftgreater" comparer, then I would give this as a parameter.

var i, j, k int

for {
if i >= len(a) || j >= len(b) {
break
}
if a[i] == b[j] {
a[k], a[i] = a[i], a[k]
i++
j++
k++
continue
}
if leftGreater(i, j) {
j++
continue
}
i++
continue
}
return a[:k]
}

// Hash has complexity: O(n * x) where x is a factor of hash function efficiency (between 1 and 2)
func HashGeneric[T comparable](a []T, b []T) []T {
set := make([]T, 0)
Expand Down
40 changes: 40 additions & 0 deletions intersect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"math"
"math/rand"
"sort"
"testing"

"github.com/bmizerany/assert"
Expand Down Expand Up @@ -63,6 +64,39 @@ func TestHash(t *testing.T) {
assert.Equal(t, s, []interface{}{2})
}

func TestSortedGenericV2(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like two test cases - Please create a data struct which each test and then loop through them.

a := []int{1}
b := []int{2}
s := SortedGenericV2(a, b, func(i, j int) bool { return a[i] > b[j] })
assert.Equal(t, len(s), 0)
assert.Equal(t, s, []int{})
a = []int{1, 2}
b = []int{2}
s = SortedGenericV2(a, b, func(i, j int) bool { return a[i] > b[j] })
assert.Equal(t, s, []int{2})
assert.Equal(t, a, []int{2, 1})
}

var blackholeSortedGenericV2 []int
var blackholeSortedGeneric []int

func BenchmarkSortedGeneric(b *testing.B) {
for _, v := range []int{1, 10, 100, 1_000, 10_000, 100_000} {
sortedArr1 := createSortedRandomSlice(v)
sortedArr2 := createSortedRandomSlice(v)
b.Run(fmt.Sprintf("SortedGenericV2 - %d", v), func(b *testing.B) {
for i := 0; i < b.N; i++ {
blackholeSortedGenericV2 = SortedGenericV2(sortedArr1, sortedArr2, func(i, j int) bool { return sortedArr1[i] > sortedArr2[j] })
}
})
b.Run(fmt.Sprintf("SortedGeneric - %d", v), func(b *testing.B) {
for i := 0; i < b.N; i++ {
blackholeSortedGeneric = SortedGeneric(sortedArr1, sortedArr2)
}
})
}
}

var blackholeHashGeneric []int
var blackholeHash []interface{}

Expand Down Expand Up @@ -90,3 +124,9 @@ func createRandomSlice(size int) []int {
}
return slice
}

func createSortedRandomSlice(size int) []int {
slice := createRandomSlice(size)
sort.Slice(slice, func(i, j int) bool { return slice[i] < slice[j] })
return slice
}