14
14
15
15
extension RangeReplaceableCollection {
16
16
private static func fastApplicationEnumeration(
17
- of diff: OrderedCollectionDifference < Element > ,
18
- _ f: ( OrderedCollectionDifference < Element > . Change ) -> Void
17
+ of diff: CollectionDifference < Element > ,
18
+ _ f: ( CollectionDifference < Element > . Change ) -> Void
19
19
) {
20
20
let totalRemoves = diff. removals. count
21
21
let totalInserts = diff. insertions. count
22
22
var enumeratedRemoves = 0
23
23
var enumeratedInserts = 0
24
24
25
25
while enumeratedRemoves < totalRemoves || enumeratedInserts < totalInserts {
26
- let consume : OrderedCollectionDifference < Element > . Change
26
+ let consume : CollectionDifference < Element > . Change
27
27
if enumeratedRemoves < diff. removals. count && enumeratedInserts < diff. insertions. count {
28
28
let removeOffset = diff. removals [ enumeratedRemoves] . offset
29
29
let insertOffset = diff. insertions [ enumeratedInserts] . offset
@@ -62,8 +62,8 @@ extension RangeReplaceableCollection {
62
62
///
63
63
/// - Complexity: O(*n* + *c*), where *n* is `self.count` and *c* is the
64
64
/// number of changes contained by the parameter.
65
- // @available(swift, introduced: 5.1)
66
- public func applying( _ difference: OrderedCollectionDifference < Element > ) -> Self ? {
65
+ @available ( swift, introduced: 5.1 )
66
+ public func applying( _ difference: CollectionDifference < Element > ) -> Self ? {
67
67
var result = Self ( )
68
68
var enumeratedRemoves = 0
69
69
var enumeratedInserts = 0
@@ -105,34 +105,31 @@ extension RangeReplaceableCollection {
105
105
106
106
// MARK: Definition of API
107
107
108
- // @available(swift, introduced: 5.1)
108
+ @available ( swift, introduced: 5.1 )
109
109
extension BidirectionalCollection {
110
110
/// Returns the difference needed to produce the receiver's state from the
111
- /// parameter's state with the fewest possible changes, using the provided
112
- /// closure to establish equivalence between elements.
111
+ /// parameter's state, using the provided closure to establish equivalence
112
+ /// between elements.
113
113
///
114
- /// This function does not infer element moves, but they can be computed
115
- /// using `OrderedCollectionDifference.inferringMoves()` if desired.
116
- ///
117
- /// Implementation is an optimized variation of the algorithm described by
118
- /// E. Myers (1986).
114
+ /// This function does not infer moves.
119
115
///
120
116
/// - Parameters:
121
117
/// - other: The base state.
122
118
/// - areEquivalent: A closure that returns whether the two
123
- /// parameters are equivalent.
119
+ /// parameters are equivalent.
124
120
///
125
121
/// - Returns: The difference needed to produce the reciever's state from
126
122
/// the parameter's state.
127
123
///
128
- /// - Complexity: O(*n* * *d*), where *n* is `other.count + self.count` and
129
- /// *d* is the number of changes between the two ordered collections.
124
+ /// - Complexity: For pathological inputs, worst case performance is
125
+ /// O(`self.count` * `other.count`). Faster execution can be expected
126
+ /// when the collections share many common elements.
130
127
public func difference< C> (
131
128
from other: C , by areEquivalent: ( Element , C . Element ) -> Bool
132
- ) -> OrderedCollectionDifference < Element >
129
+ ) -> CollectionDifference < Element >
133
130
where C : BidirectionalCollection , C. Element == Self . Element
134
131
{
135
- var rawChanges : [ OrderedCollectionDifference < Element > . Change ] = [ ]
132
+ var rawChanges : [ CollectionDifference < Element > . Change ] = [ ]
136
133
137
134
let source = CountingIndexCollection ( other)
138
135
let target = CountingIndexCollection ( self )
@@ -159,30 +156,29 @@ extension BidirectionalCollection {
159
156
}
160
157
}
161
158
162
- return OrderedCollectionDifference < Element > ( validatedChanges: rawChanges)
159
+ return CollectionDifference < Element > ( validatedChanges: rawChanges)
163
160
}
164
161
}
165
162
166
163
extension BidirectionalCollection where Element : Equatable {
167
164
/// Returns the difference needed to produce the receiver's state from the
168
- /// parameter's state with the fewest possible changes , using equality to
169
- /// establish equivalence between elements.
165
+ /// parameter's state, using equality to establish equivalence between
166
+ /// elements.
170
167
///
171
168
/// This function does not infer element moves, but they can be computed
172
- /// using `OrderedCollectionDifference.inferringMoves()` if desired.
173
- ///
174
- /// Implementation is an optimized variation of the algorithm described by
175
- /// E. Myers (1986).
169
+ /// using `CollectionDifference.inferringMoves()` if desired.
176
170
///
177
171
/// - Parameters:
178
172
/// - other: The base state.
179
173
///
180
174
/// - Returns: The difference needed to produce the reciever's state from
181
175
/// the parameter's state.
182
176
///
183
- /// - Complexity: O(*n* * *d*), where *n* is `other.count + self.count` and
184
- /// *d* is the number of changes between the two ordered collections.
185
- public func difference< C> ( from other: C ) -> OrderedCollectionDifference < Element >
177
+ /// - Complexity: For pathological inputs, worst case performance is
178
+ /// O(`self.count` * `other.count`). Faster execution can be expected
179
+ /// when the collections share many common elements, or if `Element`
180
+ /// also conforms to `Hashable`.
181
+ public func difference< C> ( from other: C ) -> CollectionDifference < Element >
186
182
where C: BidirectionalCollection , C. Element == Self . Element
187
183
{
188
184
return difference ( from: other, by: == )
0 commit comments