https://github.com/arjunsk/intervalst
Golang Interval Search Tree extracted from etcd
https://github.com/arjunsk/intervalst
Last synced: about 1 year ago
JSON representation
Golang Interval Search Tree extracted from etcd
- Host: GitHub
- URL: https://github.com/arjunsk/intervalst
- Owner: arjunsk
- License: apache-2.0
- Created: 2023-10-11T04:41:16.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2023-10-23T20:06:06.000Z (over 2 years ago)
- Last Synced: 2025-01-31T12:29:02.163Z (over 1 year ago)
- Language: Go
- Homepage:
- Size: 1.85 MB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Interval Search Tree
[](https://pkg.go.dev/github.com/arjunsk/intervalst)
[](https://goreportcard.com/report/github.com/arjunsk/intervalst)
[](https://codecov.io/gh/arjunsk/intervalst)
An [interval tree](https://en.wikipedia.org/wiki/Interval_tree) is a balanced binary search tree designed to efficiently
store and query intervals. It allows for queries of the form "find all intervals that overlap with a
given interval" or "find an interval that contains a specific point."
This library is extracted from [etcd](https://github.com/etcd-io/etcd/tree/main)
## Installing
```sh
$ go get github.com/arjunsk/intervalst
```
## Usage
```go
package main
import (
"fmt"
"github.com/arjunsk/intervalst"
)
func main() {
ist := intervalst.NewIntervalTree()
ist.Insert(intervalst.NewInt64Interval(1, 3), "user1")
ist.Insert(intervalst.NewInt64Interval(9, 13), "user2")
ist.Insert(intervalst.NewInt64Interval(7, 20), "user3")
// Output :
// given interval {Begin:4 End:8} overlaps with these existing intervals
// interval_st interval: &{Ivl:{Begin:7 End:20} Val:user3}
findIntervalIntersections(ist, intervalst.NewInt64Interval(4, 8))
// Output :
// given point {Begin:10 End:11} overlaps with these existing intervals
// interval_st interval: &{Ivl:{Begin:7 End:20} Val:user3}
// interval_st interval: &{Ivl:{Begin:9 End:13} Val:user2}
findPointIntersections(ist, intervalst.NewInt64Point(10))
// Output : 3
fmt.Println(ist.Len())
// Output:
// given interval {Begin:4 End:8} does not overlap with any existing intervals
ist.Delete(intervalst.NewInt64Interval(7, 20))
findIntervalIntersections(ist, intervalst.NewInt64Interval(4, 8))
}
func findPointIntersections(ist intervalst.IntervalTree, newPoint intervalst.Interval) {
if ist.Intersects(newPoint) {
fmt.Printf("given point %+v overlaps with these existing intervals \n", newPoint)
rs := ist.Stab(newPoint)
for _, v := range rs {
fmt.Printf("interval_st interval: %+v\n", v)
}
fmt.Println()
}
}
func findIntervalIntersections(ist intervalst.IntervalTree, newInterval intervalst.Interval) {
if ist.Intersects(newInterval) {
fmt.Printf("given interval %+v overlaps with these existing intervals \n", newInterval)
rs := ist.Stab(newInterval)
for _, v := range rs {
fmt.Printf("interval_st interval: %+v\n", v)
}
fmt.Println()
} else {
fmt.Printf("given interval %+v does not overlap with any existing intervals \n", newInterval)
}
}
```