|
8 | 8 |
|
9 | 9 | import Foundation
|
10 | 10 |
|
| 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. |
11 | 18 | public enum Operation: Equatable {
|
12 | 19 | case insert(Int)
|
13 | 20 | case delete(Int)
|
14 | 21 | case move(Int, Int)
|
15 | 22 | case update(Int)
|
16 | 23 | }
|
17 | 24 |
|
| 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. |
18 | 31 | public func ==(lhs: Operation, rhs: Operation) -> Bool {
|
19 | 32 | switch (lhs, rhs) {
|
20 | 33 | case let (.insert(lhsIndex), .insert(rhsIndex)):
|
@@ -62,12 +75,19 @@ enum Entry {
|
62 | 75 | case index(Int)
|
63 | 76 | }
|
64 | 77 |
|
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 |
71 | 91 | public func diff<T: Collection>(_ old: T, _ new: T) -> [Operation] where T.Iterator.Element: Hashable, T.IndexDistance == Int, T.Index == Int {
|
72 | 92 | var table = [Int: SymbolEntry]()
|
73 | 93 | var oa = [Entry]()
|
@@ -174,6 +194,12 @@ public func diff<T: Collection>(_ old: T, _ new: T) -> [Operation] where T.Itera
|
174 | 194 | return steps
|
175 | 195 | }
|
176 | 196 |
|
| 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. |
177 | 203 | public func orderedDiff<T: Collection>(_ old: T, _ new: T) -> [Operation] where T.Iterator.Element: Hashable, T.IndexDistance == Int, T.Index == Int {
|
178 | 204 | let steps = diff(old, new)
|
179 | 205 |
|
|
0 commit comments