It is a good description. The writing is rather dry and my car park metaphor
is more interesting.
My writing fluctuates between excessively dry, overly playful, and
just pain awful. ;-) As for the car park metaphor, I don't think it's
appropriate when valets can park cars in the first available slot and
write the slot number on a key tag. Your readers are very likely to
wonder why this particular car park is going to all of that trouble
with registration numbers for something that could be solved with the
equivalent of a list of pointers.
I'm thinking that a library catalogue would make more sense for this
purpose. The Dewey Decimal system is well known (by anyone over 25
most likely) and can act as a gentle introduction into hashing
concepts.
Also I describe how to do deletions with linear probing.
True. I glossed over that technique in favor of what you referred to
as "lazy deletion". I've found marking items as "occupied but deleted"
and deferring physical removal until a more expensive but far less
frequent rebuild more practical. And my tutorials focus heavily on
practicality. ;-)
The code works on global variables. Whilst this is acceptable for
demonstration code, it also tends to encourage bad programming habits.
I have no excuse, but I can defend the decision if you'd like. In
order to keep the code examples short and highly nutritious I cut out
all of the fluff I could manage. That includes showing the definition
of the variables used with as little framework as possible. Now that
I've been outed on clc, I suppose I'll have to add a section about
good practices that covers my butt. ;-)
This looks like a bug to me
In complete examples rather than minimal snippest, I'd agree that it's
an issue. Calling global variables a bug is a tad extreme though. I
welcome any suggestions for improvement, so please don't hesitate to
send me an email if you have any. I want my tutorials to be as
accurate and useful as possible.
(Quote recommended hash table website)
Insertion and deletion are equally simple relative to linear probing:
1void jsw_insert ( void *key, int len )
2...{
3 unsigned h = hash ( key, len ) % N;
4 unsigned step;
5
6 for ( step = 1; table[h] != NULL; ++step )
7 h = ( h + ( step * step - step ) / 2 ) % N;
8
9 table[h] = key;
10}
1void jsw_remove ( void *key, int len )
2...{
3 unsigned h = hash ( key, len ) % N;
4 unsigned step;
5
6 for ( step = 1; table[h] != NULL; ++step )
7 h = ( h + ( step * step - step ) / 2 ) % N;
8
9 table[h] = DELETED;
10}
You need to call the comparison function and break if equal. As it is the
code marks a null at the end of the cluster as deleted.
Ouch. That's what I get for writing on autopilot. I do the same thing
for all of the probing examples. How embarrassing. I'll fix it
straight away.