An associative container

J

jacob navia

Moi a écrit :
jacob navia said:
Ben Pfaff a écrit :

Errors: None. Note that this function will NOT work for
signed characters. The input MUST be unsigned
------------------------------------------------------------------------*/
static unsigned hash(unsigned char *str) {
unsigned int h = scatter[*str];
str++;
while(*str) {
h = (h >> 1) ^ scatter[*str++];
}
return h;
}
Why will this function not work for signed characters?
because if the character is negative, the indexing of the array will
not work OK.
But you access the characters as "unsigned char". How can there be a
problem, then?


The (unsigned char *) argument is strange.
It will force you to cast the arguments to this function *on every call*
An easier way would be to use a (void*) argument and cast it to
(unsigned char*) *inside the function*

For hashfunctions I normally use the form

****/
unsigned hash_da_thing(void * ptr, size_t siz)
{
unsigned hash;
unsigned char * cp= ptr;

if (!siz) siz = strlen(cp);

for(hash=0 ; siz>0; )
{ ... }
...
return hash;
}

****/

The hash table container will be designed as a general hash table with a key
of arbitrary binary data. The dictionary container is a "light" version of it
that associates text keys with arbitrary data. The hash table container will have
automatic resizing and all the needed features of a general hash table. I am
still working on it.

; this will allow non-string to be hashed, by providing a non-null size;
(plus: add some "const" ;-)

I think the Zobrist hashing is awkward, too.

Can you elaborate? Why is "awkward"?
It should make the hash code independent of the character frequency.
 
P

Phil Carmody

Ben Pfaff said:
Phil Carmody said:
jacob navia said:
/*------------------------------------------------------------------------
Module: dictionary.c
Author: jacob
[SNIP - crap code]

Not a const in sight. You are pitifully ill-equipped to be writing
C. Take up knitting instead, perhaps.

It's a "first try" according to the comments. In my own code, at
least, I often don't worry about const on the first try, and then
I go back later and sprinkle it in as appropriate.

I'm quite the opposite.

Phil
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top