trietree

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 13, 2025 License: MIT Imports: 9 Imported by: 2

README

koron-go/trietree

PkgGoDev Actions/Go Go Report Card

trietree implement trie-tree and Aho-Corasick algorithm.

How to install or update

$ go get github.com/koron-go/trietree@latest

Desription

The trietree package provides two trie-tree implementations. One is DTree which allows you to dynamically add elements one by one. Another is STree, which is static and cannot add elements to it, but can be serialized and deserialized and is compact. STree can be constructed from DTree. Both trie-trees implement an efficient search based on the Aho–Corasick algorithm.

Japanese

trietree パッケージは2つのトライ木の実装を提供します。 1つは1個ずつ動的に要素を追加できる DTree です。 もう1つは静的で要素の追加はできませんが、シリアライズ・デシリアライズが可能でコンパクトな STree です。 STreeDTree から構築できます。 どちらのトライ木もエイホ–コラシック法に基づく効率の良い探索を実装しています。

Documentation

Overview

Package trietree provides two trie-tree (prefix tree) implementations.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DNode

type DNode struct {
	// Label is a rune assigned this node. The label is unique among sibling
	// nodes
	Label rune

	// EdgeID indicates the node has a corresponding key or not.
	EdgeID int

	// Level is equals key length when EdgeID is not zero.
	Level int

	// Low is sibling nodes which have smaller Label.
	Low *DNode

	// High is sibling node which have greater Label.
	High *DNode

	// Child is a top node of children nods.
	Child *DNode

	// Failure is used as a search destination when the desired Label is not
	// found in Child. This will be filled by FillFailure().
	Failure *DNode
}

DNode is a node of dynamic tree.

func (*DNode) CountAll

func (dn *DNode) CountAll() int

CountAll counts all descended nodes.

func (*DNode) CountChild

func (dn *DNode) CountChild() int

CountChild counts child nodes.

func (*DNode) Get

func (dn *DNode) Get(r rune) *DNode

Get obtains an existing child node for rune.

type DTree

type DTree struct {
	Root DNode
	// contains filtered or unexported fields
}

DTree is dynamic tree.

func (*DTree) FillFailure

func (dt *DTree) FillFailure()

FillFailure fill Failure field with Aho-Corasick algorithm.

func (*DTree) Get

func (dt *DTree) Get(k string) *DNode

Get retrieve a node for key, otherwise returns nil.

func (*DTree) LongestPrefix added in v0.0.2

func (dt *DTree) LongestPrefix(s string) (prefix string, edgeID int)

LongestPrefix finds a longest prefix node/edge matches given s string.

func (*DTree) Predict added in v1.2.0

func (dt *DTree) Predict(query string) iter.Seq[Prediction]

Predict returns an iterator which enumerates Prediction: key suggestions that match the query in the tree.

func (*DTree) PredictIter added in v1.1.0

func (dt *DTree) PredictIter(query string) PredictionIter

PredictIter returns an iterator function PredictionIter, which enumerates Prediction: key suggestions that match the query in the tree.

func (*DTree) Put

func (dt *DTree) Put(k string) int

Put allocates an edge node for k key and emits ID for it. The ID will be greater than zero.

func (*DTree) Scan

func (dt *DTree) Scan(s string, r ScanReporter) error

Scan scans a string to find matched words.

func (*DTree) ScanContext

func (dt *DTree) ScanContext(ctx context.Context, s string, r ScanReporter) error

ScanContext scans a string to find matched words. ScanReporter r will receive reports for each characters when scan.

type Prediction added in v1.1.0

type Prediction struct {
	Start int // Start is start index of key in query.
	End   int // End is end index of key in query.
	ID    int // ID is for edge node identifier.
}

Prediction is identifier of a key.

type PredictionIter added in v1.1.0

type PredictionIter func() *Prediction

PredictionIter is the iterator of Prediction.

type SNode

type SNode struct {
	Label  rune
	Start  int // start index for children (inclusive)
	End    int // end index of children (exclusive)
	Fail   int // index to failure node
	EdgeID int
}

SNode is a node for static tree.

type STree

type STree struct {
	Nodes  []SNode
	Levels []int
}

STree is static tree. It is optimized for serialization.

func Freeze

func Freeze(src *DTree) *STree

Freeze converts dynamic tree to static tree.

func Read

func Read(r io.Reader) (*STree, error)

Read reads static tree from io.Reader.

func (*STree) LongestPrefix added in v0.0.2

func (st *STree) LongestPrefix(s string) (prefix string, edgeID int)

LongestPrefix finds a longest prefix node/edge matches given s string.

func (*STree) Predict added in v1.2.0

func (st *STree) Predict(query string) iter.Seq[Prediction]

Predict returns an iterator which enumerates Prediction: key suggestions that match the query in the tree.

func (*STree) PredictIter added in v1.1.0

func (st *STree) PredictIter(query string) PredictionIter

PredictIter returns an iterator function PredictionIter, which enumerates Prediction: key suggestions that match the query in the tree.

func (*STree) Scan

func (st *STree) Scan(s string, r ScanReporter) error

Scan scans a string to find matched words.

func (*STree) ScanContext

func (st *STree) ScanContext(ctx context.Context, s string, r ScanReporter) error

ScanContext scans a string to find matched words. ScanReporter r will receive reports for each characters when scan.

func (*STree) Write

func (st *STree) Write(w io.Writer) error

Write serializes a tree to io.Writer.

type ScanEvent

type ScanEvent struct {
	Index int
	Label rune
	Nodes []ScanNode
}

ScanEvent is an event which detected in Scan/ScanContext.

type ScanNode

type ScanNode struct {
	ID    int
	Level int
}

ScanNode is scanned node information.

type ScanReportFunc

type ScanReportFunc func(ev ScanEvent)

ScanReportFunc is a utility type to implements ScanReporter.

func (ScanReportFunc) ScanReport

func (f ScanReportFunc) ScanReport(ev ScanEvent)

ScanReport implements a method of ScanReporter.

type ScanReporter

type ScanReporter interface {
	ScanReport(ev ScanEvent)
}

ScanReporter receive reports of scan.

Directories

Path Synopsis
Package trie provides trie (prefix tree) algorithm.
Package trie provides trie (prefix tree) algorithm.
Package trie2 provides two implementations of a trie-tree that can associate arbitrary values to nodes.
Package trie2 provides two implementations of a trie-tree that can associate arbitrary values to nodes.

Jump to

Keyboard shortcuts

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