Skip to content

Commit 37146d3

Browse files
committed
Add readme and example
1 parent 82dd8ef commit 37146d3

File tree

2 files changed

+156
-0
lines changed

2 files changed

+156
-0
lines changed

README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
[![Travis](https://travis-ci.org/spacewander/go-suffix-tree.svg?branch=master)](https://travis-ci.org/spacewander/go-suffix-tree)
2+
[![GoReportCard](http://goreportcard.com/badge/spacewander/go-suffix-tree)](http://goreportcard.com/report/spacewander/go-suffix-tree)
3+
[![codecov.io](https://codecov.io/github/spacewander/go-suffix-tree/coverage.svg?branch=master)](https://codecov.io/github/spacewander/go-suffix-tree?branch=master)
4+
[![license](https://img.shields.io/badge/License-MIT-yellow.svg)](https://github.com/spacewander/go-suffix-tree/blob/master/LICENSE)
5+
[![godoc](https://img.shields.io/badge/godoc-reference-blue.svg)](https://godoc.org/github.com/spacewander/go-suffix-tree)
6+
7+
# go-suffix-tree
8+
9+
This "suffix" package implements a [suffix tree](https://en.wikipedia.org/wiki/Suffix_tree).
10+
11+
As a suffix tree, it allows to lookup a key in O(k) operations.
12+
In some cases(for example, some scenes in our production), this can be faster than a hash table because
13+
the hash function is an O(n) operation, with poor cache locality.
14+
15+
Plus suffix tree is more memory-effective than a hash table.
16+
17+
## Example
18+
19+
A simple use case:
20+
```go
21+
import (
22+
suffix "github.com/spacewander/go-suffix-tree"
23+
)
24+
25+
var (
26+
TubeNameTree *suffix.Tree
27+
TubeNames = []string{
28+
// ...
29+
}
30+
)
31+
32+
func init() {
33+
tree := suffix.NewTree()
34+
for _, s := range TubeNames {
35+
tree.Insert([]byte(s), &s)
36+
}
37+
TubeNameTree = tree
38+
}
39+
40+
func getTubeName(name []byte) *string {
41+
res, found := TubeNameTree.Get(name)
42+
if found {
43+
return res.(*string)
44+
}
45+
return nil
46+
}
47+
```
48+
49+
For more usage, see the [godoc](https://godoc.org/github.com/spacewander/go-suffix-tree).

example_test.go

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package suffix
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
func ExampleInsert() {
8+
tree := NewTree()
9+
tree.Insert([]byte("sth"), "sth")
10+
oldValue, ok := tree.Insert([]byte("sth"), "else")
11+
// Always ok
12+
if ok {
13+
fmt.Println(oldValue.(string))
14+
}
15+
value, found := tree.Get([]byte("sth"))
16+
if found {
17+
fmt.Println(value.(string))
18+
}
19+
// Output:
20+
// sth
21+
// else
22+
}
23+
24+
func ExampleGet() {
25+
tree := NewTree()
26+
tree.Insert([]byte("sth"), "sth")
27+
value, found := tree.Get([]byte("sth"))
28+
if found {
29+
fmt.Println(value.(string))
30+
}
31+
// Output: sth
32+
}
33+
34+
func ExampleLongestSuffix() {
35+
tree := NewTree()
36+
tree.Insert([]byte("table"), "table")
37+
tree.Insert([]byte("able"), "able")
38+
tree.Insert([]byte("present"), "present")
39+
key, value, found := tree.LongestSuffix([]byte("presentable"))
40+
if found {
41+
fmt.Println("Matched key:", string(key))
42+
fmt.Println(value.(string))
43+
}
44+
// Output:
45+
// Matched key: table
46+
// table
47+
}
48+
49+
func ExampleRemove() {
50+
tree := NewTree()
51+
tree.Insert([]byte("sth"), "sth")
52+
oldValue, found := tree.Remove([]byte("sth"))
53+
if found {
54+
fmt.Println(oldValue.(string))
55+
}
56+
_, found = tree.Get([]byte("sth"))
57+
if !found {
58+
fmt.Println("Already removed")
59+
}
60+
// Output:
61+
// sth
62+
// Already removed
63+
}
64+
65+
func ExampleWalk() {
66+
tree := NewTree()
67+
tree.Insert([]byte("able"), 1)
68+
tree.Insert([]byte("table"), 2)
69+
tree.Insert([]byte("presentable"), 3)
70+
tree.Walk(func(key []byte, _ interface{}) (stop bool) {
71+
fmt.Println(string(key))
72+
return false
73+
})
74+
fmt.Println("Walk and stop in the middle:")
75+
tree.Walk(func(key []byte, _ interface{}) (stop bool) {
76+
fmt.Println(string(key))
77+
return string(key) == "able"
78+
})
79+
// Output:
80+
// able
81+
// table
82+
// presentable
83+
// Walk and stop in the middle:
84+
// able
85+
}
86+
87+
func ExampleWalkSuffix() {
88+
tree := NewTree()
89+
tree.Insert([]byte("able"), 1)
90+
tree.Insert([]byte("table"), 2)
91+
tree.Insert([]byte("presentable"), 3)
92+
tree.Insert([]byte("present"), 4)
93+
tree.WalkSuffix([]byte("table"), func(key []byte, _ interface{}) (stop bool) {
94+
fmt.Println(string(key))
95+
return false
96+
})
97+
fmt.Println("Walk and stop in the middle:")
98+
tree.WalkSuffix([]byte("table"), func(key []byte, _ interface{}) (stop bool) {
99+
fmt.Println(string(key))
100+
return string(key) == "table"
101+
})
102+
// Output:
103+
// table
104+
// presentable
105+
// Walk and stop in the middle:
106+
// table
107+
}

0 commit comments

Comments
 (0)