爲了使我搜索foreach「symbol」我想從我的hashTable中刪除,我選擇了生成我插入它的hashkey。但是,我看到在我的刪除功能的問題是,當我需要從發現碰撞的地方刪除一個符號它以前導致我的while循環條件測試錯誤,我不想。刪除C++時的hashkey碰撞
bool hashmap::get(char const * const symbol, stock& s) const
{
int hash = this->hashStr(symbol);
while (hashTable[hash].m_symbol != NULL)
{ // try to find a match for the stock associated with the symbol.
if (strcmp(hashTable[hash].m_symbol , symbol) == 0)
{
s = &hashTable[hash];
return true;
}
++hash %= maxSize;
}
return false;
}
bool hashmap::put(const stock& s, int& usedIndex, int& hashIndex, int& symbolHash)
{
hashIndex = this->hashStr(s.m_symbol); // Get remainder, Insert at that index.
symbolHash = (int&)s.m_symbol;
usedIndex = hashIndex;
while (hashTable[hashIndex].m_symbol != NULL) // collision found
{
++usedIndex %= maxSize; // if necessary wrap index around
if (hashTable[usedIndex].m_symbol == NULL)
{
hashTable[usedIndex] = s;
return true;
}
else if (strcmp(hashTable[usedIndex].m_symbol , s.m_symbol) == 0)
{
return false; // prevent duplicate entry
}
}
hashTable[hashIndex] = s; // insert if no collision
return true;
}
// What if I need to remove an index i generate?
bool hashmap::remove(char const * const symbol)
{
int hashVal = this->hashStr(symbol);
while (hashTable[hashVal].m_symbol != NULL)
{
if (strcmp(hashTable[hashVal].m_symbol, symbol) == 0)
{
stock temp = hashTable[hashVal]; // we cansave it
hashTable[hashVal].m_symbol = NULL;
return true;
}
++hashVal %= maxSize; // wrap around if needed
} // go to the next cell meaning their was a previous collision
return false;
}
int hashmap::hashStr(char const * const str)
{
size_t length = strlen(str);
int hash = 0;
for (unsigned i = 0; i < length; i++)
{
hash = 31 * hash + str[i];
}
return hash % maxSize;
}
我需要做什麼才能從前一次碰撞中從我的hashTable中刪除「符號」? 我希望這不是直接在上面的Java公式。