Skip to content

Commit 82a554a

Browse files
authored
TSCBasic: Use collection diff api for EditDistance if possible (#302)
* Use collection diff api if possible * Separate func for unit test
1 parent 33534cb commit 82a554a

File tree

2 files changed

+37
-11
lines changed

2 files changed

+37
-11
lines changed

Sources/TSCBasic/EditDistance.swift

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,28 @@
88
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
99
*/
1010

11-
/// Computes the number of edits needed to transform first string to second.
11+
/// Computes the number of edits needed to transform source string to target.
1212
///
13-
/// - Complexity: O(_n*m_), where *n* is the length of the first String and
14-
/// *m* is the length of the second one.
15-
public func editDistance(_ first: String, _ second: String) -> Int {
13+
/// - Complexity: O(_n*m_), where *n* is the length of the source String and
14+
/// *m* is the length of the target one.
15+
public func editDistance(_ source: String, _ target: String) -> Int {
1616
// FIXME: We should use the new `CollectionDifference` API once the
1717
// deployment target is bumped.
18+
if #available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) {
19+
return collectionDiffEditDistance(source, target)
20+
}
21+
else {
22+
return internalEditDistance(source, target)
23+
}
24+
}
25+
26+
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
27+
func collectionDiffEditDistance(_ source: String, _ target: String) -> Int {
28+
let difference = target.difference(from: source)
29+
return max(difference.insertions.count, difference.removals.count)
30+
}
31+
32+
func internalEditDistance(_ first: String, _ second: String) -> Int {
1833
let a = Array(first.utf16)
1934
let b = Array(second.utf16)
2035
var distance = [[Int]](repeating: [Int](repeating: 0, count: b.count + 1), count: a.count + 1)

Tests/TSCBasicTests/EditDistanceTests.swift

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,29 @@
88
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
99
*/
1010

11+
@testable import TSCBasic
1112
import XCTest
1213

1314
import TSCBasic
1415

1516
class EditDistanceTests: XCTestCase {
1617

17-
func testBasics() {
18-
XCTAssertEqual(editDistance("Foo", "Fo"), 1)
19-
XCTAssertEqual(editDistance("Foo", "Foo"), 0)
20-
XCTAssertEqual(editDistance("Bar", "Foo"), 3)
21-
XCTAssertEqual(editDistance("ABCDE", "ABDE"), 1)
22-
XCTAssertEqual(editDistance("sunday", "saturday"), 3)
23-
XCTAssertEqual(editDistance("FOO", "foo"), 3)
18+
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
19+
func testEditDistanceWithCollectionDiff() {
20+
XCTAssertEqual(collectionDiffEditDistance("Foo", "Fo"), 1)
21+
XCTAssertEqual(collectionDiffEditDistance("Foo", "Foo"), 0)
22+
XCTAssertEqual(collectionDiffEditDistance("Bar", "Foo"), 3)
23+
XCTAssertEqual(collectionDiffEditDistance("ABCDE", "ABDE"), 1)
24+
XCTAssertEqual(collectionDiffEditDistance("sunday", "saturday"), 3)
25+
XCTAssertEqual(collectionDiffEditDistance("FOO", "foo"), 3)
26+
}
27+
28+
func testInternalEditDistance() {
29+
XCTAssertEqual(internalEditDistance("Foo", "Fo"), 1)
30+
XCTAssertEqual(internalEditDistance("Foo", "Foo"), 0)
31+
XCTAssertEqual(internalEditDistance("Bar", "Foo"), 3)
32+
XCTAssertEqual(internalEditDistance("ABCDE", "ABDE"), 1)
33+
XCTAssertEqual(internalEditDistance("sunday", "saturday"), 3)
34+
XCTAssertEqual(internalEditDistance("FOO", "foo"), 3)
2435
}
2536
}

0 commit comments

Comments
 (0)