I have been struggling to understand how this works for a while in gdb but I’m having a hard time understanding it. Basically there is an array (elements_) into which things are hashed, these things are pointers to structs containing certain hooks used for the chaining. Here is a sample struct:
struct foo
{
Data data;
foo* next;
foo** prevNext;
};
then the hash table gets instantiated with
HashTable<foo> hash;
and it’s insertion function looks like this (i’ve ommitted the resizing and all for simplicity)
template <class T>
void HashTable<T>::insert(T* x)
{
int bucket = hashFunc(x->data);
T** xPtr = &elements_[bucket];
x->next = *xPtr;
x->prevNext = xPtr;
if (x->next)
x->next->prevNext = &x->next;
*xPtr = x;
}
and the removal is as follows
template<class T>
void HashTable<T>::remove(T* x)
{
if (x->next)
x->next->prevNext = x->prevNext;
*x->prevNext = x->next;
}
And for reference the way it is searched is like this:
template<class T>
T* HashTable<T>::find(Data& data)
{
int bucket = hashFunc(data);
T* ptr = elements_[bucket];
while (ptr != 0)
{
if(ptr->data == data)
return ptr;
ptr = ptr->next;
}
return 0;
}
I keep trying to follow insertion of 2-3 colliding elements (by setting the table size to be small to begin with) and deletions to see what the logic is but I’m not understanding it. Here is a diagram of what I think the removal operation (removing node 2) is doing, though it’s still a bit fuzzy in my head:

Elements that fall into the same bucket are stored as a doubly-linked list (sort of).
Lookup traverses that list until it finds an element with matching
datavalue.