tree

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 20, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CosineDistance

func CosineDistance(p1, p2 *Point) float32

CosineDistance returns the cosine distance (1 - cosine similarity).

func EuclideanDistance

func EuclideanDistance(p1, p2 *Point) float32

EuclideanDistance returns the Euclidean distance between two points.

Types

type BoundStrategy

type BoundStrategy int

BoundStrategy selects which lower-bound radius to use when pruning.

const (
	// BoundPerNode uses cached per-node subtree radius (tighter pruning).
	BoundPerNode BoundStrategy = iota
	// BoundLevel uses a geometric bound derived from the node level.
	BoundLevel
)

type DistanceFunc

type DistanceFunc func(p1, p2 *Point) float32

DistanceFunc computes the distance between two points.

type DistanceFunction

type DistanceFunction string

DistanceFunction enumerates supported distance metrics for the cover tree.

const (
	DistanceFunctionCosine    DistanceFunction = "cosine"
	DistanceFunctionEuclidean DistanceFunction = "euclidean"
)

func (DistanceFunction) Function

func (d DistanceFunction) Function() DistanceFunc

Function resolves the callable distance implementation.

type Neighbor

type Neighbor struct {
	Point    *Point
	Distance float32
}

Neighbor describes a candidate returned by a kNN search.

type Neighbors

type Neighbors []Neighbor

Neighbors implements heap.Interface sorted by descending distance (max-heap).

func (Neighbors) Len

func (h Neighbors) Len() int

func (Neighbors) Less

func (h Neighbors) Less(i, j int) bool

func (*Neighbors) Pop

func (h *Neighbors) Pop() interface{}

func (*Neighbors) Push

func (h *Neighbors) Push(x interface{})

func (Neighbors) Swap

func (h Neighbors) Swap(i, j int)

type Node

type Node struct {
	// contains filtered or unexported fields
}

Node represents a cover-tree node.

func NewNode

func NewNode(point *Point, level int32, base float32) Node

NewNode constructs a node for the provided point and level.

type Point

type Point struct {
	Magnitude float32
	Vector    []float32
	// contains filtered or unexported fields
}

Point represents a vector in the cover tree.

func NewPoint

func NewPoint(vector ...float32) *Point

NewPoint constructs a point for the given vector.

func (*Point) HasValue

func (p *Point) HasValue() bool

HasValue reports whether the point has an associated value.

type Tree

type Tree[T any] struct {
	// contains filtered or unexported fields
}

Tree represents a cover tree for cosine/euclidean kNN queries.

func NewTree

func NewTree[T any](base float32, distanceFn DistanceFunction) *Tree[T]

NewTree constructs a cover tree with the provided base and distance metric.

func (*Tree[T]) DecodeBinary

func (t *Tree[T]) DecodeBinary(r io.Reader, decodeValue func(io.Reader) (T, error)) error

DecodeBinary reconstructs the tree from the binary stream.

func (*Tree[T]) DistanceFunction

func (t *Tree[T]) DistanceFunction() DistanceFunction

DistanceFunction reports the configured distance metric.

func (*Tree[T]) EncodeBinary

func (t *Tree[T]) EncodeBinary(w io.Writer, encodeValue func(io.Writer, T) error) error

EncodeBinary writes the tree (structure + values) using the provided encoder.

func (*Tree[T]) FindPointByIndex

func (t *Tree[T]) FindPointByIndex(index int32) *Point

FindPointByIndex returns the point for a stored index.

func (*Tree[T]) ForEach

func (t *Tree[T]) ForEach(fn func(value T, point *Point))

ForEach visits each unique value/point pair.

func (*Tree[T]) Insert

func (t *Tree[T]) Insert(value T, point *Point) int32

Insert adds a new value/vector pair to the tree and returns its index.

func (*Tree[T]) KNearestNeighbors

func (t *Tree[T]) KNearestNeighbors(point *Point, k int) []*Neighbor

KNearestNeighbors runs a depth-first kNN search.

func (*Tree[T]) KNearestNeighborsBestFirst

func (t *Tree[T]) KNearestNeighborsBestFirst(point *Point, k int) []*Neighbor

KNearestNeighborsBestFirst performs a best-first search with a node priority queue.

func (*Tree[T]) SetBoundStrategy

func (t *Tree[T]) SetBoundStrategy(s BoundStrategy)

SetBoundStrategy switches the pruning strategy at runtime.

func (*Tree[T]) Value

func (t *Tree[T]) Value(point *Point) T

Value returns the stored value for the given point.

func (*Tree[T]) Values

func (t *Tree[T]) Values(points []*Point) []T

Values resolves the stored values for the provided points.

Jump to

Keyboard shortcuts

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