imbue11235 / search

A simple binary search library, in Go, using generics

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

search Test Status Go Reference

A generic simple binary search library to allow finding the index of any element in an ordered slice of comparable elements

πŸ›  Installation

Make sure to have Go installed (Version 1.18 or higher).

Install search with go get:

$ go get -u github.com/imbue11235/search

πŸ’» Usage

Search slices of primitive types like int, float etc.

search.Slice([]int{1, 2, 3, 4, 5, 6}, 2) // => index: 1

Search slices of custom structs

type ComparableStruct struct {
	value int
}

func (c ComparableStruct) CompareTo(other ComparableStruct) int {
	return c.value - other.value
}

element := ComparableStruct{2}
list := []ComparableStruct{{1}, {2}, {3}, {4}, {5}, {6}}

search.ComparableSlice(list, element) // => index: 1

πŸ“œ License

This project is licensed under the MIT license.

About

A simple binary search library, in Go, using generics

License:MIT License


Languages

Language:Go 93.4%Language:Makefile 6.6%