Skip to content

Commit 8f1e509

Browse files
authored
Dedicated LinearRing, which makes sure it's closed (#15)
1 parent aaba697 commit 8f1e509

1 file changed

Lines changed: 23 additions & 3 deletions

File tree

Sources/GeoJSONKit/GeoJSON.swift

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,29 @@ public struct GeoJSON: Hashable {
203203
}
204204

205205
public struct Polygon: Hashable {
206-
public typealias LinearRing = LineString
207-
206+
public struct LinearRing: Hashable {
207+
public var positions: [Position]
208+
209+
// We precompute this as it's static, but slow to re-compute
210+
private let precomputedHash: Int
211+
212+
public init(positions: [Position]) {
213+
var adjusted = positions
214+
if let first = adjusted.first, first != adjusted.last {
215+
adjusted.append(first)
216+
}
217+
self.positions = adjusted
218+
219+
var hasher = Hasher()
220+
hasher.combine(adjusted)
221+
precomputedHash = hasher.finalize()
222+
}
223+
224+
public func hash(into hasher: inout Hasher) {
225+
hasher.combine(precomputedHash)
226+
}
227+
}
228+
208229
public var exterior: LinearRing
209230
public var interiors: [LinearRing]
210231

@@ -227,7 +248,6 @@ public struct GeoJSON: Hashable {
227248
self.exterior = exterior
228249
self.interiors = interiors
229250

230-
231251
var hasher = Hasher()
232252
hasher.combine(exterior)
233253
hasher.combine(interiors)

0 commit comments

Comments
 (0)