@@ -62,8 +62,8 @@ final internal class _SwiftDictionaryNSEnumerator<Key: Hashable, Value>
62
62
63
63
@nonobjc internal var base : _NativeDictionary < Key , Value >
64
64
@nonobjc internal var bridgedKeys : _BridgingHashBuffer ?
65
- @nonobjc internal var nextIndex : _NativeDictionary < Key , Value > . Index
66
- @nonobjc internal var endIndex : _NativeDictionary < Key , Value > . Index
65
+ @nonobjc internal var nextBucket : _NativeDictionary < Key , Value > . Bucket
66
+ @nonobjc internal var endBucket : _NativeDictionary < Key , Value > . Bucket
67
67
68
68
@objc
69
69
internal override required init ( ) {
@@ -74,35 +74,35 @@ final internal class _SwiftDictionaryNSEnumerator<Key: Hashable, Value>
74
74
_sanityCheck ( _isBridgedVerbatimToObjectiveC ( Key . self) )
75
75
self . base = base
76
76
self . bridgedKeys = nil
77
- self . nextIndex = base. startIndex
78
- self . endIndex = base. endIndex
77
+ self . nextBucket = base. hashTable . startBucket
78
+ self . endBucket = base. hashTable . endBucket
79
79
}
80
80
81
81
@nonobjc
82
82
internal init ( _ deferred: __owned _SwiftDeferredNSDictionary< Key , Value > ) {
83
83
_sanityCheck ( !_isBridgedVerbatimToObjectiveC( Key . self) )
84
84
self . base = deferred. native
85
85
self . bridgedKeys = deferred. bridgeKeys ( )
86
- self . nextIndex = base. startIndex
87
- self . endIndex = base. endIndex
86
+ self . nextBucket = base. hashTable . startBucket
87
+ self . endBucket = base. hashTable . endBucket
88
88
}
89
89
90
- private func bridgedKey( at index : _HashTable . Index ) -> AnyObject {
91
- _sanityCheck ( base. hashTable. isOccupied ( index ) )
90
+ private func bridgedKey( at bucket : _HashTable . Bucket ) -> AnyObject {
91
+ _sanityCheck ( base. hashTable. isOccupied ( bucket ) )
92
92
if let bridgedKeys = self . bridgedKeys {
93
- return bridgedKeys [ index ]
93
+ return bridgedKeys [ bucket ]
94
94
}
95
- return _bridgeAnythingToObjectiveC ( base. uncheckedKey ( at: index ) )
95
+ return _bridgeAnythingToObjectiveC ( base. uncheckedKey ( at: bucket ) )
96
96
}
97
97
98
98
@objc
99
99
internal func nextObject( ) -> AnyObject ? {
100
- if nextIndex == endIndex {
100
+ if nextBucket == endBucket {
101
101
return nil
102
102
}
103
- let index = nextIndex
104
- nextIndex = base. index ( after: nextIndex )
105
- return self . bridgedKey ( at: index )
103
+ let bucket = nextBucket
104
+ nextBucket = base. hashTable . occupiedBucket ( after: nextBucket )
105
+ return self . bridgedKey ( at: bucket )
106
106
}
107
107
108
108
@objc ( countByEnumeratingWithState: objects: count: )
@@ -118,16 +118,16 @@ final internal class _SwiftDictionaryNSEnumerator<Key: Hashable, Value>
118
118
theState. mutationsPtr = _fastEnumerationStorageMutationsPtr
119
119
}
120
120
121
- if nextIndex == endIndex {
121
+ if nextBucket == endBucket {
122
122
state. pointee = theState
123
123
return 0
124
124
}
125
125
126
126
// Return only a single element so that code can start iterating via fast
127
127
// enumeration, terminate it, and continue via NSEnumerator.
128
128
let unmanagedObjects = _UnmanagedAnyObjectArray ( objects)
129
- unmanagedObjects [ 0 ] = self . bridgedKey ( at: nextIndex )
130
- nextIndex = base. index ( after: nextIndex )
129
+ unmanagedObjects [ 0 ] = self . bridgedKey ( at: nextBucket )
130
+ nextBucket = base. hashTable . occupiedBucket ( after: nextBucket )
131
131
state. pointee = theState
132
132
return 1
133
133
}
@@ -141,6 +141,9 @@ final internal class _SwiftDictionaryNSEnumerator<Key: Hashable, Value>
141
141
final internal class _SwiftDeferredNSDictionary < Key: Hashable , Value>
142
142
: __SwiftNativeNSDictionary , _NSDictionaryCore {
143
143
144
+ @usableFromInline
145
+ internal typealias Bucket = _HashTable . Bucket
146
+
144
147
// This stored property must be stored at offset zero. We perform atomic
145
148
// operations on it.
146
149
//
@@ -225,9 +228,9 @@ final internal class _SwiftDeferredNSDictionary<Key: Hashable, Value>
225
228
let bridged = _BridgingHashBuffer. allocate (
226
229
owner: native. _storage,
227
230
hashTable: native. hashTable)
228
- for index in native. hashTable {
229
- let object = _bridgeAnythingToObjectiveC ( native. uncheckedKey ( at: index ) )
230
- bridged. initialize ( at: index , to: object)
231
+ for bucket in native. hashTable {
232
+ let object = _bridgeAnythingToObjectiveC ( native. uncheckedKey ( at: bucket ) )
233
+ bridged. initialize ( at: bucket , to: object)
231
234
}
232
235
233
236
// Atomically put the bridged keys in place.
@@ -244,59 +247,54 @@ final internal class _SwiftDeferredNSDictionary<Key: Hashable, Value>
244
247
let bridged = _BridgingHashBuffer. allocate (
245
248
owner: native. _storage,
246
249
hashTable: native. hashTable)
247
- for index in native. hashTable {
248
- let object = _bridgeAnythingToObjectiveC ( native. uncheckedValue ( at: index) )
249
- bridged. initialize ( at: index, to: object)
250
+ for bucket in native. hashTable {
251
+ let value = native. uncheckedValue ( at: bucket)
252
+ let cocoaValue = _bridgeAnythingToObjectiveC ( value)
253
+ bridged. initialize ( at: bucket, to: cocoaValue)
250
254
}
251
255
252
256
// Atomically put the bridged values in place.
253
257
_initializeBridgedValues ( bridged)
254
258
return _bridgedValues!
255
259
}
256
260
257
- @usableFromInline
258
- internal typealias Index = _HashTable . Index
259
-
260
261
@objc ( copyWithZone: )
261
262
internal func copy( with zone: _SwiftNSZone ? ) -> AnyObject {
262
263
// Instances of this class should be visible outside of standard library as
263
264
// having `NSDictionary` type, which is immutable.
264
265
return self
265
266
}
266
267
267
- @objc ( objectForKey: )
268
- internal func object( forKey aKey: AnyObject ) -> AnyObject ? {
269
- guard let nativeKey = _conditionallyBridgeFromObjectiveC ( aKey, Key . self)
270
- else { return nil }
271
-
272
- let ( index, found) = native. find ( nativeKey)
273
- guard found else { return nil }
274
- if let bridgedValues = bridgeValues ( ) {
275
- return bridgedValues [ index]
276
- }
277
- return _bridgeAnythingToObjectiveC ( native. uncheckedValue ( at: index) )
278
- }
279
-
280
268
@inline ( __always)
281
269
private func _key(
282
- at index : Index ,
270
+ at bucket : Bucket ,
283
271
bridgedKeys: _BridgingHashBuffer ?
284
272
) -> AnyObject {
285
273
if let bridgedKeys = bridgedKeys {
286
- return bridgedKeys [ index ]
274
+ return bridgedKeys [ bucket ]
287
275
}
288
- return _bridgeAnythingToObjectiveC ( native. uncheckedKey ( at: index ) )
276
+ return _bridgeAnythingToObjectiveC ( native. uncheckedKey ( at: bucket ) )
289
277
}
290
278
291
279
@inline ( __always)
292
280
private func _value(
293
- at index : Index ,
281
+ at bucket : Bucket ,
294
282
bridgedValues: _BridgingHashBuffer ?
295
283
) -> AnyObject {
296
284
if let bridgedValues = bridgedValues {
297
- return bridgedValues [ index ]
285
+ return bridgedValues [ bucket ]
298
286
}
299
- return _bridgeAnythingToObjectiveC ( native. uncheckedValue ( at: index) )
287
+ return _bridgeAnythingToObjectiveC ( native. uncheckedValue ( at: bucket) )
288
+ }
289
+
290
+ @objc ( objectForKey: )
291
+ internal func object( forKey aKey: AnyObject ) -> AnyObject ? {
292
+ guard let nativeKey = _conditionallyBridgeFromObjectiveC ( aKey, Key . self)
293
+ else { return nil }
294
+
295
+ let ( bucket, found) = native. find ( nativeKey)
296
+ guard found else { return nil }
297
+ return _value ( at: bucket, bridgedValues: bridgeValues ( ) )
300
298
}
301
299
302
300
@objc
@@ -324,21 +322,21 @@ final internal class _SwiftDeferredNSDictionary<Key: Hashable, Value>
324
322
325
323
switch ( _UnmanagedAnyObjectArray ( keys) , _UnmanagedAnyObjectArray ( objects) ) {
326
324
case ( let unmanagedKeys? , let unmanagedObjects? ) :
327
- for index in native. hashTable {
328
- unmanagedKeys [ i] = _key ( at: index , bridgedKeys: bridgedKeys)
329
- unmanagedObjects [ i] = _value ( at: index , bridgedValues: bridgedValues)
325
+ for bucket in native. hashTable {
326
+ unmanagedKeys [ i] = _key ( at: bucket , bridgedKeys: bridgedKeys)
327
+ unmanagedObjects [ i] = _value ( at: bucket , bridgedValues: bridgedValues)
330
328
i += 1
331
329
guard i < count else { break }
332
330
}
333
331
case ( let unmanagedKeys? , nil ) :
334
- for index in native. hashTable {
335
- unmanagedKeys [ i] = _key ( at: index , bridgedKeys: bridgedKeys)
332
+ for bucket in native. hashTable {
333
+ unmanagedKeys [ i] = _key ( at: bucket , bridgedKeys: bridgedKeys)
336
334
i += 1
337
335
guard i < count else { break }
338
336
}
339
337
case ( nil , let unmanagedObjects? ) :
340
- for index in native. hashTable {
341
- unmanagedObjects [ i] = _value ( at: index , bridgedValues: bridgedValues)
338
+ for bucket in native. hashTable {
339
+ unmanagedObjects [ i] = _value ( at: bucket , bridgedValues: bridgedValues)
342
340
i += 1
343
341
guard i < count else { break }
344
342
}
@@ -362,9 +360,9 @@ final internal class _SwiftDeferredNSDictionary<Key: Hashable, Value>
362
360
defer { _fixLifetime ( self ) }
363
361
364
362
var stop : UInt8 = 0
365
- for index in native. hashTable {
366
- let key = _key ( at: index , bridgedKeys: bridgedKeys)
367
- let value = _value ( at: index , bridgedValues: bridgedValues)
363
+ for bucket in native. hashTable {
364
+ let key = _key ( at: bucket , bridgedKeys: bridgedKeys)
365
+ let value = _value ( at: bucket , bridgedValues: bridgedValues)
368
366
block (
369
367
Unmanaged . passUnretained ( key) ,
370
368
Unmanaged . passUnretained ( value) ,
@@ -384,12 +382,15 @@ final internal class _SwiftDeferredNSDictionary<Key: Hashable, Value>
384
382
objects: UnsafeMutablePointer < AnyObject > ? ,
385
383
count: Int
386
384
) -> Int {
385
+ defer { _fixLifetime ( self ) }
386
+ let hashTable = native. hashTable
387
+
387
388
var theState = state. pointee
388
389
if theState. state == 0 {
389
390
theState. state = 1 // Arbitrary non-zero value.
390
391
theState. itemsPtr = AutoreleasingUnsafeMutablePointer ( objects)
391
392
theState. mutationsPtr = _fastEnumerationStorageMutationsPtr
392
- theState. extra. 0 = CUnsignedLong ( native . startIndex . bucket )
393
+ theState. extra. 0 = CUnsignedLong ( hashTable . startBucket . offset )
393
394
}
394
395
395
396
// Test 'objects' rather than 'count' because (a) this is very rare anyway,
@@ -400,21 +401,21 @@ final internal class _SwiftDeferredNSDictionary<Key: Hashable, Value>
400
401
}
401
402
402
403
let unmanagedObjects = _UnmanagedAnyObjectArray ( objects!)
403
- var index = _HashTable. Index ( bucket : Int ( theState. extra. 0 ) )
404
- let endIndex = native . endIndex
405
- _precondition ( index == endIndex || native . hashTable. isOccupied ( index ) )
404
+ var bucket = _HashTable. Bucket ( offset : Int ( theState. extra. 0 ) )
405
+ let endBucket = hashTable . endBucket
406
+ _precondition ( bucket == endBucket || hashTable. isOccupied ( bucket ) )
406
407
var stored = 0
407
408
408
409
// Only need to bridge once, so we can hoist it out of the loop.
409
410
let bridgedKeys = bridgeKeys ( )
410
411
for i in 0 ..< count {
411
- if index == endIndex { break }
412
+ if bucket == endBucket { break }
412
413
413
- unmanagedObjects [ i] = _key ( at: index , bridgedKeys: bridgedKeys)
414
+ unmanagedObjects [ i] = _key ( at: bucket , bridgedKeys: bridgedKeys)
414
415
stored += 1
415
- index = native . index ( after: index )
416
+ bucket = hashTable . occupiedBucket ( after: bucket )
416
417
}
417
- theState. extra. 0 = CUnsignedLong ( index . bucket)
418
+ theState. extra. 0 = CUnsignedLong ( bucket. offset )
418
419
state. pointee = theState
419
420
return stored
420
421
}
0 commit comments