Skip to content

Instantly share code, notes, and snippets.

@JJL772
Created October 22, 2019 19:03
Show Gist options
  • Save JJL772/11be0eba8f31078b11b75f27ece213c0 to your computer and use it in GitHub Desktop.
Save JJL772/11be0eba8f31078b11b75f27ece213c0 to your computer and use it in GitHub Desktop.
/* your current constructor looks like this in class HTable: */
HTable():
d(1),
t(*new Array<SLList<T>>((int)std::pow(2,d))),
z(rand() | 1),
n(0)
{
}
/* In this you're initializing t (which is your array of lists) to new Array<...>(std::pow(2,d) */
/* t is already allocated within the space of your class, which means the call to new Array is redundant */
/* this will also create a memory leak since you're allocating a block of memory and not recording the pointer */
/* or giving ownership to another object which will delete it */
/* In addition, you have a call to std::pow(2,d) in the constructor there, which is also redundant */
/* Just prior, you set d to 1, which means std::pow(2,d) will always be 2, so just pass a constant value instead */
HTable() :
d(1),
n(0),
t(2),
z(rand() | 1)
{
}
/* Your find function should also be changed to be faster and to better handle user types */
/* Currently, you're using hash to find the list in which x is located, then printing that list to a string stream */
/* then it seems like you're looping through the string stream to search for x */
/* this looks like it would work (it's actually quite clever), but it is much less than ideal */
/* for user-defined types or types like double, this will not work since they are not always a single char long when */
/* printed to a string. a double might be 1.3293 for example */
/* you are also comparing a char to an arbitrary type T, which can be double, int, struct mystruct, etc. */
/* it might work for our specific use-case right here, but once you try to create a hash table of doubles or something */
/* the compiler will complain */
bool find(T x){
std::ostringstream oss;
oss<< t[hash(x)];
for(auto y : oss.str() )
{
if(y == x) return true; //found x
}
return false; //x not found
}
/* Here is a fixed up version of the find function */
/* This actually depends on a function called find in SLList, which will just loop through all the nodes in the list and */
/* return if it found the specified value */
bool find(T x) {
SLList<T> list = t[hash(x)];
return list.find(x);
}
/* Here is how I would implement that find function in SLList */
bool find(T x) {
/* ,-this will create a loop variable node and set it to head */
/* | ,-this will check if node is NOT nullptr before each loop */
/* | | ,-this will set node to the next node after each loop */
/* v v v
for(Node* node = head; node; node = node->next)
{
if(node->x == x)
return true;
}
return false;
}
/* The resize function in HTable doesn't actually resize any arrays */
/* resize seems to create two arrays on the stack, copy one to the other and then return the nth element in b */
/* Below is an example of how I would implement the resize function */
void resize() {
/* First you need to figure out the new size of the array */
z = std::pow(2,n);
Array<SLList<T>> newarray(z);
/* Now, for each item in this hash table, rehash it and insert it into the new array */
for(int i = 0; i < t.getLength(); i++)
{
/* each bucket it a list, so grab it */
SLList<T> list = t[i];
for(int j = 0; j < list.getN(); j++)
{
/* remove the element and get it's value */
T x = list.remove();
/* add it to the list at the bucket */
/* NOTE: the hash function will work here since we resized the hash table earlier */
newarray[hash(x)].Push(x);
}
}
/* Now set the internal array to the new one */
t = newarray;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment