Skip to content

Commit

Permalink
remove std::abs as it is unecessary for size_t which is already unsigned
Browse files Browse the repository at this point in the history
  • Loading branch information
kvedala committed Jul 23, 2020
1 parent e2a94da commit c81705e
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions hashing/double_hash_hash_table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ struct Entry {
};

// Hash a key
int hashFxn(int key) {
size_t hashFxn(int key) {
std::hash<int> hash;
return hash(key);
}
Expand All @@ -51,13 +51,12 @@ size_t otherHashFxn(int key) {

// Performs double hashing to resolve collisions
int doubleHash(int key, bool searching) {
int hash = static_cast<int>(std::abs(hashFxn(key)));
int hash = static_cast<int>(hashFxn(key));
int i = 0;
Entry entry;
do {
int index =
static_cast<int>(std::abs((hash + (i * otherHashFxn(key))))) %
totalSize;
static_cast<int>(hash + (i * otherHashFxn(key))) % totalSize;
entry = table[index];
if (searching) {
if (entry.key == notPresent) {
Expand All @@ -79,8 +78,7 @@ int doubleHash(int key, bool searching) {
if (!rehashing) {
cout << "Spot taken, looking at next (next index:"
<< " "
<< static_cast<int>(
std::abs((hash + (i * otherHashFxn(key))))) %
<< static_cast<int>(hash + (i * otherHashFxn(key))) %
totalSize
<< ")" << endl;
}
Expand Down Expand Up @@ -177,7 +175,7 @@ void addInfo(int key) {
display();
cout << endl;
cout << "hash of " << key << " is " << hashFxn(key) << " % " << totalSize
<< " == " << std::abs(hashFxn(key) % totalSize);
<< " == " << hashFxn(key) % totalSize;
cout << endl;
add(key);
cout << "New table: ";
Expand Down Expand Up @@ -239,7 +237,7 @@ int main() {
case 4:
cout << "Enter element to generate hash = ";
cin >> key;
cout << "Hash of " << key << " is = " << std::abs(hashFxn(key));
cout << "Hash of " << key << " is = " << hashFxn(key);
break;
case 5:
display();
Expand Down

0 comments on commit c81705e

Please sign in to comment.