Hi, there is an experimental package golang.org/x/exp/slices which has a generic binary search. I have not much to add but hope it helps.

https://pkg.go.dev/golang.org/x/exp/slices
https://cs.opensource.google/go/x/exp/+/79cabaa2:slices/sort.go;l=64

On 7/19/22 14:53, Slawomir Pryczek wrote:
Hi Guys, is it possible to implement generic, efficient binary search using generics or interfaces. So i'll have some index, and data inside single struct and then could just define a comparison function between 2 variables of same type index which will return bool.

Will have 20-30 million datapoints

type abc struct {
index  uint32
data1 []byte
data2 []string
}
type bcd struct {
index  [4]byte
data1 []byte
data2 []string
}

a = []abc{...}
b = []bcd{...}
find(a, 217621)
find(b, [4]byte{1,2,54,11})

Currently i have this, which is probably incorrect:
type comparable[TC any] interface {
compare(TC, TC) bool
}

func bin[T comparable](data []T, find T) int {
}

Is that even possible to do efficiently or i should just go with writing separated code for each struct type ?

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com <mailto:golang-nuts+unsubscr...@googlegroups.com>. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/a7e769c3-70a7-4aa6-96a5-453a55db3e60n%40googlegroups.com <https://groups.google.com/d/msgid/golang-nuts/a7e769c3-70a7-4aa6-96a5-453a55db3e60n%40googlegroups.com?utm_medium=email&utm_source=footer>.

--
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/aec771c8-daa9-5a3b-ebcf-28f93ee71b10%40mb0.org.

Reply via email to