critbitgo

package module
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2019 License: MIT Imports: 8 Imported by: 22

README

Build Status

critbitgo

Crit-bit trees in golang and its applications.

This implementation extended to handle the key that contains a null character from C implementation.

Usage

// Create Trie
trie := critbitgo.NewTrie()

// Insert
trie.Insert([]byte("aa"), "value1")
trie.Insert([]byte("bb"), "value2")
trie.Insert([]byte("ab"), "value3")

// Get
v, ok := trie.Get([]byte("aa"))
fmt.Println(v, ok)    // -> value1 true

// Iterate containing keys
trie.Allprefixed([]byte{}, func(key []byte, value interface{}) bool {
    fmt.Println(key, value) // -> [97 97] value1
                            //    [97 98] value3
                            //    [98 98] value2
    return true
})

// Delete
v, ok = trie.Delete([]byte("aa"))
fmt.Println(v, ok)    // -> value1 true
v, ok = trie.Delete([]byte("aa"))
fmt.Println(v, ok)    // -> <nil> false

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Net

type Net struct {
	// contains filtered or unexported fields
}

IP routing table.

func NewNet

func NewNet() *Net

Create IP routing table

func (*Net) Add

func (n *Net) Add(r *net.IPNet, value interface{}) (err error)

Add a route. If `r` is not IPv4/IPv6 network, returns an error.

func (*Net) AddCIDR

func (n *Net) AddCIDR(s string, value interface{}) (err error)

Add a route. If `s` is not CIDR notation, returns an error.

func (*Net) Clear

func (n *Net) Clear()

Deletes all routes.

func (*Net) ContainedIP added in v1.2.0

func (n *Net) ContainedIP(ip net.IP) (contained bool, err error)

Return a bool indicating whether a route would be found

func (*Net) Delete

func (n *Net) Delete(r *net.IPNet) (value interface{}, ok bool, err error)

Delete a specific route. If `r` is not IP4/IPv6 network or a route is not found, `ok` is false.

func (*Net) DeleteCIDR

func (n *Net) DeleteCIDR(s string) (value interface{}, ok bool, err error)

Delete a specific route. If `s` is not CIDR notation or a route is not found, `ok` is false.

func (*Net) Get

func (n *Net) Get(r *net.IPNet) (value interface{}, ok bool, err error)

Get a specific route. If `r` is not IPv4/IPv6 network or a route is not found, `ok` is false.

func (*Net) GetCIDR

func (n *Net) GetCIDR(s string) (value interface{}, ok bool, err error)

Get a specific route. If `s` is not CIDR notation or a route is not found, `ok` is false.

func (*Net) Match

func (n *Net) Match(r *net.IPNet) (route *net.IPNet, value interface{}, err error)

Return a specific route by using the longest prefix matching. If `r` is not IPv4/IPv6 network or a route is not found, `route` is nil.

func (*Net) MatchCIDR

func (n *Net) MatchCIDR(s string) (route *net.IPNet, value interface{}, err error)

Return a specific route by using the longest prefix matching. If `s` is not CIDR notation, or a route is not found, `route` is nil.

func (*Net) MatchIP

func (n *Net) MatchIP(ip net.IP) (route *net.IPNet, value interface{}, err error)

Return a specific route by using the longest prefix matching. If `ip` is invalid IP, or a route is not found, `route` is nil.

func (*Net) Size

func (n *Net) Size() int

Returns number of routes.

func (*Net) Walk added in v1.3.0

func (n *Net) Walk(r *net.IPNet, handle func(*net.IPNet, interface{}) bool)

Walk iterates routes from a given route. handle is called with arguments route and value (if handle returns `false`, the iteration is aborted)

func (*Net) WalkMatch added in v1.4.0

func (n *Net) WalkMatch(r *net.IPNet, handle func(*net.IPNet, interface{}) bool)

WalkMatch interates routes that match a given route. handle is called with arguments route and value (if handle returns `false`, the iteration is aborted)

func (*Net) WalkPrefix added in v1.4.0

func (n *Net) WalkPrefix(r *net.IPNet, handle func(*net.IPNet, interface{}) bool)

WalkPrefix interates routes that have a given prefix. handle is called with arguments route and value (if handle returns `false`, the iteration is aborted)

type SortedMap

type SortedMap struct {
	// contains filtered or unexported fields
}

The map is sorted according to the natural ordering of its keys

func NewSortedMap

func NewSortedMap() *SortedMap

Create a SortedMap

func (*SortedMap) Clear

func (m *SortedMap) Clear()

func (*SortedMap) Contains

func (m *SortedMap) Contains(key string) bool

func (*SortedMap) Delete

func (m *SortedMap) Delete(key string) (value interface{}, ok bool)

func (*SortedMap) Each

func (m *SortedMap) Each(prefix string, handle func(key string, value interface{}) bool) bool

Executes a provided function for each element that has a given prefix. if handle returns `false`, the iteration is aborted.

func (*SortedMap) Get

func (m *SortedMap) Get(key string) (value interface{}, ok bool)

func (*SortedMap) Keys

func (m *SortedMap) Keys() []string

Returns a slice of sorted keys

func (*SortedMap) Set

func (m *SortedMap) Set(key string, value interface{})

func (*SortedMap) Size

func (m *SortedMap) Size() int

type Trie

type Trie struct {
	// contains filtered or unexported fields
}

Crit-bit Tree

func NewTrie

func NewTrie() *Trie

create a tree.

func (*Trie) Allprefixed

func (t *Trie) Allprefixed(prefix []byte, handle func(key []byte, value interface{}) bool) bool

fetching elements with a given prefix. handle is called with arguments key and value (if handle returns `false`, the iteration is aborted)

func (*Trie) Clear

func (t *Trie) Clear()

clearing a tree.

func (*Trie) Contains

func (t *Trie) Contains(key []byte) bool

membership testing.

func (*Trie) Delete

func (t *Trie) Delete(key []byte) (value interface{}, ok bool)

deleting elements. if `key` is in Trie, `ok` is true.

func (*Trie) Dump

func (t *Trie) Dump(w io.Writer)

dump tree. (for debugging)

func (*Trie) Get

func (t *Trie) Get(key []byte) (value interface{}, ok bool)

get member. if `key` is in Trie, `ok` is true.

func (*Trie) Insert

func (t *Trie) Insert(key []byte, value interface{}) bool

insert into the tree. if `key` is alredy in Trie, return false.

func (*Trie) LongestPrefix added in v1.1.0

func (t *Trie) LongestPrefix(given []byte) (key []byte, value interface{}, ok bool)

Search for the longest matching key from the beginning of the given key. if `key` is in Trie, `ok` is true.

func (*Trie) Set

func (t *Trie) Set(key []byte, value interface{})

set into the tree.

func (*Trie) Size

func (t *Trie) Size() int

return the number of key in a tree.

func (*Trie) Walk added in v1.1.0

func (t *Trie) Walk(start []byte, handle func(key []byte, value interface{}) bool) bool

Iterating elements from a given start key. handle is called with arguments key and value (if handle returns `false`, the iteration is aborted)

Jump to

Keyboard shortcuts

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