Implementing C ++ hash tables (insert and lazy delete)

I am implementing a Hashtable class in C ++. The collision resolution method I use is linear sensing with lazy deletion. I saw implementations of this, but had a question regarding the insertion method. Each hash table cell has a state (active, remote, empty). For some reason, in the implementation that I saw when inserting a new element, they hash the key and then examine the table until the EMPTY cell is found (or until the cell containing the same key is found).

Code example:

int findPos(const string &key){
     int currentPos=hash(key);
     while(data[currentPos].state!=EMPTY && data[currentPos].key!=key){
         currentPos++;
         if (currentPos>=data.size())
            currentPos-=data.size()
         }
      return currentPos;
}

bool insert(const string &key){
     int currentPos=findPos(key);
     if (isActive(currentPos))
          return false; //already exists
     data[currentPos]=hashEntry(key,ACTIVE);
     if (++currentSize>data.size()/2)
          rehash();
     return true;   //element inserted
}

: , ? , findPos while while(data[currentPos].state==ACTIVE && data[currentPos].key!=key) , , / . . , , false; .

+5
2

, . .

+3

, , DELETED . , "" . , , , @Thomas.

0

All Articles