一旦阈值超过预定量 0.65,此方法应该调整数组的大小。问题是在调整大小后,析构函数会删除包含所有新复制信息的数组。我不知道如何解决它。
关于那里的一些评论只是我集思广益可能是什么问题。
void Hashtable::insert(int value)
{
int i = 0;
int key = hash(value); // hash(value) % N?
//check load factor to see if it needs to be resized
double q = ((double)(currsize+1) / (double)currcapacity); //calculate current threshold
if ( q >= threshhold) { //if current threshold is >= to 0.65
int newSize = nextPrime(currcapacity * 2); //get new size at the next prime number
Hashtable newtable(newSize); //create a new table; HOW DO I CREATE THIS ON THE HEAP?
for (int j = 0; j < currcapacity; j++) { //runs through table calling insert to insert new items into table
if (htable[j] != EmptyBeforeStart && htable[j] != EmptyAfterRemoval) {
newtable.insert(htable[j]);
}
}
delete[] htable; //delete old table
this->htable = newtable.htable; //re-assign address of htbale to be newtable.htable //THIS ASSINGMENTS GETS DELETED BY THE DESTRUCTOR
this->currcapacity = newSize; //update capacity
this->insert(value);
//THE PROBLEM IS THAT THE DESTRUCTOR GETS CALLED AND DELETES THE htable BECAUSE THE NEWTABLE HTABLE WAS DECLARED AUTOMAITCALLY NOT ON THE HEAP.
}
else {
while (i < currcapacity) {
if (htable[key] == EmptyBeforeStart || htable[key] == EmptyAfterRemoval) {
htable[key] = value;
currsize++;
return;
}
i++;
key = (hash(value) + 0 * i + 1 * i * i) % (int)currcapacity;
}
}
}