Skip to content

Commit 7df89db

Browse files
authored
Merge pull request #1605 from RobertPieta/nsindexset_isequal_performance_enhancement
2 parents 74eb084 + 01fa634 commit 7df89db

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

Foundation/NSIndexSet.swift

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,16 +111,21 @@ 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 (range, element) in zip(_ranges, indexSet.rangeView) {
121+
let elementLength = element.upperBound - element.lowerBound
122+
123+
// Return false if the ranges do not match
124+
if range.location != element.lowerBound || range.length != elementLength {
121125
return false
122126
}
123127
}
128+
124129
return true
125130
}
126131

0 commit comments

Comments
 (0)