intervalmap

package
v0.0.11 Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2024 License: Apache-2.0 Imports: 11 Imported by: 2

Documentation

Overview

Package intervalmap stores a set of (potentially overlapping) intervals. It supports searching for intervals that overlap user-provided interval.

The implementation uses an 1-D version of Kd tree with randomized surface-area heuristic (http://www.sci.utah.edu/~wald/Publications/2007/ParallelBVHBuild/fastbuild.pdf).

Code generated by generate_randomized_freepool.py. DO NOT EDIT.

Example
newEntry := func(start, limit Key) Entry {
	return Entry{
		Interval: Interval{start, limit},
		Data:     fmt.Sprintf("[%d,%d)", start, limit),
	}
}

doGet := func(tree *T, start, limit Key) string {
	matches := []*Entry{}
	tree.Get(Interval{start, limit}, &matches)
	s := []string{}
	for _, m := range matches {
		s = append(s, m.Data.(string))
	}
	sort.Strings(s)
	return strings.Join(s, ",")
}

tree := New([]Entry{newEntry(1, 4), newEntry(3, 5), newEntry(6, 7)})
fmt.Println(doGet(tree, 0, 2))
fmt.Println(doGet(tree, 0, 4))
fmt.Println(doGet(tree, 4, 6))
fmt.Println(doGet(tree, 4, 7))
Output:

[1,4)
[1,4),[3,5)
[3,5)
[3,5),[6,7)
Example (Gob)

Example_gob is an example of serializing an intervalmap using Gob.

newEntry := func(start, limit Key) Entry {
	return Entry{
		Interval: Interval{start, limit},
		Data:     fmt.Sprintf("[%d,%d)", start, limit),
	}
}

tree := New([]Entry{newEntry(1, 4), newEntry(3, 5), newEntry(6, 7)})

buf := bytes.Buffer{}
enc := gob.NewEncoder(&buf)
if err := enc.Encode(tree); err != nil {
	panic(err)
}
dec := gob.NewDecoder(&buf)
var tree2 *T
if err := dec.Decode(&tree2); err != nil {
	panic(err)
}

doGet := func(tree *T, start, limit Key) string {
	matches := []*Entry{}
	tree.Get(Interval{start, limit}, &matches)
	s := []string{}
	for _, m := range matches {
		s = append(s, m.Data.(string))
	}
	sort.Strings(s)
	return strings.Join(s, ",")
}

fmt.Println(doGet(tree2, 0, 2))
fmt.Println(doGet(tree2, 0, 4))
fmt.Println(doGet(tree2, 4, 6))
fmt.Println(doGet(tree2, 4, 7))
Output:

[1,4)
[1,4),[3,5)
[3,5)
[3,5),[6,7)

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewsearcherFreePool

func NewsearcherFreePool(new func() *searcher, maxSize int) *searcherFreePool

NewsearcherFreePool creates a new free object pool. new should create a new object. It is called when the pool is empty on Get(). maxSize bounds the approx max number of objects that can be stored in the pool. Beyond this limit, Put() call will drop the objects.

Types

type Entry

type Entry struct {
	// Interval defines a half-open interval, [Start,Limit)
	Interval Interval
	// Data is an arbitrary user-defined payload
	Data interface{}
}

Entry represents one interval.

type Interval

type Interval struct {
	// Start is included
	Start Key
	// Limit is excluded.
	Limit Key
}

Interval defines an half-open interval, [Start, Limit).

func (Interval) Empty

func (i Interval) Empty() bool

Empty checks if the interval is empty.

func (Interval) Intersect

func (i Interval) Intersect(j Interval) Interval

Intersect computes i ∩ j.

func (Interval) Intersects

func (i Interval) Intersects(j Interval) bool

Intersects checks if (i∩j) != ∅

func (Interval) Span

func (i Interval) Span(j Interval) Interval

Span computes a minimal interval that spans over both i and j. If either i or j is an empty set, this function returns the other set.

type Key

type Key = int64

Key is the type for interval boundaries.

type T

type T struct {
	// contains filtered or unexported fields
}

T represents the intervalmap. It must be created using New().

func New

func New(ents []Entry) *T

New creates a new tree with the given set of entries. The intervals may overlap, and they need not be sorted.

func (*T) Any added in v0.0.2

func (t *T) Any(interval Interval) bool

Any checks if any of the entries intersect the given interval.

func (*T) Get

func (t *T) Get(interval Interval, ents *[]*Entry)

Get finds all the entries that intersect the given interval and return them in *ents.

func (*T) MarshalBinary added in v0.0.2

func (t *T) MarshalBinary() (data []byte, err error)

MarshalBinary implements encoding.BinaryMarshaler interface. It allows T to be encoded and decoded using Gob.

func (*T) Stats

func (t *T) Stats() TreeStats

Stats returns tree-wide stats.

func (*T) UnmarshalBinary added in v0.0.2

func (t *T) UnmarshalBinary(data []byte) error

UnmarshalBinary implements encoding.BinaryUnmarshaler interface. It allows T to be encoded and decoded using Gob.

type TreeStats

type TreeStats struct {
	// Nodes is the total number of tree nodes.
	Nodes int
	// Nodes is the total number of leaf nodes.
	//
	// Invariant: LeafNodes < Nodes
	LeafNodes int
	// MaxDepth is the max depth of the tree.
	MaxDepth int
	// MaxLeafNodeSize is the maximum len(node.ents) of all nodes in the tree.
	MaxLeafNodeSize int
	// TotalLeafDepth is the sum of depth of all leaf nodes.
	TotalLeafDepth int
	// TotalLeafDepth is the sum of len(node.ents) of all leaf nodes.
	TotalLeafNodeSize int
}

TreeStats shows tree-wide stats.

Jump to

Keyboard shortcuts

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