From a4bfa6cb15b3300b865de286ca56b135b5383dac Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Thu, 12 Sep 2024 00:58:15 +0900 Subject: [PATCH] all: use Go 1.21's min/max builtin functions --- audio/internal/convert/float32.go | 7 ------- audio/vorbis/int16.go | 7 ------- examples/blend/main.go | 12 +----------- examples/blocks/blocks/field.go | 7 ------- examples/life/main.go | 14 -------------- examples/lines/main.go | 7 ------- examples/masking/main.go | 14 -------------- image_test.go | 20 -------------------- internal/atlas/image.go | 7 ------- internal/buffered/image.go | 5 +---- internal/clock/clock.go | 7 ------- internal/graphicscommand/commandqueue.go | 7 ------- internal/png/gen.go | 12 ------------ internal/png/stdlibreader.go | 7 ------- 14 files changed, 2 insertions(+), 131 deletions(-) diff --git a/audio/internal/convert/float32.go b/audio/internal/convert/float32.go index 3908b2dac..c41897c0b 100644 --- a/audio/internal/convert/float32.go +++ b/audio/internal/convert/float32.go @@ -93,10 +93,3 @@ func (r *float32BytesReader) Seek(offset int64, whence int) (int64, error) { } return n / 2 * 4, nil } - -func min(a, b int) int { - if a < b { - return a - } - return b -} diff --git a/audio/vorbis/int16.go b/audio/vorbis/int16.go index c095cc076..eca609797 100644 --- a/audio/vorbis/int16.go +++ b/audio/vorbis/int16.go @@ -34,13 +34,6 @@ type int16BytesReader struct { fbuf []float32 } -func max(a, b int) int { - if a < b { - return b - } - return a -} - func (r *int16BytesReader) Read(buf []byte) (int, error) { if r.eof { return 0, io.EOF diff --git a/examples/blend/main.go b/examples/blend/main.go index d68806aa0..8206f6524 100644 --- a/examples/blend/main.go +++ b/examples/blend/main.go @@ -163,19 +163,9 @@ func loadImage(data []byte) (*ebiten.Image, error) { } // max returns the largest of x or y. -func max(x, y int) int { - if x > y { - return x - } - return y -} - // maxSide returns the largest side of a or b images. func maxSide(a, b *ebiten.Image) int { - return max( - max(a.Bounds().Dx(), b.Bounds().Dx()), - max(a.Bounds().Dy(), b.Bounds().Dy()), - ) + return max(a.Bounds().Dx(), b.Bounds().Dx(), a.Bounds().Dy(), b.Bounds().Dy()) } // drawCenteredText is a util function for drawing blend mode description. diff --git a/examples/blocks/blocks/field.go b/examples/blocks/blocks/field.go index 612d24c4a..e1c51d7a0 100644 --- a/examples/blocks/blocks/field.go +++ b/examples/blocks/blocks/field.go @@ -187,13 +187,6 @@ func (f *Field) Update() { } } -func min(a, b float64) float64 { - if a > b { - return b - } - return a -} - func flushingColor(rate float64) colorm.ColorM { var clr colorm.ColorM alpha := min(1, rate*2) diff --git a/examples/life/main.go b/examples/life/main.go index 8971e43f1..dfa773973 100644 --- a/examples/life/main.go +++ b/examples/life/main.go @@ -96,20 +96,6 @@ func (w *World) Draw(pix []byte) { } } -func max(a, b int) int { - if a < b { - return b - } - return a -} - -func min(a, b int) int { - if a < b { - return a - } - return b -} - // neighbourCount calculates the Moore neighborhood of (x, y). func neighbourCount(a []bool, width, height, x, y int) int { c := 0 diff --git a/examples/lines/main.go b/examples/lines/main.go index e94f92852..49f385043 100644 --- a/examples/lines/main.go +++ b/examples/lines/main.go @@ -27,13 +27,6 @@ import ( "github.com/hajimehoshi/ebiten/v2/vector" ) -func min(x, y int) int { - if x < y { - return x - } - return y -} - var ( whiteImage = ebiten.NewImage(3, 3) diff --git a/examples/masking/main.go b/examples/masking/main.go index 5fdb53dfd..ce7851af9 100644 --- a/examples/masking/main.go +++ b/examples/masking/main.go @@ -145,20 +145,6 @@ func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) { return screenWidth, screenHeight } -func max(a, b int) int { - if a < b { - return b - } - return a -} - -func min(a, b int) int { - if a < b { - return a - } - return b -} - func main() { ebiten.SetWindowSize(screenWidth*2, screenHeight*2) ebiten.SetWindowTitle("Masking (Ebitengine Demo)") diff --git a/image_test.go b/image_test.go index 9fbc0d1c6..fc12afc6b 100644 --- a/image_test.go +++ b/image_test.go @@ -348,26 +348,6 @@ func TestImageDeallocate(t *testing.T) { } } -type ordered interface { - ~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr | ~float32 | ~float64 | ~string -} - -// TODO: Use the built-in function min from Go 1.21. -func min[T ordered](a, b T) T { - if a < b { - return a - } - return b -} - -// TODO: Use the built-in function max from Go 1.21. -func max[T ordered](a, b T) T { - if a < b { - return b - } - return a -} - func TestImageBlendLighter(t *testing.T) { img0, _, err := openEbitenImage() if err != nil { diff --git a/internal/atlas/image.go b/internal/atlas/image.go index 71165ea6e..e667dcbd6 100644 --- a/internal/atlas/image.go +++ b/internal/atlas/image.go @@ -35,13 +35,6 @@ var ( maxSize = 0 ) -func min(a, b int) int { - if a < b { - return a - } - return b -} - func appendDeferred(f func()) { deferredM.Lock() defer deferredM.Unlock() diff --git a/internal/buffered/image.go b/internal/buffered/image.go index 5c74a41ae..40d387be4 100644 --- a/internal/buffered/image.go +++ b/internal/buffered/image.go @@ -314,8 +314,5 @@ func (i *Image) syncPixelsIfNeeded() { blend := graphicsdriver.BlendCopy i.img.DrawTriangles(srcs, vs, is, blend, dr, [graphics.ShaderSrcImageCount]image.Rectangle{sr}, atlas.NearestFilterShader, nil, graphicsdriver.FillRuleFillAll, restorable.HintNone) - // TODO: Use clear if Go 1.21 is available. - for pos := range i.dotsBuffer { - delete(i.dotsBuffer, pos) - } + clear(i.dotsBuffer) } diff --git a/internal/clock/clock.go b/internal/clock/clock.go index 8a446b4c8..452f3cd75 100644 --- a/internal/clock/clock.go +++ b/internal/clock/clock.go @@ -64,13 +64,6 @@ func ActualTPS() float64 { return actualTPS } -func max(a, b int64) int64 { - if a < b { - return b - } - return a -} - func calcCountFromTPS(tps int64, now int64) int { if tps == 0 { return 0 diff --git a/internal/graphicscommand/commandqueue.go b/internal/graphicscommand/commandqueue.go index 7176cb240..8bb07b854 100644 --- a/internal/graphicscommand/commandqueue.go +++ b/internal/graphicscommand/commandqueue.go @@ -507,13 +507,6 @@ func roundUpPower2(x int) int { return p2 } -func max(a, b int) int { - if a < b { - return b - } - return a -} - func (b *uint32sBuffer) alloc(n int) []uint32 { buf := b.buf if len(buf)+n > cap(buf) { diff --git a/internal/png/gen.go b/internal/png/gen.go index 1623bd667..12675e8a3 100644 --- a/internal/png/gen.go +++ b/internal/png/gen.go @@ -136,18 +136,6 @@ func run() error { fmt.Fprintln(w) format.Node(w, fset, tree) - if f == "reader.go" { - // The min function was removed as of Go 1.22, but this is needed for old Go. - // TODO: Remove this when Go 1.21 is the minimum supported version. - fmt.Fprintln(w, ` -func min(a, b int) int { - if a < b { - return a - } - return b -}`) - } - if err := w.Flush(); err != nil { return err } diff --git a/internal/png/stdlibreader.go b/internal/png/stdlibreader.go index d12bb769e..f51be64f8 100644 --- a/internal/png/stdlibreader.go +++ b/internal/png/stdlibreader.go @@ -1054,10 +1054,3 @@ func DecodeConfig(r io.Reader) (image.Config, error) { func init() { } - -func min(a, b int) int { - if a < b { - return a - } - return b -}