how to implement exact C# hashcode in Java -


i have piece of code generate signature in c#, , sake of convenience, used hashcode , fine.

however, boss told me signature has generated in java side too. drives me crazy. , digged .net source code.

currently, need hashcode of int, double, string , bool. int , bool easy. real thing can't think of easy way double , string. environment 64-bit. have source in following:

for string:

        public override int gethashcode() {  #if feature_randomized_string_hashing             if(hashhelpers.s_userandomizedstringhashing)             {                 return internalmarvin32hashstring(this, this.length, 0);             } #endif // feature_randomized_string_hashing              unsafe {                 fixed (char *src = this) {                     contract.assert(src[this.length] == '\0', "src[this.length] == '\\0'");                     contract.assert( ((int)src)%4 == 0, "managed string should start @ 4 bytes boundary");  #if win32                     int hash1 = (5381<<16) + 5381; #else                     int hash1 = 5381; #endif                     int hash2 = hash1;  #if win32                     // 32 bit machines.                     int* pint = (int *)src;                     int len = this.length;                     while (len > 2)                     {                         hash1 = ((hash1 << 5) + hash1 + (hash1 >> 27)) ^ pint[0];                         hash2 = ((hash2 << 5) + hash2 + (hash2 >> 27)) ^ pint[1];                         pint += 2;                         len  -= 4;                     }                      if (len > 0)                     {                         hash1 = ((hash1 << 5) + hash1 + (hash1 >> 27)) ^ pint[0];                     } #else                     int     c;                     char *s = src;                     while ((c = s[0]) != 0) {                         hash1 = ((hash1 << 5) + hash1) ^ c;                         c = s[1];                         if (c == 0)                             break;                         hash2 = ((hash2 << 5) + hash2) ^ c;                         s += 2;                     } #endif #if debug                     // want ensure can change our hash function daily.                     // fine long don't persist                     // value gethashcode disk or count on string                      // hashing before string b.  bugs in code.                     hash1 ^= thisassembly.dailybuildnumber; #endif                     return hash1 + (hash2 * 1566083941);                 }             }         } 

i not sure feature_randomized_string_hashing(i guess it's not set though), , pointer casting here:

int* pint = (int *)src; 

doesn't sound straightforward in java.

for double:

public unsafe override int gethashcode() {     double d = m_value;     if (d == 0) {         // ensure 0 , -0 have same hash code         return 0;     }     long value = *(long*)(&d);     return unchecked((int)value) ^ ((int)(value >> 32)); } 

the same issue. there pointer casting, reference , dereference.

how can in java(no native code)?

i wonder if aren't making more complicated needs whole unsafe section , pointers. why don't start solution in java port c#.

i bet there bunch of solutions on net coming hash in java, , port java c# should trivial.

edit: in fact, looked you: good hash function strings

please don't assume pointers necessary performance either--using pointers stops compiler optimizations causing code slower if you'd used arrays/strings java solutions above.

in response comment: if want same function between c# , java need solution doesn't use pointers. solution perform or better anyway (because compiler has more freedom when optimizing it) , more readable if want use solution recode without pointers first use in both c# , java versions.

if can't recode in primary language--c#--you won't able in java.

maintain compatibility having unit test coverage, if don't have enough unit tests now, write them before making changes--if test existing hash codes (you appear persisting them somewhere) might able write c# tests test both c# , java hash codes prove current effort successful.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -