gointervaltree

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2022 License: MIT Imports: 3 Imported by: 0

README

GoIntervalTree

An IntervalTree package for Go


Description

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.

Installation

Use go get to install gointervaltree.

go get github.com/danilovkiri/gointervaltree

Usage

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

Contributing

Any contribution is appreciated unless no tests are provided and/or updated accordingly.

License

FOSSA Status

Documentation

Overview

Package gointervaltree provides functionality for indexing a set of integer intervals, e.g. [start, end) based on http://en.wikipedia.org/wiki/Interval_tree. Copyright 2022, Kirill Danilov. Licensed under MIT license.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewIntervalTree

func NewIntervalTree[T constraints.Signed](min, max T) (*intervalTree[T], error)

NewIntervalTree creates and returns an IntervalTree object.

Example
t, _ := 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())
fmt.Println(t.Iter())
fmt.Println(t.Query(33))
Output:

4
[{1 10 [a b]} {32 35 [1 2 3]} {32 38 <nil>} {20 30 [true false]}]
[{32 35 [1 2 3]} {32 38 <nil>}]

Types

This section is empty.

Jump to

Keyboard shortcuts

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