OzZy_M

Linear Probing Hashtable Remove Function

Nov 26th, 2011
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.70 KB | None | 0 0
  1. template <class TYPE>
  2. bool LPHash<TYPE>::remove(const char* key) {
  3.   // Variable Declaration & Initialisation
  4.   bool RC = false;
  5.   int nCurrentEmptyPosition;
  6.   //int nKeyExpectedPosition = HashFunction(key, this->m_nAllocatedSpace);
  7.   int nKeyActualPosition = this->getPosition(key);
  8.  
  9.   // Function Main
  10.   if (nKeyActualPosition > -1) {
  11.     // Delete key
  12.     delete this->m_pData[nKeyActualPosition];
  13.     this->m_pData[nKeyActualPosition] = 0;
  14.     this->m_nUsedSpace--;
  15.     RC = true;
  16.     nCurrentEmptyPosition = nKeyActualPosition;
  17.  
  18.     // Swap applicable nodes backwards after key
  19.     for (int i = (nCurrentEmptyPosition + 1) % this->m_nAllocatedSpace;
  20.       i != nKeyActualPosition && this->m_pData[i] != 0;
  21.       i = (i + 1) % this->m_nAllocatedSpace) {
  22.       int nIExpectedPosition = HashFunction(this->m_pData[i]->key(), this->m_nAllocatedSpace);
  23.       if (
  24.         /*
  25.           (nCurrentEmptyPosition >= HashFunction(this->m_pData[i]->key(), this->m_nAllocatedSpace) && i < HashFunction(this->m_pData[i]->key(), this->m_nAllocatedSpace) && i < nCurrentEmptyPosition) ||
  26.           (nCurrentEmptyPosition >= HashFunction(this->m_pData[i]->key(), this->m_nAllocatedSpace) && i > HashFunction(this->m_pData[i]->key(), this->m_nAllocatedSpace) && i > nCurrentEmptyPosition) ||
  27.           (nCurrentEmptyPosition <  HashFunction(this->m_pData[i]->key(), this->m_nAllocatedSpace) && i < HashFunction(this->m_pData[i]->key(), this->m_nAllocatedSpace) && i > nCurrentEmptyPosition)          
  28.        
  29.          
  30.       (HashFunction(this->m_pData[i]->key(), this->m_nAllocatedSpace) > nCurrentEmptyPosition && i > nCurrentEmptyPosition && HashFunction(this->m_pData[i]->key(), this->m_nAllocatedSpace) >= nCurrentEmptyPosition) ||
  31.       (HashFunction(this->m_pData[i]->key(), this->m_nAllocatedSpace) < i && i < nCurrentEmptyPosition && HashFunction(this->m_pData[i]->key(), this->m_nAllocatedSpace) < nCurrentEmptyPosition) ||
  32.       (HashFunction(this->m_pData[i]->key(), this->m_nAllocatedSpace) < i && i > nCurrentEmptyPosition && HashFunction(this->m_pData[i]->key(), this->m_nAllocatedSpace) <= nCurrentEmptyPosition)
  33.  
  34.       (nIExpectedPosition > nCurrentEmptyPosition && i > nCurrentEmptyPosition && nIExpectedPosition >= nCurrentEmptyPosition) ||
  35.       (nIExpectedPosition < i && i < nCurrentEmptyPosition && nIExpectedPosition < nCurrentEmptyPosition) ||
  36.       (nIExpectedPosition < i && i > nCurrentEmptyPosition && nIExpectedPosition <= nCurrentEmptyPosition)
  37.          
  38.  
  39.       (nIExpectedPosition > i && i > nCurrentEmptyPosition && nIExpectedPosition >= nCurrentEmptyPosition) ||
  40.       (nIExpectedPosition < i && i < nCurrentEmptyPosition && nIExpectedPosition < nCurrentEmptyPosition) ||
  41.       (nIExpectedPosition < i && i > nCurrentEmptyPosition && nIExpectedPosition <= nCurrentEmptyPosition)
  42.  
  43.  
  44.       (nCurrentEmptyPosition >= nIExpectedPosition && i < nIExpectedPosition && i < nCurrentEmptyPosition) ||
  45.       (nCurrentEmptyPosition >= nIExpectedPosition && i > nIExpectedPosition && i > nCurrentEmptyPosition) ||
  46.       (nCurrentEmptyPosition <  nIExpectedPosition && i < nIExpectedPosition && i > nCurrentEmptyPosition)
  47.  
  48. */
  49.  
  50.       (nCurrentEmptyPosition >= nIExpectedPosition && i < nIExpectedPosition && i < nCurrentEmptyPosition) ||
  51.       (nCurrentEmptyPosition >= nIExpectedPosition && i > nIExpectedPosition && i > nCurrentEmptyPosition) ||
  52.       (nCurrentEmptyPosition <  nIExpectedPosition && i < nIExpectedPosition && i > nCurrentEmptyPosition)
  53.  
  54.      
  55.       ) {
  56.         this->m_pData[nCurrentEmptyPosition] = this->m_pData[i];
  57.         nCurrentEmptyPosition = i;
  58.         delete this->m_pData[nCurrentEmptyPosition];
  59.         this->m_pData[nCurrentEmptyPosition] = 0;
  60.       }
  61.     }
  62.   }
  63.   return RC;
  64. }
  65.  
  66.  
Advertisement
Add Comment
Please, Sign In to add comment