prefix

package
v0.0.0-...-157c9c8 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2024 License: GPL-3.0 Imports: 5 Imported by: 0

Documentation

Overview

Package prefix holds a prefix tree for strings. The term word is used to refer to values which are present in the tree. Gram is used to refer to any sequence that exists in the tree. For instance is the word 'test' is inserted then the tree contains the gram 'tes' but it will not be a word.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Node

type Node interface {
	// Child looks up the child node by rune
	Child(rune) Node
	// Children returns all the child runes
	Children() []rune
	// ChildrenCount returns the number of children
	ChildrenCount() int
	// IsWord returns true if this Node was inserted as word into the tree
	IsWord() bool
	// Gram returns the string this node represents
	Gram() string
	Suggest(max int) []Suggestion
	// AllWords returns all child nodes (including self) that are a word.
	AllWords() Nodes
}

Node in a prefix tree.

type Nodes

type Nodes []Node

Nodes is a slice of Nodes

func (Nodes) AllWords

func (ns Nodes) AllWords() Nodes

func (Nodes) Strings

func (ns Nodes) Strings() list.Wrapper[string]

Strings returns a list.Wrapper for getting the strings from nodes.

type Prefix

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

Prefix is the root of a prefix tree

func New

func New() *Prefix

New Prefix tree.

func (*Prefix) Containing

func (p *Prefix) Containing(gram string) Nodes

Containing returns all nodes in the tree containing the specified gram, even if they do not begin with the specified gram.

func (*Prefix) Find

func (p *Prefix) Find(gram string) Node

Find a node by it's gram. If there are no prefixes starting with the gram, nil is returned.

func (*Prefix) Purge

func (p *Prefix) Purge()

func (*Prefix) Remove

func (p *Prefix) Remove(word string)

func (*Prefix) Upsert

func (p *Prefix) Upsert(word string) (n Node, insert bool)

Upsert a word into the prefix tree returning the Node for that word and a bool indicating if an insert happened.

type Suggestion

type Suggestion struct {
	Word      string
	Terminals []int
}

func (Suggestion) Words

func (s Suggestion) Words(prefix string) []string

Jump to

Keyboard shortcuts

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