From 721af5d569661effa976e88fc65a14fd3961b826 Mon Sep 17 00:00:00 2001 From: Vladislav Badulin Date: Mon, 29 Apr 2019 21:24:24 +0200 Subject: [PATCH] This fixes the unlikely event that the generated hashValue happens to be the equal to Int.min, in which case the abs value of it would be Int.max + 1 causing overflow. --- Hash Table/HashTable.playground/Sources/HashTable.swift | 2 +- Hash Table/README.markdown | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Hash Table/HashTable.playground/Sources/HashTable.swift b/Hash Table/HashTable.playground/Sources/HashTable.swift index 476a51174..f2a1be040 100644 --- a/Hash Table/HashTable.playground/Sources/HashTable.swift +++ b/Hash Table/HashTable.playground/Sources/HashTable.swift @@ -131,7 +131,7 @@ public struct HashTable { 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) } } diff --git a/Hash Table/README.markdown b/Hash Table/README.markdown index 9c4065a51..fb0bda788 100644 --- a/Hash Table/README.markdown +++ b/Hash Table/README.markdown @@ -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) } ```