https://github.com/hashicorp/go-immutable-radix
An immutable radix tree implementation in Golang
https://github.com/hashicorp/go-immutable-radix
Last synced: 10 months ago
JSON representation
An immutable radix tree implementation in Golang
- Host: GitHub
- URL: https://github.com/hashicorp/go-immutable-radix
- Owner: hashicorp
- License: mpl-2.0
- Created: 2015-06-01T19:17:46.000Z (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2025-04-17T06:59:03.000Z (11 months ago)
- Last Synced: 2025-05-03T07:37:28.540Z (11 months ago)
- Language: Go
- Size: 854 KB
- Stars: 1,038
- Watchers: 291
- Forks: 78
- Open Issues: 15
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
- go-awesome - go-immutable-radix - Radix 树 (开源类库 / 数据结构)
- my-awesome - hashicorp/go-immutable-radix - 01 star:1.1k fork:0.1k An immutable radix tree implementation in Golang (Go)
README
go-immutable-radix [](https://github.com/hashicorp/go-immutable-radix/actions/workflows/ci.yaml)
=========
Provides the `iradix` package that implements an immutable [radix tree](http://en.wikipedia.org/wiki/Radix_tree).
The package only provides a single `Tree` implementation, optimized for sparse nodes.
As a radix tree, it provides the following:
* O(k) operations. In many cases, this can be faster than a hash table since
the hash function is an O(k) operation, and hash tables have very poor cache locality.
* Minimum / Maximum value lookups
* Ordered iteration
A tree supports using a transaction to batch multiple updates (insert, delete)
in a more efficient manner than performing each operation one at a time.
For a mutable variant, see [go-radix](https://github.com/armon/go-radix).
V2
==
The v2 of go-immutable-radix introduces generics to improve compile-time type
safety for users of the package. The module name for v2 is
`github.com/hashicorp/go-immutable-radix/v2`.
Documentation
=============
The full documentation is available on [Godoc](http://godoc.org/github.com/hashicorp/go-immutable-radix).
Example
=======
Below is a simple example of usage
```go
// Create a tree
r := iradix.New[int]()
r, _, _ = r.Insert([]byte("foo"), 1)
r, _, _ = r.Insert([]byte("bar"), 2)
r, _, _ = r.Insert([]byte("foobar"), 2)
// Find the longest prefix match
m, _, _ := r.Root().LongestPrefix([]byte("foozip"))
if string(m) != "foo" {
panic("should be foo")
}
```
Here is an example of performing a range scan of the keys.
```go
// Create a tree
r := iradix.New[int]()
r, _, _ = r.Insert([]byte("001"), 1)
r, _, _ = r.Insert([]byte("002"), 2)
r, _, _ = r.Insert([]byte("005"), 5)
r, _, _ = r.Insert([]byte("010"), 10)
r, _, _ = r.Insert([]byte("100"), 10)
// Range scan over the keys that sort lexicographically between [003, 050)
it := r.Root().Iterator()
it.SeekLowerBound([]byte("003"))
for key, _, ok := it.Next(); ok; key, _, ok = it.Next() {
if string(key) >= "050" {
break
}
fmt.Println(string(key))
}
// Output:
// 005
// 010
```