1

I am looking for a quick hash algorithm, that is not used for cryptography, but give me better coverage than the standard GetHashCode().

I have seen CityHash but can't see a C# port.

Ideally, I would like to specify the hash size (64 bit, 128 bit, etc....), but this is not a requirement.

Any ideas?

leppie
  • 115,091
  • 17
  • 196
  • 297
MartinHT
  • 289
  • 1
  • 9
  • 2
    What do you mean by "better coverage"? What will this be used for? How is your data distributed? Do you want to be reasonably sure that similar inputs will give very different outputs? – Mark Byers May 28 '12 at 14:28
  • Duplicate: http://stackoverflow.com/questions/2062596/fast-hashcode-of-a-complex-object-graph – Abdul Munim May 28 '12 at 14:29
  • So you do not intend to override `GetHashCode()` for your own implementation but use is for something else? – Magnus May 28 '12 at 14:33
  • 1
    [What have you tried?](http://www.whathaveyoutried.com) – Agnius Vasiliauskas May 28 '12 at 14:33
  • What are you hashing? How can you hash something by value if you don't know what you're hashing? I'm tempted to suggest that you use Random for hashing. That certainly provides good coverage. – GregRos May 28 '12 at 14:53
  • By 'better coverage' I mean that with an int, I can only aspire to 65768 unique values, and I need more. The most important issue for me is uniqueness, then speed. With 64 bits, I would have enough values. I don't intend to override GetHashCode(). I am hashing in-memory objects with between 3 and 8 properties that should make up the 'uniqueness', some properties are strings, others are integers, and others are doubles or decimals. – MartinHT May 28 '12 at 17:05
  • Martin, int is 32-bit, not 16-bit, so you have 4294967296 possible values. – Andrey Tarantsov Feb 12 '13 at 03:22

2 Answers2

1

CityHash C# port

https://github.com/gmarz/CityHash

Usage:

using CityHash;

ulong hash = "...".GetCityHash64();

You'll need to add a Reference to CityHash.dll from the main project, and ensure that CityHash.Win32.dll, built from the C++ portion of the project, is available in the bin folder of your project.

For me, using git, that's a major nuisance - I don't want to have to check in anything in my bin folder. So, I put the Win32 dll in my lib folder and added this Pre-build event:

start /MIN xcopy /y /c lib\CityHash.Win32.dll bin

That ensures it copies in, even for someone freshly cloning and building, without having to take the bin folder off of the .gitignore list.

Note also that as far as I can tell CityHash's C++ portion will not build in Release mode in Visual Studio - Debug only.

Chris Moschini
  • 36,764
  • 19
  • 160
  • 190
-2

You may try something like this : For a 64 bits hash

bit Data[64*L] ; // Your input filled to be divided by 64 
bit Arr[64] ;    // eg. initially filled with '0' 
for(int k=0 ; k<L ; k++){
    for(int i=0 ; i<64 ; i++){
        Arr[i]=Arr[i]^data[k*64+i];  // '^' as the XOR operator
    }
}

Arr[] will get a 64bit fast and efficient hash.