Documentation
¶
Overview ¶
Package cont provides algorithms and data structures for continuous ranges.
Index ¶
- func EqRange[T Continuous](lhs, rhs Range[T]) bool
- type Bound
- type Continuous
- type FormatListFunc
- type FormatMapFunc
- type Range
- func (r Range[T]) Adjacent(rr Range[T]) (bool, bool)
- func (r Range[T]) Equal(rhs Range[T]) bool
- func (r Range[T]) Includes(v T) bool
- func (r Range[T]) Intersect(rr Range[T]) RangeOrEmpty[T]
- func (r Range[T]) String() string
- func (r Range[T]) Subtract(rr Range[T]) (RangeOrEmpty[T], RangeOrEmpty[T])
- func (r Range[T]) Valid() bool
- type RangeList
- type RangeListOpts
- type RangeMap
- type RangeMapOpts
- type RangeOrEmpty
- type RangeValue
- type ResolverFunc
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 ¶
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 ¶
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 ¶
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]) Intersect ¶
func (r Range[T]) Intersect(rr Range[T]) RangeOrEmpty[T]
Intersect returns the intersection of two continuous ranges.
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.
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.