Skip to content

Hash implementation for NSIndexSet #2881

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
merged 1 commit into from
Oct 1, 2020
Merged
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
9 changes: 9 additions & 0 deletions Sources/Foundation/NSIndexSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,15 @@ open class NSIndexSet : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
return isEqual(to: indexSet._swiftObject)
}

open override var hash: Int {
var hasher = Hasher()
hasher.combine(_count)
hasher.combine(_ranges.count)
hasher.combine(firstIndex)
hasher.combine(lastIndex)
return hasher.finalize()
}

open var count: Int {
return _count
}
Expand Down
31 changes: 31 additions & 0 deletions Tests/Foundation/Tests/TestIndexSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1240,6 +1240,36 @@ class TestIndexSet : XCTestCase {
}
}

func testHashValue() throws {
var sample1 = IndexSet()
sample1.insert(integersIn: 1..<2)
sample1.insert(integersIn: 100..<200)
sample1.insert(integersIn: 1000..<2000)

var sample2 = IndexSet()
sample2.insert(integersIn: 1..<2)
sample2.insert(integersIn: 100..<200)
sample2.insert(integersIn: 1000..<2000)

XCTAssertEqual(sample1.hashValue, sample2.hashValue)

let sample3 = IndexSet([3, 5, 6, 7, 9])
let sample4 = IndexSet([3, 5, 6, 7, 9])
XCTAssertEqual(sample3.hashValue, sample4.hashValue)

let sample5 = IndexSet([100, NSNotFound - 1, 200])
let sample6 = IndexSet([100, NSNotFound - 1, 200])
XCTAssertEqual(sample5.hashValue, sample6.hashValue)

let sample7 = IndexSet(integer: NSNotFound - 1)
let sample8 = IndexSet(integer: NSNotFound - 1)
XCTAssertEqual(sample7.hashValue, sample8.hashValue)

let sample9 = IndexSet()
let sample10 = IndexSet()
XCTAssertEqual(sample9.hashValue, sample10.hashValue)
}

static var allTests: [(String, (TestIndexSet) -> () throws -> Void)] {
return [
("test_BasicConstruction", test_BasicConstruction),
Expand Down Expand Up @@ -1288,6 +1318,7 @@ class TestIndexSet : XCTestCase {
("testRemoveSplitting", testRemoveSplitting),
("testCodingRoundtrip", testCodingRoundtrip),
("testLoadedValuesMatch", testLoadedValuesMatch),
("testHashValue", testHashValue),
]
}

Expand Down