Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Making Databases Work The Pragmatic Wisdom Of Michael Stonebraker

Authors: Michael L. Brodie

1st Edition

1947487167, 978-1947487161

More Books

Students also viewed these Databases questions

Question

LO1 Identify why performance management is necessary.

Answered: 1 week ago