From 367fd92d1a2616d36cb8e6d1d3702c7959d395a3 Mon Sep 17 00:00:00 2001 From: Jacek Olszak Date: Sat, 29 Jul 2023 23:48:30 +0200 Subject: [PATCH] Remove pi.MaxInt and pi.MinInt Because Go 1.21 supports generic max and min functions which work for integers. --- examples/shapes/main.go | 2 +- internal/bench/math_bench_test.go | 28 ---------------------------- internal/fuzz/math_test.go | 12 ------------ math.go | 18 ------------------ math_test.go | 18 ------------------ pixmap.go | 12 ++++++------ 6 files changed, 7 insertions(+), 83 deletions(-) diff --git a/examples/shapes/main.go b/examples/shapes/main.go index bde93cc..e404bef 100644 --- a/examples/shapes/main.go +++ b/examples/shapes/main.go @@ -101,7 +101,7 @@ func drawMousePointer() { func radius(x0, y0, x1, y1 int) int { dx := math.Abs(float64(x0 - x1)) dy := math.Abs(float64(y0 - y1)) - return int(math.Max(dx, dy)) + return int(max(dx, dy)) } func printCmd(command string) { diff --git a/internal/bench/math_bench_test.go b/internal/bench/math_bench_test.go index 84c036c..d04d571 100644 --- a/internal/bench/math_bench_test.go +++ b/internal/bench/math_bench_test.go @@ -9,34 +9,6 @@ import ( "github.com/elgopher/pi" ) -func BenchmarkMinInt(b *testing.B) { - b.ReportAllocs() - b.ResetTimer() - - for i := 0; i < b.N; i++ { - for j := 0; j < 60; j++ { - pi.MinInt(j, j+1) - } - for j := 0; j < 60; j++ { - pi.MinInt(j+1, j) - } - } -} - -func BenchmarkMaxInt(b *testing.B) { - b.ReportAllocs() - b.ResetTimer() - - for i := 0; i < b.N; i++ { - for j := 0; j < 60; j++ { - pi.MaxInt(j, j+1) - } - for j := 0; j < 60; j++ { - pi.MaxInt(j+1, j) - } - } -} - func BenchmarkMidInt(b *testing.B) { b.ReportAllocs() b.ResetTimer() diff --git a/internal/fuzz/math_test.go b/internal/fuzz/math_test.go index b221336..715ba60 100644 --- a/internal/fuzz/math_test.go +++ b/internal/fuzz/math_test.go @@ -11,18 +11,6 @@ import ( "github.com/elgopher/pi" ) -func FuzzMinInt(f *testing.F) { - f.Fuzz(func(t *testing.T, x, y int) { - pi.MinInt(x, y) - }) -} - -func FuzzMaxInt(f *testing.F) { - f.Fuzz(func(t *testing.T, x, y int) { - pi.MaxInt(x, y) - }) -} - func FuzzMidInt(f *testing.F) { f.Fuzz(func(t *testing.T, x, y, z int) { pi.MidInt(x, y, z) diff --git a/math.go b/math.go index e1d2c70..965edce 100644 --- a/math.go +++ b/math.go @@ -41,24 +41,6 @@ type Int interface { ~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr } -// MinInt returns minimum of two integer numbers. -func MinInt[T Int](x, y T) T { - if x < y { - return x - } - - return y -} - -// MaxInt returns maximum of two integer numbers. -func MaxInt[T Int](x, y T) T { - if x > y { - return x - } - - return y -} - // MidInt returns the middle of three integer numbers. Very useful for clamping. func MidInt[T Int](x, y, z T) T { if x > y { diff --git a/math_test.go b/math_test.go index 8b9acdc..942b725 100644 --- a/math_test.go +++ b/math_test.go @@ -114,24 +114,6 @@ func TestAtan2(t *testing.T) { } } -func TestMinInt(t *testing.T) { - assert.Equal(t, 0, pi.MinInt(0, 0)) - assert.Equal(t, 1, pi.MinInt(1, 2)) - assert.Equal(t, 1, pi.MinInt(1, 1)) - assert.Equal(t, 1, pi.MinInt(2, 1)) - assert.Equal(t, -2, pi.MinInt(-1, -2)) - assert.Equal(t, -2, pi.MinInt(-2, 2)) -} - -func TestMaxInt(t *testing.T) { - assert.Equal(t, 0, pi.MaxInt(0, 0)) - assert.Equal(t, 2, pi.MaxInt(2, 1)) - assert.Equal(t, 1, pi.MaxInt(1, 1)) - assert.Equal(t, 2, pi.MaxInt(1, 2)) - assert.Equal(t, -1, pi.MaxInt(-1, -2)) - assert.Equal(t, 2, pi.MaxInt(-2, 2)) -} - func TestMidInt(t *testing.T) { assert.Equal(t, 0, pi.MidInt(0, 0, 0)) assert.Equal(t, 1, pi.MidInt(0, 1, 2)) diff --git a/pixmap.go b/pixmap.go index 661da32..5a0612c 100644 --- a/pixmap.go +++ b/pixmap.go @@ -206,8 +206,8 @@ func (p PixMap) Pointer(x, y, w, h int) (ptr Pointer, ok bool) { DeltaX: dx, DeltaY: dy, Pix: pix, - RemainingPixels: MinInt(w, clip.X+clip.W-x), - RemainingLines: MinInt(h, clip.Y+clip.H-y), + RemainingPixels: min(w, clip.X+clip.W-x), + RemainingLines: min(h, clip.Y+clip.H-y), }, true } @@ -224,13 +224,13 @@ type Pointer struct { func (p PixMap) Copy(x, y, w, h int, dst PixMap, dstX, dstY int) { dstPtr, srcPtr := p.pointersForCopy(x, y, w, h, dst, dstX, dstY) - remainingLines := MinInt(dstPtr.RemainingLines, srcPtr.RemainingLines) + remainingLines := min(dstPtr.RemainingLines, srcPtr.RemainingLines) if remainingLines == 0 { return } - remainingPixels := MinInt(dstPtr.RemainingPixels, srcPtr.RemainingPixels) + remainingPixels := min(dstPtr.RemainingPixels, srcPtr.RemainingPixels) copy(dstPtr.Pix[:remainingPixels], srcPtr.Pix) for i := 1; i < remainingLines; i++ { @@ -244,13 +244,13 @@ func (p PixMap) Copy(x, y, w, h int, dst PixMap, dstX, dstY int) { func (p PixMap) Merge(x, y, w, h int, dst PixMap, dstX, dstY int, merge func(dst, src []byte)) { dstPtr, srcPtr := p.pointersForCopy(x, y, w, h, dst, dstX, dstY) - remainingLines := MinInt(dstPtr.RemainingLines, srcPtr.RemainingLines) + remainingLines := min(dstPtr.RemainingLines, srcPtr.RemainingLines) if remainingLines == 0 { return } - remainingPixels := MinInt(dstPtr.RemainingPixels, srcPtr.RemainingPixels) + remainingPixels := min(dstPtr.RemainingPixels, srcPtr.RemainingPixels) merge(dstPtr.Pix[:remainingPixels], srcPtr.Pix) for i := 1; i < remainingLines; i++ {