core

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Aug 20, 2025 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Distances = map[string]DistanceFunc{
	"euclidean":         Euclidean,
	"squared_euclidean": SquaredEuclidean,
	"manhattan":         Manhattan,
	"cosine":            CosineDistance,
}

Distances is a map of human–readable names to distance functions. You can use it to choose a distance metric by name.

Functions

func CosineDistance

func CosineDistance(a, b []float32) (float64, error)

CosineDistance computes the cosine distance between two vectors.

func Euclidean

func Euclidean(a, b []float32) (float64, error)

Euclidean computes the Euclidean (L2) distance between two vectors.

func GetSeed

func GetSeed() int64

GetSeed receives a seed value for random number generation from the HANN_SEED environment variable.

func Manhattan

func Manhattan(a, b []float32) (float64, error)

Manhattan computes the Manhattan (L1) distance between two vectors.

func NormalizeBatch

func NormalizeBatch(vecs [][]float32)

NormalizeBatch normalizes multiple vectors in parallel using a worker pool.

func NormalizeVector

func NormalizeVector(vec []float32)

NormalizeVector normalizes a single float32 slice using AVX instructions.

func SquaredEuclidean

func SquaredEuclidean(a, b []float32) (float64, error)

SquaredEuclidean computes the squared Euclidean distance between two vectors.

Types

type CPUFeatureLevel

type CPUFeatureLevel int

CPUFeatureLevel defines the level of SIMD support.

const (
	// Fallback indicates no SIMD support.
	Fallback CPUFeatureLevel = 0
	// AVX indicates AVX support.
	AVX CPUFeatureLevel = 1
	// AVX2 indicates AVX2 and FMA support.
	AVX2 CPUFeatureLevel = 2
)

type DistanceFunc

type DistanceFunc func(a, b []float32) (float64, error)

DistanceFunc computes the distance between two vectors. a: the first vector. b: the second vector. Returns the computed distance as a float64 and an error if validation fails.

type Index

type Index interface {

	// Add inserts a vector with a given id into the index.
	// id: the identifier for the vector.
	// vector: the vector to be added.
	// Returns an error if the operation fails.
	Add(id int, vector []float32) error

	// BulkAdd inserts multiple vectors into the index.
	// vectors: a map where the key is the vector id and the value is the vector.
	// Returns an error if the operation fails.
	BulkAdd(vectors map[int][]float32) error

	// Delete removes the vector with the given id from the index.
	// id: the identifier for the vector to be removed.
	// Returns an error if the operation fails.
	Delete(id int) error

	// BulkDelete removes multiple vectors from the index.
	// ids: a slice of vector ids to be removed.
	// Returns an error if the operation fails.
	BulkDelete(ids []int) error

	// Update modifies the vector associated with the given id.
	// id: the identifier for the vector to be updated.
	// vector: the new vector.
	// Returns an error if the operation fails.
	Update(id int, vector []float32) error

	// BulkUpdate updates multiple vectors in the index.
	// updates: a map where the key is the vector id and the value is the new vector.
	// Returns an error if the operation fails.
	BulkUpdate(updates map[int][]float32) error

	// Search returns the ids and distances of the k nearest neighbors for a query vector.
	// query: the vector to search for.
	// k: the number of nearest neighbors to return.
	// Returns a slice of Neighbor structs and an error if the operation fails.
	Search(query []float32, k int) ([]Neighbor, error)

	// Stats returns metadata about the index, such as count and dimensionality.
	// Returns an IndexStats struct containing the metadata.
	Stats() IndexStats

	// Save persists the index state to the specified file.
	// w: the writer to which the index state will be saved.
	// Returns an error if the operation fails.
	Save(w io.Writer) error

	// Load initializes the index from a previously saved state.
	// r: the reader from which the index state will be loaded.
	// Returns an error if the operation fails.
	Load(r io.Reader) error
}

Index represents a generic interface for an approximate nearest neighbors search index. All indexes in Hann must implement the functions defined in this interface.

type IndexStats

type IndexStats struct {
	Count     int    // total number of indexed vectors.
	Dimension int    // dimensionality of vectors.
	Distance  string // name of the distance function used by the index.
}

IndexStats contains metadata about the index.

type Neighbor

type Neighbor struct {
	ID       int     // the identifier of the neighbor.
	Distance float64 // the computed distance to the neighbor.
}

Neighbor holds a neighbor's id and its computed distance.

Jump to

Keyboard shortcuts

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