Skip to content

Commit c7d33e7

Browse files
authored
Merge pull request #13003 from DougGregor/stdlib-equality-methods
2 parents 8dc2762 + 9f1cd0b commit c7d33e7

File tree

5 files changed

+296
-297
lines changed

5 files changed

+296
-297
lines changed

stdlib/public/core/Arrays.swift.gyb

+38-43
Original file line numberDiff line numberDiff line change
@@ -2177,61 +2177,56 @@ extension _ArrayBufferProtocol {
21772177
}
21782178

21792179
% for (Self, a_Self) in arrayTypes:
2180-
// NOTE: The '==' and '!=' below only handles array types
2181-
// that are the same, e.g. Array<Int> and Array<Int>, not
2182-
// ArraySlice<Int> and Array<Int>.
21832180

2184-
/// Returns `true` if these arrays contain the same elements.
2185-
@_inlineable
2186-
public func == <Element : Equatable>(
2187-
lhs: ${Self}<Element>, rhs: ${Self}<Element>
2188-
) -> Bool {
2189-
let lhsCount = lhs.count
2190-
if lhsCount != rhs.count {
2191-
return false
2192-
}
2181+
extension ${Self} where Element : Equatable {
2182+
/// Returns `true` if these arrays contain the same elements.
2183+
@_inlineable
2184+
public static func ==(lhs: ${Self}<Element>, rhs: ${Self}<Element>) -> Bool {
2185+
let lhsCount = lhs.count
2186+
if lhsCount != rhs.count {
2187+
return false
2188+
}
21932189

2194-
// Test referential equality.
2195-
if lhsCount == 0 || lhs._buffer.identity == rhs._buffer.identity {
2196-
return true
2197-
}
2190+
// Test referential equality.
2191+
if lhsCount == 0 || lhs._buffer.identity == rhs._buffer.identity {
2192+
return true
2193+
}
21982194

2199-
%if Self == 'ArraySlice':
2195+
%if Self == 'ArraySlice':
22002196

2201-
var streamLHS = lhs.makeIterator()
2202-
var streamRHS = rhs.makeIterator()
2197+
var streamLHS = lhs.makeIterator()
2198+
var streamRHS = rhs.makeIterator()
22032199

2204-
var nextLHS = streamLHS.next()
2205-
while nextLHS != nil {
2206-
let nextRHS = streamRHS.next()
2207-
if nextLHS != nextRHS {
2208-
return false
2200+
var nextLHS = streamLHS.next()
2201+
while nextLHS != nil {
2202+
let nextRHS = streamRHS.next()
2203+
if nextLHS != nextRHS {
2204+
return false
2205+
}
2206+
nextLHS = streamLHS.next()
22092207
}
2210-
nextLHS = streamLHS.next()
2211-
}
22122208

2213-
%else:
2209+
%else:
22142210

2215-
_sanityCheck(lhs.startIndex == 0 && rhs.startIndex == 0)
2216-
_sanityCheck(lhs.endIndex == lhsCount && rhs.endIndex == lhsCount)
2211+
_sanityCheck(lhs.startIndex == 0 && rhs.startIndex == 0)
2212+
_sanityCheck(lhs.endIndex == lhsCount && rhs.endIndex == lhsCount)
22172213

2218-
// We know that lhs.count == rhs.count, compare element wise.
2219-
for idx in 0..<lhsCount {
2220-
if lhs[idx] != rhs[idx] {
2221-
return false
2214+
// We know that lhs.count == rhs.count, compare element wise.
2215+
for idx in 0..<lhsCount {
2216+
if lhs[idx] != rhs[idx] {
2217+
return false
2218+
}
22222219
}
2223-
}
2224-
%end
2220+
%end
22252221

2226-
return true
2227-
}
2222+
return true
2223+
}
22282224

2229-
/// Returns `true` if the arrays do not contain the same elements.
2230-
@_inlineable
2231-
public func != <Element : Equatable>(
2232-
lhs: ${Self}<Element>, rhs: ${Self}<Element>
2233-
) -> Bool {
2234-
return !(lhs == rhs)
2225+
/// Returns `true` if the arrays do not contain the same elements.
2226+
@_inlineable
2227+
public static func !=(lhs: ${Self}<Element>, rhs: ${Self}<Element>) -> Bool {
2228+
return !(lhs == rhs)
2229+
}
22352230
}
22362231

22372232
extension ${Self} {

0 commit comments

Comments
 (0)