cont

package
v0.14.0 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2025 License: ISC Imports: 5 Imported by: 0

README

Continuous Range

Documentation

Overview

Package cont provides algorithms and data structures for continuous ranges.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EqRange

func EqRange[T Continuous](lhs, rhs Range[T]) bool

EqRange compares two continuous ranges for equality.

Types

type Bound

type Bound[T Continuous] struct {
	Val  T
	Open bool
}

Bound represents a bound in a continuous range.

type Continuous

type Continuous interface {
	~float32 | ~float64
}

Continuous represents continuous numerical types.

type FormatListFunc

type FormatListFunc[T Continuous] func(iter.Seq[Range[T]]) string

FormatListFunc is a function type for formatting a range list into a single string representation.

type FormatMapFunc

type FormatMapFunc[K Continuous, V any] func(iter.Seq2[Range[K], V]) string

FormatMapFunc is a function type for formatting a range map into a single string representation.

type Range

type Range[T Continuous] struct {
	Lo Bound[T]
	Hi Bound[T]
}

Range represents a range of continuous values.

func (Range[T]) Adjacent

func (r Range[T]) Adjacent(rr Range[T]) (bool, bool)

Adjacent checks if two continuous ranges are adjacent. The first return value indicates if r is immediately before rr. The second return value indicates if r is immediately after rr.

func (Range[T]) Equal

func (r Range[T]) Equal(rhs Range[T]) bool

Equal implements the generic.Equaler interface.

func (Range[T]) Includes

func (r Range[T]) Includes(v T) bool

Includes checks if the discrete range includes the given value.

func (Range[T]) Intersect

func (r Range[T]) Intersect(rr Range[T]) RangeOrEmpty[T]

Intersect returns the intersection of two continuous ranges.

func (Range[T]) String

func (r Range[T]) String() string

String implements the fmt.Stringer interface.

func (Range[T]) Subtract

func (r Range[T]) Subtract(rr Range[T]) (RangeOrEmpty[T], RangeOrEmpty[T])

Subtract returns the subtraction of two continuous ranges. It returns two ranges representing the left and right parts of the subtraction.

func (Range[T]) Valid

func (r Range[T]) Valid() bool

Valid determines if the range is valid.

type RangeList

type RangeList[T Continuous] interface {
	fmt.Stringer
	generic.Equaler[RangeList[T]]
	generic.Cloner[RangeList[T]]

	Size() int
	Find(T) (Range[T], bool)
	Add(...Range[T])
	Remove(...Range[T])
	All() iter.Seq[Range[T]]
}

RangeList represents a list of continuous ranges. The ranges are always non-overlapping and sorted.

func NewRangeList

func NewRangeList[T Continuous](opts *RangeListOpts[T], rs ...Range[T]) RangeList[T]

NewRangeList creates a new range list from the given ranges. It panics if any of the provided ranges are invalid.

Ranges stored in the list are always non-overlapping and sorted.

When a new range overlaps existing ranges, the ranges are merged.

type RangeListOpts

type RangeListOpts[T Continuous] struct {
	Format FormatListFunc[T]
}

RangeListOpts holds optional settings for creating a RangeList.

type RangeMap

type RangeMap[K Continuous, V any] interface {
	fmt.Stringer
	generic.Equaler[RangeMap[K, V]]
	generic.Cloner[RangeMap[K, V]]

	Size() int
	Find(K) (Range[K], V, bool)
	Add(Range[K], V)
	Remove(Range[K])
	All() iter.Seq2[Range[K], V]
}

RangeMap represents a map from continuous ranges to values. The ranges are always non-overlapping and sorted.

func NewRangeMap

func NewRangeMap[K Continuous, V any](equal generic.EqualFunc[V], opts *RangeMapOpts[K, V], pairs ...RangeValue[K, V]) RangeMap[K, V]

NewRangeMap creates a new range map from the given ranges. It panics if any of the provided ranges are invalid.

Ranges stored in the map are always non-overlapping and sorted.

When a new range overlaps existing ranges, overlapping portions are resolved as follows:

  • If the existing range's value equals the new range's value, the ranges are merged.
  • If the values differ, the resolver function determines the value for the overlapping part. The default behavior is to use the new range's value (when no resolver is provided). When a custom resolver is provided, the overlapping part will either be merged into the existing range, remain with the new range, or be split out as a separate range with the resolver's returned value.

type RangeMapOpts

type RangeMapOpts[K Continuous, V any] struct {
	Format  FormatMapFunc[K, V]
	Resolve ResolverFunc[V]
}

RangeMapOpts holds optional settings for creating a RangeMap.

type RangeOrEmpty

type RangeOrEmpty[T Continuous] struct {
	Range[T]
	Empty bool
}

RangeOrEmpty represents a continuous range that can be empty.

type RangeValue

type RangeValue[K Continuous, V any] struct {
	Range[K]
	Value V
}

RangeValue associates a continuous range with a value.

type ResolverFunc

type ResolverFunc[V any] func(V, V) V

ResolverFunc is a function type for resolving the conflicting values of overlapping ranges.

Jump to

Keyboard shortcuts

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