https://github.com/ajiyoshi-vg/hairetsu
hairetsu is a TRIE implementation by double array.
https://github.com/ajiyoshi-vg/hairetsu
double-array go trie
Last synced: 5 months ago
JSON representation
hairetsu is a TRIE implementation by double array.
- Host: GitHub
- URL: https://github.com/ajiyoshi-vg/hairetsu
- Owner: ajiyoshi-vg
- License: mit
- Created: 2022-01-10T06:44:06.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2024-03-04T11:35:12.000Z (over 2 years ago)
- Last Synced: 2024-06-20T06:38:26.570Z (almost 2 years ago)
- Topics: double-array, go, trie
- Language: Go
- Homepage:
- Size: 313 KB
- Stars: 6
- Watchers: 1
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# hairetsu
hairetsu is a TRIE implementation by double array.
**alpha quality** : things would change.
## feature
* support ExactMatchSearch CommonPrefixSearch
* can use any binary as a label
* including `\0`
* Some other implementations treat `\0` as the end of string, so that they can't use keys including `\0` as a label.
* can customize the character code of the label
* can use as a key value store
* can store 30bit uint value as a leaf
## how to use
build byte based TRIE and query
```go
func TestByteTrie(t *testing.T) {
data := [][]byte{
[]byte("aa"),
[]byte("aaa"),
[]byte("ab"),
[]byte("abb"),
[]byte("abc"),
[]byte("abcd"),
[]byte("b"),
[]byte("ba"),
[]byte("bb"),
[]byte("c"),
[]byte("cd"),
[]byte("cddd"),
[]byte("ccd"),
[]byte("ddd"),
[]byte("eab"),
[]byte("日本語"),
[]byte{math.MaxUint8, 0, math.MaxInt8},
}
x, err := composer.NewBytes(bytes.NewIdentityDict()).ComposeFromSlice(data)
assert.NoError(t, err)
trie := x.Searcher()
for i, x := range data {
actual, err := trie.ExactMatchSearch(x)
assert.NoError(t, err, x)
assert.Equal(t, node.Index(i), actual)
}
ng := [][]byte{
[]byte("a"),
[]byte("aac"),
}
for _, x := range ng {
_, err := trie.ExactMatchSearch(x)
assert.Error(t, err, x)
}
target := []byte("abcedfg")
is, err := trie.CommonPrefixSearch(target)
assert.NoError(t, err)
n := 0
for _, x := range data {
if bytes.HasPrefix(target, x) {
n++
}
}
assert.Equal(t, n, len(is))
}
```
build rune based trie and query
```go
func TestRuneTrie(t *testing.T) {
data := []string{
"aa",
"aaa",
"ab",
"abb",
"abc",
"abcd",
"b",
"ba",
"bb",
"c",
"cd",
"cddd",
"ccd",
"ddd",
"eab",
"日本語",
}
x, err := composer.NewRunes(runes.NewIdentityDict()).ComposeFromSlice(data)
assert.NoError(t, err)
trie := x.Searcher()
for i, x := range data {
actual, err := trie.ExactMatchSearch(x)
assert.NoError(t, err, x)
assert.Equal(t, node.Index(i), actual)
}
ng := []string{
"a",
"aac",
}
for _, x := range ng {
_, err := trie.ExactMatchSearch(x)
assert.Error(t, err, x)
}
target := "abcedfg"
is, err := trie.CommonPrefixSearch(target)
assert.NoError(t, err)
n := 0
for _, x := range data {
if strings.HasPrefix(target, x) {
n++
}
}
assert.Equal(t, n, len(is))
}
```
## test
```bash
$ make test
```