@@ -33,33 +33,27 @@ struct CollectionDifference<ChangeElement> {
33
33
34
34
// Internal common field accessors
35
35
internal var _offset : Int {
36
- get {
37
- switch self {
38
- case . insert( offset: let o, element: _, associatedWith: _) :
39
- return o
40
- case . remove( offset: let o, element: _, associatedWith: _) :
41
- return o
42
- }
36
+ switch self {
37
+ case . insert( offset: let o, element: _, associatedWith: _) :
38
+ return o
39
+ case . remove( offset: let o, element: _, associatedWith: _) :
40
+ return o
43
41
}
44
42
}
45
43
internal var _element : ChangeElement {
46
- get {
47
- switch self {
48
- case . insert( offset: _, element: let e, associatedWith: _) :
49
- return e
50
- case . remove( offset: _, element: let e, associatedWith: _) :
51
- return e
52
- }
44
+ switch self {
45
+ case . insert( offset: _, element: let e, associatedWith: _) :
46
+ return e
47
+ case . remove( offset: _, element: let e, associatedWith: _) :
48
+ return e
53
49
}
54
50
}
55
51
internal var _associatedOffset : Int ? {
56
- get {
57
- switch self {
58
- case . insert( offset: _, element: _, associatedWith: let o) :
59
- return o
60
- case . remove( offset: _, element: _, associatedWith: let o) :
61
- return o
62
- }
52
+ switch self {
53
+ case . insert( offset: _, element: _, associatedWith: let o) :
54
+ return o
55
+ case . remove( offset: _, element: _, associatedWith: let o) :
56
+ return o
63
57
}
64
58
}
65
59
}
@@ -162,12 +156,12 @@ struct CollectionDifference<ChangeElement> {
162
156
///
163
157
/// Complexity: O(`changes.count`)
164
158
private static func _validateChanges< Changes: Collection > (
165
- _ changes : Changes
159
+ _ changes: Changes
166
160
) -> Bool where Changes. Element == Change {
167
161
if changes. isEmpty { return true }
168
162
169
- var insertAssocToOffset = Dictionary < Int , Int > ( )
170
- var removeOffsetToAssoc = Dictionary < Int , Int > ( )
163
+ var insertAssocToOffset = [ Int: Int] ( )
164
+ var removeOffsetToAssoc = [ Int: Int] ( )
171
165
var insertOffset = Set < Int > ( )
172
166
var removeOffset = Set < Int > ( )
173
167
@@ -201,14 +195,15 @@ struct CollectionDifference<ChangeElement> {
201
195
}
202
196
203
197
func inverse( ) -> Self {
204
- return CollectionDifference ( _validatedChanges: self . map { c in
205
- switch c {
198
+ return CollectionDifference (
199
+ _validatedChanges: self . map { c in
200
+ switch c {
206
201
case . remove( let o, let e, let a) :
207
202
return . insert( offset: o, element: e, associatedWith: a)
208
203
case . insert( let o, let e, let a) :
209
204
return . remove( offset: o, element: e, associatedWith: a)
210
- }
211
- } )
205
+ }
206
+ } )
212
207
}
213
208
}
214
209
@@ -321,8 +316,9 @@ extension CollectionDifference where ChangeElement: Hashable {
321
316
///
322
317
/// - Complexity: O(*n*) where *n* is the number of collection differences.
323
318
func inferringMoves( ) -> CollectionDifference < ChangeElement > {
324
- let uniqueRemovals : [ ChangeElement : Int ? ] = {
325
- var result = [ ChangeElement: Int? ] ( minimumCapacity: Swift . min ( removals. count, insertions. count) )
319
+ let uniqueRemovals : [ ChangeElement : Int ? ] = {
320
+ var result = [ ChangeElement: Int? ] (
321
+ minimumCapacity: Swift . min ( removals. count, insertions. count) )
326
322
for removal in removals {
327
323
let element = removal. _element
328
324
if result [ element] != . none {
@@ -334,8 +330,9 @@ extension CollectionDifference where ChangeElement: Hashable {
334
330
return result. filter { ( _, v) -> Bool in v != . none }
335
331
} ( )
336
332
337
- let uniqueInsertions : [ ChangeElement : Int ? ] = {
338
- var result = [ ChangeElement: Int? ] ( minimumCapacity: Swift . min ( removals. count, insertions. count) )
333
+ let uniqueInsertions : [ ChangeElement : Int ? ] = {
334
+ var result = [ ChangeElement: Int? ] (
335
+ minimumCapacity: Swift . min ( removals. count, insertions. count) )
339
336
for insertion in insertions {
340
337
let element = insertion. _element
341
338
if result [ element] != . none {
@@ -347,25 +344,26 @@ extension CollectionDifference where ChangeElement: Hashable {
347
344
return result. filter { ( _, v) -> Bool in v != . none }
348
345
} ( )
349
346
350
- return CollectionDifference ( _validatedChanges: map ( { ( change: Change ) -> Change in
351
- switch change {
352
- case . remove( offset: let offset, element: let element, associatedWith: _) :
353
- if uniqueRemovals [ element] == nil {
354
- return change
355
- }
356
- if let assoc = uniqueInsertions [ element] {
357
- return . remove( offset: offset, element: element, associatedWith: assoc)
358
- }
359
- case . insert( offset: let offset, element: let element, associatedWith: _) :
360
- if uniqueInsertions [ element] == nil {
361
- return change
362
- }
363
- if let assoc = uniqueRemovals [ element] {
364
- return . insert( offset: offset, element: element, associatedWith: assoc)
347
+ return CollectionDifference (
348
+ _validatedChanges: map ( { ( change: Change ) -> Change in
349
+ switch change {
350
+ case . remove( let offset, let element, associatedWith: _) :
351
+ if uniqueRemovals [ element] == nil {
352
+ return change
353
+ }
354
+ if let assoc = uniqueInsertions [ element] {
355
+ return . remove( offset: offset, element: element, associatedWith: assoc)
356
+ }
357
+ case . insert( let offset, let element, associatedWith: _) :
358
+ if uniqueInsertions [ element] == nil {
359
+ return change
360
+ }
361
+ if let assoc = uniqueRemovals [ element] {
362
+ return . insert( offset: offset, element: element, associatedWith: assoc)
363
+ }
365
364
}
366
- }
367
- return change
368
- } ) )
365
+ return change
366
+ } ) )
369
367
}
370
368
}
371
369
0 commit comments