|
10 | 10 | //
|
11 | 11 | //===----------------------------------------------------------------------===//
|
12 | 12 |
|
13 |
| -/// A type that represents the difference between two ordered collection states. |
| 13 | +/// A collection of insertions and removals that describe the difference |
| 14 | +/// between two ordered collection states. |
14 | 15 | @available(macOS 9999, iOS 9999, tvOS 9999, watchOS 9999, *) // FIXME(availability-5.1)
|
15 | 16 | public struct CollectionDifference<ChangeElement> {
|
16 |
| - /// A type that represents a single change to a collection. |
17 |
| - /// |
18 |
| - /// The `offset` of each `insert` refers to the offset of its `element` in |
19 |
| - /// the final state after the difference is fully applied. The `offset` of |
20 |
| - /// each `remove` refers to the offset of its `element` in the original |
21 |
| - /// state. Non-`nil` values of `associatedWith` refer to the offset of the |
22 |
| - /// complementary change. |
| 17 | + /// A single change to a collection. |
23 | 18 | @_frozen
|
24 | 19 | public enum Change {
|
| 20 | + /// An insertion. |
| 21 | + /// |
| 22 | + /// The `offset` value is the offset of the inserted element in the final |
| 23 | + /// state of the collection after the difference is fully applied. |
| 24 | + /// A non-`nil` `associatedWith` value is the offset of the complementary |
| 25 | + /// change. |
25 | 26 | case insert(offset: Int, element: ChangeElement, associatedWith: Int?)
|
| 27 | + |
| 28 | + /// A removal. |
| 29 | + /// |
| 30 | + /// The `offset` value is the offset of the element to be removed in the |
| 31 | + /// original state of the collection. A non-`nil` `associatedWith` value is |
| 32 | + /// the offset of the complementary change. |
26 | 33 | case remove(offset: Int, element: ChangeElement, associatedWith: Int?)
|
27 | 34 |
|
28 | 35 | // Internal common field accessors
|
@@ -58,10 +65,11 @@ public struct CollectionDifference<ChangeElement> {
|
58 | 65 | }
|
59 | 66 | }
|
60 | 67 |
|
61 |
| - /// The `.insert` changes contained by this difference, from lowest offset to highest |
| 68 | + /// The insertions contained by this difference, from lowest offset to |
| 69 | + /// highest. |
62 | 70 | public let insertions: [Change]
|
63 | 71 |
|
64 |
| - /// The `.remove` changes contained by this difference, from lowest offset to highest |
| 72 | + /// The removals contained by this difference, from lowest offset to highest. |
65 | 73 | public let removals: [Change]
|
66 | 74 |
|
67 | 75 | /// The public initializer calls this function to ensure that its parameter
|
@@ -117,20 +125,20 @@ public struct CollectionDifference<ChangeElement> {
|
117 | 125 | return removeOffsetToAssoc == insertAssocToOffset
|
118 | 126 | }
|
119 | 127 |
|
120 |
| - /// Creates an instance from a collection of changes. |
| 128 | + /// Creates a new collection difference from a collection of changes. |
121 | 129 | ///
|
122 |
| - /// For clients interested in the difference between two collections, see |
123 |
| - /// `BidirectionalCollection.difference(from:)`. |
| 130 | + /// To find the difference between two collections, use the |
| 131 | + /// `difference(from:)` method declared on the `BidirectionalCollection` |
| 132 | + /// protocol. |
124 | 133 | ///
|
125 |
| - /// To guarantee that instances are unambiguous and safe for compatible base |
126 |
| - /// states, this initializer will fail unless its parameter meets to the |
127 |
| - /// following requirements: |
| 134 | + /// The collection of changes passed as `changes` must meet these |
| 135 | + /// requirements: |
128 | 136 | ///
|
129 |
| - /// 1. All insertion offsets are unique |
130 |
| - /// 2. All removal offsets are unique |
131 |
| - /// 3. All associations between insertions and removals are symmetric |
| 137 | + /// - All insertion offsets are unique |
| 138 | + /// - All removal offsets are unique |
| 139 | + /// - All associations between insertions and removals are symmetric |
132 | 140 | ///
|
133 |
| - /// - Parameter c: A collection of changes that represent a transition |
| 141 | + /// - Parameter changes: A collection of changes that represent a transition |
134 | 142 | /// between two states.
|
135 | 143 | ///
|
136 | 144 | /// - Complexity: O(*n* * log(*n*)), where *n* is the length of the
|
@@ -218,6 +226,7 @@ public struct CollectionDifference<ChangeElement> {
|
218 | 226 | extension CollectionDifference: Collection {
|
219 | 227 | public typealias Element = Change
|
220 | 228 |
|
| 229 | + /// The position of a collection difference. |
221 | 230 | @_fixed_layout
|
222 | 231 | public struct Index {
|
223 | 232 | // Opaque index type is isomorphic to Int
|
@@ -305,12 +314,12 @@ extension CollectionDifference: Hashable where ChangeElement: Hashable {}
|
305 | 314 |
|
306 | 315 | @available(macOS 9999, iOS 9999, tvOS 9999, watchOS 9999, *) // FIXME(availability-5.1)
|
307 | 316 | extension CollectionDifference where ChangeElement: Hashable {
|
308 |
| - /// Infers which `ChangeElement`s have been both inserted and removed only |
309 |
| - /// once and returns a new difference with those associations. |
| 317 | + /// Returns a new collection difference with associations between individual |
| 318 | + /// elements that have been removed and inserted only once. |
310 | 319 | ///
|
311 |
| - /// - Returns: an instance with all possible moves inferred. |
| 320 | + /// - Returns: An collection difference with all possible moves inferred. |
312 | 321 | ///
|
313 |
| - /// - Complexity: O(*n*) where *n* is `self.count` |
| 322 | + /// - Complexity: O(*n*) where *n* is the number of collection differences. |
314 | 323 | public func inferringMoves() -> CollectionDifference<ChangeElement> {
|
315 | 324 | let uniqueRemovals: [ChangeElement:Int?] = {
|
316 | 325 | var result = [ChangeElement:Int?](minimumCapacity: Swift.min(removals.count, insertions.count))
|
|
0 commit comments