Skip to content

Commit 7773d64

Browse files
Robert PietaRobert Pieta
Robert Pieta
authored and
Robert Pieta
committed
Improved performance of NSIndexSet.isEqual
1 parent e49dcf0 commit 7773d64

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

Foundation/NSIndexSet.swift

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,16 +111,19 @@ open class NSIndexSet : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
111111
}
112112

113113
open func isEqual(to indexSet: IndexSet) -> Bool {
114-
115-
let otherRanges = indexSet.rangeView.map { NSRange(location: $0.lowerBound, length: $0.upperBound - $0.lowerBound) }
116-
if _ranges.count != otherRanges.count {
114+
// Exit early if the IndexSets do not have the same number of intervals
115+
if _ranges.count != indexSet.rangeView.count {
117116
return false
118117
}
119-
for (r1, r2) in zip(_ranges, otherRanges) {
120-
if r1.length != r2.length || r1.location != r2.location {
118+
119+
// Iterate over indexes to compare each
120+
for (r1, r2) in zip(_ranges, indexSet.rangeView) {
121+
// Return false if the ranges do not match
122+
if r1.location != r2.lowerBound || r1.length != r2.upperBound - r2.lowerBound {
121123
return false
122124
}
123125
}
126+
124127
return true
125128
}
126129

0 commit comments

Comments
 (0)