Skip to content

Commit

Permalink
Remove pi.MaxInt and pi.MinInt
Browse files Browse the repository at this point in the history
Because Go 1.21 supports generic max and min functions which work for integers.
  • Loading branch information
elgopher committed Jul 29, 2023
1 parent 70daae2 commit 367fd92
Show file tree
Hide file tree
Showing 6 changed files with 7 additions and 83 deletions.
2 changes: 1 addition & 1 deletion examples/shapes/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
28 changes: 0 additions & 28 deletions internal/bench/math_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
12 changes: 0 additions & 12 deletions internal/fuzz/math_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 0 additions & 18 deletions math.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
18 changes: 0 additions & 18 deletions math_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
12 changes: 6 additions & 6 deletions pixmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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++ {
Expand All @@ -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++ {
Expand Down

0 comments on commit 367fd92

Please sign in to comment.