trie

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2024 License: MIT Imports: 1 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Trie

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

A trie that maps a string to a value, supports unicode.

func New

func New[V any]() Trie[V]

func (*Trie[V]) Get

func (t *Trie[V]) Get(key string) (value V, exist bool)

Return the value and the exist indicator.

If the key exists, it returns (value, true).

Otherwise, it returns (zero value, false).

Example
trie := New[int]()
trie.Insert("hello, 世界", 123)

fmt.Println(trie.Get("hello, world"))
fmt.Println(trie.Get("hello, 世界"))
Output:

0 false
123 true

func (*Trie[V]) Insert

func (t *Trie[V]) Insert(key string, value V)

Insert a key value pair to the trie.

If the key value pair entry already exists, it updates the value.

Example
trie := New[int]()
trie.Insert("hello", 1)
trie.Insert("world", 2)
trie.Insert("你好", 3)
trie.Insert("世界", 4)
fmt.Println(trie.Len())
Output:

4

func (*Trie[V]) Len

func (t *Trie[V]) Len() int

Return the number of element.

func (*Trie[V]) Remove

func (t *Trie[V]) Remove(key string)

Remove key entry from the tree.

Example
trie := New[int]()
trie.Insert("hello, 世界", 123)

value, exist := trie.Get("hello, 世界")
fmt.Println(value, exist)

trie.Remove("hello, 世界")
_, exist = trie.Get("hello, 世界")
fmt.Println(exist)
Output:

123 true
false

Jump to

Keyboard shortcuts

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