trie

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Feb 10, 2026 License: Apache-2.0, MIT Imports: 3 Imported by: 2

Documentation

Overview

Package trie provides an implementation of a XOR Trie.

Package trie provides an implementation of a XOR Trie

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Equal

func Equal[K kad.Key[K], D any](a, b *Trie[K, D]) bool

func Find

func Find[K kad.Key[K], D any](tr *Trie[K, D], kk K) (bool, D)

Find looks for a key in the trie. It reports whether the key was found along with data value held with the key.

func Locate

func Locate[K kad.Key[K], D any](tr *Trie[K, D], target K) (bool, int)

Locate looks for the position of a key in the trie. It reports whether the key was found along with the depth of the leaf reached along the path of the key, regardless of whether the key was found in that leaf.

Types

type Entry

type Entry[K kad.Key[K], D any] struct {
	Key  K
	Data D
}

func Closest

func Closest[K kad.Key[K], D any](tr *Trie[K, D], target K, n int) []Entry[K, D]

type Trie

type Trie[K kad.Key[K], D any] struct {
	// contains filtered or unexported fields
}

Trie is a trie for equal-length bit vectors, which stores values only in the leaves. A node may optionally hold data of type D Trie node invariants: (1) Either both branches are nil, or both are non-nil. (2) If branches are non-nil, key must be nil. (3) If both branches are leaves, then they are both non-empty (have keys).

func Add

func Add[K kad.Key[K], D any](tr *Trie[K, D], kk K, data D) (*Trie[K, D], error)

Add adds the key to trie, returning a new trie if the key was not already in the trie. Add is immutable/non-destructive: the original trie remains unchanged.

func New

func New[K kad.Key[K], D any]() *Trie[K, D]

func Remove

func Remove[K kad.Key[K], D any](tr *Trie[K, D], kk K) (*Trie[K, D], error)

Remove removes the key from the trie. Remove is immutable/non-destructive: the original trie remains unchanged. If the key did not exist in the trie then the original trie is returned.

func (*Trie[K, D]) Add

func (tr *Trie[K, D]) Add(kk K, data D) bool

Add attempts to add a key to the trie, mutating the trie. Returns true if the key was added, false otherwise.

func (*Trie[K, D]) AddMany

func (tr *Trie[K, D]) AddMany(entries ...Entry[K, D]) int

AddMany attempts to add multiple entries to the trie, mutating the trie. Returns the number of entries that were successfully added. Duplicate keys within entries or keys already in the trie are ignored.

Performance: This function sorts entries once (O(N log N)) and removes duplicates in a single pass (O(N)). The sorted entries enable zero-allocation partitioning during recursive insertion via subslicing.

func (*Trie[K, D]) Branch

func (tr *Trie[K, D]) Branch(dir int) *Trie[K, D]

func (*Trie[K, D]) Copy

func (tr *Trie[K, D]) Copy() *Trie[K, D]

func (*Trie[K, D]) Data

func (tr *Trie[K, D]) Data() D

func (*Trie[K, D]) HasKey

func (tr *Trie[K, D]) HasKey() bool

HasKey reports whether the Trie node holds a key.

func (*Trie[K, D]) IsEmptyLeaf

func (tr *Trie[K, D]) IsEmptyLeaf() bool

IsEmptyLeaf reports whether the Trie is a leaf node without branches that also has no key.

func (*Trie[K, D]) IsLeaf

func (tr *Trie[K, D]) IsLeaf() bool

IsLeaf reports whether the Trie is a leaf node. A leaf node has no child branches but may hold a key and data.

func (*Trie[K, D]) IsNonEmptyLeaf

func (tr *Trie[K, D]) IsNonEmptyLeaf() bool

IsNonEmptyLeaf reports whether the Trie is a leaf node without branches but has a key.

func (*Trie[K, D]) Key

func (tr *Trie[K, D]) Key() *K

func (*Trie[K, D]) Remove

func (tr *Trie[K, D]) Remove(kk K) bool

Remove attempts to remove a key from the trie, mutating the trie. Returns true if the key was removed, false otherwise.

func (*Trie[K, D]) Size

func (tr *Trie[K, D]) Size() int

Size returns the number of keys added to the trie.

Jump to

Keyboard shortcuts

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