Skip to content

Commit cdf1846

Browse files
authored
Merge pull request #9 from ra1028/v0.5.0
v0.5.0
2 parents 2254970 + ea514f9 commit cdf1846

32 files changed

+505
-388
lines changed

DifferenceKit.playground/Contents.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,18 @@ PlaygroundPage.current.needsIndefiniteExecution = true
1818
PlaygroundPage.current.liveView = navigationController.view
1919

2020
let source = [
21-
Section(model: "Section 1", elements: ["A", "B", "C"]),
22-
Section(model: "Section 2", elements: ["D", "E", "F"]),
23-
Section(model: "Section 3", elements: ["G", "H", "I"]),
24-
Section(model: "Section 4", elements: ["J", "K", "L"])
21+
ArraySection(model: "Section 1", elements: ["A", "B", "C"]),
22+
ArraySection(model: "Section 2", elements: ["D", "E", "F"]),
23+
ArraySection(model: "Section 3", elements: ["G", "H", "I"]),
24+
ArraySection(model: "Section 4", elements: ["J", "K", "L"])
2525
]
2626

2727
let target = [
28-
Section(model: "Section 5", elements: ["M", "N", "O"]),
29-
Section(model: "Section 1", elements: ["A", "C"]),
30-
Section(model: "Section 4", elements: ["J", "I", "K", "L"]),
31-
Section(model: "Section 3", elements: ["G", "H", "Z"]),
32-
Section(model: "Section 6", elements: ["P", "Q", "R"]),
28+
ArraySection(model: "Section 5", elements: ["M", "N", "O"]),
29+
ArraySection(model: "Section 1", elements: ["A", "C"]),
30+
ArraySection(model: "Section 4", elements: ["J", "I", "K", "L"]),
31+
ArraySection(model: "Section 3", elements: ["G", "H", "Z"]),
32+
ArraySection(model: "Section 6", elements: ["P", "Q", "R"])
3333
]
3434

3535
viewController.dataInput = source

DifferenceKit.playground/Sources/TableViewController.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ extension String: Differentiable {}
66
public final class TableViewController: UITableViewController {
77
public var refreshAction: (() -> Void)?
88

9-
public var dataInput: [Section<String, String>] {
9+
public var dataInput: [ArraySection<String, String>] {
1010
get { return data }
1111
set {
1212
let changeset = StagedChangeset(source: data, target: newValue)
@@ -16,7 +16,7 @@ public final class TableViewController: UITableViewController {
1616
}
1717
}
1818

19-
private var data = [Section<String, String>]()
19+
private var data = [ArraySection<String, String>]()
2020

2121
public init() {
2222
super.init(style: .grouped)

DifferenceKit.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |spec|
22
spec.name = 'DifferenceKit'
3-
spec.version = '0.4.1'
3+
spec.version = '0.5.0'
44
spec.author = { 'ra1028' => '[email protected]' }
55
spec.homepage = 'https://github.com/ra1028/DifferenceKit'
66
spec.documentation_url = 'https://ra1028.github.io/DifferenceKit'

DifferenceKit.xcodeproj/project.pbxproj

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
/* Begin PBXBuildFile section */
1010
6B2DF878210E2C12004D2D40 /* DifferenceKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6B2DF86E210E2C12004D2D40 /* DifferenceKit.framework */; };
1111
6B5B409C211066BF00A931DB /* AlgorithmTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6B5B4093211066BF00A931DB /* AlgorithmTest.swift */; };
12-
6B5B409D211066BF00A931DB /* SectionTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6B5B4094211066BF00A931DB /* SectionTest.swift */; };
12+
6B5B409D211066BF00A931DB /* ArraySectionTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6B5B4094211066BF00A931DB /* ArraySectionTest.swift */; };
1313
6B5B409E211066BF00A931DB /* StagedChangesetTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6B5B4095211066BF00A931DB /* StagedChangesetTest.swift */; };
1414
6B5B409F211066BF00A931DB /* AnyDifferentiableTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6B5B4096211066BF00A931DB /* AnyDifferentiableTest.swift */; };
1515
6B5B40A0211066BF00A931DB /* ElementPathTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6B5B4097211066BF00A931DB /* ElementPathTest.swift */; };
@@ -21,7 +21,7 @@
2121
6B5B40A7211066EA00A931DB /* DifferentiableSection.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6B5B408E211066B300A931DB /* DifferentiableSection.swift */; };
2222
6B5B40A8211066EA00A931DB /* Changeset.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6B5B408F211066B300A931DB /* Changeset.swift */; };
2323
6B5B40A9211066EA00A931DB /* StagedChangeset.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6B5B408C211066B300A931DB /* StagedChangeset.swift */; };
24-
6B5B40AA211066EA00A931DB /* Section.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6B5B408A211066B300A931DB /* Section.swift */; };
24+
6B5B40AA211066EA00A931DB /* ArraySection.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6B5B408A211066B300A931DB /* ArraySection.swift */; };
2525
6B5B40AB211066EA00A931DB /* AnyDifferentiable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6B5B408B211066B300A931DB /* AnyDifferentiable.swift */; };
2626
6B5B40AC211066EA00A931DB /* ElementPath.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6B5B408D211066B300A931DB /* ElementPath.swift */; };
2727
6B956B762110B25300DE3D29 /* UIKitExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6B5B4088211066B300A931DB /* UIKitExtension.swift */; };
@@ -43,7 +43,7 @@
4343
6B2DF88A210E39A8004D2D40 /* DifferenceKit.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = DifferenceKit.xcconfig; sourceTree = "<group>"; };
4444
6B5B4086211066B300A931DB /* Algorithm.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Algorithm.swift; sourceTree = "<group>"; };
4545
6B5B4088211066B300A931DB /* UIKitExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIKitExtension.swift; sourceTree = "<group>"; };
46-
6B5B408A211066B300A931DB /* Section.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Section.swift; sourceTree = "<group>"; };
46+
6B5B408A211066B300A931DB /* ArraySection.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ArraySection.swift; sourceTree = "<group>"; };
4747
6B5B408B211066B300A931DB /* AnyDifferentiable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AnyDifferentiable.swift; sourceTree = "<group>"; };
4848
6B5B408C211066B300A931DB /* StagedChangeset.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StagedChangeset.swift; sourceTree = "<group>"; };
4949
6B5B408D211066B300A931DB /* ElementPath.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ElementPath.swift; sourceTree = "<group>"; };
@@ -52,7 +52,7 @@
5252
6B5B4090211066B300A931DB /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
5353
6B5B4091211066B300A931DB /* Differentiable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Differentiable.swift; sourceTree = "<group>"; };
5454
6B5B4093211066BF00A931DB /* AlgorithmTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AlgorithmTest.swift; sourceTree = "<group>"; };
55-
6B5B4094211066BF00A931DB /* SectionTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SectionTest.swift; sourceTree = "<group>"; };
55+
6B5B4094211066BF00A931DB /* ArraySectionTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ArraySectionTest.swift; sourceTree = "<group>"; };
5656
6B5B4095211066BF00A931DB /* StagedChangesetTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StagedChangesetTest.swift; sourceTree = "<group>"; };
5757
6B5B4096211066BF00A931DB /* AnyDifferentiableTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AnyDifferentiableTest.swift; sourceTree = "<group>"; };
5858
6B5B4097211066BF00A931DB /* ElementPathTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ElementPathTest.swift; sourceTree = "<group>"; };
@@ -113,12 +113,12 @@
113113
children = (
114114
6B5B4087211066B300A931DB /* UIExtensions */,
115115
6B5B4086211066B300A931DB /* Algorithm.swift */,
116-
6B5B4091211066B300A931DB /* Differentiable.swift */,
117-
6B5B408E211066B300A931DB /* DifferentiableSection.swift */,
118116
6B5B408F211066B300A931DB /* Changeset.swift */,
119117
6B5B408C211066B300A931DB /* StagedChangeset.swift */,
120-
6B5B408A211066B300A931DB /* Section.swift */,
118+
6B5B4091211066B300A931DB /* Differentiable.swift */,
119+
6B5B408E211066B300A931DB /* DifferentiableSection.swift */,
121120
6B5B408B211066B300A931DB /* AnyDifferentiable.swift */,
121+
6B5B408A211066B300A931DB /* ArraySection.swift */,
122122
6B5B408D211066B300A931DB /* ElementPath.swift */,
123123
6B5B4090211066B300A931DB /* Info.plist */,
124124
);
@@ -137,7 +137,7 @@
137137
isa = PBXGroup;
138138
children = (
139139
6B5B4093211066BF00A931DB /* AlgorithmTest.swift */,
140-
6B5B4094211066BF00A931DB /* SectionTest.swift */,
140+
6B5B4094211066BF00A931DB /* ArraySectionTest.swift */,
141141
6B5B4095211066BF00A931DB /* StagedChangesetTest.swift */,
142142
6B5B4096211066BF00A931DB /* AnyDifferentiableTest.swift */,
143143
6B5B4097211066BF00A931DB /* ElementPathTest.swift */,
@@ -264,7 +264,7 @@
264264
6B5B40A5211066EA00A931DB /* Algorithm.swift in Sources */,
265265
6B5B40AC211066EA00A931DB /* ElementPath.swift in Sources */,
266266
6B5B40A6211066EA00A931DB /* Differentiable.swift in Sources */,
267-
6B5B40AA211066EA00A931DB /* Section.swift in Sources */,
267+
6B5B40AA211066EA00A931DB /* ArraySection.swift in Sources */,
268268
6B5B40A9211066EA00A931DB /* StagedChangeset.swift in Sources */,
269269
6B5B40A7211066EA00A931DB /* DifferentiableSection.swift in Sources */,
270270
);
@@ -279,7 +279,7 @@
279279
6B5B40A0211066BF00A931DB /* ElementPathTest.swift in Sources */,
280280
6B5B40A4211066BF00A931DB /* TestTools.swift in Sources */,
281281
6B5B40A2211066BF00A931DB /* MeasurementTest.swift in Sources */,
282-
6B5B409D211066BF00A931DB /* SectionTest.swift in Sources */,
282+
6B5B409D211066BF00A931DB /* ArraySectionTest.swift in Sources */,
283283
6B5B409F211066BF00A931DB /* AnyDifferentiableTest.swift in Sources */,
284284
6B5B409E211066BF00A931DB /* StagedChangesetTest.swift in Sources */,
285285
);

Examples/Example-iOS/Sources/HeaderFooterSectionViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ private struct HeaderFooterSectionModel: Differentiable {
1818
}
1919
}
2020

21-
private typealias HeaderFooterSection = Section<HeaderFooterSectionModel, String>
21+
private typealias HeaderFooterSection = ArraySection<HeaderFooterSectionModel, String>
2222

2323
final class HeaderFooterSectionViewController: UITableViewController {
2424
private var data = [HeaderFooterSection]()

Examples/Example-iOS/Sources/ShuffleEmoticonViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ private enum EmoticonSectionID: Differentiable {
1010
}
1111
}
1212

13-
private typealias EmoticonSection = Section<EmoticonSectionID, String>
13+
private typealias EmoticonSection = ArraySection<EmoticonSectionID, String>
1414

1515
final class ShuffleEmoticonViewController: UIViewController {
1616
private let collectionView: UICollectionView

Examples/Example-tvOS/Sources/HeaderFooterSectionViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ private struct HeaderFooterSectionModel: Differentiable {
1818
}
1919
}
2020

21-
private typealias HeaderFooterSection = Section<HeaderFooterSectionModel, String>
21+
private typealias HeaderFooterSection = ArraySection<HeaderFooterSectionModel, String>
2222

2323
final class HeaderFooterSectionViewController: UITableViewController {
2424
private var data = [HeaderFooterSection]()

Examples/Example-tvOS/Sources/ShuffleEmoticonViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ private enum EmoticonSectionID: Differentiable {
1010
}
1111
}
1212

13-
private typealias EmoticonSection = Section<EmoticonSectionID, String>
13+
private typealias EmoticonSection = ArraySection<EmoticonSectionID, String>
1414

1515
final class ShuffleEmoticonViewController: UIViewController {
1616
private let collectionView: UICollectionView

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -112,21 +112,21 @@ let source = [
112112
```
113113

114114
In the case of sectioned collection, the section itself must have a unique identifier and be able to compare whether there is an update.
115-
So each section must conform to `DifferentiableSection` protocol, but in most cases you can use `Section` that general type conformed to it.
116-
`Section` requires a model conforming to `Differentiable` for differentiate from other sections:
115+
So each section must conform to `DifferentiableSection` protocol, but in most cases you can use `ArraySection` that general type conformed to it.
116+
`ArraySection` requires a model conforming to `Differentiable` for differentiate from other sections:
117117
```swift
118118
enum Model: Differentiable {
119119
case a, b, c
120120
}
121121

122-
let source: [Section<Model, String>] = [
123-
Section(model: .a, elements: ["A", "B"]),
124-
Section(model: .b, elements: ["C"])
122+
let source: [ArraySection<Model, String>] = [
123+
ArraySection(model: .a, elements: ["A", "B"]),
124+
ArraySection(model: .b, elements: ["C"])
125125
]
126-
let target: [Section<Model, String>] = [
127-
Section(model: .c, elements: ["D", "E"]),
128-
Section(model: .a, elements: ["A"]),
129-
Section(model: .b, elements: ["B", "C"])
126+
let target: [ArraySection<Model, String>] = [
127+
ArraySection(model: .c, elements: ["D", "E"]),
128+
ArraySection(model: .a, elements: ["A"]),
129+
ArraySection(model: .b, elements: ["B", "C"])
130130
]
131131

132132
let changeset = StagedChangeset(source: source, target: target)

Sources/Algorithm.swift

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ public extension StagedChangeset where Collection: RangeReplaceableCollection, C
6565
target: targetElements,
6666
trackTargetIndexAsUpdated: false,
6767
mapIndex: { ElementPath(element: $0, section: section) },
68-
differentiable: { $0 },
6968
remainedInTarget: { firstStageElements.append($0) }
7069
)
7170

@@ -124,7 +123,7 @@ public extension StagedChangeset where Collection: RangeReplaceableCollection, C
124123
/// - Complexity: O(n)
125124
public init(source: Collection, target: Collection) {
126125
typealias Section = Collection.Element
127-
typealias SectionIdentifier = Collection.Element.Model.DifferenceIdentifier
126+
typealias SectionIdentifier = Collection.Element.DifferenceIdentifier
128127
typealias Element = Collection.Element.Collection.Element
129128
typealias ElementIdentifier = Collection.Element.Collection.Element.DifferenceIdentifier
130129

@@ -161,8 +160,7 @@ public extension StagedChangeset where Collection: RangeReplaceableCollection, C
161160
source: sourceSections,
162161
target: targetSections,
163162
trackTargetIndexAsUpdated: true,
164-
mapIndex: { $0 },
165-
differentiable: { $0.model }
163+
mapIndex: { $0 }
166164
)
167165

168166
// Calculate the element differences.
@@ -265,7 +263,7 @@ public extension StagedChangeset where Collection: RangeReplaceableCollection, C
265263
offsetByDelete += 1
266264
}
267265

268-
let section = Section(model: sourceSections[sourceSectionIndex].model, elements: firstStageElements)
266+
let section = Section(source: sourceSections[sourceSectionIndex], elements: firstStageElements)
269267
firstStageSections.append(section)
270268
}
271269

@@ -286,7 +284,7 @@ public extension StagedChangeset where Collection: RangeReplaceableCollection, C
286284
let secondStageSection = firstStageSections[sourceSectionIndex - sectionDeleteOffset]
287285
secondStageSections.append(secondStageSection)
288286

289-
let thirdStageSection = Section(model: secondStageSection.model, elements: targetElements)
287+
let thirdStageSection = Section(source: secondStageSection, elements: targetElements)
290288
thirdStageSections.append(thirdStageSection)
291289

292290
for targetElementIndex in targetElements.indices {
@@ -381,12 +379,11 @@ public extension StagedChangeset where Collection: RangeReplaceableCollection, C
381379

382380
/// The shared algorithm to calculate differences between two linear collections.
383381
@discardableResult
384-
private func differentiate<E, D: Differentiable, I>(
382+
private func differentiate<E: Differentiable, I>(
385383
source: ContiguousArray<E>,
386384
target: ContiguousArray<E>,
387385
trackTargetIndexAsUpdated: Bool,
388386
mapIndex: (Int) -> I,
389-
differentiable: (E) -> D,
390387
remainedInTarget: ((E) -> Void)? = nil
391388
) -> DifferentiateResult<I> {
392389
var deleted = [I]()
@@ -396,19 +393,19 @@ private func differentiate<E, D: Differentiable, I>(
396393

397394
var sourceTraces = ContiguousArray<Trace<Int>>()
398395
var targetReferences = ContiguousArray<Int?>(repeating: nil, count: target.count)
399-
var sourceIdentifiers = ContiguousArray<D.DifferenceIdentifier>()
396+
var sourceIdentifiers = ContiguousArray<E.DifferenceIdentifier>()
400397

401398
sourceIdentifiers.reserveCapacity(source.count)
402399
sourceTraces.reserveCapacity(source.count)
403400

404401
for sourceElement in source {
405402
sourceTraces.append(Trace())
406-
sourceIdentifiers.append(differentiable(sourceElement).differenceIdentifier)
403+
sourceIdentifiers.append(sourceElement.differenceIdentifier)
407404
}
408405

409406
sourceIdentifiers.withUnsafeBufferPointer { bufferPointer in
410407
// The pointer and the table key are for optimization.
411-
var sourceOccurrencesTable = [TableKey<D.DifferenceIdentifier>: Occurrence](minimumCapacity: source.count * 2)
408+
var sourceOccurrencesTable = [TableKey<E.DifferenceIdentifier>: Occurrence](minimumCapacity: source.count * 2)
412409

413410
// Record the index where the element was found in source collection into occurrences table.
414411
for sourceIndex in sourceIdentifiers.indices {
@@ -430,7 +427,7 @@ private func differentiate<E, D: Differentiable, I>(
430427

431428
// Record the target index and the source index that the element having the same identifier.
432429
for targetIndex in target.indices {
433-
var targetIdentifier = differentiable(target[targetIndex]).differenceIdentifier
430+
var targetIdentifier = target[targetIndex].differenceIdentifier
434431
let key = TableKey(pointer: &targetIdentifier)
435432

436433
switch sourceOccurrencesTable[key] {
@@ -481,7 +478,7 @@ private func differentiate<E, D: Differentiable, I>(
481478
let sourceElement = source[sourceIndex]
482479
let targetElement = target[targetIndex]
483480

484-
if !differentiable(targetElement).isContentEqual(to: differentiable(sourceElement)) {
481+
if !targetElement.isContentEqual(to: sourceElement) {
485482
updated.append(mapIndex(trackTargetIndexAsUpdated ? targetIndex : sourceIndex))
486483
}
487484

0 commit comments

Comments
 (0)