trie

package module
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2024 License: MIT Imports: 3 Imported by: 6

README

trie

Go Report Card GoDoc GitHub license

Trie is a Compressed Prefix Tree Implementation in Golang Generic.

> go test -benchmem -run=^$ -bench . github.com/wzshiming/trie -v
goos: linux
goarch: amd64
pkg: github.com/wzshiming/trie
cpu: Intel(R) Xeon(R) Platinum 8272CL CPU @ 2.60GHz
BenchmarkTrie_Get1
BenchmarkTrie_Get1-4    60400056                19.19 ns/op            0 B/op          0 allocs/op
BenchmarkTrie_Get2
BenchmarkTrie_Get2-4    64019403                19.07 ns/op            0 B/op          0 allocs/op
BenchmarkTrie_Put1
BenchmarkTrie_Put1-4     1234398               939.6 ns/op             0 B/op          0 allocs/op
BenchmarkTrie_Put2
BenchmarkTrie_Put2-4     1000000              1228 ns/op             339 B/op          3 allocs/op
BenchmarkTrie_Put3
BenchmarkTrie_Put3-4     3234558               398.5 ns/op            96 B/op          1 allocs/op
PASS
ok      github.com/wzshiming/trie       7.499s

License

Licensed under the MIT License. See LICENSE for the full license text.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotFound = fmt.Errorf("not found")
)

Functions

This section is empty.

Types

type Mapping added in v0.2.0

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

Mapping is a getter of the trie.

func (*Mapping[T]) Get added in v0.2.0

func (m *Mapping[T]) Get(key []byte) (val T, current *Mapping[T], finish bool)

Get returns the val in the trie for a key.

func (*Mapping[T]) String added in v0.2.0

func (m *Mapping[T]) String() string

type Trie

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

Trie is a trie tree implementation.

func NewTrie

func NewTrie[T any]() *Trie[T]

NewTrie returns a new Trie.

func (*Trie[T]) Depth added in v0.1.0

func (t *Trie[T]) Depth() int

Depth returns the depth of the trie.

func (*Trie[T]) Get

func (t *Trie[T]) Get(key []byte) (val T, current *Mapping[T], finish bool)

Get returns the val in the trie for a key.

func (*Trie[T]) Keys

func (t *Trie[T]) Keys() [][]byte

Keys returns all key of Trie.

func (*Trie[T]) Mapping

func (t *Trie[T]) Mapping() (m *Mapping[T])

Mapping gets the Mapping for get only.

func (*Trie[T]) MatchWithReader added in v0.3.0

func (t *Trie[T]) MatchWithReader(r io.Reader) (handler T, prefix []byte, err error)

MatchWithReader returns most matching handler and prefix bytes data to use for the given reader.

func (*Trie[T]) Put

func (t *Trie[T]) Put(key []byte, val T) (finish bool)

Put sets the val in the trie for a key.

func (*Trie[T]) Size added in v0.1.0

func (t *Trie[T]) Size() int

Size returns the size of the trie.

func (*Trie[T]) String

func (t *Trie[T]) String() string

String returns format trie in a friendly.

func (*Trie[T]) Walk

func (t *Trie[T]) Walk(f func(k []byte, v T))

Walk calls f sequentially for each key and value present in the trie.

Jump to

Keyboard shortcuts

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