Skip to content

Commit 58f5312

Browse files
authored
Merge pull request #6 from mcudich/docs
Docs
2 parents c6f73ac + 9f7ae86 commit 58f5312

File tree

3 files changed

+43
-6
lines changed

3 files changed

+43
-6
lines changed

Source/Diff.swift

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,26 @@
88

99
import Foundation
1010

11+
/// Used to represent the operation to perform on the source array. Indices indicate the position at
12+
/// which to perform the given operation.
13+
///
14+
/// - insert: Insert a new value at the given index.
15+
/// - delete: Delete a value at the given index.
16+
/// - move: Move a value from the given origin index, to the given destination index.
17+
/// - update: Update the value at the given index.
1118
public enum Operation: Equatable {
1219
case insert(Int)
1320
case delete(Int)
1421
case move(Int, Int)
1522
case update(Int)
1623
}
1724

25+
/// Returns whether the two `Operation` values are equal.
26+
///
27+
/// - parameter lhs: The left-hand side value to compare.
28+
/// - parameter rhs: The right-hand side value to compare.
29+
///
30+
/// - returns: `true` if the two values are equal, `false` otherwise.
1831
public func ==(lhs: Operation, rhs: Operation) -> Bool {
1932
switch (lhs, rhs) {
2033
case let (.insert(lhsIndex), .insert(rhsIndex)):
@@ -62,12 +75,19 @@ enum Entry {
6275
case index(Int)
6376
}
6477

65-
// Based on http://dl.acm.org/citation.cfm?id=359467.
66-
//
67-
// And other similar implementations at:
68-
// * https://github.com/Instagram/IGListKit
69-
// * https://github.com/andre-alves/PHDiff
70-
//
78+
/// Returns a diff, given an old and a new representation of a given collection (such as an `Array`).
79+
/// The return value is a list of `Operation` values, each which instructs how to transform the old
80+
/// collection into the new collection.
81+
///
82+
/// - parameter old: The old collection.
83+
/// - parameter new: The new collection.
84+
/// - returns: A list of `Operation` values, representing the diff.
85+
///
86+
/// Based on http://dl.acm.org/citation.cfm?id=359467.
87+
///
88+
/// And other similar implementations at:
89+
/// * https://github.com/Instagram/IGListKit
90+
/// * https://github.com/andre-alves/PHDiff
7191
public func diff<T: Collection>(_ old: T, _ new: T) -> [Operation] where T.Iterator.Element: Hashable, T.IndexDistance == Int, T.Index == Int {
7292
var table = [Int: SymbolEntry]()
7393
var oa = [Entry]()
@@ -174,6 +194,12 @@ public func diff<T: Collection>(_ old: T, _ new: T) -> [Operation] where T.Itera
174194
return steps
175195
}
176196

197+
/// Similar to to `diff`, except that this returns the same set of operations but in an order that
198+
/// can be applied step-wise to transform the old array into the new one.
199+
///
200+
/// - parameter old: The old collection.
201+
/// - parameter new: The new collection.
202+
/// - returns: A list of `Operation` values, representing the diff.
177203
public func orderedDiff<T: Collection>(_ old: T, _ new: T) -> [Operation] where T.Iterator.Element: Hashable, T.IndexDistance == Int, T.Index == Int {
178204
let steps = diff(old, new)
179205

Source/UICollectionView+Diff.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ import Foundation
1010
import UIKit
1111

1212
public extension UICollectionView {
13+
/// Applies a batch update to the receiver, efficiently reporting changes between old and new.
14+
///
15+
/// - parameter old: The previous state of the collection view.
16+
/// - parameter new: The current state of the collection view.
17+
/// - parameter section: The section where these changes took place.
1318
func applyDiff<T: Collection>(_ old: T, _ new: T, inSection section: Int, completion: ((Bool) -> Void)?) where T.Iterator.Element: Hashable, T.IndexDistance == Int, T.Index == Int {
1419
let update = ListUpdate(diff(old, new), section)
1520

Source/UITableView+Diff.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ import Foundation
1010
import UIKit
1111

1212
public extension UITableView {
13+
/// Applies a batch update to the receiver, efficiently reporting changes between old and new.
14+
///
15+
/// - parameter old: The previous state of the table view.
16+
/// - parameter new: The current state of the table view.
17+
/// - parameter section: The section where these changes took place.
18+
/// - parameter animation: The animation type.
1319
func applyDiff<T: Collection>(_ old: T, _ new: T, inSection section: Int, withAnimation animation: UITableViewRowAnimation) where T.Iterator.Element: Hashable, T.IndexDistance == Int, T.Index == Int {
1420
let update = ListUpdate(diff(old, new), section)
1521

0 commit comments

Comments
 (0)