In both implementations, the containers have store a raw array of node_type which is essentially a simple linked list that stores a type T.
// From SGI
template <class _Val>
struct _Hashtable_node
{
_Hashtable_node* _M_next;
_Val _M_val;
};
I am implementing my own version for educational reasons and I am wondering why they are are not using std::list<> container for the buckets? Why write code that already exists in a std::list<>?
One reason for this that I came across may be that the std::list<> is doubly linked, so there is wasted space. But what if one uses a single linked list? And why not have the bucket_type a template parameter, so that it can be changed?
The reason for not using a list class [template] for the buckets in these implementation is that they didn’t have a singly linked list and
std::list<T>is doubly linked: the extra cost of the unnecessary back pointer in terms of run-time and size would be considered bad. C++2011 now hasstd::forward_list<T>which pretty much came out of the desire to use it e.g. in a hashed container. This might raise the question: why didn’t they add a singly linked list then? The answer to this is also simple: a singly linked list can’t follow the standard’s container requirements and it was left for later to make the appropriate choices.