Thread: Hashing In C
View Single Post
Old Jan 9th, 2008, 7:24 AM   #5
Narue
Professional Programmer
 
Narue's Avatar
 
Join Date: Sep 2005
Posts: 419
Rep Power: 4 Narue is on a distinguished road
Re: Hashing In C

>you should just use c++ and take advantage of the stl
C++0x hasn't been ratified yet, so any use of hash tables (unordered_list) or anything like it would be risking no support by the compiler.

>but only if it's our own
The problem with hashing is that if you make something up, it's highly likely to suck. That's why most wise programmers use existing algorithms. You can still use the algorithm without using someone else's code, but with the one I'm suggesting it's pretty hard to "personalize" something so simple.

>so it has to be something compact
A good, fast, and small algorithm is Bernstein's hash. It's so short that it's nearly impossible to forget, and it has surprisingly good properties:
c Syntax (Toggle Plain Text)
  1. unsigned djb_hash ( void *key, int len )
  2. {
  3. unsigned char *p = key;
  4. unsigned h = 0;
  5. int i;
  6.  
  7. for ( i = 0; i < len; i++ )
  8. h = 33 * h ^ p[i];
  9.  
  10. return h;
  11. }
__________________
Even if the voices aren't real, they have some pretty good ideas.
Narue is offline   Reply With Quote