-
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.
- Loading branch information
Showing
20 changed files
with
1,328 additions
and
90 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1 +1,3 @@ | ||
*.out | ||
*.txt | ||
testdata |
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
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
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 |
---|---|---|
@@ -1,2 +1,4 @@ | ||
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= | ||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= | ||
pgregory.net/rapid v1.1.0 h1:CMa0sjHSru3puNx+J0MIAuiiEV4N0qj8/cMWGBBCsjw= | ||
pgregory.net/rapid v1.1.0/go.mod h1:PY5XlDGj0+V1FCq0o192FdRhpKHGTRIWBgqjDBTrq04= |
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 slice | ||
|
||
func CountUniqueInSorted[T comparable](s []T) int { | ||
out := 0 | ||
var previous T | ||
for i, b := range s { | ||
if i == 0 { | ||
previous = b | ||
out = 1 | ||
continue | ||
} | ||
if b != previous { | ||
out++ | ||
previous = b | ||
} | ||
} | ||
return out | ||
} | ||
|
||
type IndexRange struct { | ||
Offset int | ||
Length int | ||
} | ||
|
||
func GroupSorted[E any, K comparable](s []E, sKeys []K) (map[K]IndexRange, []K) { | ||
numGroups := CountUniqueInSorted(sKeys) | ||
groups := make(map[K]IndexRange, numGroups) | ||
keys := make([]K, numGroups) | ||
{ | ||
var previous K | ||
var previousIdx int | ||
j := 0 | ||
|
||
for i, b := range sKeys { | ||
if i == 0 { | ||
keys[0] = b | ||
previous = b | ||
previousIdx = 0 | ||
j = 1 | ||
continue | ||
} | ||
if b != previous { | ||
groups[previous] = IndexRange{ | ||
Offset: previousIdx, | ||
Length: i - previousIdx, | ||
} | ||
keys[j] = b | ||
j++ | ||
previous = b | ||
previousIdx = i | ||
} | ||
} | ||
groups[previous] = IndexRange{ | ||
Offset: previousIdx, | ||
Length: len(s) - previousIdx, | ||
} | ||
} | ||
return groups, keys | ||
} |
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,14 @@ | ||
package slice | ||
|
||
func OrAlloc[T any](s []T, n int) []T { | ||
if len(s) == n { | ||
return s | ||
} | ||
if len(s) > n { | ||
return s[:n] | ||
} | ||
if cap(s) < n { | ||
return make([]T, n) | ||
} | ||
return s[:n] | ||
} |
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,10 @@ | ||
package slice | ||
|
||
func ReorderInPlace(swap func(i, j int), indices []int) { | ||
for i, targetIdx := range indices { | ||
for targetIdx < i { | ||
targetIdx = indices[targetIdx] | ||
} | ||
swap(i, targetIdx) | ||
} | ||
} |
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,33 @@ | ||
package testrandom | ||
|
||
import "math/rand/v2" | ||
|
||
var Source = rand.New(rand.NewPCG(0xB0, 0xA4)) | ||
|
||
func Query() uint64 { | ||
return Source.Uint64() | ||
} | ||
|
||
func Data(size int) []uint64 { | ||
data := make([]uint64, size) | ||
for i := range data { | ||
data[i] = Source.Uint64() | ||
} | ||
return data | ||
} | ||
|
||
func Labels(size int) []int { | ||
labels := make([]int, size) | ||
for i := range labels { | ||
labels[i] = int(Source.Uint32N(256)) | ||
} | ||
return labels | ||
} | ||
|
||
func Values(size int) []float64 { | ||
labels := make([]float64, size) | ||
for i := range labels { | ||
labels[i] = Source.Float64() | ||
} | ||
return labels | ||
} |
Oops, something went wrong.