Skip to content

Commit

Permalink
chore: update readme and add an example
Browse files Browse the repository at this point in the history
  • Loading branch information
Jia He authored and ihexxa committed May 16, 2022
1 parent 82fe41a commit eb11664
Show file tree
Hide file tree
Showing 5 changed files with 431 additions and 16 deletions.
62 changes: 46 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,65 @@
[![Go Report](https://goreportcard.com/badge/github.com/ihexxa/q-radix)](https://goreportcard.com/report/github.com/ihexxa/q-radix)
[![PkgGoDev](https://pkg.go.dev/badge/github.com/ihexxa/q-radix)](https://pkg.go.dev/github.com/ihexxa/q-radix)

_A simple (400+ lines) radix tree implementation in Go/Golang._
_A simple and serializable radix tree implementation in Go/Golang._

### Features

* good performance ([benchmark](https://github.com/ihexxa/radix-bench))
* more ways to query (Get, GetAllPrefixMatches, GetBestMatch, BFS)
* well tested (unit tests and random tests)
- Simple APIs: Insert, Get, Remove, GetAllPrefixMatches, GetBestMatch.
- Serializable: String() method is supported, then it can be persisted.
- Well tested: it is covered by unit tests and random tests.
- Good performance: [benchmark](https://github.com/ihexxa/radix-bench).

### Examples

Document is [here](https://pkg.go.dev/github.com/ihexxa/q-radix).

```go
// import q-radix
import qradix "github.com/ihexxa/q-radix"
package main

import (
"fmt"

rTree := qradix.NewRTree() // create a new radix tree
ok := rTree.Insert("key", value) // insert value in any type with a string key
treeSize := rTree.Size() // get the size of the radix tree
qradix "github.com/ihexxa/q-radix"
)

val, ok := rTree.Get("key") // get the value by key
func main() {
// create a new radix tree
rTree := qradix.NewRTree()

// override the value of the key if it exists in the radix tree
// and the old value will be returned if it exists in the radix tree
oldVal, ok := rTree.Insert("key", newValue)
ok := rTree.Remove("key") // remove the value from the radix tree
// insert value in any type with a string key
_, err := rTree.Insert("key", "value")
if err != nil {
panic(err)
}

keyValMap, ok := rTree.GetAllPrefixMatches("key") // get all prefix matches of the key
key, val, ok := rTree.GetBestMatch("key") // get the best prefix match of the key
// get the value by key
val, err := rTree.Get("key")
if err != nil {
panic(err)
}

// get all prefix matches of the key
keyValues := rTree.GetAllPrefixMatches("key")
if err != nil {
panic(err)
}
for key, value := range keyValues {
fmt.Println(key, value)
}

// get the longest prefix match of the key
key, val, ok := rTree.GetBestMatch("key")
fmt.Println(key, val, ok)

// override the value of the key if it exists in the radix tree
// and the old value will be returned if it exists
oldValue, err := rTree.Insert("key", "newValue")
ok = rTree.Remove("key") // remove the value from the radix tree
fmt.Println(oldValue, err, ok)

// get the size of the radix tree
fmt.Println(rTree.Size())
}

```
46 changes: 46 additions & 0 deletions examples/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package main

import (
"fmt"

qradix "github.com/ihexxa/q-radix"
)

func main() {
// create a new radix tree
rTree := qradix.NewRTree()

// insert value in any type with a string key
_, err := rTree.Insert("key", "value")
if err != nil {
panic(err)
}

// get the value by key
val, err := rTree.Get("key")
if err != nil {
panic(err)
}

// get all prefix matches of the key
keyValues := rTree.GetAllPrefixMatches("key")
if err != nil {
panic(err)
}
for key, value := range keyValues {
fmt.Println(key, value)
}

// get the longest prefix match of the key
key, val, ok := rTree.GetBestMatch("key")
fmt.Println(key, val, ok)

// override the value of the key if it exists in the radix tree
// and the old value will be returned if it exists
oldValue, err := rTree.Insert("key", "newValue")
ok = rTree.Remove("key") // remove the value from the radix tree
fmt.Println(oldValue, err, ok)

// get the size of the radix tree
fmt.Println(rTree.Size())
}
10 changes: 10 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
module github.com/ihexxa/q-radix

go 1.12

require (
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 // indirect
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d // indirect
github.com/golang/protobuf v1.4.3 // indirect
github.com/google/uuid v1.1.2 // indirect
github.com/kellydunn/go-art v0.0.1 // indirect
github.com/spf13/cobra v1.1.1 // indirect
github.com/stretchr/testify v1.6.1 // indirect
)
Loading

0 comments on commit eb11664

Please sign in to comment.