An IntervalTree package for Go
This package provides functionality for indexing a set of integer intervals (e.g. [start, end)) with corresponding per-interval data based on Wikipedia reference. No interval removal is implemented. Inspired by Centered Interval Tree Python implementation.
Use go get
to install gointervaltree.
go get github.com/danilovkiri/gointervaltree
package main
import (
"fmt"
"github.com/danilovkiri/gointervaltree"
)
func main() {
t, _ := gointervaltree.NewIntervalTree(0, 100)
_ = t.AddInterval(1, 10, []string{"a", "b"})
_ = t.AddInterval(20, 30, []bool{true, false})
_ = t.AddInterval(32, 35, []int{1, 2, 3})
_ = t.AddInterval(32, 38, nil)
t.Sort()
fmt.Println(t.Len())
// 4
fmt.Println(t.Iter())
// [{1 10 [a b]} {32 35 [1 2 3]} {32 38 <nil>} {20 30 [true false]}]
fmt.Println(t.Query(33))
// [{32 35 [1 2 3]} {32 38 <nil>}]
}
Any contribution is appreciated unless no tests are provided and/or updated accordingly.