From 5672e7f0a62bd3cafe0a7419ef3fa436c18271ee Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Thu, 31 Oct 2019 09:30:20 +1100 Subject: [PATCH] Add IntRange.Unite and IntRange.Intersect --- lib/interval/interval.go | 70 +++++++++++++++++++++++++++++++++++ lib/interval/interval_test.go | 46 +++++++++++++++++++++++ lib/interval/radial_test.go | 63 ++++++++++++++++++++++++------- 3 files changed, 165 insertions(+), 14 deletions(-) diff --git a/lib/interval/interval.go b/lib/interval/interval.go index e0b154ab..b848a4c2 100644 --- a/lib/interval/interval.go +++ b/lib/interval/interval.go @@ -42,6 +42,13 @@ var ( one = big.NewInt(1) ) +func bigIntClone(i *big.Int) *big.Int { + if i != nil { + return big.NewInt(0).Set(i) + } + return nil +} + func bigIntMul(i *big.Int, j *big.Int) *big.Int { return big.NewInt(0).Mul(i, j) } func bigIntQuo(i *big.Int, j *big.Int) *big.Int { return big.NewInt(0).Quo(i, j) } @@ -277,6 +284,69 @@ func (x IntRange) split() (neg IntRange, pos IntRange, negEmpty bool, hasZero bo return neg, pos, neg.Empty(), x.ContainsZero(), pos.Empty() } +// Unite returns z = x ∪ y, the union of two intervals. +func (x IntRange) Unite(y IntRange) (z IntRange) { + if x.Empty() { + return IntRange{ + bigIntClone(y[0]), + bigIntClone(y[1]), + } + } + if y.Empty() { + return IntRange{ + bigIntClone(x[0]), + bigIntClone(x[1]), + } + } + + if (x[0] != nil) && (y[0] != nil) { + if x[0].Cmp(y[0]) < 0 { + z[0] = big.NewInt(0).Set(x[0]) + } else { + z[0] = big.NewInt(0).Set(y[0]) + } + } + + if (x[1] != nil) && (y[1] != nil) { + if x[1].Cmp(y[1]) > 0 { + z[1] = big.NewInt(0).Set(x[1]) + } else { + z[1] = big.NewInt(0).Set(y[1]) + } + } + + return z +} + +// Intersect returns z = x ∩ y, the intersection of two intervals. +func (x IntRange) Intersect(y IntRange) (z IntRange) { + if x.Empty() || y.Empty() { + return empty() + } + + if x[0] == nil { + z[0] = bigIntClone(y[0]) + } else if y[0] == nil { + z[0] = bigIntClone(x[0]) + } else if x[0].Cmp(y[0]) < 0 { + z[0] = big.NewInt(0).Set(y[0]) + } else { + z[0] = big.NewInt(0).Set(x[0]) + } + + if x[1] == nil { + z[1] = bigIntClone(y[1]) + } else if y[1] == nil { + z[1] = bigIntClone(x[1]) + } else if x[1].Cmp(y[1]) < 0 { + z[1] = big.NewInt(0).Set(x[1]) + } else { + z[1] = big.NewInt(0).Set(y[1]) + } + + return z +} + // Add returns z = x + y. func (x IntRange) Add(y IntRange) (z IntRange) { if x.Empty() || y.Empty() { diff --git a/lib/interval/interval_test.go b/lib/interval/interval_test.go index 492f8eb7..4e924ba9 100644 --- a/lib/interval/interval_test.go +++ b/lib/interval/interval_test.go @@ -418,6 +418,8 @@ func testOp1(s string) error { } var intOperators = map[string]func(IntRange, IntRange) (IntRange, bool){ + "∪": func(x IntRange, y IntRange) (z IntRange, ok bool) { return x.Unite(y), true }, + "∩": func(x IntRange, y IntRange) (z IntRange, ok bool) { return x.Intersect(y), true }, "+": func(x IntRange, y IntRange) (z IntRange, ok bool) { return x.Add(y), true }, "-": func(x IntRange, y IntRange) (z IntRange, ok bool) { return x.Sub(y), true }, "*": func(x IntRange, y IntRange) (z IntRange, ok bool) { return x.Mul(y), true }, @@ -437,6 +439,50 @@ func init() { sort.Strings(intOperatorsKeys) } +func TestOpUnite(tt *testing.T) { + testOp(tt, + "[ 3, 3] ∪ [ -5, -5] == [ -5, 3]", + "[ 3, 3] ∪ [ 0, 0] == [ 0, 3]", + "[ 0, 0] ∪ [ -7, 7] == [ -7, 7]", + "[ 0, 2] ∪ [ 0, 5] == [ 0, 5]", + "[ 3, 6] ∪ [ 10, 15] == [ 3, 15]", + "[ 3, +∞) ∪ [ -4, -2] == [ -4, +∞)", + "[ 3, +∞) ∪ [ 10, 15] == [ 3, +∞)", + "[ 3, +∞) ∪ ( -∞, 15] == ( -∞, +∞)", + "[ 3, 6] ∪ ( -∞, 15] == ( -∞, 15]", + "[ 3, 6] ∪ ( -∞, +∞) == ( -∞, +∞)", + "( -∞, +∞) ∪ ( -∞, +∞) == ( -∞, +∞)", + "( -∞, +∞) ∪ [ 1, 2] == ( -∞, +∞)", + "( -∞, +∞) ∪ [ 0, 0] == ( -∞, +∞)", + "[ 3, 6] ∪ [...empty..] == [ 3, 6]", + "[...empty..] ∪ [ 10, 15] == [ 10, 15]", + "[...empty..] ∪ [...empty..] == [...empty..]", + "( -∞, +∞) ∪ [...empty..] == ( -∞, +∞)", + ) +} + +func TestOpIntersect(tt *testing.T) { + testOp(tt, + "[ 3, 3] ∩ [ -5, -5] == [...empty..]", + "[ 3, 3] ∩ [ 0, 0] == [...empty..]", + "[ 0, 0] ∩ [ -7, 7] == [ 0, 0]", + "[ 0, 2] ∩ [ 0, 5] == [ 0, 2]", + "[ 3, 6] ∩ [ 10, 15] == [...empty..]", + "[ 3, +∞) ∩ [ -4, -2] == [...empty..]", + "[ 3, +∞) ∩ [ 10, 15] == [ 10, 15]", + "[ 3, +∞) ∩ ( -∞, 15] == [ 3, 15]", + "[ 3, 6] ∩ ( -∞, 15] == [ 3, 6]", + "[ 3, 6] ∩ ( -∞, +∞) == [ 3, 6]", + "( -∞, +∞) ∩ ( -∞, +∞) == ( -∞, +∞)", + "( -∞, +∞) ∩ [ 1, 2] == [ 1, 2]", + "( -∞, +∞) ∩ [ 0, 0] == [ 0, 0]", + "[ 3, 6] ∩ [...empty..] == [...empty..]", + "[...empty..] ∩ [ 10, 15] == [...empty..]", + "[...empty..] ∩ [...empty..] == [...empty..]", + "( -∞, +∞) ∩ [...empty..] == [...empty..]", + ) +} + func TestOpAdd(tt *testing.T) { testOp(tt, "[ 3, 3] + [ -5, -5] == [ -2, -2]", diff --git a/lib/interval/radial_test.go b/lib/interval/radial_test.go index 3a54b50f..3a23594e 100644 --- a/lib/interval/radial_test.go +++ b/lib/interval/radial_test.go @@ -412,23 +412,58 @@ func bruteForce(x IntRange, y IntRange, opKey string) (z IntRange, ok bool) { jMin, jMax := enumerate(y) result := radialOutPair{} first := true + merge := func(k radialOutPair) { + if first { + result = k + first = false + return + } + if result[0] > k[0] { + result[0] = k[0] + } + if result[1] < k[1] { + result[1] = k[1] + } + } - for i := iMin; i <= iMax; i++ { + switch opKey { + case "∪": + if iMinC, iMaxC := iMin.canonicalize(), iMax.canonicalize(); iMinC <= iMaxC { + merge(radialOutPair{iMinC, iMaxC}) + } + if jMinC, jMaxC := jMin.canonicalize(), jMax.canonicalize(); jMinC <= jMaxC { + merge(radialOutPair{jMinC, jMaxC}) + } + if result[0] < -riRadius { + result[0] = -roRadius - 1 + } + if result[1] > +riRadius { + result[1] = +roRadius + 1 + } + + case "∩": + iMinC, iMaxC := iMin.canonicalize(), iMax.canonicalize() for j := jMin; j <= jMax; j++ { - k := op(i, j) - if k[0] == radialNaN || k[1] == radialNaN { - return IntRange{}, false + jC := j.canonicalize() + if (iMinC <= jC) && (jC <= iMaxC) { + merge(radialOutPair{jC, jC}) } - if first { - result = k - first = false - continue - } - if result[0] > k[0] { - result[0] = k[0] - } - if result[1] < k[1] { - result[1] = k[1] + } + if result[0] < -riRadius { + result[0] = -roRadius - 1 + } + if result[1] > +riRadius { + result[1] = +roRadius + 1 + } + + default: + for i := iMin; i <= iMax; i++ { + for j := jMin; j <= jMax; j++ { + k := op(i, j) + if k[0] == radialNaN || k[1] == radialNaN { + return IntRange{}, false + } + merge(k) } } }