interval

package
v0.0.0-...-a510a1c Latest Latest
Warning

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

Go to latest
Published: Jan 1, 2024 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package interval implements intervals of integers.

Interval types are named by whether or not either of their ends is open/closed. For example, the type CO is closed on the left and open on the right, so it describes the interval type [a,b).

Only signed integers are supported, to make arithmetic simpler. In general, the package will note behave well in the presence of overflows.

The package also includes a Set type, which can efficiently store sparse integer intervals. It is parameterized, so can be used with any of the interval types in this package.

The easiest way to use it is to define a local alias for the interval kind appropriate for the problem, e.g.

type Interval = interval.CO[int]

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Convert

func Convert[J, I Interval[T], T constraints.Signed](i I) J

Convert between interval kinds.

Types

type CC

type CC[T constraints.Signed] struct {
	Min T
	Max T
}

CC is a closed interval of T, that is [Min, Max]. Well-formed intervals have Min≤Max+1, the empty interval is {0,-1}.

func MakeCC

func MakeCC[T constraints.Signed](a, b T) CC[T]

func (CC[T]) Contains

func (i CC[T]) Contains(v T) bool

Contains returns whether i contains v.

func (CC[T]) Empty

func (i CC[T]) Empty() bool

Empty returns if i is the empty interval.

func (CC[T]) Intersect

func (i CC[T]) Intersect(j CC[T]) CC[T]

Intersect returns the intersection of i and j.

func (CC[T]) Intersects

func (i CC[T]) Intersects(j CC[T]) bool

Intersects returns whether i and j intersect.

func (CC[T]) Len

func (i CC[T]) Len() int

Len returns the size of the interval.

func (CC[T]) String

func (i CC[T]) String() string

String implements fmt.Stringer.

func (CC[T]) Union

func (i CC[T]) Union(j CC[T]) (CC[T], bool)

Union returns the union of i and j, if it is an interval. Otherwise, it returns the empty interval and false.

type CO

type CO[T constraints.Signed] struct {
	Min T
	Max T
}

CO is a right half-open interval of T, that is [Min, Max). Well-formed intervals have Min≤Max, the empty interval is {0,0}.

func MakeCO

func MakeCO[T constraints.Signed](a, b T) CO[T]

func (CO[T]) Contains

func (i CO[T]) Contains(v T) bool

Contains returns whether i contains v.

func (CO[T]) Empty

func (i CO[T]) Empty() bool

Empty returns if i is the empty interval.

func (CO[T]) Intersect

func (i CO[T]) Intersect(j CO[T]) CO[T]

Intersect returns the intersection of i and j.

func (CO[T]) Intersects

func (i CO[T]) Intersects(j CO[T]) bool

Intersects returns whether i and j intersect.

func (CO[T]) Len

func (i CO[T]) Len() int

Len returns the size of the interval.

func (CO[T]) String

func (i CO[T]) String() string

String implements fmt.Stringer.

func (CO[T]) Union

func (i CO[T]) Union(j CO[T]) (CO[T], bool)

Union returns the union of i and j, if it is an interval. Otherwise, it returns the empty interval and false.

type Interval

type Interval[T constraints.Signed] interface {
	CO[T] | OC[T] | OO[T] | CC[T]
	// contains filtered or unexported methods
}

Interval is a constraint for any of the interval types in this package

type OC

type OC[T constraints.Signed] struct {
	Min T
	Max T
}

OC is a left half-open interval of T, that is (Min, Max]. Well-formed intervals have Min≤Max, the empty interval is {0,0}.

func MakeOC

func MakeOC[T constraints.Signed](a, b T) OC[T]

func (OC[T]) Contains

func (i OC[T]) Contains(v T) bool

Contains returns whether i contains v.

func (OC[T]) Empty

func (i OC[T]) Empty() bool

Empty returns if i is the empty interval.

func (OC[T]) Intersect

func (i OC[T]) Intersect(j OC[T]) OC[T]

Intersect returns the intersection of i and j.

func (OC[T]) Intersects

func (i OC[T]) Intersects(j OC[T]) bool

Intersects returns whether i and j intersect.

func (OC[T]) Len

func (i OC[T]) Len() int

Len returns the size of the interval.

func (OC[T]) String

func (i OC[T]) String() string

String implements fmt.Stringer.

func (OC[T]) Union

func (i OC[T]) Union(j OC[T]) (OC[T], bool)

Union returns the union of i and j, if it is an interval. Otherwise, it returns the empty interval and false.

type OO

type OO[T constraints.Signed] struct {
	Min T
	Max T
}

OO is an open interval of T, that is (Min, Max). Well-formed intervals have Min≤Max+1, the empty interval is {0,1}.

func MakeOO

func MakeOO[T constraints.Signed](a, b T) OO[T]

func (OO[T]) Contains

func (i OO[T]) Contains(v T) bool

Contains returns whether i contains v.

func (OO[T]) Empty

func (i OO[T]) Empty() bool

Empty returns if i is the empty interval.

func (OO[T]) Intersect

func (i OO[T]) Intersect(j OO[T]) OO[T]

Intersect returns the intersection of i and j.

func (OO[T]) Intersects

func (i OO[T]) Intersects(j OO[T]) bool

Intersects returns whether i and j intersect.

func (OO[T]) Len

func (i OO[T]) Len() int

Len returns the size of the interval.

func (OO[T]) String

func (i OO[T]) String() string

String implements fmt.Stringer.

func (OO[T]) Union

func (i OO[T]) Union(j OO[T]) (OO[T], bool)

Union returns the union of i and j, if it is an interval. Otherwise, it returns the empty interval and false.

type Set

type Set[I Interval[T], T constraints.Signed] struct {
	// contains filtered or unexported fields
}

Set is a set of T, built out of intervals. The zero value is an empty set.

func (*Set[I, T]) Add

func (s *Set[I, T]) Add(i I)

Add adds the interval i to s.

func (*Set[I, T]) Clone

func (s *Set[I, T]) Clone() *Set[I, T]

Clone returns a copy of s.

func (*Set[I, T]) Contains

func (s *Set[I, T]) Contains(v T) bool

Contains returns whether s contains v.

func (*Set[I, T]) Continuous

func (s *Set[I, T]) Continuous() bool

Continuous returns whether s is representable by a single interval. The empty set is considered continuous.

func (*Set[I, T]) Intersect

func (s *Set[I, T]) Intersect(i I)

Intersect intersects i into s.

func (*Set[I, T]) Intervals

func (s *Set[I, T]) Intervals() []I

Intervals returns a copy of the intervals in s.

func (*Set[I, T]) Len

func (s *Set[I, T]) Len() int

Len returns the number of elements in s.

func (Set[I, T]) String

func (s Set[I, T]) String() string

String implements fmt.Stringer.

Jump to

Keyboard shortcuts

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