search

package module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 1, 2022 License: MIT Imports: 1 Imported by: 0

README ¶

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

Using constraints.Ordered

Search slices of primitive types like int, float etc.

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

Search slices of custom structs

type ComparableStruct struct {
	value int
}

func (c ComparableStruct) CompareTo(other ComparableStruct) int {
	return c.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.

Documentation ¶

Overview ¶

Package search implements a simple binary search to allow finding the index of any element in an ordered slice of comparable elements

Index ¶

Constants ¶

This section is empty.

Variables ¶

This section is empty.

Functions ¶

func ComparableSlice ¶

func ComparableSlice[T Comparable[T]](slice []T, element T) int

ComparableSlice takes an ordered slice of comparable elements (Comparable[T]) and the element to search for, and performs a binary search on the slice. It returns the index of given element, if found, or otherwise -1

func Slice ¶

func Slice[T constraints.Ordered](slice []T, element T) int

Slice takes an ordered slice of comparable elements (constraints.Ordered) and the element to search for, and performs a binary search on the slice. It returns the index of given element, if found, or otherwise -1

Types ¶

type Comparable ¶

type Comparable[T any] interface {
	CompareTo(other T) int
}

Comparable exposes a method for comparing current struct, with another struct

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL