Rename interval.Int to interval.IntRange

This commit is contained in:
Nigel Tao committed 2018-01-18 10:14:41 +11:00
1 parent af1ea0b425
commit 3aa95ba88f
3 files changed
+87 -83

No files matched your search

+48 -44
View File
@@ -96,14 +96,14 @@ func (x *biggerIntPair) raiseMax(y biggerInt) {
} }
} }
func (x *biggerIntPair) toInt() Int { func (x *biggerIntPair) toIntRange() IntRange {
if x[0].extra > 0 || x[1].extra < 0 { if x[0].extra > 0 || x[1].extra < 0 {
return empty() return empty()
} }
return Int{x[0].i, x[1].i} return IntRange{x[0].i, x[1].i}
} }
func (x *biggerIntPair) fromInt(y Int) { func (x *biggerIntPair) fromIntRange(y IntRange) {
if y[0] != nil { if y[0] != nil {
x[0] = biggerInt{i: big.NewInt(0).Set(y[0])} x[0] = biggerInt{i: big.NewInt(0).Set(y[0])}
} else { } else {
@@ -116,9 +116,9 @@ func (x *biggerIntPair) fromInt(y Int) {
} }
} }
// Int is an integer interval. The array elements are the minimum and maximum // IntRange is an integer interval. The array elements are the minimum and
// values, inclusive (if non-nil) on both ends. A nil element means unbounded: // maximum values, inclusive (if non-nil) on both ends. A nil element means
// negative or positive infinity. // unbounded: negative or positive infinity.
// //
// The zero value (zero in the Go sense, not in the integer sense) is a valid, // The zero value (zero in the Go sense, not in the integer sense) is a valid,
// infinitely sized interval, unbounded at both ends. // infinitely sized interval, unbounded at both ends.
@@ -131,10 +131,14 @@ func (x *biggerIntPair) fromInt(y Int) {
// It is also valid for the first element to be greater than the second // It is also valid for the first element to be greater than the second
// element. This represents an empty interval. There is more than one // element. This represents an empty interval. There is more than one
// representation of an empty interval. // representation of an empty interval.
type Int [2]*big.Int //
// "Int" abbreviates "Integer" (the same as for big.Int), not "Interval".
// Similarly, we use "Range" instead of "Interval" to avoid unnecessary
// confusion, even though this type is indeed an integer interval.
type IntRange [2]*big.Int
// String returns a string representation of x. // String returns a string representation of x.
func (x Int) String() string { func (x IntRange) String() string {
if x.Empty() { if x.Empty() {
return "<empty>" return "<empty>"
} }
@@ -155,13 +159,13 @@ func (x Int) String() string {
return string(buf) return string(buf)
} }
// empty returns an empty Int: one that contains no elements. // empty returns an empty IntRange: one that contains no elements.
func empty() Int { func empty() IntRange {
return Int{big.NewInt(+1), big.NewInt(-1)} return IntRange{big.NewInt(+1), big.NewInt(-1)}
} }
// ContainsNegative returns whether x contains at least one negative value. // ContainsNegative returns whether x contains at least one negative value.
func (x Int) ContainsNegative() bool { func (x IntRange) ContainsNegative() bool {
if x[0] == nil { if x[0] == nil {
return true return true
} }
@@ -172,7 +176,7 @@ func (x Int) ContainsNegative() bool {
} }
// ContainsPositive returns whether x contains at least one positive value. // ContainsPositive returns whether x contains at least one positive value.
func (x Int) ContainsPositive() bool { func (x IntRange) ContainsPositive() bool {
if x[1] == nil { if x[1] == nil {
return true return true
} }
@@ -183,13 +187,13 @@ func (x Int) ContainsPositive() bool {
} }
// ContainsZero returns whether x contains zero. // ContainsZero returns whether x contains zero.
func (x Int) ContainsZero() bool { func (x IntRange) ContainsZero() bool {
return (x[0] == nil || x[0].Sign() <= 0) && return (x[0] == nil || x[0].Sign() <= 0) &&
(x[1] == nil || x[1].Sign() >= 0) (x[1] == nil || x[1].Sign() >= 0)
} }
// Eq returns whether x equals y. // Eq returns whether x equals y.
func (x Int) Eq(y Int) bool { func (x IntRange) Eq(y IntRange) bool {
if xe, ye := x.Empty(), y.Empty(); xe || ye { if xe, ye := x.Empty(), y.Empty(); xe || ye {
return xe == ye return xe == ye
} }
@@ -207,20 +211,20 @@ func (x Int) Eq(y Int) bool {
} }
// Empty returns whether x is empty. // Empty returns whether x is empty.
func (x Int) Empty() bool { func (x IntRange) Empty() bool {
return x[0] != nil && x[1] != nil && x[0].Cmp(x[1]) > 0 return x[0] != nil && x[1] != nil && x[0].Cmp(x[1]) > 0
} }
// justZero returns whether x is the [0, 0] interval, containing exactly one // justZero returns whether x is the [0, 0] interval, containing exactly one
// element: the integer zero. // element: the integer zero.
func (x Int) justZero() bool { func (x IntRange) justZero() bool {
return x[0] != nil && x[1] != nil && x[0].Sign() == 0 && x[1].Sign() == 0 return x[0] != nil && x[1] != nil && x[0].Sign() == 0 && x[1].Sign() == 0
} }
// split splits x into negative, zero and positive sub-intervals. The Int // split splits x into negative, zero and positive sub-intervals. The IntRange
// values returned may be empty, which means that x does not contain any // values returned may be empty, which means that x does not contain any
// negative or positive elements. // negative or positive elements.
func (x Int) split() (neg Int, pos Int, negEmpty bool, hasZero bool, posEmpty bool) { func (x IntRange) split() (neg IntRange, pos IntRange, negEmpty bool, hasZero bool, posEmpty bool) {
if x[0] != nil && x[0].Sign() > 0 { if x[0] != nil && x[0].Sign() > 0 {
return empty(), x, true, false, x.Empty() return empty(), x, true, false, x.Empty()
} }
@@ -244,7 +248,7 @@ func (x Int) split() (neg Int, pos Int, negEmpty bool, hasZero bool, posEmpty bo
} }
// Add returns z = x + y. // Add returns z = x + y.
func (x Int) Add(y Int) (z Int) { func (x IntRange) Add(y IntRange) (z IntRange) {
if x.Empty() || y.Empty() { if x.Empty() || y.Empty() {
return empty() return empty()
} }
@@ -258,7 +262,7 @@ func (x Int) Add(y Int) (z Int) {
} }
// Sub returns z = x - y. // Sub returns z = x - y.
func (x Int) Sub(y Int) (z Int) { func (x IntRange) Sub(y IntRange) (z IntRange) {
if x.Empty() || y.Empty() { if x.Empty() || y.Empty() {
return empty() return empty()
} }
@@ -272,28 +276,28 @@ func (x Int) Sub(y Int) (z Int) {
} }
// Mul returns z = x * y. // Mul returns z = x * y.
func (x Int) Mul(y Int) (z Int) { func (x IntRange) Mul(y IntRange) (z IntRange) {
return x.mulLsh(y, false) return x.mulLsh(y, false)
} }
// Lsh returns z = x << y. // Lsh returns z = x << y.
// //
// ok is false (and z will be Int{nil, nil}) if x is non-empty and y contains // ok is false (and z will be IntRange{nil, nil}) if x is non-empty and y
// at least one negative value, as it's invalid to shift by a negative number. // contains at least one negative value, as it's invalid to shift by a negative
// Otherwise, ok is true. // number. Otherwise, ok is true.
func (x Int) Lsh(y Int) (z Int, ok bool) { func (x IntRange) Lsh(y IntRange) (z IntRange, ok bool) {
if !x.Empty() && y.ContainsNegative() { if !x.Empty() && y.ContainsNegative() {
return Int{}, false return IntRange{}, false
} }
return x.mulLsh(y, true), true return x.mulLsh(y, true), true
} }
func (x Int) mulLsh(y Int, shift bool) (z Int) { func (x IntRange) mulLsh(y IntRange, shift bool) (z IntRange) {
if x.Empty() || y.Empty() { if x.Empty() || y.Empty() {
return empty() return empty()
} }
if x.justZero() || (!shift && y.justZero()) { if x.justZero() || (!shift && y.justZero()) {
return Int{big.NewInt(0), big.NewInt(0)} return IntRange{big.NewInt(0), big.NewInt(0)}
} }
combine := bigIntMul combine := bigIntMul
@@ -308,7 +312,7 @@ func (x Int) mulLsh(y Int, shift bool) (z Int) {
negY, posY, negYEmpty, zeroY, posYEmpty := y.split() negY, posY, negYEmpty, zeroY, posYEmpty := y.split()
if zeroY && shift { if zeroY && shift {
ret.fromInt(x) ret.fromIntRange(x)
} else if (zeroY && !shift) || zeroX { } else if (zeroY && !shift) || zeroX {
ret[0] = biggerInt{i: big.NewInt(0)} ret[0] = biggerInt{i: big.NewInt(0)}
ret[1] = biggerInt{i: big.NewInt(0)} ret[1] = biggerInt{i: big.NewInt(0)}
@@ -364,23 +368,23 @@ func (x Int) mulLsh(y Int, shift bool) (z Int) {
} }
} }
return ret.toInt() return ret.toIntRange()
} }
// Quo returns z = x / y. Like the big.Int.Quo method (and unlike the // Quo returns z = x / y. Like the big.Int.Quo method (and unlike the
// big.Int.Div method), it truncates towards zero. // big.Int.Div method), it truncates towards zero.
// //
// ok is false (and z will be Int{nil, nil}) if x is non-empty and y contains // ok is false (and z will be IntRange{nil, nil}) if x is non-empty and y
// zero, as it's invalid to divide by zero. Otherwise, ok is true. // contains zero, as it's invalid to divide by zero. Otherwise, ok is true.
func (x Int) Quo(y Int) (z Int, ok bool) { func (x IntRange) Quo(y IntRange) (z IntRange, ok bool) {
if x.Empty() || y.Empty() { if x.Empty() || y.Empty() {
return empty(), true return empty(), true
} }
if y.ContainsZero() { if y.ContainsZero() {
return Int{}, false return IntRange{}, false
} }
if x.justZero() { if x.justZero() {
return Int{big.NewInt(0), big.NewInt(0)}, true return IntRange{big.NewInt(0), big.NewInt(0)}, true
} }
ret := newBiggerIntPair() ret := newBiggerIntPair()
@@ -454,23 +458,23 @@ func (x Int) Quo(y Int) (z Int, ok bool) {
} }
} }
return ret.toInt(), true return ret.toIntRange(), true
} }
// Rsh returns z = x >> y. // Rsh returns z = x >> y.
// //
// ok is false (and z will be Int{nil, nil}) if x is non-empty and y contains // ok is false (and z will be IntRange{nil, nil}) if x is non-empty and y
// at least one negative value, as it's invalid to shift by a negative number. // contains at least one negative value, as it's invalid to shift by a negative
// Otherwise, ok is true. // number. Otherwise, ok is true.
func (x Int) Rsh(y Int) (z Int, ok bool) { func (x IntRange) Rsh(y IntRange) (z IntRange, ok bool) {
if x.Empty() || y.Empty() { if x.Empty() || y.Empty() {
return empty(), true return empty(), true
} }
if y.ContainsNegative() { if y.ContainsNegative() {
return Int{}, false return IntRange{}, false
} }
if x.justZero() { if x.justZero() {
return Int{big.NewInt(0), big.NewInt(0)}, true return IntRange{big.NewInt(0), big.NewInt(0)}, true
} }
ret := newBiggerIntPair() ret := newBiggerIntPair()
@@ -511,5 +515,5 @@ func (x Int) Rsh(y Int) (z Int, ok bool) {
} }
} }
return ret.toInt(), true return ret.toIntRange(), true
} }
+34 -34
View File
@@ -27,11 +27,11 @@ import (
// TestMotivatingExample tests the "motivating example" given in the package // TestMotivatingExample tests the "motivating example" given in the package
// doc comment. // doc comment.
func TestMotivatingExample(tt *testing.T) { func TestMotivatingExample(tt *testing.T) {
i := Int{big.NewInt(0), big.NewInt(255)} i := IntRange{big.NewInt(0), big.NewInt(255)}
j := Int{big.NewInt(0), big.NewInt(3)} j := IntRange{big.NewInt(0), big.NewInt(3)}
four := Int{big.NewInt(4), big.NewInt(4)} four := IntRange{big.NewInt(4), big.NewInt(4)}
got := four.Mul(i).Add(j) got := four.Mul(i).Add(j)
want := Int{big.NewInt(0), big.NewInt(1023)} want := IntRange{big.NewInt(0), big.NewInt(1023)}
if !got.Eq(want) { if !got.Eq(want) {
tt.Fatalf("got %v, want %v", got, want) tt.Fatalf("got %v, want %v", got, want)
} }
@@ -145,38 +145,38 @@ func parseInt(s string) (x *big.Int, remaining string, err error) {
// It also parses infinite (unbounded) intervals like "[0, +∞)", and the // It also parses infinite (unbounded) intervals like "[0, +∞)", and the
// special syntax "[...empty..]" for an empty interval (one that contains no // special syntax "[...empty..]" for an empty interval (one that contains no
// elements). // elements).
func parseInterval(s string) (x Int, remaining string, err error) { func parseInterval(s string) (x IntRange, remaining string, err error) {
const emptySyntax = "[...empty..]" const emptySyntax = "[...empty..]"
s = trimLeadingSpaces(s) s = trimLeadingSpaces(s)
if strings.HasPrefix(s, emptySyntax) { if strings.HasPrefix(s, emptySyntax) {
s = s[len(emptySyntax):] s = s[len(emptySyntax):]
return Int{big.NewInt(+1), big.NewInt(-1)}, s, nil return IntRange{big.NewInt(+1), big.NewInt(-1)}, s, nil
} }
if s[0] != '(' && s[0] != '[' { if s[0] != '(' && s[0] != '[' {
return Int{}, "", fmt.Errorf("expected '(' or '['") return IntRange{}, "", fmt.Errorf("expected '(' or '['")
} }
s = s[1:] s = s[1:]
x0, s, err := parseInt(s) x0, s, err := parseInt(s)
if err != nil { if err != nil {
return Int{}, "", err return IntRange{}, "", err
} }
if s[0] != ',' { if s[0] != ',' {
return Int{}, "", fmt.Errorf("expected ','") return IntRange{}, "", fmt.Errorf("expected ','")
} }
s = s[1:] s = s[1:]
x1, s, err := parseInt(s) x1, s, err := parseInt(s)
if err != nil { if err != nil {
return Int{}, "", err return IntRange{}, "", err
} }
if s[0] != ')' && s[0] != ']' { if s[0] != ')' && s[0] != ']' {
return Int{}, "", fmt.Errorf("expected ')' or ']'") return IntRange{}, "", fmt.Errorf("expected ')' or ']'")
} }
s = s[1:] s = s[1:]
if x0 != nil && x1 != nil && x0.Cmp(x1) > 0 { if x0 != nil && x1 != nil && x0.Cmp(x1) > 0 {
return Int{}, "", fmt.Errorf("invalid empty interval") return IntRange{}, "", fmt.Errorf("invalid empty interval")
} }
return Int{x0, x1}, s, nil return IntRange{x0, x1}, s, nil
} }
func TestContainsEtc(tt *testing.T) { func TestContainsEtc(tt *testing.T) {
@@ -211,24 +211,24 @@ func TestContainsEtc(tt *testing.T) {
} }
// eqTestCases is appended to throughout the "range testCases" loop, but it // eqTestCases is appended to throughout the "range testCases" loop, but it
// always contains exactly one empty Int. // always contains exactly one empty IntRange.
eqTestCases := []Int{ eqTestCases := []IntRange{
empty(), empty(),
} }
for _, tc := range testCases { for _, tc := range testCases {
x := Int{} x := IntRange{}
switch tc.s { switch tc.s {
case "<empty-->": case "<empty-->":
x = Int{big.NewInt(-1), big.NewInt(-2)} x = IntRange{big.NewInt(-1), big.NewInt(-2)}
case "<empty0->": case "<empty0->":
x = Int{big.NewInt(00), big.NewInt(-2)} x = IntRange{big.NewInt(00), big.NewInt(-2)}
case "<empty+->": case "<empty+->":
x = Int{big.NewInt(+2), big.NewInt(-2)} x = IntRange{big.NewInt(+2), big.NewInt(-2)}
case "<empty+0>": case "<empty+0>":
x = Int{big.NewInt(+2), big.NewInt(00)} x = IntRange{big.NewInt(+2), big.NewInt(00)}
case "<empty++>": case "<empty++>":
x = Int{big.NewInt(+2), big.NewInt(+1)} x = IntRange{big.NewInt(+2), big.NewInt(+1)}
default: default:
err := error(nil) err := error(nil)
x, _, err = parseInterval(tc.s) x, _, err = parseInterval(tc.s)
@@ -291,7 +291,7 @@ func TestContainsEtc(tt *testing.T) {
} }
} }
func testBruteForceAgrees(x Int, y Int, opKey string) error { func testBruteForceAgrees(x IntRange, y IntRange, opKey string) error {
brute, bruteOK := bruteForce(x, y, opKey) brute, bruteOK := bruteForce(x, y, opKey)
got, gotOK := intOperators[opKey](x, y) got, gotOK := intOperators[opKey](x, y)
if !got.Eq(brute) || gotOK != bruteOK { if !got.Eq(brute) || gotOK != bruteOK {
@@ -324,11 +324,11 @@ func TestBruteForceAgreesSystematically(tt *testing.T) {
for _, x0 := range ints { for _, x0 := range ints {
for _, x1 := range ints { for _, x1 := range ints {
x := Int{x0, x1} x := IntRange{x0, x1}
for _, y0 := range ints { for _, y0 := range ints {
for _, y1 := range ints { for _, y1 := range ints {
y := Int{y0, y1} y := IntRange{y0, y1}
if err := testBruteForceAgrees(x, y, opKey); err != nil { if err := testBruteForceAgrees(x, y, opKey); err != nil {
tt.Fatalf("%v %s %v: %v", x, opKey, y, err) tt.Fatalf("%v %s %v: %v", x, opKey, y, err)
@@ -352,8 +352,8 @@ func TestBruteForceAgreesRandomly(tt *testing.T) {
rng := rand.New(rand.NewSource(0)) rng := rand.New(rand.NewSource(0))
for _, opKey := range intOperatorsKeys { for _, opKey := range intOperatorsKeys {
for i := 0; i < 10000; i++ { for i := 0; i < 10000; i++ {
x := Int{gen(rng), gen(rng)} x := IntRange{gen(rng), gen(rng)}
y := Int{gen(rng), gen(rng)} y := IntRange{gen(rng), gen(rng)}
if err := testBruteForceAgrees(x, y, opKey); err != nil { if err := testBruteForceAgrees(x, y, opKey); err != nil {
tt.Fatalf("%v %s %v: %v", x, opKey, y, err) tt.Fatalf("%v %s %v: %v", x, opKey, y, err)
} }
@@ -391,7 +391,7 @@ func testOp1(s string) error {
} }
s = s[2:] s = s[2:]
s = trimLeadingSpaces(s) s = trimLeadingSpaces(s)
want, wantOK := Int{}, false want, wantOK := IntRange{}, false
if s == "invalid" { if s == "invalid" {
s = "" s = ""
} else { } else {
@@ -417,13 +417,13 @@ func testOp1(s string) error {
return nil return nil
} }
var intOperators = map[string]func(Int, Int) (Int, bool){ var intOperators = map[string]func(IntRange, IntRange) (IntRange, bool){
"+": func(x Int, y Int) (z Int, ok bool) { return x.Add(y), true }, "+": func(x IntRange, y IntRange) (z IntRange, ok bool) { return x.Add(y), true },
"-": func(x Int, y Int) (z Int, ok bool) { return x.Sub(y), true }, "-": func(x IntRange, y IntRange) (z IntRange, ok bool) { return x.Sub(y), true },
"*": func(x Int, y Int) (z Int, ok bool) { return x.Mul(y), true }, "*": func(x IntRange, y IntRange) (z IntRange, ok bool) { return x.Mul(y), true },
"/": Int.Quo, "/": IntRange.Quo,
"<<": Int.Lsh, "<<": IntRange.Lsh,
">>": Int.Rsh, ">>": IntRange.Rsh,
} }
var intOperatorsKeys []string var intOperatorsKeys []string
+5 -5
View File
@@ -60,7 +60,7 @@ package interval
// useful. They are only used for brute force testing in interval_test.go. When // useful. They are only used for brute force testing in interval_test.go. When
// tests in that other file use radial numbers (via calling the bruteForce // tests in that other file use radial numbers (via calling the bruteForce
// function), the test cases are constructed so that every non-nil member of an // function), the test cases are constructed so that every non-nil member of an
// interval.Int maps to a "small" radialInput, and asSmallRadialInput will // interval.IntRange maps to a "small" radialInput, and asSmallRadialInput will
// panic if that doesn't hold. // panic if that doesn't hold.
// //
// These types exist in order to simplify calculations. The general problem of // These types exist in order to simplify calculations. The general problem of
@@ -128,7 +128,7 @@ func asSmallRadialInput(x *big.Int, defaultIfXIsNil radialInput) radialInput {
return radialInput(i) return radialInput(i)
} }
func enumerate(x Int) (min radialInput, max radialInput) { func enumerate(x IntRange) (min radialInput, max radialInput) {
if x.Empty() { if x.Empty() {
return +1, -1 return +1, -1
} }
@@ -346,7 +346,7 @@ var riOperators = map[string]func(radialInput, radialInput) radialOutPair{
">>": radialInput.Rsh, ">>": radialInput.Rsh,
} }
func bruteForce(x Int, y Int, opKey string) (z Int, ok bool) { func bruteForce(x IntRange, y IntRange, opKey string) (z IntRange, ok bool) {
op := riOperators[opKey] op := riOperators[opKey]
iMin, iMax := enumerate(x) iMin, iMax := enumerate(x)
jMin, jMax := enumerate(y) jMin, jMax := enumerate(y)
@@ -357,7 +357,7 @@ func bruteForce(x Int, y Int, opKey string) (z Int, ok bool) {
for j := jMin; j <= jMax; j++ { for j := jMin; j <= jMax; j++ {
k := op(i, j) k := op(i, j)
if k[0] == radialNaN || k[1] == radialNaN { if k[0] == radialNaN || k[1] == radialNaN {
return Int{}, false return IntRange{}, false
} }
if first { if first {
result = k result = k
@@ -376,5 +376,5 @@ func bruteForce(x Int, y Int, opKey string) (z Int, ok bool) {
if first { if first {
return empty(), true return empty(), true
} }
return Int{result[0].bigInt(), result[1].bigInt()}, true return IntRange{result[0].bigInt(), result[1].bigInt()}, true
} }