trie

package
v0.7.16 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2022 License: MIT, MIT Imports: 0 Imported by: 0

README

trie

import "github.com/fufuok/utils/generic/trie"

Package trie provides an implementation of a ternary search trie.

Example

package main

import (
	"fmt"
	"github.com/fufuok/utils/generic/trie"
)

func main() {
	tr := trie.New[int]()
	tr.Put("foo", 1)
	tr.Put("fo", 2)
	tr.Put("bar", 3)

	fmt.Println(tr.Contains("f"))
	fmt.Println(tr.KeysWithPrefix(""))
	fmt.Println(tr.KeysWithPrefix("f"))
}
Output
false
[bar fo foo]
[fo foo]

Index

type Trie

A Trie is a data structure that supports common prefix operations.

type Trie[V any] struct {
    // contains filtered or unexported fields
}
func New
func New[V any]() *Trie[V]

New returns an empty trie.

func (*Trie[V]) Contains
func (t *Trie[V]) Contains(key string) bool

Contains returns whether this trie contains 'key'.

func (*Trie[V]) Get
func (t *Trie[V]) Get(key string) (v V, ok bool)

Get returns the value associated with 'key'.

func (*Trie[V]) Keys
func (t *Trie[V]) Keys() (queue []string)

Keys returns all keys in the trie.

func (*Trie[V]) KeysWithPrefix
func (t *Trie[V]) KeysWithPrefix(prefix string) (queue []string)

KeysWithPrefix returns all keys with prefix 'prefix'.

func (*Trie[V]) LongestPrefix
func (t *Trie[V]) LongestPrefix(query string) string

LongestPrefix returns the key that is the longest prefix of 'query'.

func (*Trie[V]) Put
func (t *Trie[V]) Put(key string, val V)

Put associates 'val' with 'key'.

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

Remove removes the value associated with 'key'.

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

Size returns the size of the trie.

Generated by gomarkdoc

Documentation

Overview

Package trie provides an implementation of a ternary search trie.

Example
package main

import (
	"fmt"

	"github.com/fufuok/utils/generic/trie"
)

func main() {
	tr := trie.New[int]()
	tr.Put("foo", 1)
	tr.Put("fo", 2)
	tr.Put("bar", 3)

	fmt.Println(tr.Contains("f"))
	fmt.Println(tr.KeysWithPrefix(""))
	fmt.Println(tr.KeysWithPrefix("f"))
}
Output:

false
[bar fo foo]
[fo foo]

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 is a data structure that supports common prefix operations.

func New

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

New returns an empty trie.

func (*Trie[V]) Contains

func (t *Trie[V]) Contains(key string) bool

Contains returns whether this trie contains 'key'.

func (*Trie[V]) Get

func (t *Trie[V]) Get(key string) (v V, ok bool)

Get returns the value associated with 'key'.

func (*Trie[V]) Keys

func (t *Trie[V]) Keys() (queue []string)

Keys returns all keys in the trie.

func (*Trie[V]) KeysWithPrefix

func (t *Trie[V]) KeysWithPrefix(prefix string) (queue []string)

KeysWithPrefix returns all keys with prefix 'prefix'.

func (*Trie[V]) LongestPrefix

func (t *Trie[V]) LongestPrefix(query string) string

LongestPrefix returns the key that is the longest prefix of 'query'.

func (*Trie[V]) Put

func (t *Trie[V]) Put(key string, val V)

Put associates 'val' with 'key'.

func (*Trie[V]) Remove

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

Remove removes the value associated with 'key'.

func (*Trie[V]) Size

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

Size returns the size of the trie.

Jump to

Keyboard shortcuts

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