intervalst

package module
v0.0.0-...-897461c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 21, 2023 License: Apache-2.0 Imports: 4 Imported by: 0

README

Interval Search Tree

Go Reference Go Report Card Codecov

An 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

Installing

$ go get github.com/arjunsk/intervalst

Usage

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)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BytesAffineComparable

type BytesAffineComparable []byte

BytesAffineComparable treats empty byte arrays as > all other byte arrays

func (BytesAffineComparable) Compare

func (b BytesAffineComparable) Compare(c Comparable) int

type Comparable

type Comparable interface {
	// Compare gives the result of a 3-way comparison
	// a.Compare(b) = 1 => a > b
	// a.Compare(b) = 0 => a == b
	// a.Compare(b) = -1 => a < b
	Compare(c Comparable) int
}

Comparable is an interface for trichotomic comparisons.

type Int64Comparable

type Int64Comparable int64

func (Int64Comparable) Compare

func (v Int64Comparable) Compare(c Comparable) int

type Interval

type Interval struct {
	Begin Comparable
	End   Comparable
}

Interval implements a Comparable interval [begin, end) TODO: support different sorts of intervals: (a,b), [a,b], (a, b]

func NewBytesAffineInterval

func NewBytesAffineInterval(begin, end []byte) Interval

func NewBytesAffinePoint

func NewBytesAffinePoint(b []byte) Interval

func NewInt64Interval

func NewInt64Interval(a int64, b int64) Interval

func NewInt64Point

func NewInt64Point(a int64) Interval

func NewStringAffineInterval

func NewStringAffineInterval(begin, end string) Interval

func NewStringAffinePoint

func NewStringAffinePoint(s string) Interval

func NewStringInterval

func NewStringInterval(begin, end string) Interval

func NewStringPoint

func NewStringPoint(s string) Interval

func (*Interval) Compare

func (ivl *Interval) Compare(c Comparable) int

Compare on an interval gives == if the interval overlaps.

type IntervalTree

type IntervalTree interface {
	// Insert adds a node with the given interval into the tree.
	Insert(ivl Interval, val any)
	// Delete removes the node with the given interval from the tree, returning
	// true if a node is in fact removed.
	Delete(ivl Interval) bool
	// Len gives the number of elements in the tree.
	Len() int
	// Height is the number of levels in the tree; one node has height 1.
	Height() int
	// MaxHeight is the expected maximum tree height given the number of nodes.
	MaxHeight() int
	// Visit calls a visitor function on every tree node intersecting the given interval.
	// It will visit each interval [x, y) in ascending order sorted on x.
	Visit(ivl Interval, ivv IntervalVisitor)
	// Find gets the IntervalValue for the node matching the given interval
	Find(ivl Interval) *IntervalValue
	// Intersects returns true if there is some tree node intersecting the given interval.
	Intersects(iv Interval) bool
	// Contains returns true if the interval tree's keys cover the entire given interval.
	Contains(ivl Interval) bool
	// Stab returns a slice with all elements in the tree intersecting the interval.
	Stab(iv Interval) []*IntervalValue
	// Union merges a given interval tree into the receiver.
	Union(inIvt IntervalTree, ivl Interval)
}

IntervalTree represents a (mostly) textbook implementation of the "Introduction to Algorithms" (Cormen et al, 3rd ed.) chapter 13 red-black tree and chapter 14.3 interval tree with search supporting "stabbing queries".

func NewIntervalTree

func NewIntervalTree() IntervalTree

NewIntervalTree returns a new interval tree.

type IntervalValue

type IntervalValue struct {
	Ivl Interval
	Val any
}

IntervalValue represents a range tree node that contains a range and a value.

type IntervalVisitor

type IntervalVisitor func(n *IntervalValue) bool

IntervalVisitor is used on tree searches; return false to stop searching.

type StringAffineComparable

type StringAffineComparable string

StringAffineComparable treats "" as > all other strings

func (StringAffineComparable) Compare

func (s StringAffineComparable) Compare(c Comparable) int

type StringComparable

type StringComparable string

func (StringComparable) Compare

func (s StringComparable) Compare(c Comparable) int

Directories

Path Synopsis
examples
simple command
vistor command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL