TreeMap
is a generic key-sorted map using a red-black tree under the hood.
It requires and relies on Go 1.18 generics feature.
Iterators are designed after C++.
package main
import (
"fmt"
"github.com/igrmk/treemap/v2"
)
func main() {
tr := treemap.New[int, string]()
tr.Set(1, "World")
tr.Set(0, "Hello")
for it := tr.Iterator(); it.Valid(); it.Next() {
fmt.Println(it.Key(), it.Value())
}
}
// Output:
// 0 Hello
// 1 World
go get github.com/igrmk/treemap/v2
Name | Time |
---|---|
Set |
O(logN) |
Del |
O(logN) |
Get |
O(logN) |
Contains |
O(logN) |
Len |
O(1) |
Clear |
O(1) |
Range |
O(logN) |
Iterator |
O(1) |
Reverse |
O(logN) |
Iterate through the entire map | O(N) |
TreeMap uses O(N) memory.
The previous version of this package used gotemplate library to generate a type specific file in your local directory. Here is the link to this version treemap v1.
Copyright © 2022 igrmk. This work is free. You can redistribute it and/or modify it under the terms of the Unlicense. See the LICENSE file for more details.