Skip to content

Commit

Permalink
Dedicated LinearRing, which makes sure it's closed (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
nighthawk authored Jun 14, 2022
1 parent aaba697 commit 8f1e509
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions Sources/GeoJSONKit/GeoJSON.swift
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,29 @@ public struct GeoJSON: Hashable {
}

public struct Polygon: Hashable {
public typealias LinearRing = LineString

public struct LinearRing: Hashable {
public var positions: [Position]

// We precompute this as it's static, but slow to re-compute
private let precomputedHash: Int

public init(positions: [Position]) {
var adjusted = positions
if let first = adjusted.first, first != adjusted.last {
adjusted.append(first)
}
self.positions = adjusted

var hasher = Hasher()
hasher.combine(adjusted)
precomputedHash = hasher.finalize()
}

public func hash(into hasher: inout Hasher) {
hasher.combine(precomputedHash)
}
}

public var exterior: LinearRing
public var interiors: [LinearRing]

Expand All @@ -227,7 +248,6 @@ public struct GeoJSON: Hashable {
self.exterior = exterior
self.interiors = interiors


var hasher = Hasher()
hasher.combine(exterior)
hasher.combine(interiors)
Expand Down

0 comments on commit 8f1e509

Please sign in to comment.