Click to See Complete Forum and Search --> : hash table with negative number
LarryChen
July 25th, 2010, 09:51 PM
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.
S_M_A
July 26th, 2010, 03:24 AM
Assuming int is 32 bits, how would you build a hash table for unsigned int a[] = {3, 4294967295, 5, 7, 4294967290}?
LarryChen
July 26th, 2010, 08:38 AM
Actually, my question is how to build a hash table for negative integer. Any idea? Thanks.
Assuming int is 32 bits, how would you build a hash table for unsigned int a[] = {3, 4294967295, 5, 7, 4294967290}?
S_M_A
July 26th, 2010, 12:58 PM
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.
LarryChen
July 26th, 2010, 01:03 PM
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.
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.
laserlight
July 26th, 2010, 02:10 PM
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.
LarryChen
July 26th, 2010, 04:58 PM
How does hash function work with an array since my hash table is implemented on an array?
You just need to construct an appropriate hash function. I don't see what is the problem.
laserlight
July 27th, 2010, 12:52 AM
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.
LarryChen
July 27th, 2010, 01:00 AM
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.
Same idea as when you want to compute a hash value given a string.
MrViggy
July 27th, 2010, 10:15 AM
Hash function (http://en.wikipedia.org/wiki/Hash_function) examples. Scroll to the bottom.
Viggy
LarryChen
August 18th, 2010, 01:52 PM
I know hash function. Which data structure would you use to store the keys and values? Thanks.
Hash function (http://en.wikipedia.org/wiki/Hash_function) examples. Scroll to the bottom.
Viggy
MrViggy
August 18th, 2010, 01:53 PM
One method is to use an array of lists.
Viggy
LarryChen
August 18th, 2010, 02:33 PM
What is array of lists? Would you mind giving me a simple example? Thanks.
One method is to use an array of lists.
Viggy
MrViggy
August 18th, 2010, 02:59 PM
An array, who's data type is a list:
std::vector <std::list<foo> >
Viggy
LarryChen
August 18th, 2010, 03:13 PM
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.
An array, who's data type is a list:
std::vector <std::list<foo> >
Viggy
laserlight
August 19th, 2010, 01:04 AM
Can you use hash function without using any of STL?
Obviously :)
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?
Why not start by taking MrViggy's suggestion of using an array of lists? There is no requirement to use any existing library in that suggestion. It makes sense to use an existing library where it is applicable (and it does not have to be the STL part of the C++ standard library: it could be a library component from whatever programming language that is in use), but if you insist otherwise, just implement what you need.
That is, if you want the functionality of a std::vector<std::list<foo> >, but you insist on not using these containers, then start by implementing your own version of std::vector and std::list.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.