Skip to content

NSIndexset isEqual performance enhancement #1605

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

Merged
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
15 changes: 10 additions & 5 deletions Foundation/NSIndexSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,16 +111,21 @@ open class NSIndexSet : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
}

open func isEqual(to indexSet: IndexSet) -> Bool {

let otherRanges = indexSet.rangeView.map { NSRange(location: $0.lowerBound, length: $0.upperBound - $0.lowerBound) }
if _ranges.count != otherRanges.count {
// Exit early if the IndexSets do not have the same number of intervals
if _ranges.count != indexSet.rangeView.count {
return false
}
for (r1, r2) in zip(_ranges, otherRanges) {
if r1.length != r2.length || r1.location != r2.location {

// Iterate over indexes to compare each
for (range, element) in zip(_ranges, indexSet.rangeView) {
let elementLength = element.upperBound - element.lowerBound

// Return false if the ranges do not match
if range.location != element.lowerBound || range.length != elementLength {
return false
}
}

return true
}

Expand Down