import "github.com/cznic/interval"
Package interval handles sets of ordered values laying between two, possibly infinite, bounds.
Note: Intervals are usually defined as a set of [extended] real numbers. The model of the interval provided by Interface does not know - and thus does not care about the type of the bounds, if any. That may lead to correct but possibly surprising effects when the bounds domain is not ℝ. An interval may be non empty, like for example the open interval (1, 2), but no integer value lies between the bounds.
See also: http://en.wikipedia.org/wiki/Interval_(mathematics)
class_string.go gen.go interval.go
BigInt is an interval having math/big.Int bounds.
Code:
x := &BigInt{LeftOpen, big.NewInt(1), big.NewInt(2)} y := &BigInt{LeftClosed, big.NewInt(2), big.NewInt(3)} fmt.Printf("x %v, y %v: x ∩ y %v, x ∪ y %v", x, y, Intersection(x, y), Union(x, y))
Output:
x (1, 2], y [2, 3): x ∩ y {2}, x ∪ y (1, 3)
Class implements Interface.
Clone implements Interface.
CompareAA implements Interface.
CompareAB implements Interface.
CompareBB implements Interface.
SetAB implements Interface.
SetB implements Interface.
SetBA implements Interface.
SetClass implements Interface.
String implements fmt.Stringer.
BigRat is an interval having math/big.Rat bounds.
Code:
x := &BigRat{LeftOpen, big.NewRat(1, 1), big.NewRat(2, 1)} y := &BigRat{LeftClosed, big.NewRat(2, 1), big.NewRat(3, 1)} fmt.Printf("x %v, y %v: x ∩ y %v, x ∪ y %v", x, y, Intersection(x, y), Union(x, y))
Output:
x (1/1, 2/1], y [2/1, 3/1): x ∩ y {2/1}, x ∪ y (1/1, 3/1)
Class implements Interface.
Clone implements Interface.
CompareAA implements Interface.
CompareAB implements Interface.
CompareBB implements Interface.
SetAB implements Interface.
SetB implements Interface.
SetBA implements Interface.
SetClass implements Interface.
String implements fmt.Stringer.
Byte is an interval having byte bounds.
Code:
x := &Byte{LeftOpen, 1, 2} y := &Byte{LeftClosed, 2, 3} fmt.Printf("x %v, y %v: x ∩ y %v, x ∪ y %v", x, y, Intersection(x, y), Union(x, y))
Output:
x (1, 2], y [2, 3): x ∩ y {2}, x ∪ y (1, 3)
Class implements Interface.
Clone implements Interface.
CompareAA implements Interface.
CompareAB implements Interface.
CompareBB implements Interface.
SetAB implements Interface.
SetB implements Interface.
SetBA implements Interface.
SetClass implements Interface.
String implements fmt.Stringer.
Class represent a type of an interval.
const ( Unbounded Class = iota // (-∞, ∞). Empty // {}. Degenerate // [a, a] = {a}. // Proper and bounded: Open // (a, b) = {x | a < x < b}. Closed // [a, b] = {x | a <= x <= b}. LeftOpen // (a, b] = {x | a < x <= b}. LeftClosed // [a, b) = {x | a <= x < b}. // Left-bounded and right-unbounded: LeftBoundedOpen // (a, ∞) = {x | x > a}. LeftBoundedClosed // [a, ∞) = {x | x >= a}. // Left-unbounded and right-bounded: RightBoundedOpen // (-∞, b) = {x | x < b}. RightBoundedClosed // (-∞, b] = {x | x <= b}. )
Interval classes.
Duration is an interval having time.Duration bounds.
Code:
x := &Duration{LeftOpen, 1, 2} y := &Duration{LeftClosed, 2, 3} fmt.Printf("x %v, y %v: x ∩ y %v, x ∪ y %v", x, y, Intersection(x, y), Union(x, y))
Output:
x (1ns, 2ns], y [2ns, 3ns): x ∩ y {2ns}, x ∪ y (1ns, 3ns)
Class implements Interface.
Clone implements Interface.
CompareAA implements Interface.
CompareAB implements Interface.
CompareBB implements Interface.
SetAB implements Interface.
SetB implements Interface.
SetBA implements Interface.
SetClass implements Interface.
String implements fmt.Stringer.
Float32 is an interval having float32 bounds.
Note: Using NaNs as bounds has undefined behavior.
Code:
x := &Float32{LeftOpen, 1, 2} y := &Float32{LeftClosed, 2, 3} fmt.Printf("x %v, y %v: x ∩ y %v, x ∪ y %v", x, y, Intersection(x, y), Union(x, y))
Output:
x (1, 2], y [2, 3): x ∩ y {2}, x ∪ y (1, 3)
Class implements Interface.
Clone implements Interface.
CompareAA implements Interface.
CompareAB implements Interface.
CompareBB implements Interface.
SetAB implements Interface.
SetB implements Interface.
SetBA implements Interface.
SetClass implements Interface.
String implements fmt.Stringer.
Float64 is an interval having float64 bounds.
Note: Using NaNs as bounds has undefined behavior.
Code:
x := &Float32{LeftOpen, 1, 2} y := &Float32{LeftClosed, 2, 3} fmt.Printf("x %v, y %v: x ∩ y %v, x ∪ y %v", x, y, Intersection(x, y), Union(x, y))
Output:
x (1, 2], y [2, 3): x ∩ y {2}, x ∪ y (1, 3)
Class implements Interface.
Clone implements Interface.
CompareAA implements Interface.
CompareAB implements Interface.
CompareBB implements Interface.
SetAB implements Interface.
SetB implements Interface.
SetBA implements Interface.
SetClass implements Interface.
String implements fmt.Stringer.
Int is an interval having int bounds.
Code:
x := &Int{LeftOpen, 1, 2} y := &Int{LeftClosed, 2, 3} fmt.Printf("x %v, y %v: x ∩ y %v, x ∪ y %v", x, y, Intersection(x, y), Union(x, y))
Output:
x (1, 2], y [2, 3): x ∩ y {2}, x ∪ y (1, 3)
Class implements Interface.
Clone implements Interface.
CompareAA implements Interface.
CompareAB implements Interface.
CompareBB implements Interface.
SetAB implements Interface.
SetB implements Interface.
SetBA implements Interface.
SetClass implements Interface.
String implements fmt.Stringer.
Int128 is an interval having Int128 bounds.
Code:
x := &Int128{LeftOpen, int128.SetInt64(1), int128.SetInt64(2)} y := &Int128{LeftClosed, int128.SetInt64(2), int128.SetInt64(3)} fmt.Printf("x %v, y %v: x ∩ y %v, x ∪ y %v", x, y, Intersection(x, y), Union(x, y))
Output:
x (1, 2], y [2, 3): x ∩ y {2}, x ∪ y (1, 3)
Class implements Interface.
Clone implements Interface.
CompareAA implements Interface.
CompareAB implements Interface.
CompareBB implements Interface.
SetAB implements Interface.
SetB implements Interface.
SetBA implements Interface.
SetClass implements Interface.
String implements fmt.Stringer.
Int16 is an interval having int16 bounds.
Code:
x := &Int16{LeftOpen, 1, 2} y := &Int16{LeftClosed, 2, 3} fmt.Printf("x %v, y %v: x ∩ y %v, x ∪ y %v", x, y, Intersection(x, y), Union(x, y))
Output:
x (1, 2], y [2, 3): x ∩ y {2}, x ∪ y (1, 3)
Class implements Interface.
Clone implements Interface.
CompareAA implements Interface.
CompareAB implements Interface.
CompareBB implements Interface.
SetAB implements Interface.
SetB implements Interface.
SetBA implements Interface.
SetClass implements Interface.
String implements fmt.Stringer.
Int32 is an interval having int32 bounds.
Code:
x := &Int32{LeftOpen, 1, 2} y := &Int32{LeftClosed, 2, 3} fmt.Printf("x %v, y %v: x ∩ y %v, x ∪ y %v", x, y, Intersection(x, y), Union(x, y))
Output:
x (1, 2], y [2, 3): x ∩ y {2}, x ∪ y (1, 3)
Class implements Interface.
Clone implements Interface.
CompareAA implements Interface.
CompareAB implements Interface.
CompareBB implements Interface.
SetAB implements Interface.
SetB implements Interface.
SetBA implements Interface.
SetClass implements Interface.
String implements fmt.Stringer.
Int64 is an interval having int64 bounds.
Code:
x := &Int64{LeftOpen, 1, 2} y := &Int64{LeftClosed, 2, 3} fmt.Printf("x %v, y %v: x ∩ y %v, x ∪ y %v", x, y, Intersection(x, y), Union(x, y))
Output:
x (1, 2], y [2, 3): x ∩ y {2}, x ∪ y (1, 3)
Class implements Interface.
Clone implements Interface.
CompareAA implements Interface.
CompareAB implements Interface.
CompareBB implements Interface.
SetAB implements Interface.
SetB implements Interface.
SetBA implements Interface.
SetClass implements Interface.
String implements fmt.Stringer.
Int8 is an interval having int8 bounds.
Code:
x := &Int8{LeftOpen, 1, 2} y := &Int8{LeftClosed, 2, 3} fmt.Printf("x %v, y %v: x ∩ y %v, x ∪ y %v", x, y, Intersection(x, y), Union(x, y))
Output:
x (1, 2], y [2, 3): x ∩ y {2}, x ∪ y (1, 3)
Class implements Interface.
Clone implements Interface.
CompareAA implements Interface.
CompareAB implements Interface.
CompareBB implements Interface.
SetAB implements Interface.
SetB implements Interface.
SetBA implements Interface.
SetClass implements Interface.
String implements fmt.Stringer.
type Interface interface { // Class returns the interval class. Class() Class // Clone clones the interval. Clone() Interface // CompareAA compares interval.A and other.A. CompareAA(other Interface) int // CompareAB compares interval.A and other.B. CompareAB(other Interface) int // CompareBB compares interval.B and other.B. CompareBB(other Interface) int // SetClass sets the interval class. SetClass(Class) // SetAB sets interval.A using interval.B. SetAB() // SetB sets interval.B using other.B. SetB(other Interface) // SetBA sets interval.B using other.A. SetBA(other Interface) }
Interface represents an unbounded, empty, degenerate, proper, left-bounded or right-bounded interval. Where appropriate, the interval bounds are defined by ordered values, named by convention a and b, when present.
Proper intervals have an invariant: a < b.
CompareXX return value must obey these rules
< 0 if interval A or B < other interval A or B 0 if interval A or B == other interval A or B > 0 if interval A or B > other interval A or B
Intersection returns the intersection of x and y.
Union returns the union of x and y. If the union is not an interval, nil is returned instead. The union is not an interval if it is a disjoint set.
String is an interval having string bounds.
Code:
x := &String{LeftOpen, "aqua", "bar"} y := &String{LeftClosed, "bar", "closed"} fmt.Printf("x %v, y %v: x ∩ y %v, x ∪ y %v", x, y, Intersection(x, y), Union(x, y))
Output:
x (aqua, bar], y [bar, closed): x ∩ y {bar}, x ∪ y (aqua, closed)
Class implements Interface.
Clone implements Interface.
CompareAA implements Interface.
CompareAB implements Interface.
CompareBB implements Interface.
SetAB implements Interface.
SetB implements Interface.
SetBA implements Interface.
SetClass implements Interface.
String implements fmt.Stringer.
Time is an interval having time.Time bounds.
Code:
x := &Time{LeftOpen, time.Unix(1, 0), time.Unix(2, 0)} y := &Time{LeftClosed, time.Unix(2, 0), time.Unix(3, 0)} fmt.Printf("x %v, y %v: x ∩ y %v, x ∪ y %v", x, y, Intersection(x, y), Union(x, y))
Output:
x (1970-01-01 01:00:01 +0100 CET, 1970-01-01 01:00:02 +0100 CET], y [1970-01-01 01:00:02 +0100 CET, 1970-01-01 01:00:03 +0100 CET): x ∩ y {1970-01-01 01:00:02 +0100 CET}, x ∪ y (1970-01-01 01:00:01 +0100 CET, 1970-01-01 01:00:03 +0100 CET)
Class implements Interface.
Clone implements Interface.
CompareAA implements Interface.
CompareAB implements Interface.
CompareBB implements Interface.
SetAB implements Interface.
SetB implements Interface.
SetBA implements Interface.
SetClass implements Interface.
String implements fmt.Stringer.
Uint is an interval having uint bounds.
Code:
x := &Uint{LeftOpen, 1, 2} y := &Uint{LeftClosed, 2, 3} fmt.Printf("x %v, y %v: x ∩ y %v, x ∪ y %v", x, y, Intersection(x, y), Union(x, y))
Output:
x (1, 2], y [2, 3): x ∩ y {2}, x ∪ y (1, 3)
Class implements Interface.
Clone implements Interface.
CompareAA implements Interface.
CompareAB implements Interface.
CompareBB implements Interface.
SetAB implements Interface.
SetB implements Interface.
SetBA implements Interface.
SetClass implements Interface.
String implements fmt.Stringer.
Uint16 is an interval having uint16 bounds.
Code:
x := &Uint16{LeftOpen, 1, 2} y := &Uint16{LeftClosed, 2, 3} fmt.Printf("x %v, y %v: x ∩ y %v, x ∪ y %v", x, y, Intersection(x, y), Union(x, y))
Output:
x (1, 2], y [2, 3): x ∩ y {2}, x ∪ y (1, 3)
Class implements Interface.
Clone implements Interface.
CompareAA implements Interface.
CompareAB implements Interface.
CompareBB implements Interface.
SetAB implements Interface.
SetB implements Interface.
SetBA implements Interface.
SetClass implements Interface.
String implements fmt.Stringer.
Uint32 is an interval having uint32 bounds.
Code:
x := &Uint32{LeftOpen, 1, 2} y := &Uint32{LeftClosed, 2, 3} fmt.Printf("x %v, y %v: x ∩ y %v, x ∪ y %v", x, y, Intersection(x, y), Union(x, y))
Output:
x (1, 2], y [2, 3): x ∩ y {2}, x ∪ y (1, 3)
Class implements Interface.
Clone implements Interface.
CompareAA implements Interface.
CompareAB implements Interface.
CompareBB implements Interface.
SetAB implements Interface.
SetB implements Interface.
SetBA implements Interface.
SetClass implements Interface.
String implements fmt.Stringer.
Uint64 is an interval having uint64 bounds.
Code:
x := &Uint64{LeftOpen, 1, 2} y := &Uint64{LeftClosed, 2, 3} fmt.Printf("x %v, y %v: x ∩ y %v, x ∪ y %v", x, y, Intersection(x, y), Union(x, y))
Output:
x (1, 2], y [2, 3): x ∩ y {2}, x ∪ y (1, 3)
Class implements Interface.
Clone implements Interface.
CompareAA implements Interface.
CompareAB implements Interface.
CompareBB implements Interface.
SetAB implements Interface.
SetB implements Interface.
SetBA implements Interface.
SetClass implements Interface.
String implements fmt.Stringer.
Package interval imports 4 packages (graph) and is imported by 1 packages. Updated 2018-11-28. Refresh now. Tools for package owners.