@@ -1498,20 +1498,14 @@ extension Dictionary {
1498
1498
@inlinable
1499
1499
public mutating func swapAt( _ i: Index , _ j: Index ) {
1500
1500
guard i != j else { return }
1501
- let ( a, b) : ( _HashTable . Bucket , _HashTable . Bucket )
1502
- switch _variant {
1503
- case . native( let native) :
1504
- a = native. validatedBucket ( for: i)
1505
- b = native. validatedBucket ( for: j)
1506
1501
#if _runtime(_ObjC)
1507
- case . cocoa( let cocoa) :
1508
- _variant. cocoaPath ( )
1509
- let native = _NativeDictionary < Key , Value > ( cocoa)
1510
- a = native. validatedBucket ( for: i)
1511
- b = native. validatedBucket ( for: j)
1512
- _variant = . native( native)
1513
- #endif
1502
+ if !_variant. isNative {
1503
+ _variant = . native( _NativeDictionary < Key , Value > ( _variant. asCocoa) )
1514
1504
}
1505
+ #endif
1506
+ let native = _variant. asNative
1507
+ let a = native. validatedBucket ( for: i)
1508
+ let b = native. validatedBucket ( for: j)
1515
1509
let isUnique = _variant. isUniquelyReferenced ( )
1516
1510
_variant. asNative. swapValuesAt ( a, b, isUnique: isUnique)
1517
1511
}
@@ -1827,6 +1821,17 @@ extension Dictionary.Index {
1827
1821
_conditionallyUnreachable ( )
1828
1822
}
1829
1823
}
1824
+
1825
+ @usableFromInline @_transparent
1826
+ internal var _isNative : Bool {
1827
+ switch _variant {
1828
+ case . native:
1829
+ return true
1830
+ case . cocoa:
1831
+ _cocoaPath ( )
1832
+ return false
1833
+ }
1834
+ }
1830
1835
#endif
1831
1836
1832
1837
@usableFromInline @_transparent
@@ -1899,16 +1904,14 @@ extension Dictionary.Index: Comparable {
1899
1904
extension Dictionary . Index : Hashable {
1900
1905
@_effects ( readonly) // FIXME(cocoa-index): Make inlinable
1901
1906
public func hash( into hasher: inout Hasher ) {
1902
- #if _runtime(_ObjC)
1903
- switch _variant {
1904
- case . native( let nativeIndex) :
1905
- hasher. combine ( 0 as UInt8 )
1906
- hasher. combine ( nativeIndex. bucket. offset)
1907
- case . cocoa( let cocoaIndex) :
1908
- _cocoaPath ( )
1907
+ #if _runtime(_ObjC)
1908
+ guard _isNative else {
1909
1909
hasher. combine ( 1 as UInt8 )
1910
- hasher. combine ( cocoaIndex. storage. currentKeyIndex)
1910
+ hasher. combine ( _asCocoa. storage. currentKeyIndex)
1911
+ return
1911
1912
}
1913
+ hasher. combine ( 0 as UInt8 )
1914
+ hasher. combine ( _asNative. bucket. offset)
1912
1915
#else
1913
1916
hasher. combine ( _asNative. bucket. offset)
1914
1917
#endif
@@ -1975,6 +1978,17 @@ extension Dictionary.Iterator {
1975
1978
_conditionallyUnreachable ( )
1976
1979
}
1977
1980
}
1981
+
1982
+ @usableFromInline @_transparent
1983
+ internal var _isNative : Bool {
1984
+ switch _variant {
1985
+ case . native:
1986
+ return true
1987
+ case . cocoa:
1988
+ _cocoaPath ( )
1989
+ return false
1990
+ }
1991
+ }
1978
1992
#endif
1979
1993
1980
1994
@usableFromInline @_transparent
@@ -1993,6 +2007,21 @@ extension Dictionary.Iterator {
1993
2007
self . _variant = . native( newValue)
1994
2008
}
1995
2009
}
2010
+
2011
+ #if _runtime(_ObjC)
2012
+ @usableFromInline @_transparent
2013
+ internal var _asCocoa : _CocoaDictionary . Iterator {
2014
+ get {
2015
+ switch _variant {
2016
+ case . native:
2017
+ _sanityCheckFailure ( " internal error: does not contain a Cocoa index " )
2018
+ case . cocoa( let cocoa) :
2019
+ return cocoa
2020
+ }
2021
+ }
2022
+ }
2023
+ #endif
2024
+
1996
2025
}
1997
2026
1998
2027
extension Dictionary . Iterator : IteratorProtocol {
@@ -2003,20 +2032,17 @@ extension Dictionary.Iterator: IteratorProtocol {
2003
2032
@inlinable
2004
2033
@inline ( __always)
2005
2034
public mutating func next( ) -> ( key: Key , value: Value ) ? {
2006
- switch _variant {
2007
- case . native:
2008
- return _asNative. next ( )
2009
2035
#if _runtime(_ObjC)
2010
- case . cocoa( let cocoaIterator) :
2011
- _cocoaPath ( )
2012
- if let ( cocoaKey, cocoaValue) = cocoaIterator. next ( ) {
2036
+ guard _isNative else {
2037
+ if let ( cocoaKey, cocoaValue) = _asCocoa. next ( ) {
2013
2038
let nativeKey = _forceBridgeFromObjectiveC ( cocoaKey, Key . self)
2014
2039
let nativeValue = _forceBridgeFromObjectiveC ( cocoaValue, Value . self)
2015
2040
return ( nativeKey, nativeValue)
2016
2041
}
2017
2042
return nil
2018
- #endif
2019
2043
}
2044
+ #endif
2045
+ return _asNative. next ( )
2020
2046
}
2021
2047
}
2022
2048
0 commit comments