ranges

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2021 License: BSD-2-Clause Imports: 2 Imported by: 0

Documentation

Overview

Package ranges keeps track of ranges with arbitrary Data attached. Add()ing a range overrides any previous entries for that same range. Remove() forgets about any ranges in the given range. Get() returns which ranges exist and which don't.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Data

type Data interface {
	// Slice returns the Data for a new range with the given start/end bounds. It's guaranteed to be a subset of the previous range.
	Slice(start, end int64) Data
}

Data is whatever you want to store with your ranges. Slice() is called when we're narrowing your range because of Add/Remove calls.

type Entry

type Entry struct {
	Start, End int64
	Data       Data
}

Entry is a single range.

func MergeAdjacent

func MergeAdjacent(entries []Entry, merge func(a, b Entry) (Data, bool)) []Entry

MergeAdjacent merges adjacent ranges together. merge() is called to get the new Data for the merged range. Calls to merge() are guaranteed to have a.End == b.Start. merge() can return false to indicate these shouldn't be merged. MergeAdjacent will try to merge b the range after that if they're adjacent.

type NoData

type NoData struct{}

NoData is the nil implementation of Data. It doesn't store anything but implements the Data interface.

func (NoData) Slice

func (NoData) Slice(start, end int64) Data

type Ranges

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

Ranges contains zero or more ranges with their associated data. The zero Ranges is valid.

func (*Ranges) Add

func (r *Ranges) Add(e Entry)

Add a new entry. Overrides any previously existing ranges in the same interval.

func (Ranges) Count

func (r Ranges) Count() int64

Count return the sum of all Entry.End-End.Start.

func (*Ranges) Get

func (r *Ranges) Get(start, end int64) (found []Entry, missing []Entry)

Get returns all ranges between start and end; and which ranges weren't covered.

func (*Ranges) Remove

func (r *Ranges) Remove(start, end int64)

Remove any ranges between start and end.

Jump to

Keyboard shortcuts

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