Skip to content

Commit

Permalink
Split files (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kuniwak authored Jul 24, 2017
1 parent 1404a9a commit c8b23b0
Show file tree
Hide file tree
Showing 15 changed files with 344 additions and 283 deletions.
106 changes: 78 additions & 28 deletions MirrorDiffKit.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

200 changes: 0 additions & 200 deletions Sources/Diffable+diff.swift

This file was deleted.

57 changes: 57 additions & 0 deletions Sources/Diffable.diff.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import Foundation



extension Diffable {
static func diff (between a: Diffable, and b: Diffable) -> Differentia {
guard a != b else {
return Differentia(units: [.notChanged(a)])
}

switch (a, b) {
case let (.array(l), .array(r)):
return DiffableSequence.diff(
between: DiffableSequence(l),
and: DiffableSequence(r)
)

case let (.set(l), .set(r)):
return DiffableSet.diff(
between: DiffableSet(l),
and: DiffableSet(r)
)

case let (.dictionary(l), .dictionary(r)):
return DiffableDictionary.diff(
between: DiffableDictionary.from(diffableTuples: l),
and: DiffableDictionary.from(diffableTuples: r),
forKind: .dictionary
)

case let (.anyStruct(type: lt, entries: ld), .anyStruct(type: rt, entries: rd)):
guard lt == rt else {
return Differentia(units: [.deleted(a), .inserted(b)])
}

return DiffableDictionary.diff(
between: DiffableDictionary(ld),
and: DiffableDictionary(rd),
forKind: .anyStruct(type: lt)
)

case let (.anyClass(type: lt, entries: ld), .anyClass(type: rt, entries: rd)):
guard lt == rt else {
return Differentia(units: [.deleted(a), .inserted(b)])
}

return DiffableDictionary.diff(
between: DiffableDictionary(ld),
and: DiffableDictionary(rd),
forKind: .anyClass(type: lt)
)

default:
return Differentia(units: [.deleted(a), .inserted(b)])
}
}
}
File renamed without changes.
14 changes: 7 additions & 7 deletions Sources/DiffableDictionary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ struct DiffableDictionary {
}


static func diff(between lhs: DiffableDictionary, and rhs: DiffableDictionary, forKind kind: Diffable.ChildKind) -> Diffable.Diff {
static func diff(between lhs: DiffableDictionary, and rhs: DiffableDictionary, forKind kind: DifferentiaUnit.ChildKind) -> Differentia {
// note: elements of [diffable] may not conform to hashable.
// so we cannot use o(1) algorithm such as hash map.

var result: [String: Diffable.Diff] = [:]
var result: [String: Differentia] = [:]

let keys = Set(lhs.dictionary.keys)
.union(Set(rhs.dictionary.keys))
Expand All @@ -55,25 +55,25 @@ struct DiffableDictionary {
fatalError("This case cannot be executed.")

case let (.some(lv), .none):
result[key] = Diffable.Diff(units: [.inserted(lv)])
result[key] = Differentia(units: [.inserted(lv)])

case let (.none, .some(rv)):
result[key] = Diffable.Diff(units: [.deleted(rv)])
result[key] = Differentia(units: [.deleted(rv)])

case let (.some(lv), .some(rv)):
guard lv != rv else {
result[key] = Diffable.Diff(units: [.notChanged(lv)])
result[key] = Differentia(units: [.notChanged(lv)])
return
}

result[key] = Diffable.Diff(units: [
result[key] = Differentia(units: [
.deleted(lv),
.inserted(rv),
])
}
}

return Diffable.Diff(units: [
return Differentia(units: [
.child(kind: kind, result)
])
}
Expand Down
6 changes: 3 additions & 3 deletions Sources/DiffableSequence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class DiffableSequence {
}


static func diff(between: DiffableSequence, and: DiffableSequence) -> Diffable.Diff {
static func diff(between: DiffableSequence, and: DiffableSequence) -> Differentia {
var lcs = between.elements.LCS(and.elements)
let diff = between.elements.diff(and.elements)

Expand All @@ -32,7 +32,7 @@ class DiffableSequence {
var deletions = diff.deletions.sorted { $0.idx < $1.idx }

var index = -1
var result: [Diffable.DiffUnit] = []
var result: [DifferentiaUnit] = []
var wasDeleted = false
var wasInserted = false

Expand Down Expand Up @@ -113,7 +113,7 @@ class DiffableSequence {
}
}

return Diffable.Diff(units: result)
return Differentia(units: result)
}


Expand Down
4 changes: 2 additions & 2 deletions Sources/DiffableSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ struct DiffableSet {
}


static func diff(between a: DiffableSet, and b: DiffableSet) -> Diffable.Diff {
static func diff(between a: DiffableSet, and b: DiffableSet) -> Differentia {
// NOTE: Elements of [Diffable] may not conform to Hashable.
// So we cannot use O(1) algorithm such as hash map.

Expand All @@ -27,7 +27,7 @@ struct DiffableSet {
}
}

return Diffable.Diff(units:
return Differentia(units:
(deleted
.sorted { String(describing: $0) < String(describing: $1) }
.map { .deleted($0) })
Expand Down
10 changes: 10 additions & 0 deletions Sources/Differentia+PrettyPrintable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import Foundation



extension Differentia: PrettyPrintable {
var prettyLines: [PrettyLine] {
return self.units
.flatMap { unit -> [PrettyLine] in unit.prettyLines }
}
}
15 changes: 15 additions & 0 deletions Sources/Differentia.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import Foundation



struct Differentia {
let units: [DifferentiaUnit]
}



extension Differentia: Equatable {
static func == (_ lhs: Differentia, _ rhs: Differentia) -> Bool {
return lhs.units == rhs.units
}
}
Loading

0 comments on commit c8b23b0

Please sign in to comment.