DISQUS

DISQUS Hello! Elliott Back's Blog is using DISQUS, a powerful comment system, to manage its comments. Learn more.

Community Page

Jump to original thread »
Author

Hashmap Implementation in C — Elliott C. Back

Started by elliottback · 8 months ago

No excerpt available. Jump to website »

22 comments

  • can you send me sample example on how to use this hashmap ?
  • struct minifile {
        /* Other blocks */
        map_t blocks;
    };

    typedef struct minifile* minifile_t;

    ...

    // Allocate a file
    file = (minifile_t) malloc(sizeof(struct minifile));
    if(!file) return NULL;

    // Initialize blocks
    file->blocks = hashmap_new();
    if (!file->blocks) return NULL;

    ...

    /* Get blocks */
    for(i = 0; i < num_blocks; i++){
        int *temp = (int *) malloc(sizeof(1));
        *temp = *inode; inode++;
        if(hashmap_put(file->blocks, hashmap_length(file->blocks), temp) != MAP_OK)
        return NULL;
    }
  • The above code allocates a datastructure for a filesystem I wrote, allocates the blocks hashmap, which keeps track of all the physical blocks of memory we can use, and then I show a for loop which puts the blocks into the hashmap indexed by the current length of the hashmap, i.e. 0, 1, ... , n. This way I end up with a hashmap of all the blocks currently in use in the filesystem (upon load).
  • Thanks for the example.
    Can we store any type of variable e.g. struture as a value ?
    If yes how can I retrieve the values from the hash map.
    The hashmap_get is returning me junk values.I would appreciate a code sample.
  • As you can tell from the datastructure, you need to be passing pointers to the object memory. If you allocate a static struct "s", to put it in the map you would call the

    hashmap_put(map_t in, int key, any_t value)

    method. You can think of any_t as an alias for void *: it's defined in the header somewhere, So, you'd call:

    hashmap_put(in, 1337, &s;);


    If s is already a pointer type, just call:

    hashmap_put(in, 1337, s);


    To get something out of the datastructure, again just pass a pointer:

    your_struct_name s;
    hashmap_get(in, 1337, &s;);


    Don't forget to check the return values! All the hashmap methods should return a constant indicating success.
  • I think the put function will fail, if you have two key values generating the same hash value in the array range. (say keys "abc" and "xyz" generate a hash code of '99' in the array range 0-1024 (According to the program i think the key value will be '99'..right ??). How will this put function deal with this?? It will overwrite the existing one.

    I think the solution to this is to store even the key values ("abc" or "xyz" ) in the structure hash_element, so that we can compare this while inserting and this must be unique.
  • Hey Sujay. It's not obvious, but the hashmap_put function calls hashmap_hash to find an index. Hashmap_hash uses linear probing to find the next available spot. Of course, if the hashmap is full when you call hashmap_put, a resize call will be issued. Therefore, two keys never "hash" to the same array location.
  • Can you give an example of how to use the iteration function?

    Thanks
  • Can you please attach the sync.h file and any associated implementation file, as I can see many semaphore related calls but have no idea where to find them !
  • Hi Elliott.
    The code looks good and also easly usable but the 2 include files are needed to you this code and hence makes it un-useable. pls provide me those include files as soon as possible.

    Thanks
    Sunil
  • Java Collections Framework provides a well designed set of interfaces and classes that support operations on a collections of objects.
  • Hey, thx a lot, ur code is great, it really helped me alot...but It just took me along time to figure out. but thx so much...
  • it would be better if you gave a more clear example to use the module.
  • Hi all,

    i find this project very interesting. thanks a lot Elliot. But i need the file sync.h. can you provide it to me please.

    regards
  • It's pretty cool to see everybody with interesting names demanding additional work on your part, Mr. Back.
  • I made a improment to your code and it's 100 times faster now. Even 40% faster than the stdext::hash_map in VC2005.

    I will post my code in my blog latter tonight.

    The basic idea is that:
    1. hashmap_get should exit at the first encouter with an empty slot. Or else it will loop through all elements at a miss.

    2. hashmap_remove should rehash the elements between the deleted element and the first empty slot after it.

    3. table_size should always be two times bigger than size.
    This will ensure that there will always be a empty slot not too far way. This will make hashmap_get and hashmap_remove 20 times faster in my case.

    4. some other bug fixes.
  • 厉害,不知道你能不能看懂汉字,cool, good
  • Yo! You really didn't understand the various hash methods (i.e. mod, knuth, etc). I mean, you used them all in your hash function. Read some more :)
  • Hi, Could you please let me know the steps to include n compile in windows ? I am using Win 32 C/C++ compiler comes with Windows SDK.
  • Hello Elliot,

    Thanks for this posting of the code. I was wondering if you could send me the include files so that I get the fully functional code. As part of my project, I am doing a study of some of the implementations for hash table for space consumption check.

    Thanks.

    Regards,
    Kajal
  • Hi Elliot,
    I find the code useful..
    Could you please forward me the include file so that i can do some modifications to it to serve my purpose.

    Thanks...
    Vikas
  • You hashmap overwrites existing values!

Add New Comment

Returning? Login