An immutable radix tree implementation in Golang
Find a file
James Rasell 697f37d261
admin: Remove unused go-version file. (#110)
The .go-version file was not used anywhere in the library and was
a target for automated scanners. These scanners raised issues
about the version detailed which has no relevance to the library
at all.
History 2026-10-26 13:20:41 +00:00
.github [chore] : Bump actions/upload-artifact from 7.0.1 to 7.0.2 (#109) 2026-10-13 07:02:56 +01:00
.gitignore Initial commit 2015-06-01 21:17:47 +02:00
.golangci.yml [COMPLIANCE] Add Copyright and License Headers 2025-11-03 13:23:32 +00:00
CHANGELOG.md changelog: add note for v2 2022-12-16 10:55:53 -06:00
edges.go [COMPLIANCE] Update Copyright and License Headers 2025-11-03 13:28:57 +00:00
go.mod [chore] : Bump github.com/hashicorp/golang-lru/v2 from 2.0.0 to 2.0.7 2025-06-06 11:13:44 +05:30
go.sum [chore] : Bump github.com/hashicorp/golang-lru/v2 from 2.0.0 to 2.0.7 2025-06-06 11:13:44 +05:30
iradix.go [COMPLIANCE] Update Copyright and License Headers 2025-11-03 13:28:57 +00:00
iradix_test.go [COMPLIANCE] Update Copyright and License Headers 2025-11-03 13:28:57 +00:00
iter.go [COMPLIANCE] Update Copyright and License Headers 2025-11-03 13:28:57 +00:00
LICENSE [COMPLIANCE] Update Copyright and License Headers 2025-11-03 13:28:57 +00:00
node.go [COMPLIANCE] Update Copyright and License Headers 2025-11-03 13:28:57 +00:00
node_test.go [COMPLIANCE] Update Copyright and License Headers 2025-11-03 13:28:57 +00:00
path_iter.go [COMPLIANCE] Update Copyright and License Headers 2025-11-03 13:28:57 +00:00
path_iter_test.go [COMPLIANCE] Update Copyright and License Headers 2025-11-03 13:28:57 +00:00
raw_iter.go [COMPLIANCE] Update Copyright and License Headers 2025-11-03 13:28:57 +00:00
README.md readme: update noting v2, and fix an example 2022-07-15 09:01:22 -05:00
reverse_iter.go [COMPLIANCE] Update Copyright and License Headers 2025-11-03 13:28:57 +00:00
reverse_iter_test.go [COMPLIANCE] Update Copyright and License Headers 2025-11-03 13:28:57 +00:00

go-immutable-radix Run CI Tests

Provides the iradix package that implements an immutable 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.

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.

Example

Below is a simple example of usage

// 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.

// 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