Wednesday, July 27, 2005

hash builtin

I think my brain will finally be a non starter.Anyhow here are my thoughts on the hash builtin of bash.

So i browsed haslib.h where HASH_TABLE is defined as

typedef struct hash_table {
BUCKET_CONTENTS **bucket_array; /* Where the data is kept. */
int nbuckets; /* How many buckets does this table have. */
int nentries; /* How many entries does this table have. */
}

Obviously the next question would be what is BUCKET_CONTENTS,remember a hash has buckets..
Well not quite..:) anyways as we have the source with us lets go there ..:)

typedef struct bucket_contents {
struct bucket_contents *next; /* Link to next hashed key in this bucket. */
char *key; /* What we look up. */
PTR_T data; /* What we really want. */
unsigned int khash; /* What key hashes to */
int times_found; /* Number of times this item has been found. */
} BUCKET_CONTENTS;

As we can see hash_table has bucket_array,number of buckets and number of entries.
I have feeling the number of buckets may increase as we need more...
As we look into BUCKET_CONTENTS has a pointer to the next bucket,
a key ( like 'ls'),data (/usr/bin/ls),khash ( i think this is the hashed value),
times_found ( lol..source is enough i guess :) ).

Now lets look at the functions in hashcmd.c

phash_create
phash_freedata
phash_flush
phash_insert
phash_insert
phash_search

phash_create
This in turn calls hash_create (hashlib.c) with appropriate number of buckets (FILENAME_HASH_BUCKETS=64)

phash_search
Probably this is what is interesting,this in turn ccalls hash_search.

bucket= HASH_BUCKET(string,table,hv)

this in turns calls hash_string,the real hashing function.

for (i = 0; *s; s++)
{
i *= 16777619;
i ^= *s;
}

The comment says magic is in the interesting relationship between the special prime 16777619 and 2^32 and 2^8.
As fas as i can tell,2^8 is *s and i is 2^32 :).

HASH_BUCKET ands the value of string with the number of buckets.
I am just thinking of collisions but i guess each bucketjust starts up a link list.

0 Comments:

Post a Comment

<< Home