-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Kazuhiro Hayashi
committed
Feb 23, 2017
1 parent
96fa8cb
commit b1c5526
Showing
8 changed files
with
124 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// | ||
// EditDistance.swift | ||
// EditDistance | ||
// | ||
// Created by Kazuhiro Hayashi on 2/19/17. | ||
// Copyright © 2017 Kazuhiro Hayashi. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
public class EditDistance<T: Comparable> { | ||
private let _from: [[T]] | ||
private let _to: [[T]] | ||
|
||
public init(from: [[T]], to: [[T]]) { | ||
_from = from | ||
_to = to | ||
} | ||
|
||
public init(from: [T], to: [T]) { | ||
_from = [from] | ||
_to = [to] | ||
} | ||
|
||
public func calculate<Algorithm: EditDistanceAlgorithm>(with algorithm: Algorithm) -> [EditScript<T>] where Algorithm.Element == T { | ||
return algorithm.calculate(from: _from, to: _to) | ||
} | ||
|
||
public func calculate() -> [EditScript<T>] { | ||
return Wu().calculate(from: _from, to: _to) | ||
} | ||
} | ||
|
||
public class EditDistanceProxy<T: Comparable> { | ||
private let _generator: ([T]) -> EditDistance<T> | ||
|
||
public init(_ from: [T]) { | ||
_generator = { (to: [T]) -> EditDistance<T> in | ||
return EditDistance(from: from, to: to) | ||
} | ||
} | ||
|
||
public func compare<Algorithm: EditDistanceAlgorithm>(to ary: [T], algorithm: Algorithm) -> [EditScript<T>] where Algorithm.Element == T { | ||
return _generator(ary).calculate(with: algorithm) | ||
} | ||
|
||
public func compare(to ary: [T]) -> [EditScript<T>] { | ||
return _generator(ary).calculate(with: Wu()) | ||
} | ||
} | ||
|
||
public extension Array where Element: Comparable { | ||
public var diff: EditDistanceProxy<Element> { | ||
return EditDistanceProxy(self) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// | ||
// EditDistanceProtocol.swift | ||
// EditDistance | ||
// | ||
// Created by Kazuhiro Hayashi on 2/18/17. | ||
// Copyright © 2017 Kazuhiro Hayashi. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
public protocol EditDistanceAlgorithm { | ||
associatedtype Element: Comparable | ||
|
||
func calculate(from: [[Element]], to: [[Element]]) -> [EditScript<Element>] | ||
} | ||
|
||
public class AnyEditDistance<T>: EditDistanceAlgorithm where T: Comparable { | ||
|
||
public func calculate(from: [[T]], to: [[T]]) -> [EditScript<T>] { | ||
return _calc(from, to) | ||
} | ||
|
||
public typealias Element = T | ||
public var _calc: ([[T]], [[T]]) -> [EditScript<T>] | ||
|
||
init(_ calc: @escaping ([[T]], [[T]]) -> [EditScript<T>]) { | ||
_calc = calc | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters