CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Join Date
    Jul 2005
    Posts
    1,030

    hash table with negative number

    Here when I talk about hash table, I mean it is simply an array and it has nothing to do with STL hash map. For example, there is an array, int a[] = {3, 5,6, 7, 11}. The key for hash table could be the elements of array a. What if there is negative item in the array, for example, int a[] = {3, -1, 5, 7, -6}? How'd I build a hash table based on this array? Thanks.

  2. #2
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: hash table with negative number

    Assuming int is 32 bits, how would you build a hash table for unsigned int a[] = {3, 4294967295, 5, 7, 4294967290}?
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  3. #3
    Join Date
    Jul 2005
    Posts
    1,030

    Re: hash table with negative number

    Actually, my question is how to build a hash table for negative integer. Any idea? Thanks.
    Quote Originally Posted by S_M_A View Post
    Assuming int is 32 bits, how would you build a hash table for unsigned int a[] = {3, 4294967295, 5, 7, 4294967290}?

  4. #4
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: hash table with negative number

    Well a negative number is just a positive number interpreted as being negative. I.e. a 32 bit signed integer set to -1 will have the same content as an unsigned int set to 4294967295 (0xFFFFFFFF). That's why I asked you how you would handle those numbers instead.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  5. #5
    Join Date
    Jul 2005
    Posts
    1,030

    Re: hash table with negative number

    Obviously it is not good to handle a negative numbge with a 2's complement because it will use too much space to build a hashtable. Basically I need a way to map a key as a negative number to a value in such a hash table.
    Quote Originally Posted by S_M_A View Post
    Well a negative number is just a positive number interpreted as being negative. I.e. a 32 bit signed integer set to -1 will have the same content as an unsigned int set to 4294967295 (0xFFFFFFFF). That's why I asked you how you would handle those numbers instead.

  6. #6
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: hash table with negative number

    Quote Originally Posted by LarryChen
    Obviously it is not good to handle a negative numbge with a 2's complement because it will use too much space to build a hashtable. Basically I need a way to map a key as a negative number to a value in such a hash table.
    You just need to construct an appropriate hash function. I don't see what is the problem.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  7. #7
    Join Date
    Jul 2005
    Posts
    1,030

    Re: hash table with negative number

    How does hash function work with an array since my hash table is implemented on an array?
    Quote Originally Posted by laserlight View Post
    You just need to construct an appropriate hash function. I don't see what is the problem.

  8. #8
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: hash table with negative number

    Quote Originally Posted by LarryChen
    How does hash function work with an array since my hash table is implemented on an array?
    Same idea as when you want to compute a hash value given a string.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  9. #9
    Join Date
    Jul 2005
    Posts
    1,030

    Re: hash table with negative number

    Would you please tell me how to compute a hash value given a string? Here hash table is implemented as an array and it has nothing to do with stl hash map. Thanks.
    Quote Originally Posted by laserlight View Post
    Same idea as when you want to compute a hash value given a string.

  10. #10
    Join Date
    Feb 2002
    Posts
    4,640

    Re: hash table with negative number

    Hash function examples. Scroll to the bottom.

    Viggy

  11. #11
    Join Date
    Jul 2005
    Posts
    1,030

    Re: hash table with negative number

    I know hash function. Which data structure would you use to store the keys and values? Thanks.
    Quote Originally Posted by MrViggy View Post
    Hash function examples. Scroll to the bottom.

    Viggy

  12. #12
    Join Date
    Feb 2002
    Posts
    4,640

    Re: hash table with negative number

    One method is to use an array of lists.

    Viggy

  13. #13
    Join Date
    Jul 2005
    Posts
    1,030

    Re: hash table with negative number

    What is array of lists? Would you mind giving me a simple example? Thanks.
    Quote Originally Posted by MrViggy View Post
    One method is to use an array of lists.

    Viggy

  14. #14
    Join Date
    Feb 2002
    Posts
    4,640

    Re: hash table with negative number

    An array, who's data type is a list:
    Code:
    std::vector <std::list<foo> >
    Viggy

  15. #15
    Join Date
    Jul 2005
    Posts
    1,030

    Re: hash table with negative number

    Can you use hash function without using any of STL? I repeat my original quesiton. There is an array, int a[] = {3, -1, 5, 7, -6}. How'd I build a hash table based on this array without using any of STL? Thanks.
    Quote Originally Posted by MrViggy View Post
    An array, who's data type is a list:
    Code:
    std::vector <std::list<foo> >
    Viggy

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured