Documentation
¶
Overview ¶
Package rangeable is the Go reference implementation of the Rangeable RFC — a generic, integer-coordinate, closed-interval set container with first-insert ordered active queries.
See https://github.com/ZhgChgLi/RangeableRFC for the normative specification.
Index ¶
- Variables
- type Interval
- type InvalidIntervalError
- type Option
- type Rangeable
- func (r *Rangeable[E]) At(i int) Slot[E]
- func (r *Rangeable[E]) Copy() *Rangeable[E]
- func (r *Rangeable[E]) Each(fn func(element E, intervals []Interval))
- func (r *Rangeable[E]) GetRange(element E) []Interval
- func (r *Rangeable[E]) Insert(element E, start, end int) error
- func (r *Rangeable[E]) IsEmpty() bool
- func (r *Rangeable[E]) Size() int
- func (r *Rangeable[E]) Transitions(from int, to *int) ([]TransitionEvent[E], error)
- func (r *Rangeable[E]) Version() int
- type Slot
- type TransitionEvent
- type TransitionKind
Constants ¶
This section is empty.
Variables ¶
var ErrInvalidInterval = errors.New("rangeable: invalid interval")
ErrInvalidInterval is the sentinel error returned when an interval is malformed (start > end). Use errors.Is to check.
Functions ¶
This section is empty.
Types ¶
type Interval ¶
Interval is an immutable closed integer interval [Lo, Hi].
Both ends are inclusive, matching RFC §4.1. Construct directly via a struct literal or via NewInterval for explicit validation.
func NewInterval ¶
NewInterval validates the bounds and returns an Interval or an InvalidIntervalError on Lo > Hi.
type InvalidIntervalError ¶
InvalidIntervalError is returned by Rangeable.Insert and Rangeable.Transitions when the supplied bounds violate start <= end. It wraps ErrInvalidInterval so errors.Is succeeds.
func (*InvalidIntervalError) Error ¶
func (e *InvalidIntervalError) Error() string
func (*InvalidIntervalError) Is ¶
func (e *InvalidIntervalError) Is(target error) bool
Is allows errors.Is(err, ErrInvalidInterval) to succeed for any InvalidIntervalError instance.
type Option ¶
type Option func(*config)
Option configures a Rangeable instance at construction time.
func WithIntMaxSentinel ¶
WithIntMaxSentinel opts the container into "treat hi == v as +∞" semantics for cross-language fixture parity with bounded-int languages. Default is unset.
type Rangeable ¶
type Rangeable[E comparable] struct { // contains filtered or unexported fields }
Rangeable is a generic, integer-coordinate, closed-interval set container. It pairs hashable elements with their merged disjoint integer ranges and supports three query families:
- by-element via Rangeable.GetRange
- by-position via Rangeable.At
- by-range via Rangeable.Transitions
The element type must be Go-[comparable] (RFC §4.2 — equality decided by `==`, which is reflexive, symmetric, transitive, and consistent with the runtime hash used by maps).
func New ¶
func New[E comparable](opts ...Option) *Rangeable[E]
New constructs an empty Rangeable with the given options.
func (*Rangeable[E]) At ¶
At returns the active-element list at coordinate i. RFC §3.3.
O(log |segments| + r) once the index is built. Returns an empty Slot for coordinates outside every segment.
func (*Rangeable[E]) Copy ¶
Copy returns a deep copy. Mutation on the copy MUST NOT affect this instance, and vice versa.
func (*Rangeable[E]) Each ¶
Each iterates (element, intervals) pairs in first-insert order ascending. The intervals slice is a fresh copy; mutating it does not affect the container.
func (*Rangeable[E]) GetRange ¶
GetRange returns the merged ranges for element as a freshly allocated slice. RFC §3.4. Returns an empty slice when no element equal to element has ever been inserted.
func (*Rangeable[E]) Insert ¶
Insert covers the closed interval [start, end] with element. Returns an InvalidIntervalError (matchable via errors.Is against ErrInvalidInterval) if start > end. RFC §3.2.
Idempotent: re-inserting a sub-range that is already fully contained leaves the container unchanged and does NOT bump Rangeable.Version.
func (*Rangeable[E]) IsEmpty ¶
IsEmpty reports whether Rangeable.Size is zero.
func (*Rangeable[E]) Size ¶
Size returns the number of distinct equivalence-class elements ever inserted. RFC §3.5.1.
func (*Rangeable[E]) Transitions ¶
func (r *Rangeable[E]) Transitions(from int, to *int) ([]TransitionEvent[E], error)
Transitions returns open / close events within the inclusive coordinate range [from, to]. RFC §3.5.
to == nil means +∞ (include all events through the upper bound). Returns an InvalidIntervalError if from > *to.
type Slot ¶
type Slot[E any] struct { Objs []E }
Slot wraps the ordered slice of elements active at a coordinate.
Objs is sorted by first-insertion order ascending (RFC §4.5). The same coordinate within an unmutated container always returns equivalent Objs.
type TransitionEvent ¶
type TransitionEvent[E any] struct { Coordinate *int Kind TransitionKind Element E }
TransitionEvent is a single boundary event in coordinate-sorted order.
Coordinate is normally a finite [int]; it is nil for close events whose underlying interval ends at the implementation's +∞ sentinel (RFC §4.7 C4). Comparison treats nil as greater than any finite int.
type TransitionKind ¶
type TransitionKind int
TransitionKind is the kind of a boundary event.
const ( // Open marks the start of an element's active range. Open TransitionKind = iota // Close marks the position just past the end of an element's active range. Close )
func (TransitionKind) String ¶
func (k TransitionKind) String() string
String returns the canonical lowercase JSON-style tag.