Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 60 additions & 29 deletions stdlib/public/core/Dictionary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1513,21 +1513,16 @@ extension Dictionary {
@inlinable
public mutating func swapAt(_ i: Index, _ j: Index) {
guard i != j else { return }
let (a, b): (_HashTable.Bucket, _HashTable.Bucket)
switch _variant {
case .native(let native):
a = native.validatedBucket(for: i)
b = native.validatedBucket(for: j)
#if _runtime(_ObjC)
case .cocoa(let cocoa):
_variant.cocoaPath()
let native = _NativeDictionary<Key, Value>(cocoa)
a = native.validatedBucket(for: i)
b = native.validatedBucket(for: j)
_variant = .native(native)
#endif
let isNative: Bool
do { isNative = _variant.isNative }
if !isNative {
_variant = .native(_NativeDictionary<Key, Value>(_variant.asCocoa))
}
#endif
let isUnique = _variant.isUniquelyReferenced()
let a = _variant.asNative.validatedBucket(for: i)
let b = _variant.asNative.validatedBucket(for: j)
_variant.asNative.swapValuesAt(a, b, isUnique: isUnique)
}
}
Expand Down Expand Up @@ -1850,6 +1845,17 @@ extension Dictionary.Index {
var handle = _asCocoa.handleBitPattern
return handle == 0 || _isUnique_native(&handle)
}

@usableFromInline @_transparent
internal var _isNative: Bool {
switch _variant {
case .native:
return true
case .cocoa:
_cocoaPath()
return false
}
}
#endif

@usableFromInline @_transparent
Expand Down Expand Up @@ -1935,19 +1941,19 @@ extension Dictionary.Index: Comparable {
extension Dictionary.Index: Hashable {
public // FIXME(cocoa-index): Make inlinable
func hash(into hasher: inout Hasher) {
#if _runtime(_ObjC)
switch _variant {
case .native(let nativeIndex):
hasher.combine(0 as UInt8)
hasher.combine(nativeIndex.bucket.offset)
case .cocoa(let cocoaIndex):
_cocoaPath()
#if _runtime(_ObjC)
let isNative: Bool
do { isNative = _isNative }
guard isNative else {
hasher.combine(1 as UInt8)
hasher.combine(cocoaIndex.storage.currentKeyIndex)
hasher.combine(_asCocoa.storage.currentKeyIndex)
return
}
#else
hasher.combine(0 as UInt8)
hasher.combine(_asNative.bucket.offset)
#endif
#else
hasher.combine(_asNative.bucket.offset)
#endif
}
}

Expand Down Expand Up @@ -2011,6 +2017,17 @@ extension Dictionary.Iterator {
_conditionallyUnreachable()
}
}

@usableFromInline @_transparent
internal var _isNative: Bool {
switch _variant {
case .native:
return true
case .cocoa:
_cocoaPath()
return false
}
}
#endif

@usableFromInline @_transparent
Expand All @@ -2029,6 +2046,21 @@ extension Dictionary.Iterator {
self._variant = .native(newValue)
}
}

#if _runtime(_ObjC)
@usableFromInline @_transparent
internal var _asCocoa: _CocoaDictionary.Iterator {
get {
switch _variant {
case .native:
_sanityCheckFailure("internal error: does not contain a Cocoa index")
case .cocoa(let cocoa):
return cocoa
}
}
}
#endif

}

extension Dictionary.Iterator: IteratorProtocol {
Expand All @@ -2039,20 +2071,19 @@ extension Dictionary.Iterator: IteratorProtocol {
@inlinable
@inline(__always)
public mutating func next() -> (key: Key, value: Value)? {
switch _variant {
case .native:
return _asNative.next()
#if _runtime(_ObjC)
case .cocoa(let cocoaIterator):
_cocoaPath()
if let (cocoaKey, cocoaValue) = cocoaIterator.next() {
let isNative: Bool
do { isNative = _isNative }
guard isNative else {
if let (cocoaKey, cocoaValue) = _asCocoa.next() {
let nativeKey = _forceBridgeFromObjectiveC(cocoaKey, Key.self)
let nativeValue = _forceBridgeFromObjectiveC(cocoaValue, Value.self)
return (nativeKey, nativeValue)
}
return nil
#endif
}
#endif
return _asNative.next()
}
}

Expand Down
8 changes: 3 additions & 5 deletions stdlib/public/core/DictionaryBridging.swift
Original file line number Diff line number Diff line change
Expand Up @@ -797,12 +797,10 @@ extension _CocoaDictionary.Iterator: IteratorProtocol {
extension Dictionary {
@inlinable
public __consuming func _bridgeToObjectiveCImpl() -> _NSDictionaryCore {
switch _variant {
case .native(let nativeDictionary):
return nativeDictionary.bridged()
case .cocoa(let cocoaDictionary):
return cocoaDictionary.object
guard _variant.isNative else {
return _variant.asCocoa.object
}
return _variant.asNative.bridged()
}

/// Returns the native Dictionary hidden inside this NSDictionary;
Expand Down
14 changes: 7 additions & 7 deletions stdlib/public/core/DictionaryCasting.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@ public func _dictionaryDownCast<BaseKey, BaseValue, DerivedKey, DerivedValue>(
&& _isClassOrObjCExistential(DerivedKey.self)
&& _isClassOrObjCExistential(DerivedValue.self) {

switch source._variant {
case .native(let native):
// Note: it is safe to treat the buffer as immutable here because
// Dictionary will not mutate buffer with reference count greater than 1.
return Dictionary(_immutableCocoaDictionary: native.bridged())
case .cocoa(let cocoa):
return Dictionary(_immutableCocoaDictionary: cocoa.object)
guard source._variant.isNative else {
return Dictionary(
_immutableCocoaDictionary: source._variant.asCocoa.object)
}
// Note: it is safe to treat the buffer as immutable here because
// Dictionary will not mutate buffer with reference count greater than 1.
return Dictionary(
_immutableCocoaDictionary: source._variant.asNative.bridged())
}
#endif
return _dictionaryDownCastConditional(source)!
Expand Down
Loading