Documentation
¶
Overview ¶
Package granges implements a mathematical interval operation tool.
A range (or "interval") defines the boundaries around a contiguous span of values of some Comparable type; for example, "integers from 1 to 100 inclusive".
Types of ranges ¶
Each end of the range may be bounded or unbounded. If bounded, there is an associated endpoint value, and the range is considered to be either OPEN (does not include the endpoint) or CLOSED (includes the endpoint) on that side. With three possibilities on each side, this yields nine basic types of ranges, enumerated below. (Notation: a square bracket ([]) indicates that the range is CLOSED on that side; a parenthesis (()) means it is either open or unbounded. The construct {x | statement} is read "the set of all x such that statement."
Range Types ¶
- Open: (a..b) -> {x | a < x < b}
- Closed: [a..b] -> {x | a <= x <= b}
- OpenClosed: (a..b] -> {x | a < x <= b}
- ClosedOpen: [a..b) -> {x | a <= x < b}
- GreaterThan: (a..+∞) -> {x | x > a}
- AtLeast: [a..+∞) -> {x | x >= a}
- LessThan: (-∞..b) -> {x | x < b}
- AtMost: (-∞..b] -> {x | x <= b}
- All: (-∞..+∞) -> {x}
When both endpoints exist, the upper endpoint may not be less than the lower. The endpoints may be equal only if at least one of the bounds is closed:
- [a..a] : a singleton range
- [a..a); (a..a] : empty ranges; also valid
- (a..a) : invalid; an exception will be thrown
Warnings ¶
- Use immutable value types only, if at all possible. If you must use a mutable type, do not allow the endpoint instances to mutate after the range is created!
- Your value type's comparison method should be consistent with equals if at all possible. Otherwise, be aware that concepts used throughout this documentation such as "equal", "same", "unique" and so on actually refer to whether Compare returns zero, not whether equals returns true.
Other notes ¶
- All ranges are shallow-immutable.
Index ¶
- Variables
- type BoundType
- type Comparable
- type Cut
- func (c Cut[C]) Compare(other Cut[C]) int
- func (c Cut[C]) DescribeAsLowerBound() string
- func (c Cut[C]) DescribeAsUpperBound() string
- func (c Cut[C]) Endpoint() (endpoint C, err error)
- func (c Cut[C]) Equal(other Cut[C]) bool
- func (c Cut[C]) IsLessThan(value C) bool
- func (c Cut[C]) TypeAsLowerBound() (BoundType, error)
- func (c Cut[C]) TypeAsUpperBound() (BoundType, error)
- type CutType
- type Range
- func All[C Comparable]() Range[C]
- func AtLeast[C Comparable](lower C) Range[C]
- func AtMost[C Comparable](upper C) Range[C]
- func Closed[C Comparable](lower, upper C) Range[C]
- func ClosedE[C Comparable](lower, upper C) (Range[C], error)
- func ClosedOpen[C Comparable](lower, upper C) Range[C]
- func ClosedOpenE[C Comparable](lower, upper C) (Range[C], error)
- func DownTo[C Comparable](endpoint C, boundType BoundType) (Range[C], error)
- func GreaterThan[C Comparable](lower C) Range[C]
- func Invalid[C Comparable]() Range[C]
- func LessThan[C Comparable](upper C) Range[C]
- func New[C Comparable](lower C, lowerType BoundType, upper C, upperType BoundType) Range[C]
- func NewE[C Comparable](lower C, lowerType BoundType, upper C, upperType BoundType) (Range[C], error)
- func Open[C Comparable](lower, upper C) Range[C]
- func OpenClosed[C Comparable](lower, upper C) Range[C]
- func OpenClosedE[C Comparable](lower, upper C) (Range[C], error)
- func OpenE[C Comparable](lower, upper C) (Range[C], error)
- func Singleton[C Comparable](value C) Range[C]
- func UpTo[C Comparable](endpoint C, boundType BoundType) (Range[C], error)
- func (r Range[C]) Contains(value C) bool
- func (r Range[C]) ContainsAll(values []C) bool
- func (r Range[C]) Encloses(other Range[C]) bool
- func (r Range[C]) Equal(other Range[C]) bool
- func (r Range[C]) Gap(other Range[C]) Range[C]
- func (r Range[C]) GapE(other Range[C]) (Range[C], error)
- func (r Range[C]) HasLowerBound() bool
- func (r Range[C]) HasUpperBound() bool
- func (r Range[C]) Intersection(connectedRange Range[C]) Range[C]
- func (r Range[C]) IntersectionE(connectedRange Range[C]) (Range[C], error)
- func (r Range[C]) IsConnected(other Range[C]) bool
- func (r Range[C]) IsEmpty() bool
- func (r Range[C]) IsInvalid() bool
- func (r Range[C]) LowerBoundType() BoundType
- func (r Range[C]) LowerBoundTypeE() (BoundType, error)
- func (r Range[C]) LowerEndpoint() C
- func (r Range[C]) LowerEndpointE() (C, error)
- func (r Range[C]) Span(other Range[C]) Range[C]
- func (r Range[C]) SpanE(other Range[C]) (Range[C], error)
- func (r Range[C]) String() string
- func (r Range[C]) UpperBoundType() BoundType
- func (r Range[C]) UpperBoundTypeE() (BoundType, error)
- func (r Range[C]) UpperEndpoint() C
- func (r Range[C]) UpperEndpointE() (C, error)
Constants ¶
This section is empty.
Variables ¶
Functions ¶
This section is empty.
Types ¶
type BoundType ¶
type BoundType int
BoundType indicates whether an endpoint of some range is contained in the range itself ("closed") or not ("open"). If a range is unbounded on a side, it is neither open nor closed on that side; the bound simply does not exist.
const Unbounded BoundType = -1
type Comparable ¶
type Cut ¶
type Cut[C Comparable] struct { // contains filtered or unexported fields }
Cut is the implementation detail for the internal structure of Range instances. Represents a unique way of "cutting" a "number line" (actually of instances of type C, not necessarily "numbers") into two sections; this can be done below a certain value, above a certain value, below all values or above all values. With this object defined in this way, an interval can always be represented by a pair of Cut instances.
func NewAboveAll ¶
func NewAboveAll[C Comparable]() Cut[C]
func NewAboveValue ¶
func NewAboveValue[C Comparable](value C) Cut[C]
func NewBelowAll ¶
func NewBelowAll[C Comparable]() Cut[C]
func NewBelowValue ¶
func NewBelowValue[C Comparable](value C) Cut[C]
func (Cut[C]) DescribeAsLowerBound ¶
func (Cut[C]) DescribeAsUpperBound ¶
func (Cut[C]) IsLessThan ¶
func (Cut[C]) TypeAsLowerBound ¶
func (Cut[C]) TypeAsUpperBound ¶
type Range ¶
type Range[C Comparable] struct { // contains filtered or unexported fields }
func All ¶
func All[C Comparable]() Range[C]
All returns a range that contains every value of type T.
(-∞..+∞) = {x}
func AtLeast ¶
func AtLeast[C Comparable](lower C) Range[C]
AtLeast returns a range that contains all values greater than or equal to endpoint.
[lower..+∞) = {x | lower <= x}
func AtMost ¶
func AtMost[C Comparable](upper C) Range[C]
AtMost returns a range that contains all values less than or equal to endpoint.
(-∞..upper] = {x | x <= upper}
func Closed ¶
func Closed[C Comparable](lower, upper C) Range[C]
Closed returns a range that contains all values greater than or equal to lower and less than or equal to upper.
[lower..upper] = {x | lower <= x <= upper}
An invalid range will be returned if lower is greater than upper.
func ClosedE ¶
func ClosedE[C Comparable](lower, upper C) (Range[C], error)
ClosedE returns a range that contains all values greater than or equal to lower and less than or equal to upper.
[lower..upper] = {x | lower <= x <= upper}
An invalid range with an error will be returned if lower is greater than upper.
func ClosedOpen ¶
func ClosedOpen[C Comparable](lower, upper C) Range[C]
ClosedOpen returns a range that contains all values greater than or equal to lower and strictly less than upper.
[lower..upper) = {x | lower <= x < upper}
An invalid range will be returned if lower is greater than upper.
func ClosedOpenE ¶
func ClosedOpenE[C Comparable](lower, upper C) (Range[C], error)
ClosedOpenE returns a range that contains all values greater than or equal to lower and strictly less than upper.
[lower..upper) = {x | lower <= x < upper}
An invalid range with an error will be returned if lower is greater than upper.
func DownTo ¶
func DownTo[C Comparable](endpoint C, boundType BoundType) (Range[C], error)
DownTo returns a range from the given endpoint, which may be either inclusive (closed) or exclusive (open), with no upper bound. An empty range with an error will be return if wrong arguments received.
func GreaterThan ¶
func GreaterThan[C Comparable](lower C) Range[C]
GreaterThan returns a range that contains all values strictly greater than endpoint.
(lower..+∞) = {x | lower < x}
func Invalid ¶ added in v1.0.1
func Invalid[C Comparable]() Range[C]
Invalid creates an explicitly invalid range of the specified comparable type.
This method is useful when you need to represent the concept of "no valid range" or signal an error condition in contexts where returning a range is required but no meaningful range can be constructed.
func LessThan ¶
func LessThan[C Comparable](upper C) Range[C]
LessThan returns a range that contains all values strictly less than endpoint.
(-∞..upper) = {x | x < upper}
func New ¶
func New[C Comparable](lower C, lowerType BoundType, upper C, upperType BoundType) Range[C]
New returns a range that contains any value from lower to upper, where each endpoint may be either inclusive (closed) or exclusive (open).
An invalid range will be returned if lower is greater than upper.
func NewE ¶
func NewE[C Comparable](lower C, lowerType BoundType, upper C, upperType BoundType) (Range[C], error)
NewE returns a range that contains any value from lower to upper, where each endpoint may be either inclusive (closed) or exclusive (open).
An invalid range with an error will be returned if lower is greater than upper.
func Open ¶
func Open[C Comparable](lower, upper C) Range[C]
Open returns a range that contains all values strictly greater than lower and strictly less than upper.
(lower..upper) = {x | lower < x < upper}
An invalid range will be returned if lower is greater than or equal to upper.
func OpenClosed ¶
func OpenClosed[C Comparable](lower, upper C) Range[C]
OpenClosed returns a range that contains all values strictly greater than lower and less than or equal to upper.
(lower..upper] = {x | lower < x <= upper}
An invalid range will be returned if lower is greater than upper.
func OpenClosedE ¶
func OpenClosedE[C Comparable](lower, upper C) (Range[C], error)
OpenClosedE returns a range that contains all values strictly greater than lower and less than or equal to upper.
(lower..upper] = {x | lower < x <= upper}
An invalid range with an error will be returned if lower is greater than upper.
func OpenE ¶
func OpenE[C Comparable](lower, upper C) (Range[C], error)
OpenE returns a range that contains all values strictly greater than lower and strictly less than upper.
(lower..upper) = {x | lower < x < upper}
An invalid range with an error will be returned if lower is greater than or equal to upper.
func Singleton ¶
func Singleton[C Comparable](value C) Range[C]
Singleton returns a Range that contains only the given value. The returned range is CLOSED on both ends.
(x) = {x}
func UpTo ¶
func UpTo[C Comparable](endpoint C, boundType BoundType) (Range[C], error)
UpTo returns a range with no lower bound up to the given endpoint, which may be either inclusive (closed) or exclusive (open). An empty range with an error will be return if wrong arguments received.
func (Range[C]) Contains ¶
Contains returns true if value is within the bounds of this range. For example, on the range [0..2), Contains(1) returns true, while Contains(2) returns false.
func (Range[C]) ContainsAll ¶
ContainsAll returns true if every element in values is contained in this range.
func (Range[C]) Encloses ¶
Encloses returns true if the bounds of other do not extend outside the bounds of this range.
Examples:
- [3..6] encloses [4..5]
- (3..6) encloses (3..6)
- [3..6] encloses [4..4) (even though the latter is empty)
- (3..6] does not enclose [3..6]
- [4..5] does not enclose (3..6) (even though it contains every value contained by the latter range)
- [3..6] does not enclose (1..1] (even though it contains every value contained by the latter range)
Note that if a.Encloses(b), then b.Contains(v) implies a.Contains(v), but as the last two examples illustrate, the converse is not always true.
Being reflexive, antisymmetric and transitive, the encloses relation defines a partial order over ranges. There exists a unique maximal range according to this relation, and also numerous minimal ranges. Enclosure also implies connectedness.
func (Range[C]) Equal ¶
Equal returns true if object is a range having the same endpoints and bound types as this range. Note that discrete ranges such as (1..4) and [2..3] are not equal to one another, despite the fact that they each contain precisely the same set of values. Similarly, empty ranges are not equal unless they have exactly the same representation, so [3..3), (3..3], (4..4] are all unequal.
func (Range[C]) Gap ¶
Gap returns the maximal range lying between this range and otherRange, if such a range exists. The resulting range may be empty if the two ranges are adjacent but non-overlapping.
An invalid range will be returned if this range and otherRange have a nonempty intersection.
func (Range[C]) GapE ¶
GapE returns the maximal range lying between this range and otherRange, if such a range exists. The resulting range may be empty if the two ranges are adjacent but non-overlapping.
For example, the gap of [1..5] and (7..10) is (5..7]. The resulting range may be empty; for example, the gap between [1..5) [5..7) yields the empty range [5..5).
The gap exists if and only if the two ranges are either disconnected or immediately adjacent (any intersection must be an empty range).
The gap operation is commutative.
An error will be returned if this range and otherRange have a nonempty intersection.
func (Range[C]) HasLowerBound ¶
HasLowerBound returns true if this range has a lower endpoint.
func (Range[C]) HasUpperBound ¶
HasUpperBound returns true if this range has an upper endpoint.
func (Range[C]) Intersection ¶
Intersection returns the maximal range enclosed by both this range and connectedRange, if such a range exists.
An invalid range will be returned for disconnected ranges.
func (Range[C]) IntersectionE ¶
IntersectionE returns the maximal range enclosed by both this range and connectedRange, if such a range exists.
For example, the intersection of [1..5] and (3..7) is (3..5]. The resulting range may be empty; for example, [1..5) intersected with [5..7) yields the empty range [5..5).
The intersection exists if and only if the two ranges are connected.
The intersection operation is commutative, associative and idempotent, and its identity element is All.
An error will be returned for disconnected ranges.
func (Range[C]) IsConnected ¶
IsConnected returns true if there exists a (possibly empty) range which is enclosed by both this range and other.
For example,
- [2, 4) and [5, 7) are not connected
- [2, 4) and [3, 5) are connected, because both enclose [3, 4)
- [2, 4) and [4, 6) are connected, because both enclose the empty range [4, 4)
Note that this range and other have a well-defined union and intersection (as a single, possibly-empty range) if and only if this method returns true.
The connectedness relation is both reflexive and symmetric, but does not form an equivalence relation as it is not transitive.
func (Range[C]) IsEmpty ¶
IsEmpty returns true if this range is of the form [v..v) or (v..v]. (This does not encompass ranges of the form (v..v), because such ranges are invalid and can't be constructed at all.)
func (Range[C]) LowerBoundType ¶
LowerBoundType returns the type of this range's lower bound: CLOSED if the range includes its lower endpoint, OPEN if it does not, Unbounded if this range is unbounded below (that is, HasLowerBound returns false).
func (Range[C]) LowerBoundTypeE ¶
LowerBoundTypeE returns the type of this range's lower bound: CLOSED if the range includes its lower endpoint, OPEN if it does not, Unbounded and ErrUnboundedCut error if this range is unbounded below (that is, HasLowerBound returns false).
func (Range[C]) LowerEndpoint ¶
func (r Range[C]) LowerEndpoint() C
LowerEndpoint returns the lower endpoint of this range with ignoring ErrRangeSideUnbounded error.
func (Range[C]) LowerEndpointE ¶
LowerEndpointE returns the lower endpoint of this range. If this range is unbounded below (that is, HasLowerBound returns false), the ErrRangeSideUnbounded will be returned.
func (Range[C]) Span ¶
Span returns the minimal range that encloses both this range and other. For example, the span of [1..3] and (5..7) is [1..7).
An invalid range will be returned if failed to create new range.
func (Range[C]) SpanE ¶
SpanE returns the minimal range that encloses both this range and other. For example, the span of [1..3] and (5..7) is [1..7).
If the input ranges are connected, the returned range can also be called their union. If they are not, note that the span might contain values that are not contained in either input range.
Like intersection, this operation is commutative, associative and idempotent. Unlike it, it is always well-defined for any two input ranges.
An error will be returned if failed to create new range.
func (Range[C]) UpperBoundType ¶
UpperBoundType returns the type of this range's upper bound: CLOSED if the range includes its upper endpoint, OPEN if it does not, Unbounded if this range is unbounded above (that is, HasUpperBound returns false).
func (Range[C]) UpperBoundTypeE ¶
UpperBoundTypeE returns the type of this range's upper bound: CLOSED if the range includes its upper endpoint, OPEN if it does not, Unbounded and ErrUnboundedCut error if this range is unbounded above (that is, HasUpperBound returns false).
func (Range[C]) UpperEndpoint ¶
func (r Range[C]) UpperEndpoint() C
UpperEndpoint returns the upper endpoint of this range with ignoring ErrRangeSideUnbounded error.
func (Range[C]) UpperEndpointE ¶
UpperEndpointE returns the lower endpoint of this range. If this range is unbounded above (that is, HasUpperBound returns false), the ErrRangeSideUnbounded will be returned.