Question: C++ // insert HashedObj x into the table, if it is not already in table, it should call the resolution selector function given. ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- #include

C++ // insert HashedObj x into the table, if it is not already in table, it should call the resolution selector function given.

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

#include

#include "HashTable.h"

HashTable::HashTable(int _size, ResolutionType _resolutionFunction):

resolutionFunction {_resolutionFunction},

size {0}

{

v = std::vector{size_t(nextPrime(_size-1))};

}

// accessors -------------------

// will return the current capacity of the vector storing the HashEntrys

size_t HashTable::tableCapacity() const {

return v.capacity();

}

// normal hash for strings (we saw in class)

// Takes a HashedObj and will return a hash value.

// NOTE: this does not ensure it is within range of the

// tablesize, and therefore your code should ensure it is

// after calling this function.

int HashTable::hash (const HashedObj& x) const {

unsigned int hashVal= 0;

for (int i = 0; i < std::min(3,int(x.key.size())); ++i){

hashVal = 37*hashVal + x.key[i];

}

return hashVal;

}

// a handy resolution-function selector method.

// Since we store the resolution type as part of the hash table,

// we can just call this function in place of

// linearResolution, quadraticResolution, or doubleHashResolution.

// Takes:

// i: resolution number (i.e. # of collisions occurred)

// x: HashedObj to hash on. Note this is only needed by

// doubleHashResolution().

// Returns:

// int that is a hash value offset for resolution.

int HashTable::resolution (int i, const HashedObj& x) const {

if (resolutionFunction == LINEAR){

return linearResolution(i);

} else if (resolutionFunction == QUADRATIC) {

return quadraticResolution(i);

} else {

return doubleHashResolution(i, x);

}

}

// computes the ith linear resolution offset

int HashTable::linearResolution (int i) const {

return i;

}

// computes the ith quadratic resolution offset

int HashTable::quadraticResolution (int i) const {

return i;

}

// computes the ith double hash resolution of x

int HashTable::doubleHashResolution (int i, const HashedObj& x) const {

return i*hash2(x);

}

bool HashTable::insert(const HashedObj& x){

// CODE HERE

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!