Help us shape the future of Go! Take the 2026 Go Developer Survey

radix

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2025 License: MPL-2.0 Imports: 2 Imported by: 0

Documentation

Overview

This packages contents were originally copied from github.com/armon/go-radix. After the intial copy all the data structures were made to use Go 1.18 generics instead of relying on the use of interface{} or the any type.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Tree

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

Tree implements a radix tree. This can be treated as a Dictionary abstract data type. The main advantage over a standard hash map is prefix-based lookups and ordered iteration,

func New

func New[T any]() *Tree[T]

New returns an empty Tree

func NewFromMap

func NewFromMap[T any](m map[string]T) *Tree[T]

NewFromMap returns a new tree containing the keys from an existing map

func (*Tree[T]) Delete

func (t *Tree[T]) Delete(s string) (T, bool)

Delete is used to delete a key, returning the previous value and if it was deleted

func (*Tree[T]) DeletePrefix

func (t *Tree[T]) DeletePrefix(s string) int

DeletePrefix is used to delete the subtree under a prefix Returns how many nodes were deleted Use this to delete large subtrees efficiently

func (*Tree[T]) Get

func (t *Tree[T]) Get(s string) (T, bool)

Get is used to lookup a specific key, returning the value and if it was found

func (*Tree[T]) Insert

func (t *Tree[T]) Insert(s string, v T) (T, bool)

Insert is used to add a newentry or update an existing entry. Returns true if an existing record is updated.

func (*Tree[T]) Len

func (t *Tree[T]) Len() int

Len is used to return the number of elements in the tree

func (*Tree[T]) LongestPrefix

func (t *Tree[T]) LongestPrefix(s string) (string, T, bool)

LongestPrefix is like Get, but instead of an exact match, it will return the longest prefix match.

func (*Tree[T]) Maximum

func (t *Tree[T]) Maximum() (string, T, bool)

Maximum is used to return the maximum value in the tree

func (*Tree[T]) Minimum

func (t *Tree[T]) Minimum() (string, T, bool)

Minimum is used to return the minimum value in the tree

func (*Tree[T]) ToMap

func (t *Tree[T]) ToMap() map[string]T

ToMap is used to walk the tree and convert it into a map

func (*Tree[T]) Walk

func (t *Tree[T]) Walk(fn WalkFn[T])

Walk is used to walk the tree

func (*Tree[T]) WalkPath

func (t *Tree[T]) WalkPath(path string, fn WalkFn[T])

WalkPath is used to walk the tree, but only visiting nodes from the root down to a given leaf. Where WalkPrefix walks all the entries *under* the given prefix, this walks the entries *above* the given prefix.

func (*Tree[T]) WalkPrefix

func (t *Tree[T]) WalkPrefix(prefix string, fn WalkFn[T])

WalkPrefix is used to walk the tree under a prefix

type WalkFn

type WalkFn[T any] func(s string, v T) bool

WalkFn is used when walking the tree. Takes a key and value, returning if iteration should be terminated.

Jump to

Keyboard shortcuts

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