Skip to content

Commit 18f927e

Browse files
committed
[stdlib] dictionary identical
1 parent 644f364 commit 18f927e

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

stdlib/public/core/Dictionary.swift

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2154,3 +2154,43 @@ extension Dictionary.Index: @unchecked Sendable
21542154
where Key: Sendable, Value: Sendable {}
21552155
extension Dictionary.Iterator: @unchecked Sendable
21562156
where Key: Sendable, Value: Sendable {}
2157+
2158+
extension Dictionary {
2159+
/// Returns a boolean value indicating whether this dictionary is identical to
2160+
/// `other`.
2161+
///
2162+
/// Two dictionary values are identical if there is no way to distinguish
2163+
/// between them.
2164+
///
2165+
/// Comparing dictionaries this way includes comparing (normally) hidden
2166+
/// implementation details such as the memory location of any underlying
2167+
/// dictionary storage object. Therefore, identical dictionaries are
2168+
/// guaranteed to compare equal with `==`, but not all equal dictionaries are
2169+
/// considered identical.
2170+
///
2171+
/// - Performance: O(1)
2172+
@backDeployed(before: SwiftStdlib 6.3)
2173+
public func isIdentical(to other: Self) -> Bool {
2174+
#if _runtime(_ObjC)
2175+
if
2176+
self._variant.isNative,
2177+
other._variant.isNative,
2178+
unsafe (self._variant.asNative._storage === other._variant.asNative._storage)
2179+
{
2180+
return true
2181+
}
2182+
if
2183+
!self._variant.isNative,
2184+
!other._variant.isNative,
2185+
self._variant.asCocoa.object === other._variant.asCocoa.object
2186+
{
2187+
return true
2188+
}
2189+
#else
2190+
if unsafe (self._variant.asNative._storage === other._variant.asNative._storage) {
2191+
return true
2192+
}
2193+
#endif
2194+
return false
2195+
}
2196+
}

stdlib/public/core/Set.swift

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1658,3 +1658,43 @@ extension Set.Index: @unchecked Sendable
16581658
where Element: Sendable { }
16591659
extension Set.Iterator: @unchecked Sendable
16601660
where Element: Sendable { }
1661+
1662+
extension Set {
1663+
/// Returns a boolean value indicating whether this dictionary is identical to
1664+
/// `other`.
1665+
///
1666+
/// Two dictionary values are identical if there is no way to distinguish
1667+
/// between them.
1668+
///
1669+
/// Comparing dictionaries this way includes comparing (normally) hidden
1670+
/// implementation details such as the memory location of any underlying
1671+
/// dictionary storage object. Therefore, identical dictionaries are
1672+
/// guaranteed to compare equal with `==`, but not all equal dictionaries are
1673+
/// considered identical.
1674+
///
1675+
/// - Performance: O(1)
1676+
@backDeployed(before: SwiftStdlib 6.3)
1677+
public func isIdentical(to other: Self) -> Bool {
1678+
#if _runtime(_ObjC)
1679+
if
1680+
self._variant.isNative,
1681+
other._variant.isNative,
1682+
unsafe (self._variant.asNative._storage === other._variant.asNative._storage)
1683+
{
1684+
return true
1685+
}
1686+
if
1687+
!self._variant.isNative,
1688+
!other._variant.isNative,
1689+
self._variant.asCocoa.object === other._variant.asCocoa.object
1690+
{
1691+
return true
1692+
}
1693+
#else
1694+
if unsafe (self._variant.asNative._storage === other._variant.asNative._storage) {
1695+
return true
1696+
}
1697+
#endif
1698+
return false
1699+
}
1700+
}

0 commit comments

Comments
 (0)