Skip to content

Commit

Permalink
support Two dimensional array
Browse files Browse the repository at this point in the history
  • Loading branch information
Kazuhiro Hayashi committed Feb 23, 2017
1 parent 96fa8cb commit b1c5526
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 118 deletions.
38 changes: 21 additions & 17 deletions EditDistance.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@
6EBF3E3B1E58543F0025443A /* EditDistance.h in Headers */ = {isa = PBXBuildFile; fileRef = 6EBF3E2D1E58543F0025443A /* EditDistance.h */; settings = {ATTRIBUTES = (Public, ); }; };
6EBF3E451E58549A0025443A /* EditScript.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6EBF3E441E58549A0025443A /* EditScript.swift */; };
6EBF3E471E5854C70025443A /* EditScriptConverter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6EBF3E461E5854C70025443A /* EditScriptConverter.swift */; };
6EBF3E491E5854FD0025443A /* EditDistanceProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6EBF3E481E5854FD0025443A /* EditDistanceProtocol.swift */; };
6EBF3E4B1E58552A0025443A /* Collection.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6EBF3E4A1E58552A0025443A /* Collection.swift */; };
6EBF3E491E5854FD0025443A /* EditDistanceAlgorithm.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6EBF3E481E5854FD0025443A /* EditDistanceAlgorithm.swift */; };
6EBF3E4D1E58553C0025443A /* Wu.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6EBF3E4C1E58553C0025443A /* Wu.swift */; };
6EBF3E911E59C2FB0025443A /* EditDistanceProxy.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6EBF3E901E59C2FB0025443A /* EditDistanceProxy.swift */; };
6EBF3E911E59C2FB0025443A /* EditDistance.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6EBF3E901E59C2FB0025443A /* EditDistance.swift */; };
6EBF3E931E59C3640025443A /* EditScriptConverterProxy+UITableView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6EBF3E921E59C3640025443A /* EditScriptConverterProxy+UITableView.swift */; };
6EBF3E951E59C3830025443A /* EditScriptConverterProxy+UICollectionView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6EBF3E941E59C3830025443A /* EditScriptConverterProxy+UICollectionView.swift */; };
6EBF3E971E59C3BE0025443A /* EditScriptConverterProxy.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6EBF3E961E59C3BE0025443A /* EditScriptConverterProxy.swift */; };
Expand All @@ -42,10 +41,9 @@
6EBF3E3A1E58543F0025443A /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
6EBF3E441E58549A0025443A /* EditScript.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EditScript.swift; sourceTree = "<group>"; };
6EBF3E461E5854C70025443A /* EditScriptConverter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EditScriptConverter.swift; sourceTree = "<group>"; };
6EBF3E481E5854FD0025443A /* EditDistanceProtocol.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EditDistanceProtocol.swift; sourceTree = "<group>"; };
6EBF3E4A1E58552A0025443A /* Collection.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Collection.swift; sourceTree = "<group>"; };
6EBF3E481E5854FD0025443A /* EditDistanceAlgorithm.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EditDistanceAlgorithm.swift; sourceTree = "<group>"; };
6EBF3E4C1E58553C0025443A /* Wu.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Wu.swift; sourceTree = "<group>"; };
6EBF3E901E59C2FB0025443A /* EditDistanceProxy.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EditDistanceProxy.swift; sourceTree = "<group>"; };
6EBF3E901E59C2FB0025443A /* EditDistance.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EditDistance.swift; sourceTree = "<group>"; };
6EBF3E921E59C3640025443A /* EditScriptConverterProxy+UITableView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "EditScriptConverterProxy+UITableView.swift"; sourceTree = "<group>"; };
6EBF3E941E59C3830025443A /* EditScriptConverterProxy+UICollectionView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "EditScriptConverterProxy+UICollectionView.swift"; sourceTree = "<group>"; };
6EBF3E961E59C3BE0025443A /* EditScriptConverterProxy.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EditScriptConverterProxy.swift; sourceTree = "<group>"; };
Expand All @@ -72,6 +70,17 @@
/* End PBXFrameworksBuildPhase section */

/* Begin PBXGroup section */
6E0301631E5F2DA300E24C49 /* Converter */ = {
isa = PBXGroup;
children = (
6EBF3E461E5854C70025443A /* EditScriptConverter.swift */,
6EBF3E961E59C3BE0025443A /* EditScriptConverterProxy.swift */,
6EBF3E921E59C3640025443A /* EditScriptConverterProxy+UITableView.swift */,
6EBF3E941E59C3830025443A /* EditScriptConverterProxy+UICollectionView.swift */,
);
name = Converter;
sourceTree = "<group>";
};
6EBF3E201E58543F0025443A = {
isa = PBXGroup;
children = (
Expand All @@ -96,14 +105,8 @@
6EBF3E2D1E58543F0025443A /* EditDistance.h */,
6EBF3E2E1E58543F0025443A /* Info.plist */,
6EBF3E441E58549A0025443A /* EditScript.swift */,
6EBF3E461E5854C70025443A /* EditScriptConverter.swift */,
6EBF3E961E59C3BE0025443A /* EditScriptConverterProxy.swift */,
6EBF3E921E59C3640025443A /* EditScriptConverterProxy+UITableView.swift */,
6EBF3E941E59C3830025443A /* EditScriptConverterProxy+UICollectionView.swift */,
6EBF3E481E5854FD0025443A /* EditDistanceProtocol.swift */,
6EBF3EF11E5C79330025443A /* EditDistanceContainer.swift */,
6EBF3E901E59C2FB0025443A /* EditDistanceProxy.swift */,
6EBF3E4A1E58552A0025443A /* Collection.swift */,
6EBF3E901E59C2FB0025443A /* EditDistance.swift */,
6E0301631E5F2DA300E24C49 /* Converter */,
6EBF3E4E1E58565A0025443A /* Algorithm */,
);
path = EditDistance;
Expand All @@ -121,6 +124,8 @@
6EBF3E4E1E58565A0025443A /* Algorithm */ = {
isa = PBXGroup;
children = (
6EBF3E481E5854FD0025443A /* EditDistanceAlgorithm.swift */,
6EBF3EF11E5C79330025443A /* EditDistanceContainer.swift */,
6EBF3E4C1E58553C0025443A /* Wu.swift */,
6EBF3ED91E5B2BBF0025443A /* DynamicProgramming.swift */,
);
Expand Down Expand Up @@ -243,10 +248,9 @@
6EBF3E951E59C3830025443A /* EditScriptConverterProxy+UICollectionView.swift in Sources */,
6EBF3E971E59C3BE0025443A /* EditScriptConverterProxy.swift in Sources */,
6EBF3E451E58549A0025443A /* EditScript.swift in Sources */,
6EBF3E911E59C2FB0025443A /* EditDistanceProxy.swift in Sources */,
6EBF3E4B1E58552A0025443A /* Collection.swift in Sources */,
6EBF3E911E59C2FB0025443A /* EditDistance.swift in Sources */,
6EBF3E931E59C3640025443A /* EditScriptConverterProxy+UITableView.swift in Sources */,
6EBF3E491E5854FD0025443A /* EditDistanceProtocol.swift in Sources */,
6EBF3E491E5854FD0025443A /* EditDistanceAlgorithm.swift in Sources */,
6EBF3EDA1E5B2BBF0025443A /* DynamicProgramming.swift in Sources */,
6EBF3EF21E5C79330025443A /* EditDistanceContainer.swift in Sources */,
6EBF3E4D1E58553C0025443A /* Wu.swift in Sources */,
Expand Down
21 changes: 0 additions & 21 deletions EditDistance/Collection.swift

This file was deleted.

4 changes: 2 additions & 2 deletions EditDistance/DynamicProgramming.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@

import Foundation

public class DynamicProgramming<T: Comparable>: EditDistanceProtocol {
public class DynamicProgramming<T: Comparable>: EditDistanceAlgorithm {
public typealias Element = T

public init() {}

public func calculate<Col: Collection>(from: Col, to: Col) -> [EditScript<Element>] where Col.Iterator.Element: Collection, Col.Iterator.Element.Iterator.Element == Element {
public func calculate(from: [[T]], to: [[T]]) -> [EditScript<T>] {
let flattendTo = to.enumerated().flatMap { (firstOffset, collection) in
return collection.enumerated().flatMap { (secondOffset, element) in
return EditDistanceContainer(indexPath: IndexPath(row: secondOffset, section: firstOffset), element: element)
Expand Down
56 changes: 56 additions & 0 deletions EditDistance/EditDistance.swift
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)
}
}
29 changes: 29 additions & 0 deletions EditDistance/EditDistanceAlgorithm.swift
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
}
}
15 changes: 0 additions & 15 deletions EditDistance/EditDistanceProtocol.swift

This file was deleted.

41 changes: 0 additions & 41 deletions EditDistance/EditDistanceProxy.swift

This file was deleted.

38 changes: 16 additions & 22 deletions EditDistance/Wu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,32 @@

import Foundation

public struct Wu<T: Comparable>: EditDistanceProtocol {
public struct Wu<T: Comparable>: EditDistanceAlgorithm {
public typealias Element = T

public init() {}

public func calculate<Col: Collection>(from: Col, to: Col) -> [EditScript<Element>] where Col.Iterator.Element: Collection, Col.Iterator.Element.Iterator.Element == Element {
public func calculate(from: [[T]], to: [[T]]) -> [EditScript<T>] {
let _to = to.enumerated().flatMap { (firstIdx, ary) in
return ary.enumerated().flatMap { (secondIdx, elm) in
return EditDistanceContainer(indexPath: IndexPath(row: secondIdx, section: firstIdx), element: elm)
}
}
let _from = from.enumerated().flatMap { (firstIdx, ary) in
return ary.enumerated().flatMap { (secondIdx, elm) in
return EditDistanceContainer(indexPath: IndexPath(row: secondIdx, section: firstIdx), element: elm)
}
}
let xAxis: [EditDistanceContainer<T>]
let yAxis: [EditDistanceContainer<T>]
var ctl: Ctl
if from.count >= to.count {
xAxis = to.enumerated().flatMap { (firstOffset, collection) in
return collection.enumerated().flatMap { (secondOffset, element) in
return EditDistanceContainer(indexPath: IndexPath(row: secondOffset, section: firstOffset), element: element)
}
}
yAxis = from.enumerated().flatMap { (firstOffset, collection) in
return collection.enumerated().flatMap { (secondOffset, element) in
return EditDistanceContainer(indexPath: IndexPath(row: secondOffset, section: firstOffset), element: element)
}
}
xAxis = _to
yAxis = _from
ctl = Ctl(reverse: true, path: [], pathPosition: [:])
} else {
xAxis = from.enumerated().flatMap { (firstOffset, collection) in
return collection.enumerated().flatMap { (secondOffset, element) in
return EditDistanceContainer(indexPath: IndexPath(row: secondOffset, section: firstOffset), element: element)
}
}
yAxis = to.enumerated().flatMap { (firstOffset, collection) in
return collection.enumerated().flatMap { (secondOffset, element) in
return EditDistanceContainer(indexPath: IndexPath(row: secondOffset, section: firstOffset), element: element)
}
}
xAxis = _from
yAxis = _to
ctl = Ctl(reverse: false, path: [], pathPosition: [:])
}
let offset = xAxis.count + 1
Expand Down

0 comments on commit b1c5526

Please sign in to comment.