Documentation ¶
Overview ¶
Package segment provides tools for working with collections of segments. A segment is a key-value mapping, where the key is a non-empty contiguous range of values of type Key, and the value is a single value of type Value.
Clients using this package must use the go_template_instance rule in tools/go_generics/defs.bzl to create an instantiation of this template package, providing types to use in place of Key, Range, Value, and Functions. See pkg/segment/test/BUILD for a usage example.
Index ¶
- type FlatSegment
- type Functions
- type GapIterator
- func (gap GapIterator) End() Key
- func (gap GapIterator) IsEmpty() bool
- func (gap GapIterator) NextGap() GapIterator
- func (gap GapIterator) NextLargeEnoughGap(minSize Key) GapIterator
- func (gap GapIterator) NextSegment() Iterator
- func (gap GapIterator) Ok() bool
- func (gap GapIterator) PrevGap() GapIterator
- func (gap GapIterator) PrevLargeEnoughGap(minSize Key) GapIterator
- func (gap GapIterator) PrevSegment() Iterator
- func (gap GapIterator) Range() Range
- func (gap GapIterator) Start() Key
- type Iterator
- func (seg Iterator) End() Key
- func (seg Iterator) NextGap() GapIterator
- func (seg Iterator) NextNonEmpty() (Iterator, GapIterator)
- func (seg Iterator) NextSegment() Iterator
- func (seg Iterator) Ok() bool
- func (seg Iterator) PrevGap() GapIterator
- func (seg Iterator) PrevNonEmpty() (Iterator, GapIterator)
- func (seg Iterator) PrevSegment() Iterator
- func (seg Iterator) Range() Range
- func (seg Iterator) SetEnd(end Key)
- func (seg Iterator) SetEndUnchecked(end Key)
- func (seg Iterator) SetRange(r Range)
- func (seg Iterator) SetRangeUnchecked(r Range)
- func (seg Iterator) SetStart(start Key)
- func (seg Iterator) SetStartUnchecked(start Key)
- func (seg Iterator) SetValue(val Value)
- func (seg Iterator) Start() Key
- func (seg Iterator) Value() Value
- func (seg Iterator) ValuePtr() *Value
- type Key
- type Range
- type Set
- func (s *Set) ExportSlice() []FlatSegment
- func (s *Set) Find(key Key) (Iterator, GapIterator)
- func (s *Set) FindGap(key Key) GapIterator
- func (s *Set) FindSegment(key Key) Iterator
- func (s *Set) FirstGap() GapIterator
- func (s *Set) FirstLargeEnoughGap(minSize Key) GapIterator
- func (s *Set) FirstSegment() Iterator
- func (s *Set) ImportSlice(fs []FlatSegment) error
- func (s *Set) Insert(gap GapIterator, r Range, val Value) Iterator
- func (s *Set) InsertRange(r Range, val Value) Iterator
- func (s *Set) InsertWithoutMerging(gap GapIterator, r Range, val Value) Iterator
- func (s *Set) InsertWithoutMergingRange(r Range, val Value) Iterator
- func (s *Set) InsertWithoutMergingUnchecked(gap GapIterator, r Range, val Value) Iterator
- func (s *Set) IsEmpty() bool
- func (s *Set) IsEmptyRange(r Range) bool
- func (s *Set) Isolate(seg Iterator, r Range) Iterator
- func (s *Set) LastGap() GapIterator
- func (s *Set) LastLargeEnoughGap(minSize Key) GapIterator
- func (s *Set) LastSegment() Iterator
- func (s *Set) LowerBoundGap(min Key) GapIterator
- func (s *Set) LowerBoundLargeEnoughGap(min, minSize Key) GapIterator
- func (s *Set) LowerBoundSegment(min Key) Iterator
- func (s *Set) LowerBoundSegmentSplitBefore(min Key) Iterator
- func (s *Set) Merge(first, second Iterator) Iterator
- func (s *Set) MergeAll()
- func (s *Set) MergeInsideRange(r Range)
- func (s *Set) MergeNext(seg Iterator) Iterator
- func (s *Set) MergeOutsideRange(r Range)
- func (s *Set) MergePrev(seg Iterator) Iterator
- func (s *Set) MergeUnchecked(first, second Iterator) Iterator
- func (s *Set) MutateFullRange(r Range, f func(seg Iterator) bool)
- func (s *Set) MutateRange(r Range, f func(seg Iterator) bool)
- func (s *Set) Remove(seg Iterator) GapIterator
- func (s *Set) RemoveAll()
- func (s *Set) RemoveFullRange(r Range) GapIterator
- func (s *Set) RemoveRange(r Range) GapIterator
- func (s *Set) Span() Key
- func (s *Set) SpanRange(r Range) Key
- func (s *Set) Split(seg Iterator, split Key) (Iterator, Iterator)
- func (s *Set) SplitAfter(seg Iterator, end Key) Iterator
- func (s *Set) SplitBefore(seg Iterator, start Key) Iterator
- func (s *Set) SplitUnchecked(seg Iterator, split Key) (Iterator, Iterator)
- func (s *Set) String() string
- func (s *Set) TryInsertRange(r Range, val Value) Iterator
- func (s *Set) TryInsertWithoutMergingRange(r Range, val Value) Iterator
- func (s *Set) Unisolate(seg Iterator) Iterator
- func (s *Set) UpperBoundGap(max Key) GapIterator
- func (s *Set) UpperBoundLargeEnoughGap(max, minSize Key) GapIterator
- func (s *Set) UpperBoundSegment(max Key) Iterator
- func (s *Set) UpperBoundSegmentSplitAfter(max Key) Iterator
- func (s *Set) VisitFullRange(r Range, f func(seg Iterator) bool)
- func (s *Set) VisitRange(r Range, f func(seg Iterator) bool)
- type T
- type Value
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type FlatSegment ¶
FlatSegment represents a segment as a single object. FlatSegment is used as an intermediate representation for save/restore and tests.
+stateify savable
type Functions ¶
type Functions interface { // MinKey returns the minimum allowed key. MinKey() Key // MaxKey returns the maximum allowed key + 1. MaxKey() Key // ClearValue deinitializes the given value. (For example, if Value is a // pointer or interface type, ClearValue should set it to nil.) ClearValue(*Value) // Merge attempts to merge the values corresponding to two consecutive // segments. If successful, Merge returns (merged value, true). Otherwise, // it returns (unspecified, false). // // Preconditions: r1.End == r2.Start. // // Postconditions: If merging succeeds, val1 and val2 are invalidated. Merge(r1 Range, val1 Value, r2 Range, val2 Value) (Value, bool) // Split splits a segment's value at a key within its range, such that the // first returned value corresponds to the range [r.Start, split) and the // second returned value corresponds to the range [split, r.End). // // Preconditions: r.Start < split < r.End. // // Postconditions: The original value val is invalidated. Split(r Range, val Value, split Key) (Value, Value) }
Functions is a required type parameter that must be a struct implementing the methods defined by Functions.
type GapIterator ¶
type GapIterator struct {
// contains filtered or unexported fields
}
A GapIterator is conceptually one of:
A pointer to a position between two segments, before the first segment, or after the last segment in a set, called a *gap*; or
A terminal iterator, which is a sentinel indicating that the end of iteration has been reached.
Note that the gap between two adjacent segments exists (iterators to it are non-terminal), but has a length of zero. GapIterator.IsEmpty returns true for such gaps. An empty set contains a single gap, spanning the entire range of the set's keys.
GapIterators are copyable values and are meaningfully equality-comparable. The zero value of GapIterator is a terminal iterator.
Unless otherwise specified, any mutation of a set invalidates all existing iterators into the set.
func (GapIterator) End ¶
func (gap GapIterator) End() Key
End is equivalent to Range().End, but should be preferred if only the end of the range is needed.
func (GapIterator) IsEmpty ¶
func (gap GapIterator) IsEmpty() bool
IsEmpty returns true if the iterated gap is empty (that is, the "gap" is between two adjacent segments.)
func (GapIterator) NextGap ¶
func (gap GapIterator) NextGap() GapIterator
NextGap returns the iterated gap's successor. If no such gap exists, NextGap returns a terminal iterator.
func (GapIterator) NextLargeEnoughGap ¶
func (gap GapIterator) NextLargeEnoughGap(minSize Key) GapIterator
NextLargeEnoughGap returns the iterated gap's first next gap with larger length than minSize. If not found, return a terminal gap iterator (does NOT include this gap itself).
Precondition: trackGaps must be 1.
func (GapIterator) NextSegment ¶
func (gap GapIterator) NextSegment() Iterator
NextSegment returns the segment immediately after the iterated gap. If no such segment exists, NextSegment returns a terminal iterator.
func (GapIterator) Ok ¶
func (gap GapIterator) Ok() bool
Ok returns true if the iterator is not terminal. All other methods are only valid for non-terminal iterators.
func (GapIterator) PrevGap ¶
func (gap GapIterator) PrevGap() GapIterator
PrevGap returns the iterated gap's predecessor. If no such gap exists, PrevGap returns a terminal iterator.
func (GapIterator) PrevLargeEnoughGap ¶
func (gap GapIterator) PrevLargeEnoughGap(minSize Key) GapIterator
PrevLargeEnoughGap returns the iterated gap's first prev gap with larger or equal length than minSize. If not found, return a terminal gap iterator (does NOT include this gap itself).
Precondition: trackGaps must be 1.
func (GapIterator) PrevSegment ¶
func (gap GapIterator) PrevSegment() Iterator
PrevSegment returns the segment immediately before the iterated gap. If no such segment exists, PrevSegment returns a terminal iterator.
func (GapIterator) Range ¶
func (gap GapIterator) Range() Range
Range returns the range spanned by the iterated gap.
func (GapIterator) Start ¶
func (gap GapIterator) Start() Key
Start is equivalent to Range().Start, but should be preferred if only the start of the range is needed.
type Iterator ¶
type Iterator struct {
// contains filtered or unexported fields
}
A Iterator is conceptually one of:
A pointer to a segment in a set; or
A terminal iterator, which is a sentinel indicating that the end of iteration has been reached.
Iterators are copyable values and are meaningfully equality-comparable. The zero value of Iterator is a terminal iterator.
Unless otherwise specified, any mutation of a set invalidates all existing iterators into the set.
func (Iterator) End ¶
End is equivalent to Range().End, but should be preferred if only the end of the range is needed.
func (Iterator) NextGap ¶
func (seg Iterator) NextGap() GapIterator
NextGap returns the gap immediately after the iterated segment.
func (Iterator) NextNonEmpty ¶
func (seg Iterator) NextNonEmpty() (Iterator, GapIterator)
NextNonEmpty returns the iterated segment's successor if it is adjacent, or the gap after the iterated segment otherwise. If seg.End() == Functions.MaxKey(), NextNonEmpty will return two terminal iterators. Otherwise, exactly one of the iterators returned by NextNonEmpty will be non-terminal.
func (Iterator) NextSegment ¶
NextSegment returns the iterated segment's successor. If there is no succeeding segment, NextSegment returns a terminal iterator.
func (Iterator) Ok ¶
Ok returns true if the iterator is not terminal. All other methods are only valid for non-terminal iterators.
func (Iterator) PrevGap ¶
func (seg Iterator) PrevGap() GapIterator
PrevGap returns the gap immediately before the iterated segment.
func (Iterator) PrevNonEmpty ¶
func (seg Iterator) PrevNonEmpty() (Iterator, GapIterator)
PrevNonEmpty returns the iterated segment's predecessor if it is adjacent, or the gap before the iterated segment otherwise. If seg.Start() == Functions.MinKey(), PrevNonEmpty will return two terminal iterators. Otherwise, exactly one of the iterators returned by PrevNonEmpty will be non-terminal.
func (Iterator) PrevSegment ¶
PrevSegment returns the iterated segment's predecessor. If there is no preceding segment, PrevSegment returns a terminal iterator.
func (Iterator) SetEnd ¶
SetEnd mutates the iterated segment's end. If the new end value would cause the iterated segment to overlap another segment, or would result in an invalid range, SetEnd panics. This operation does not invalidate any iterators.
func (Iterator) SetEndUnchecked ¶
SetEndUnchecked mutates the iterated segment's end. This operation does not invalidate any iterators.
Preconditions: The new end must be valid:
- end > seg.Start().
- If seg.NextSegment().Ok(), then end <= seg.NextSegment().Start().
func (Iterator) SetRange ¶
SetRange mutates the iterated segment's range key. If the new range would cause the iterated segment to overlap another segment, or if the new range is invalid, SetRange panics. This operation does not invalidate any iterators.
func (Iterator) SetRangeUnchecked ¶
SetRangeUnchecked mutates the iterated segment's range key. This operation does not invalidate any iterators.
Preconditions: - r.Length() > 0. - The new range must not overlap an existing one:
- If seg.NextSegment().Ok(), then r.end <= seg.NextSegment().Start().
- If seg.PrevSegment().Ok(), then r.start >= seg.PrevSegment().End().
func (Iterator) SetStart ¶
SetStart mutates the iterated segment's start. If the new start value would cause the iterated segment to overlap another segment, or would result in an invalid range, SetStart panics. This operation does not invalidate any iterators.
func (Iterator) SetStartUnchecked ¶
SetStartUnchecked mutates the iterated segment's start. This operation does not invalidate any iterators.
Preconditions: The new start must be valid:
- start < seg.End()
- If seg.PrevSegment().Ok(), then start >= seg.PrevSegment().End().
func (Iterator) SetValue ¶
SetValue mutates the iterated segment's value. This operation does not invalidate any iterators.
func (Iterator) Start ¶
Start is equivalent to Range().Start, but should be preferred if only the start of the range is needed.
type Range ¶
type Range any
Range is a required type parameter equivalent to Range<Key>.
func (Range) CanSplitAt ¶
CanSplitAt returns true if it is legal to split a segment spanning the range r at x; that is, splitting at x would produce two ranges, both of which have non-zero length.
func (Range) Intersect ¶
func (r Range) Intersect(r2 Range) Range
Intersect returns a range consisting of the intersection between r and r2. If r and r2 do not overlap, Intersect returns a range with unspecified bounds, but for which Length() == 0.
func (Range) IsSupersetOf ¶
func (r Range) IsSupersetOf(r2 Range) bool
IsSupersetOf returns true if r is a superset of r2; that is, the range r2 is contained within r.
func (Range) Overlaps ¶
func (r Range) Overlaps(r2 Range) bool
Overlaps returns true if r and r2 overlap.
func (Range) WellFormed ¶
func (r Range) WellFormed() bool
WellFormed returns true if r.Start <= r.End. All other methods on a Range require that the Range is well-formed.
type Set ¶
type Set struct {
// contains filtered or unexported fields
}
A Set is a mapping of segments with non-overlapping Range keys. The zero value for a Set is an empty set. Set values are not safely movable nor copyable. Set is thread-compatible.
+stateify savable
func (*Set) ExportSlice ¶
func (s *Set) ExportSlice() []FlatSegment
ExportSlice returns a copy of all segments in the given set, in ascending key order.
func (*Set) Find ¶
func (s *Set) Find(key Key) (Iterator, GapIterator)
Find returns the segment or gap whose range contains the given key. If a segment is found, the returned Iterator is non-terminal and the returned GapIterator is terminal. Otherwise, the returned Iterator is terminal and the returned GapIterator is non-terminal.
func (*Set) FindGap ¶
func (s *Set) FindGap(key Key) GapIterator
FindGap returns the gap containing the given key. If no such gap exists (i.e. the set contains a segment containing that key), FindGap returns a terminal iterator.
func (*Set) FindSegment ¶
FindSegment returns the segment whose range contains the given key. If no such segment exists, FindSegment returns a terminal iterator.
func (*Set) FirstGap ¶
func (s *Set) FirstGap() GapIterator
FirstGap returns the first gap in the set.
func (*Set) FirstLargeEnoughGap ¶
func (s *Set) FirstLargeEnoughGap(minSize Key) GapIterator
FirstLargeEnoughGap returns the first gap in the set with at least the given length. If no such gap exists, FirstLargeEnoughGap returns a terminal iterator.
Precondition: trackGaps must be 1.
func (*Set) FirstSegment ¶
FirstSegment returns the first segment in the set. If the set is empty, FirstSegment returns a terminal iterator.
func (*Set) ImportSlice ¶
func (s *Set) ImportSlice(fs []FlatSegment) error
ImportSlice initializes the given set from the given slice.
Preconditions:
- s must be empty.
- fs must represent a valid set (the segments in fs must have valid lengths that do not overlap).
- The segments in fs must be sorted in ascending key order.
func (*Set) Insert ¶
func (s *Set) Insert(gap GapIterator, r Range, val Value) Iterator
Insert inserts the given segment into the given gap. If the new segment can be merged with adjacent segments, Insert will do so. Insert returns an iterator to the segment containing the inserted value (which may have been merged with other values). All existing iterators (including gap, but not including the returned iterator) are invalidated.
If the gap cannot accommodate the segment, or if r is invalid, Insert panics.
Insert is semantically equivalent to a InsertWithoutMerging followed by a Merge, but may be more efficient. Note that there is no unchecked variant of Insert since Insert must retrieve and inspect gap's predecessor and successor segments regardless.
func (*Set) InsertRange ¶
InsertRange inserts the given segment into the set. If the new segment can be merged with adjacent segments, InsertRange will do so. InsertRange returns an iterator to the segment containing the inserted value (which may have been merged with other values). All existing iterators (excluding the returned iterator) are invalidated.
If the new segment would overlap an existing segment, or if r is invalid, InsertRange panics.
InsertRange searches the set to find the gap to insert into. If the caller already has the appropriate GapIterator, or if the caller needs to do additional work between finding the gap and insertion, use Insert instead.
func (*Set) InsertWithoutMerging ¶
func (s *Set) InsertWithoutMerging(gap GapIterator, r Range, val Value) Iterator
InsertWithoutMerging inserts the given segment into the given gap and returns an iterator to the inserted segment. All existing iterators (including gap, but not including the returned iterator) are invalidated.
If the gap cannot accommodate the segment, or if r is invalid, InsertWithoutMerging panics.
func (*Set) InsertWithoutMergingRange ¶
InsertWithoutMergingRange inserts the given segment into the set and returns an iterator to the inserted segment. All existing iterators (excluding the returned iterator) are invalidated.
If the new segment would overlap an existing segment, or if r is invalid, InsertWithoutMergingRange panics.
InsertWithoutMergingRange searches the set to find the gap to insert into. If the caller already has the appropriate GapIterator, or if the caller needs to do additional work between finding the gap and insertion, use InsertWithoutMerging instead.
func (*Set) InsertWithoutMergingUnchecked ¶
func (s *Set) InsertWithoutMergingUnchecked(gap GapIterator, r Range, val Value) Iterator
InsertWithoutMergingUnchecked inserts the given segment into the given gap and returns an iterator to the inserted segment. All existing iterators (including gap, but not including the returned iterator) are invalidated.
Preconditions:
- r.Start >= gap.Start().
- r.End <= gap.End().
func (*Set) IsEmptyRange ¶
IsEmptyRange returns true iff no segments in the set overlap the given range. This is semantically equivalent to s.SpanRange(r) == 0, but may be more efficient.
func (*Set) Isolate ¶
Isolate ensures that the given segment's range is a subset of r by splitting at r.Start and r.End if necessary, and returns an updated iterator to the bounded segment. All existing iterators (including seg, but not including the returned iterators) are invalidated.
Isolate is usually used when mutating part of a single segment, or when mutating segments in a range where the first segment is not necessarily split, making use of SplitBefore/SplitAfter complex.
Preconditions: seg.Range().Overlaps(r).
func (*Set) LastLargeEnoughGap ¶
func (s *Set) LastLargeEnoughGap(minSize Key) GapIterator
LastLargeEnoughGap returns the last gap in the set with at least the given length. If no such gap exists, LastLargeEnoughGap returns a terminal iterator.
Precondition: trackGaps must be 1.
func (*Set) LastSegment ¶
LastSegment returns the last segment in the set. If the set is empty, LastSegment returns a terminal iterator.
func (*Set) LowerBoundGap ¶
func (s *Set) LowerBoundGap(min Key) GapIterator
LowerBoundGap returns the gap with the lowest range that is greater than or equal to min.
func (*Set) LowerBoundLargeEnoughGap ¶
func (s *Set) LowerBoundLargeEnoughGap(min, minSize Key) GapIterator
LowerBoundLargeEnoughGap returns the first gap in the set with at least the given length and whose range contains a key greater than or equal to min. If no such gap exists, LowerBoundLargeEnoughGap returns a terminal iterator.
Precondition: trackGaps must be 1.
func (*Set) LowerBoundSegment ¶
LowerBoundSegment returns the segment with the lowest range that contains a key greater than or equal to min. If no such segment exists, LowerBoundSegment returns a terminal iterator.
func (*Set) LowerBoundSegmentSplitBefore ¶
LowerBoundSegmentSplitBefore combines LowerBoundSegment and SplitBefore.
LowerBoundSegmentSplitBefore is usually used when mutating segments in a range while iterating them in order of increasing keys. In such cases, LowerBoundSegmentSplitBefore provides an iterator to the first segment to be mutated, suitable as the initial value for a loop variable.
func (*Set) Merge ¶
Merge attempts to merge two neighboring segments. If successful, Merge returns an iterator to the merged segment, and all existing iterators are invalidated. Otherwise, Merge returns a terminal iterator.
If first is not the predecessor of second, Merge panics.
func (*Set) MergeAll ¶
func (s *Set) MergeAll()
MergeAll merges all mergeable adjacent segments in the set. All existing iterators are invalidated.
func (*Set) MergeInsideRange ¶
MergeInsideRange attempts to merge all adjacent segments that contain a key in the specific range. All existing iterators are invalidated.
MergeInsideRange only makes sense after mutating the set in a way that may change the mergeability of modified segments; callers should prefer to use MergePrev or MergeNext during the mutating loop instead (depending on the direction of iteration), in order to avoid a redundant search.
func (*Set) MergeNext ¶
MergeNext attempts to merge the given segment with its successor if possible, and returns an updated iterator to the extended segment. All existing iterators (including seg, but not including the returned iterator) are invalidated.
MergeNext is usually used when mutating segments while iterating them in order of decreasing keys, to attempt merging of each mutated segment with its previously-mutated successor. In such cases, merging a mutated segment with its unmutated predecessor would incorrectly cause the latter to be skipped.
func (*Set) MergeOutsideRange ¶
MergeOutsideRange attempts to merge the segment containing r.Start with its predecessor, and the segment containing r.End-1 with its successor.
MergeOutsideRange only makes sense after mutating the set in a way that may change the mergeability of modified segments; callers should prefer to use MergePrev or MergeNext during the mutating loop instead (depending on the direction of iteration), in order to avoid two redundant searches.
func (*Set) MergePrev ¶
MergePrev attempts to merge the given segment with its predecessor if possible, and returns an updated iterator to the extended segment. All existing iterators (including seg, but not including the returned iterator) are invalidated.
MergePrev is usually used when mutating segments while iterating them in order of increasing keys, to attempt merging of each mutated segment with its previously-mutated predecessor. In such cases, merging a mutated segment with its unmutated successor would incorrectly cause the latter to be skipped.
func (*Set) MergeUnchecked ¶
MergeUnchecked attempts to merge two neighboring segments. If successful, MergeUnchecked returns an iterator to the merged segment, and all existing iterators are invalidated. Otherwise, MergeUnchecked returns a terminal iterator.
Precondition: first is the predecessor of second: first.NextSegment() == second, first == second.PrevSegment().
func (*Set) MutateFullRange ¶
MutateFullRange is equivalent to MutateRange, except that if any key in r that is visited before f returns false does not correspond to a segment, MutateFullRange panics.
func (*Set) MutateRange ¶
MutateRange applies the function f to all segments intersecting the range r, in order of ascending keys. Segments that lie partially outside r are split before f is called, such that f only observes segments entirely within r. Iterated segments are merged again after f is called. Non-empty gaps between segments are skipped. If a call to f returns false, MutateRange stops iteration immediately.
MutateRange invalidates all existing iterators.
N.B. f must not invalidate iterators into s.
func (*Set) Remove ¶
func (s *Set) Remove(seg Iterator) GapIterator
Remove removes the given segment and returns an iterator to the vacated gap. All existing iterators (including seg, but not including the returned iterator) are invalidated.
func (*Set) RemoveAll ¶
func (s *Set) RemoveAll()
RemoveAll removes all segments from the set. All existing iterators are invalidated.
func (*Set) RemoveFullRange ¶
func (s *Set) RemoveFullRange(r Range) GapIterator
RemoveFullRange is equivalent to RemoveRange, except that if any key in the given range does not correspond to a segment, RemoveFullRange panics.
func (*Set) RemoveRange ¶
func (s *Set) RemoveRange(r Range) GapIterator
RemoveRange removes all segments in the given range. An iterator to the newly formed gap is returned, and all existing iterators are invalidated.
RemoveRange searches the set to find segments to remove. If the caller already has an iterator to either end of the range of segments to remove, or if the caller needs to do additional work before removing each segment, iterate segments and call Remove in a loop instead.
func (*Set) SpanRange ¶
SpanRange returns the total size of the intersection of segments in the set with the given range.
func (*Set) Split ¶
Split splits the given segment at the given key and returns iterators to the two resulting segments. All existing iterators (including seg, but not including the returned iterators) are invalidated.
If the segment cannot be split at split (because split is at the start or end of the segment's range, so splitting would produce a segment with zero length, or because split falls outside the segment's range altogether), Split panics.
func (*Set) SplitAfter ¶
SplitAfter ensures that the given segment's end is at most end by splitting at end if necessary, and returns an updated iterator to the bounded segment. All existing iterators (including seg, but not including the returned iterator) are invalidated.
SplitAfter is usually used when mutating segments in a range. In such cases, when iterating segments in order of increasing keys, each iterated segment may extend beyond the end of the range to be mutated, and needs to be SplitAfter to ensure that only the part of the segment within the range is mutated. When iterating segments in order of decreasing keys, SplitBefore and SplitAfter exchange roles; i.e. SplitBefore needs to be invoked on each segment, while SplitAfter only needs to be invoked on the first.
Preconditions: seg.Start() < end.
func (*Set) SplitBefore ¶
SplitBefore ensures that the given segment's start is at least start by splitting at start if necessary, and returns an updated iterator to the bounded segment. All existing iterators (including seg, but not including the returned iterator) are invalidated.
SplitBefore is usually when mutating segments in a range. In such cases, when iterating segments in order of increasing keys, the first segment may extend beyond the start of the range to be mutated, and needs to be SplitBefore to ensure that only the part of the segment within the range is mutated. When iterating segments in order of decreasing keys, SplitBefore and SplitAfter; i.e. SplitBefore needs to be invoked on each segment, while SplitAfter only needs to be invoked on the first.
Preconditions: start < seg.End().
func (*Set) SplitUnchecked ¶
SplitUnchecked splits the given segment at the given key and returns iterators to the two resulting segments. All existing iterators (including seg, but not including the returned iterators) are invalidated.
Preconditions: seg.Start() < key < seg.End().
func (*Set) TryInsertRange ¶
TryInsertRange attempts to insert the given segment into the set. If the new segment can be merged with adjacent segments, TryInsertRange will do so. TryInsertRange returns an iterator to the segment containing the inserted value (which may have been merged with other values). All existing iterators (excluding the returned iterator) are invalidated.
If the new segment would overlap an existing segment, TryInsertRange does nothing and returns a terminal iterator.
TryInsertRange searches the set to find the gap to insert into. If the caller already has the appropriate GapIterator, or if the caller needs to do additional work between finding the gap and insertion, use Insert instead.
func (*Set) TryInsertWithoutMergingRange ¶
TryInsertWithoutMergingRange attempts to insert the given segment into the set. If successful, it returns an iterator to the inserted segment; all existing iterators (excluding the returned iterator) are invalidated. If the new segment would overlap an existing segment, TryInsertWithoutMergingRange does nothing and returns a terminal iterator.
TryInsertWithoutMergingRange searches the set to find the gap to insert into. If the caller already has the appropriate GapIterator, or if the caller needs to do additional work between finding the gap and insertion, use InsertWithoutMerging instead.
func (*Set) Unisolate ¶
Unisolate attempts to merge the given segment with its predecessor and successor if possible, and returns an updated iterator to the extended segment. All existing iterators (including seg, but not including the returned iterator) are invalidated.
Unisolate is usually used in conjunction with Isolate when mutating part of a single segment in a way that may affect its mergeability. For the reasons described by MergePrev and MergeNext, it is usually incorrect to use the return value of Unisolate in a loop variable.
func (*Set) UpperBoundGap ¶
func (s *Set) UpperBoundGap(max Key) GapIterator
UpperBoundGap returns the gap with the highest range that is less than or equal to max.
func (*Set) UpperBoundLargeEnoughGap ¶
func (s *Set) UpperBoundLargeEnoughGap(max, minSize Key) GapIterator
UpperBoundLargeEnoughGap returns the last gap in the set with at least the given length and whose range contains a key less than or equal to max. If no such gap exists, UpperBoundLargeEnoughGap returns a terminal iterator.
Precondition: trackGaps must be 1.
func (*Set) UpperBoundSegment ¶
UpperBoundSegment returns the segment with the highest range that contains a key less than or equal to max. If no such segment exists, UpperBoundSegment returns a terminal iterator.
func (*Set) UpperBoundSegmentSplitAfter ¶
UpperBoundSegmentSplitAfter combines UpperBoundSegment and SplitAfter.
UpperBoundSegmentSplitAfter is usually used when mutating segments in a range while iterating them in order of decreasing keys. In such cases, UpperBoundSegmentSplitAfter provides an iterator to the first segment to be mutated, suitable as the initial value for a loop variable.
func (*Set) VisitFullRange ¶
VisitFullRange is equivalent to VisitRange, except that if any key in r that is visited before f returns false does not correspond to a segment, VisitFullRange panics.
func (*Set) VisitRange ¶
VisitRange applies the function f to all segments intersecting the range r, in order of ascending keys. Segments will not be split, so f may be called on segments lying partially outside r. Non-empty gaps between segments are skipped. If a call to f returns false, VisitRange stops iteration immediately.
N.B. f must not invalidate iterators into s.