Skip to content

Commit

Permalink
distinctPairs() and overlappingPairs() should always return a result
Browse files Browse the repository at this point in the history
  • Loading branch information
trasch committed Oct 25, 2023
1 parent 03a4a1d commit 8e9d269
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 13 deletions.
12 changes: 10 additions & 2 deletions Sources/GISTools/Extensions/ArrayExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,13 @@ extension Array {
/// let a = [1, 2, 3, 4, 5]
/// a.distinctPairs() -> [(1, 2), (3, 4)]
/// ```
func distinctPairs() -> [(first: Element, second: Element)] {
func distinctPairs() -> [(first: Element, second: Element?)] {
guard !isEmpty else { return [] }

if count == 1 {
return [(first: self[0], second: nil)]
}

return (0 ..< (self.count / 2)).map { (index) in
let i = index * 2
return (first: self[i], second: self[i+1])
Expand All @@ -141,9 +145,13 @@ extension Array {
/// let a = [1, 2, 3, 4, 5]
/// a.overlappingPairs() -> [(1, 2), (2, 3), (3, 4), (4, 5)]
/// ```
func overlappingPairs() -> [(first: Element, second: Element)] {
func overlappingPairs() -> [(first: Element, second: Element?)] {
guard !isEmpty else { return [] }

if count == 1 {
return [(first: self[0], second: nil)]
}

return (0 ..< (self.count - 1)).map { (index) in
return (first: self[index], second: self[index + 1])
}
Expand Down
12 changes: 7 additions & 5 deletions Sources/GISTools/GeoJson/LineString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,13 @@ public struct LineString:
}
}

if current.first != previous.second {
coordinates.append(current.first)
}
if current.second != current.first {
coordinates.append(current.second)
if let current {
if current.first != previous.second {
coordinates.append(current.first)
}
if current.second != current.first {
coordinates.append(current.second)
}
}
}

Expand Down
10 changes: 6 additions & 4 deletions Sources/GISTools/Turf/LineSegments.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,19 @@ extension GeoJson {
return []

case let lineString as LineString:
return lineString.coordinates.overlappingPairs().map { (first, second) in
LineSegment(first: first, second: second)
return lineString.coordinates.overlappingPairs().compactMap { (first, second) in
guard let second else { return nil }
return LineSegment(first: first, second: second)
}

case let multiLineString as MultiLineString:
return multiLineString.lineStrings.flatMap { $0.lineSegments }

case let polygon as Polygon:
return polygon.rings.flatMap({ (ring) in
ring.coordinates.overlappingPairs().map { (first, second) in
LineSegment(first: first, second: second)
ring.coordinates.overlappingPairs().compactMap { (first, second) in
guard let second else { return nil }
return LineSegment(first: first, second: second)
}
})

Expand Down
4 changes: 2 additions & 2 deletions Tests/GISToolsTests/Extensions/ArrayExtensionsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ final class ArrayExtensionsTests: XCTestCase {
let smallPairs = small.distinctPairs()

XCTAssertEqual(emptyPairs.count, 0)
XCTAssertEqual(smallPairs.count, 0)
XCTAssertEqual(smallPairs.count, 1)
}

func testOverlappingPairs() {
Expand Down Expand Up @@ -76,7 +76,7 @@ final class ArrayExtensionsTests: XCTestCase {
let smallPairs = small.overlappingPairs()

XCTAssertEqual(emptyPairs.count, 0)
XCTAssertEqual(smallPairs.count, 0)
XCTAssertEqual(smallPairs.count, 1)
}

func testGet() {
Expand Down

0 comments on commit 8e9d269

Please sign in to comment.