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.
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}?
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
Assuming int is 32 bits, how would you build a hash table for unsigned int a[] = {3, 4294967295, 5, 7, 4294967290}?
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.
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
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.
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.
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
You just need to construct an appropriate hash function. I don't see what is the problem.
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.
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
Same idea as when you want to compute a hash value given a string.
Re: hash table with negative number
Hash function examples. Scroll to the bottom.
Viggy
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
Re: hash table with negative number
One method is to use an array of lists.
Viggy
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
One method is to use an array of lists.
Viggy
Re: hash table with negative number
An array, who's data type is a list:
Code:
std::vector <std::list<foo> >
Viggy
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
An array, who's data type is a list:
Code:
std::vector <std::list<foo> >
Viggy