intervals

package
v0.28.0 Latest Latest
Warning

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

Go to latest
Published: May 8, 2024 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// BasisInt is a default basis for type int
	BasisInt = NewBasis(math.MinInt, math.MaxInt, cmp.Compare[int])

	// BasisString is a default basis for type string
	BasisString = NewBasis("", string(utf8.MaxRune), cmp.Compare[string])

	// BasisTime is a default basis for type time.Time
	BasisTime = NewBasis(
		time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
		time.Date(2070, 1, 1, 0, 0, 0, 0, time.UTC),
		func(t1, t2 time.Time) int {
			if t1.Before(t2) {
				return -1
			}
			if t1.Equal(t2) {
				return 0
			}
			return 1
		},
	)
)

Functions

This section is empty.

Types

type Basis

type Basis[T any] struct {
	Min  T                  // min value of the type T
	Max  T                  // max value of the type T
	CmpF func(v1, v2 T) int // comparison function of T type values
}

Basis represents a basis for a set of vectors, it defines what [min,max] range the vectors have and what operations are available for the vectors, like: create, intersect, union, etc.

func NewBasis

func NewBasis[T any](min, max T, cmpF func(v1, v2 T) int) Basis[T]

NewBasis creates new basis

func (Basis[T]) After

func (b Basis[T]) After(i1, i2 Interval[T]) bool

After returns true if the L border of the i1 interval is greater than the R border of the i2 interval. NOTE: The `(L=2, R=4)` and `(L=1, R=3)` intervals are considered to have an intersection (see Intersection description) and the `(L=2, R=4).After.((L=1, R=3))` call returns false.

func (Basis[T]) Before

func (b Basis[T]) Before(i1, i2 Interval[T]) bool

Before returns true if the R border of i1 interval is lesser than the L border of the i2 interval. NOTE: The `(L=1, R=3)` and `(L=2, R=4)` intervals are considered to have an intersection (see Intersection description) and the `(L=1, R=3).Before.((L=2, R=4))` call returns false.

func (Basis[T]) Closed

func (b Basis[T]) Closed(L, R T) Interval[T]

Closed creates a closed interval `[1, 5]`

func (Basis[T]) EndsAfter

func (b Basis[T]) EndsAfter(i1, i2 Interval[T]) bool

EndsAfter returns true if the R border of the i1 interval is greater than the R border of the i2 interval. NOTE: The `..., R=5)` and `..., R=4]` ends are equivalent, but the function returns true for `(L=2, R=5).EndsAfter([L=3, R=4])`.

func (Basis[T]) Intersect

func (b Basis[T]) Intersect(i1, i2 Interval[T]) (Interval[T], bool)

Intersect returns the intersection of the i1 interval with the i2 interval. If no intersection is found the function returns false in the second return value. NOTE: The `(L=1, R=3)` and `(L=2, R=4)` intervals are considered to have an intersection and the `(L=1, R=3).Intersect((L=2, R=4))` call returns `(L=2, R=3)`.

func (Basis[T]) Negate

func (b Basis[T]) Negate(i Interval[T]) []Interval[T]

Negate negates the current interval, meaning that it returns the complement of the current interval.

func (Basis[T]) Open

func (b Basis[T]) Open(L, R T) Interval[T]

Open create an open interval `(1, 5)`

func (Basis[T]) OpenL

func (b Basis[T]) OpenL(L, R T) Interval[T]

OpenL create a left-open interval `(1, 5]`

func (Basis[T]) OpenR

func (b Basis[T]) OpenR(L, R T) Interval[T]

OpenR creates a right-open interval `[1, 5)`

func (Basis[T]) StartsBefore

func (b Basis[T]) StartsBefore(i1, i2 Interval[T]) bool

StartsBefore returns true if the L border of the i1 interval is lesser than the L border of the i2 interval. NOTE: The `(L=2, ...` and `[L=3, ...` starts are equivalent, but the function returns true for `(L=2, R=5).StartsBefore([L=3, R=4])`.

func (Basis[T]) Union

func (b Basis[T]) Union(i1, i2 Interval[T]) (Interval[T], bool)

Union returns the union of the i1 interval with the i2 interval only if there is an intersection. If no intersection is found the function returns false in the second return value. NOTE: The `(L=1, R=3)` and `(L=2, R=4)` intervals are considered to have an intersection (see Intersection description) the `(L=1, R=3).Union((L=2, R=4))` call returns `(L=1, R=4)`.

type Interval

type Interval[T any] struct {
	L   T
	R   T
	LIn bool
	RIn bool
}

Interval represents an interval of values of type T, interval can be open, half-open, closed.

func (Interval[T]) IsClosed

func (i Interval[T]) IsClosed() bool

IsClosed returns true if interval is closed

func (Interval[T]) IsOpen

func (i Interval[T]) IsOpen() bool

IsOpen returns true if interval is open

func (Interval[T]) IsOpenL

func (i Interval[T]) IsOpenL() bool

IsOpenL returns true if interval is left-open

func (Interval[T]) IsOpenR

func (i Interval[T]) IsOpenR() bool

IsOpenR returns true if interval is right-open

func (Interval[T]) String

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

String returns the string representation of the interval

Jump to

Keyboard shortcuts

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