Skip to content

Commit e8fe6a7

Browse files
authored
refactor: use slices.Contains and ContainsFunc (#185)
Use `go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -fix -test ./...` to make the code more modern and easy to read. Signed-off-by: cuishuang <[email protected]>
1 parent 18014e7 commit e8fe6a7

File tree

5 files changed

+11
-29
lines changed

5 files changed

+11
-29
lines changed

s2/cellunion.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package s2
1717
import (
1818
"fmt"
1919
"io"
20+
"slices"
2021
"sort"
2122

2223
"github.com/golang/geo/s1"
@@ -404,13 +405,7 @@ func (cu *CellUnion) Contains(o CellUnion) bool {
404405

405406
// Intersects reports whether this CellUnion intersects any of the CellIDs of the given CellUnion.
406407
func (cu *CellUnion) Intersects(o CellUnion) bool {
407-
for _, c := range *cu {
408-
if o.IntersectsCellID(c) {
409-
return true
410-
}
411-
}
412-
413-
return false
408+
return slices.ContainsFunc(*cu, o.IntersectsCellID)
414409
}
415410

416411
// lowerBound returns the index in this CellUnion to the first element whose value

s2/convex_hull_query_test.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package s2
1616

1717
import (
1818
"math"
19+
"slices"
1920
"testing"
2021

2122
"github.com/golang/geo/s1"
@@ -95,12 +96,7 @@ func TestConvexHullAntipodalPoints(t *testing.T) {
9596
}
9697

9798
func loopHasVertex(l *Loop, p Point) bool {
98-
for _, v := range l.vertices {
99-
if v == p {
100-
return true
101-
}
102-
}
103-
return false
99+
return slices.Contains(l.vertices, p)
104100
}
105101

106102
func TestConvexHullQueryEmptyLoop(t *testing.T) {

s2/polygon.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"fmt"
1919
"io"
2020
"math"
21+
"slices"
2122
)
2223

2324
// Polygon represents a sequence of zero or more loops; recall that the
@@ -880,12 +881,7 @@ func (p *Polygon) Intersects(o *Polygon) bool {
880881
}
881882

882883
if !p.hasHoles && !o.hasHoles {
883-
for _, l := range o.loops {
884-
if p.anyLoopIntersects(l) {
885-
return true
886-
}
887-
}
888-
return false
884+
return slices.ContainsFunc(o.loops, p.anyLoopIntersects)
889885
}
890886

891887
// Polygon A is disjoint from B if A excludes the entire boundary of B and B

s2/polyline.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"fmt"
1919
"io"
2020
"math"
21+
"slices"
2122

2223
"github.com/golang/geo/s1"
2324
)
@@ -135,10 +136,8 @@ func (p *Polyline) IntersectsCell(cell Cell) bool {
135136
// We only need to check whether the cell contains vertex 0 for correctness,
136137
// but these tests are cheap compared to edge crossings so we might as well
137138
// check all the vertices.
138-
for _, v := range *p {
139-
if cell.ContainsPoint(v) {
140-
return true
141-
}
139+
if slices.ContainsFunc(*p, cell.ContainsPoint) {
140+
return true
142141
}
143142

144143
cellVertices := []Point{

s2/shapeindex.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package s2
1616

1717
import (
1818
"math"
19+
"slices"
1920
"sort"
2021
"sync"
2122
"sync/atomic"
@@ -100,12 +101,7 @@ func (c *clippedShape) numEdges() int {
100101
func (c *clippedShape) containsEdge(id int) bool {
101102
// Linear search is fast because the number of edges per shape is typically
102103
// very small (less than 10).
103-
for _, e := range c.edges {
104-
if e == id {
105-
return true
106-
}
107-
}
108-
return false
104+
return slices.Contains(c.edges, id)
109105
}
110106

111107
// ShapeIndexCell stores the index contents for a particular CellID.

0 commit comments

Comments
 (0)