https://github.com/lestrrat-go/trie
Trie tree with arbitrary keys and values
https://github.com/lestrrat-go/trie
Last synced: 4 months ago
JSON representation
Trie tree with arbitrary keys and values
- Host: GitHub
- URL: https://github.com/lestrrat-go/trie
- Owner: lestrrat-go
- License: mit
- Created: 2024-10-12T22:36:27.000Z (9 months ago)
- Default Branch: v2
- Last Pushed: 2024-12-12T00:10:14.000Z (7 months ago)
- Last Synced: 2025-01-09T03:41:40.977Z (6 months ago)
- Language: Go
- Size: 61.5 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# github.com/lestrrat-go/trie  [](https://pkg.go.dev/github.com/lestrrat-go/trie)
This trie is implemented such that generic Key types can be used.
Most other trie implementations are optimized for string based keys, but my use
case is to match certain numeric opcodes to arbitrary data.```go
package trie_testimport (
"fmt""github.com/lestrrat-go/trie/v2"
)func Example() {
tree := trie.New[string, rune, any](trie.String())// Put values in the trie
_ = tree.Put("foo", "one")
_ = tree.Put("bar", 2)
_ = tree.Put("baz", 3.0)
_ = tree.Put("日本語", []byte{'f', 'o', 'u', 'r'})// Get a value from the trie
v, ok := tree.Get("日本語")
if !ok {
fmt.Printf("failed to find key '日本語'\n")
return
}
_ = v// Delete a key from the trie
if !tree.Delete("日本語") {
fmt.Printf("failed to delete key '日本語'\n")
return
}// This time Get() should fail
v, ok = tree.Get("日本語")
if ok {
fmt.Printf("key '日本語' should not exist\n")
return
}
_ = v// OUTPUT:
}
```
source: [trie_example_test.go](https://github.com/lestrrat-go/trie/blob/refs/heads/v2/trie_example_test.go)# Acknowledgements
This repository started as a fork of github.com/koron/go-trie. It has been re-created to avoid GitHub forcing PRs to be made against the upstream source instead of this library.