Skip to content

[StdLib][RFC][DNM] Add isIdentical Methods for Quick Comparisons to Array, ArraySlice, and ContiguousArray #82438

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
30 changes: 30 additions & 0 deletions stdlib/public/core/Array.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2136,3 +2136,33 @@ internal struct _ArrayAnyHashableBox<Element: Hashable>
}

extension Array: @unchecked Sendable where Element: Sendable { }

extension Array {
/// Returns a boolean value indicating whether this array is identical to
/// `other`.
///
/// Two array values are identical if there is no way to distinguish between
/// them.
///
/// Comparing arrays this way includes comparing (normally) hidden
/// implementation details such as the memory location of any underlying
/// array storage object. Therefore, identical arrays are guaranteed to
/// compare equal with `==`, but not all equal arrays are considered
/// identical.
///
/// - Performance: O(1)
@_alwaysEmitIntoClient
public func isIdentical(to other: Self) -> Bool {
let lhsCount = self.count
if lhsCount != other.count {
return false
}

// Test referential equality.
if unsafe lhsCount == 0 || self._buffer.identity == other._buffer.identity {
return true
}

return false
}
}
30 changes: 30 additions & 0 deletions stdlib/public/core/ArraySlice.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1586,3 +1586,33 @@ extension ArraySlice {
}
}
#endif

extension ArraySlice {
/// Returns a boolean value indicating whether this array is identical to
/// `other`.
///
/// Two array values are identical if there is no way to distinguish between
/// them.
///
/// Comparing arrays this way includes comparing (normally) hidden
/// implementation details such as the memory location of any underlying
/// array storage object. Therefore, identical arrays are guaranteed to
/// compare equal with `==`, but not all equal arrays are considered
/// identical.
///
/// - Performance: O(1)
@_alwaysEmitIntoClient
public func isIdentical(to other: Self) -> Bool {
let lhsCount = self.count
if lhsCount != other.count {
return false
}

// Test referential equality.
if unsafe lhsCount == 0 || self._buffer.identity == other._buffer.identity {
return true
}

return false
}
}
30 changes: 30 additions & 0 deletions stdlib/public/core/ContiguousArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1495,3 +1495,33 @@ extension ContiguousArray {

extension ContiguousArray: @unchecked Sendable
where Element: Sendable { }

extension ContiguousArray {
/// Returns a boolean value indicating whether this array is identical to
/// `other`.
///
/// Two array values are identical if there is no way to distinguish between
/// them.
///
/// Comparing arrays this way includes comparing (normally) hidden
/// implementation details such as the memory location of any underlying
/// array storage object. Therefore, identical arrays are guaranteed to
/// compare equal with `==`, but not all equal arrays are considered
/// identical.
///
/// - Performance: O(1)
@_alwaysEmitIntoClient
public func isIdentical(to other: Self) -> Bool {
let lhsCount = self.count
if lhsCount != other.count {
return false
}

// Test referential equality.
if unsafe lhsCount == 0 || self._buffer.identity == other._buffer.identity {
return true
}

return false
}
}