Skip to content

Commit d57a190

Browse files
rsneddsymonds
authored andcommitted
s2: Add EdgeIterator.
Signed-off-by: David Symonds <dsymonds@golang.org>
1 parent c8d99d9 commit d57a190

File tree

2 files changed

+196
-0
lines changed

2 files changed

+196
-0
lines changed

s2/shapeutil_edge_iterator.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright 2020 Google Inc. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package s2
16+
17+
// EdgeIterator is an iterator that advances through all edges in an ShapeIndex.
18+
// This is different to the ShapeIndexIterator, which advances through the cells in the
19+
// ShapeIndex.
20+
type EdgeIterator struct {
21+
index *ShapeIndex
22+
shapeID int32
23+
numEdges int32
24+
edgeID int32
25+
}
26+
27+
// NewEdgeIterator creates a new edge iterator for the given index.
28+
func NewEdgeIterator(index *ShapeIndex) *EdgeIterator {
29+
e := &EdgeIterator{
30+
index: index,
31+
shapeID: -1,
32+
edgeID: -1,
33+
}
34+
35+
e.Next()
36+
return e
37+
}
38+
39+
// ShapeID returns the current shape ID.
40+
func (e *EdgeIterator) ShapeID() int32 { return e.shapeID }
41+
42+
// EdgeID returns the current edge ID.
43+
func (e *EdgeIterator) EdgeID() int32 { return e.edgeID }
44+
45+
// ShapeEdgeID returns the current (shapeID, edgeID).
46+
func (e *EdgeIterator) ShapeEdgeID() ShapeEdgeID { return ShapeEdgeID{e.shapeID, e.edgeID} }
47+
48+
// Edge returns the current edge.
49+
func (e *EdgeIterator) Edge() Edge {
50+
return e.index.Shape(e.shapeID).Edge(int(e.edgeID))
51+
}
52+
53+
// Done reports if the iterator is positioned at or after the last index edge.
54+
func (e *EdgeIterator) Done() bool { return e.shapeID >= int32(len(e.index.shapes)) }
55+
56+
// Next positions the iterator at the next index edge.
57+
func (e *EdgeIterator) Next() {
58+
e.edgeID++
59+
for ; e.edgeID >= e.numEdges; e.edgeID++ {
60+
e.shapeID++
61+
if e.shapeID >= int32(len(e.index.shapes)) {
62+
break
63+
}
64+
shape := e.index.Shape(e.shapeID)
65+
if shape == nil {
66+
e.numEdges = 0
67+
} else {
68+
e.numEdges = int32(shape.NumEdges())
69+
}
70+
e.edgeID = -1
71+
}
72+
}

s2/shapeutil_edge_iterator_test.go

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
// Copyright 2020 Google Inc. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package s2
16+
17+
import (
18+
"testing"
19+
)
20+
21+
// allEdgesInShapeIndex returns the full list of edges in the given index in shapeID order.
22+
func allEdgesInShapeIndex(index *ShapeIndex) []Edge {
23+
var result []Edge
24+
// Iterator works over the shapes in shape ID order, so we don't just
25+
// range over the map here because order is not guaranteed.
26+
for i := 0; i < len(index.shapes); i++ {
27+
shape := index.shapes[int32(i)]
28+
if shape == nil {
29+
continue
30+
}
31+
for j := 0; j < shape.NumEdges(); j++ {
32+
result = append(result, shape.Edge(j))
33+
}
34+
}
35+
return result
36+
}
37+
38+
func verifyEdgeIterator(t *testing.T, index *ShapeIndex) {
39+
expected := allEdgesInShapeIndex(index)
40+
i := 0
41+
42+
for iter := NewEdgeIterator(index); !iter.Done(); iter.Next() {
43+
if got, want := iter.Edge(), expected[i]; got != want {
44+
t.Errorf("edge[%d] = %v, want %v", i, got, want)
45+
}
46+
i++
47+
}
48+
}
49+
50+
func TestShapeutilEdgeIteratorEmpty(t *testing.T) {
51+
index := makeShapeIndex("##")
52+
verifyEdgeIterator(t, index)
53+
}
54+
55+
func TestShapeutilEdgeIteratorPoints(t *testing.T) {
56+
index := makeShapeIndex("0:0|1:1##")
57+
verifyEdgeIterator(t, index)
58+
}
59+
60+
func TestShapeutilEdgeIteratorLines(t *testing.T) {
61+
index := makeShapeIndex("#0:0,10:10|5:5,5:10|1:2,2:1#")
62+
verifyEdgeIterator(t, index)
63+
}
64+
65+
func TestShapeutilEdgeIteratorPolygons(t *testing.T) {
66+
index := makeShapeIndex("##10:10,10:0,0:0|-10:-10,-10:0,0:0,0:-10")
67+
verifyEdgeIterator(t, index)
68+
}
69+
70+
func TestShapeutilEdgeIteratorCollection(t *testing.T) {
71+
index := makeShapeIndex("1:1|7:2#1:1,2:2,3:3|2:2,1:7#10:10,10:0,0:0;20:20,20:10,10:10|15:15,15:0,0:0")
72+
verifyEdgeIterator(t, index)
73+
}
74+
75+
func TestShapeutilEdgeIteratorRemove(t *testing.T) {
76+
index := makeShapeIndex("1:1|7:2#1:1,2:2,3:3|2:2,1:7#10:10,10:0,0:0;20:20,20:10,10:10|15:15,15:0,0:0")
77+
index.Remove(index.Shape(0))
78+
79+
verifyEdgeIterator(t, index)
80+
}
81+
82+
// edgeIteratorEq reports if two edge iterators are the same.
83+
func edgeIteratorEq(a, b *EdgeIterator) bool {
84+
return a.shapeID == b.shapeID && a.edgeID == b.edgeID && a.index == b.index
85+
}
86+
87+
// copyEdgeIterator copies the state of the given iterator into a new instance.
88+
func copyEdgeIterator(a *EdgeIterator) *EdgeIterator {
89+
return &EdgeIterator{
90+
index: a.index,
91+
shapeID: a.shapeID,
92+
edgeID: a.edgeID,
93+
numEdges: a.numEdges,
94+
}
95+
}
96+
97+
func TestShapeutilEdgeIteratorAssignmentAndEquality(t *testing.T) {
98+
index1 := makeShapeIndex("1:1|7:2#1:1,2:2,3:3|2:2,1:7#10:10,10:0,0:0;20:20,20:10,10:10|15:15,15:0,0:0")
99+
100+
index2 := makeShapeIndex("1:1|7:2#1:1,2:2,3:3|2:2,1:7#10:10,10:0,0:0;20:20,20:10,10:10|15:15,15:0,0:0")
101+
102+
it1 := NewEdgeIterator(index1)
103+
it2 := NewEdgeIterator(index2)
104+
105+
// The underlying indexes have the same data, but are not the same index.
106+
if edgeIteratorEq(it1, it2) {
107+
t.Errorf("edgeIterators equal but shouldn't be")
108+
}
109+
110+
it1 = copyEdgeIterator(it2)
111+
if !edgeIteratorEq(it1, it2) {
112+
t.Errorf("edgeIterators not equal but should be")
113+
}
114+
115+
it1.Next()
116+
if edgeIteratorEq(it1, it2) {
117+
t.Errorf("edgeIterators equal but shouldn't be after one is advanced. \n1: %+v\n2: %+v", it1, it2)
118+
}
119+
120+
it2.Next()
121+
if !edgeIteratorEq(it1, it2) {
122+
t.Errorf("edgeIterators not equal but should be after both advanced same amount")
123+
}
124+
}

0 commit comments

Comments
 (0)