Skip to content

Commit

Permalink
This fixes the unlikely event that the generated hashValue happens to…
Browse files Browse the repository at this point in the history
… be the equal to Int.min, in which case the abs value of it would be Int.max + 1 causing overflow.
  • Loading branch information
Linslav committed Apr 29, 2019
1 parent 114becb commit 721af5d
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Hash Table/HashTable.playground/Sources/HashTable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public struct HashTable<Key: Hashable, Value> {
Returns the given key's array index.
*/
private func index(forKey key: Key) -> Int {
return abs(key.hashValue) % buckets.count
return abs(key.hashValue % buckets.count)
}
}

Expand Down
2 changes: 1 addition & 1 deletion Hash Table/README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ The hash table does not do anything yet, so let's add the remaining functionalit

```swift
private func index(forKey key: Key) -> Int {
return abs(key.hashValue) % buckets.count
return abs(key.hashValue % buckets.count)
}
```

Expand Down

0 comments on commit 721af5d

Please sign in to comment.