Discussion:
Designing a hashing function in C++
(too old to reply)
Jack
2008-06-24 09:49:59 UTC
Permalink
Raw Message
Hi guys,
What is the general approach to design a hashing function without
collisions?

static inline int computeHash(int x,int z)
{
int hash = z<<16|x;
if ( hash == 0 ) hash = 1;
return hash;
}

Is this adequate for most situations?
Thanks
Jack
Igor Tandetnik
2008-06-24 11:53:04 UTC
Permalink
Raw Message
Post by Jack
What is the general approach to design a hashing function without
collisions?
There ain't no such thing. The whole point of a hash function is to map
a large number of possible values to a smaller number of keys. Since
there are more values than keys, you would necessarily have collisions.
Post by Jack
static inline int computeHash(int x,int z)
{
int hash = z<<16|x;
Use XOR (^) instead. With OR, keys with more 1-bits will occur
disproportionately more often than keys with more 0-bits. You want your
hash to be as close to uniformly distributed as possible.

Do you expect z to be small most of the time? If not, don't shift. You
lose high-order bits: if they are significant this would increase the
number of collisions.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925
Jack
2008-06-24 12:03:32 UTC
Permalink
Raw Message
Post by Jack
What is the general approach to design a hashing function without
collisions?
There ain't no such thing. The whole point of a hash function is to map a
large number of possible values to a smaller number of keys. Since there
are more values than keys, you would necessarily have collisions.
which means hash functions can be very general? :)
as I've seen different algorithms to implement a hashing function... correct
me if I am wrong....
Igor Tandetnik
2008-06-24 12:17:25 UTC
Permalink
Raw Message
Post by Jack
Post by Igor Tandetnik
Post by Jack
What is the general approach to design a hashing function without
collisions?
There ain't no such thing. The whole point of a hash function is to
map a large number of possible values to a smaller number of keys.
Since there are more values than keys, you would necessarily have
collisions.
which means hash functions can be very general? :)
I'm not familiar with the term "general function". I don't understand
this question (nor do I get the joke, assuming it was one as the smiley
seems to indicate).
Post by Jack
as I've seen different algorithms to implement a hashing function...
correct me if I am wrong....
How would I know whether or not you have seen different algorithms? If
you say you did, I have no reason to doubt you.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925
Wenwei Peng
2015-04-22 01:26:10 UTC
Permalink
Raw Message
在 2008年6月24日星期二 UTC+8下午5:49:59,Jack写道:
Post by Jack
Hi guys,
What is the general approach to design a hashing function without
collisions?
static inline int computeHash(int x,int z)
{
int hash = z<<16|x;
if ( hash == 0 ) hash = 1;
return hash;
}
Is this adequate for most situations?
Thanks
Jack
Title: The core of the core of the big data solutions -- Map
Author: pengwenwei
Email:
Language: c++
Platform: Windows, linux
Technology: Perfect hash algorithm
Level: Advanced
Description: Map algorithm with high performance
Section MFC c++ map stl
SubSection c++ algorithm
License: (GPLv3)

Download demo project - 1070 Kb
Download source - 1070 Kb

Introduction:
For the c++ program, map is used everywhere.And bottleneck of program performance is often the performance of map.Especially in the case of large data,and the business association closely and unable to realize the data distribution and parallel processing condition.So the performance of map becomes the key technology.

In the work experience with telecommunications industry and the information security industry, I was dealing with the big bottom data,especially the most complex information security industry data,all can’t do without map.

For example, IP table, MAC table, telephone number list, domain name resolution table, ID number table query, the Trojan horse virus characteristic code of cloud killing etc..

The map of STL library using binary chop, its has the worst performance.Google Hash map has the optimal performance and memory at present, but it has repeated collision probability.Now the big data rarely use a collision probability map,especially relating to fees, can’t be wrong.

Now I put my algorithms out here,there are three kinds of map,after the build is Hash map.We can test the comparison,my algorithm has the zero probability of collision,but its performance is also better than the hash algorithm, even its ordinary performance has no much difference with Google.

My algorithm is perfect hash algorithm,its key index and the principle of compression algorithm is out of the ordinary,the most important is a completely different structure,so the key index compression is fundamentally different.The most direct benefit for program is that for the original map need ten servers for solutions but now I only need one server.
Declare: the code can not be used for commercial purposes, if for commercial applications,you can contact me with QQ 75293192.
Download:
https://sourceforge.net/projects/pwwhashmap/files

Applications:
First,modern warfare can’t be without the mass of information query, if the query of enemy target information slows down a second, it could lead to the delaying fighter, leading to failure of the entire war. Information retrieval is inseparable from the map, if military products use pwwhashMap instead of the traditional map,you must be the winner.

Scond,the performance of the router determines the surfing speed, just replace open source router code map for pwwHashMap, its speed can increase ten times.
There are many tables to query and set in the router DHCP ptotocol,such as IP,Mac ,and all these are completed by map.But until now,all map are using STL liabrary,its performance is very low,and using the Hash map has error probability,so it can only use multi router packet dispersion treatment.If using pwwHashMap, you can save at least ten sets of equipment.

Third,Hadoop is recognized as the big data solutions at present,and its most fundamental thing is super heavy use of the map,instead of SQL and table.Hadoop assumes the huge amounts of data so that the data is completely unable to move, people must carry on the data analysis in the local.But as long as the open source Hadoop code of the map changes into pwwHashMap, the performance will increase hundredfold without any problems.


Background to this article that may be useful such as an introduction to the basic ideas presented:
http://blog.csdn.net/chixinmuzi/article/details/1727195

Loading...