-
-
Notifications
You must be signed in to change notification settings - Fork 24
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
func SortedGenericV2[T comparable](a []T, b []T, leftGreater Comparator) []T { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ import ( | |
"fmt" | ||
"math" | ||
"math/rand" | ||
"sort" | ||
"testing" | ||
|
||
"github.com/bmizerany/assert" | ||
|
@@ -63,6 +64,39 @@ func TestHash(t *testing.T) { | |
assert.Equal(t, s, []interface{}{2}) | ||
} | ||
|
||
func TestSortedGenericV2(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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{} | ||
|
||
|
@@ -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 | ||
} |
There was a problem hiding this comment.
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.