ranges

package
v2.6.1+incompatible Latest Latest
Warning

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

Go to latest
Published: Jun 3, 2018 License: MIT Imports: 4 Imported by: 2

Documentation

Overview

Package ranges provides objects that can represent simple integer ranges with an optional stepping, as well as complex non-contiguous ranges.

Simple range:

1-10
20-100x3
-10-50

Complex ranges:

1-10,20-40x2,30,80-100x3

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type InclusiveRange

type InclusiveRange struct {
	// contains filtered or unexported fields
}

InclusiveRange represents a start and end value, and a stepping amount between each value in the range. The range includes the end value. It dynamically acts like a slice of values in an integer range, without needing to store the discreet values. So it can represent very large ranges.

func NewInclusiveRange

func NewInclusiveRange(start, end, step int) *InclusiveRange

NewInclusiveRange creates a new InclusiveRange instance

func (*InclusiveRange) Contains

func (r *InclusiveRange) Contains(value int) bool

Contains returns true if the given value is a valid value within the value range.

func (*InclusiveRange) End

func (r *InclusiveRange) End() int

End returns the end of the range. This value may be different from the end value given when the range was first initialized, since it takes into account the stepping value. The end value may be shifted to the closest valid value within the stepped range.

func (*InclusiveRange) Index

func (f *InclusiveRange) Index(value int) int

Index returns the 0-based index of the first occurrence given value, within the range. If the value does not exist in the range, a value of -1 will be returned

func (*InclusiveRange) IterValues

func (f *InclusiveRange) IterValues() Iterator

IterValues returns an iterator that will loop through every value in the range. Iterator is valid while IsDone() returns false. Next value on valid iterator can be retrieved via Next()

func (*InclusiveRange) Len

func (r *InclusiveRange) Len() int

Len returns the number of values in the range

func (*InclusiveRange) Max

func (r *InclusiveRange) Max() int

Max returns the highest value in the range

func (*InclusiveRange) Min

func (r *InclusiveRange) Min() int

Min returns the smallest value in the range

func (*InclusiveRange) Start

func (r *InclusiveRange) Start() int

Start returns the start of the range

func (*InclusiveRange) Step

func (r *InclusiveRange) Step() int

Step returns the stepping value used in the range

func (*InclusiveRange) String

func (r *InclusiveRange) String() string

String returns a formatted string representation of the integer range

func (*InclusiveRange) Value

func (r *InclusiveRange) Value(idx int) (int, error)

Value returns the value at the given index in the range. If the index is invalid/exceeds the valid range, an error will be returned.

type InclusiveRanges

type InclusiveRanges struct {
	// contains filtered or unexported fields
}

InclusiveRanges is able to represent multiple non-contiguous ranges, with non-uniform stepping, ie. 1-10,20-30x2,100,120 Because range values are evaluated dynamically as offsets within the start/stop, they can represent very large ranges. The zero value is a ready to use instance.

func (*InclusiveRanges) Append

func (l *InclusiveRanges) Append(start, end, step int)

Append creates and adds another range of values to the total range list.

func (*InclusiveRanges) AppendUnique

func (l *InclusiveRanges) AppendUnique(start, end, step int)

AppendUnique creates and adds another range of values to the total range list. Only unique values from the given range are appended to the total range.

func (*InclusiveRanges) Contains

func (l *InclusiveRanges) Contains(value int) bool

Contains returns true if a given value is a valid value within the total range.

func (*InclusiveRanges) End

func (l *InclusiveRanges) End() int

End returns the last value of the last range

func (*InclusiveRanges) Index

func (l *InclusiveRanges) Index(value int) int

Index returns the 0-based index of the first occurrence of the given value, within the range. If the value does not exist in the range, a value of -1 will be returned.

func (*InclusiveRanges) Inverted

func (l *InclusiveRanges) Inverted() *InclusiveRanges

Inverted returns a new instance with a range containing all values within the start/end that are not in the current range. Original ordering is not preserved. New inverted range will be in an increasing value.

func (*InclusiveRanges) IterValues

func (l *InclusiveRanges) IterValues() Iterator

IterValues returns an iterator that will loop through every value in the total range. Iterator is valid while IsDone() returns false. Next value on valid iterator can be retrieved via Next()

func (*InclusiveRanges) Len

func (l *InclusiveRanges) Len() int

Len returns the total number of values across all ranges

func (*InclusiveRanges) Max

func (l *InclusiveRanges) Max() int

Max returns the highest value in the total range

func (*InclusiveRanges) Min

func (l *InclusiveRanges) Min() int

Min returns the smallest value in the total range

func (*InclusiveRanges) Normalized

func (l *InclusiveRanges) Normalized() *InclusiveRanges

Normalized returns a new instance, where all values have been sorted and compacted (where possible)

func (*InclusiveRanges) Start

func (l *InclusiveRanges) Start() int

Start returns the first value of the first range

func (*InclusiveRanges) String

func (l *InclusiveRanges) String() string

String returns the formatted representation of the combination of all internal InclusiveRange instances

func (*InclusiveRanges) Value

func (l *InclusiveRanges) Value(idx int) (int, error)

Value returns the value at the given index in the total range. If the index is invalid/exceeds the valid range, an error will be returned.

type Iterator

type Iterator interface {
	// The Iterator has another valid value
	// to produce, while IsDone() returns true
	IsDone() bool
	// Next retrieves the next available value,
	// and advances the Iterator. It is only
	// valid to call Next() while IsDone()
	// returns true
	Next() int
}

Iterator is an interface type that can iterate over int values.

Jump to

Keyboard shortcuts

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