Skip to content

[StdLib][RFC][DNM] Add isIdentical Methods for Quick Comparisons to Dictionary and Set #82439

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
40 changes: 40 additions & 0 deletions stdlib/public/core/Dictionary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2154,3 +2154,43 @@ extension Dictionary.Index: @unchecked Sendable
where Key: Sendable, Value: Sendable {}
extension Dictionary.Iterator: @unchecked Sendable
where Key: Sendable, Value: Sendable {}

extension Dictionary {
/// Returns a boolean value indicating whether this dictionary is identical to
/// `other`.
///
/// Two dictionary values are identical if there is no way to distinguish
/// between them.
///
/// Comparing dictionaries this way includes comparing (normally) hidden
/// implementation details such as the memory location of any underlying
/// dictionary storage object. Therefore, identical dictionaries are
/// guaranteed to compare equal with `==`, but not all equal dictionaries are
/// considered identical.
///
/// - Performance: O(1)
@_alwaysEmitIntoClient
public func isIdentical(to other: Self) -> Bool {
#if _runtime(_ObjC)
if
self._variant.isNative,
other._variant.isNative,
unsafe (self._variant.asNative._storage === other._variant.asNative._storage)
{
return true
}
if
!self._variant.isNative,
!other._variant.isNative,
self._variant.asCocoa.object === other._variant.asCocoa.object
{
return true
}
#else
if unsafe (self._variant.asNative._storage === other._variant.asNative._storage) {
return true
}
#endif
return false
}
}
39 changes: 39 additions & 0 deletions stdlib/public/core/Set.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1658,3 +1658,42 @@ extension Set.Index: @unchecked Sendable
where Element: Sendable { }
extension Set.Iterator: @unchecked Sendable
where Element: Sendable { }

extension Set {
/// Returns a boolean value indicating whether this set is identical to
/// `other`.
///
/// Two set values are identical if there is no way to distinguish between
/// them.
///
/// Comparing sets this way includes comparing (normally) hidden
/// implementation details such as the memory location of any underlying set
/// storage object. Therefore, identical sets are guaranteed to compare equal
/// with `==`, but not all equal sets are considered identical.
///
/// - Performance: O(1)
@_alwaysEmitIntoClient
public func isIdentical(to other: Self) -> Bool {
#if _runtime(_ObjC)
if
self._variant.isNative,
other._variant.isNative,
unsafe (self._variant.asNative._storage === other._variant.asNative._storage)
{
return true
}
if
!self._variant.isNative,
!other._variant.isNative,
self._variant.asCocoa.object === other._variant.asCocoa.object
{
return true
}
#else
if unsafe (self._variant.asNative._storage === other._variant.asNative._storage) {
return true
}
#endif
return false
}
}