Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript calculate hashcode from real number and integer number

Hi there I need function to calculate unique integer number from number (real number double precision) and integer.

Try explain I am developing GIS application in javascript and I am working with complex vector object like polygon (array of points object with two coordinate in ring) and lines array of points. I need fast algorithm to recognize that element has been changed it must be really fast because my vector object is collection of thousand points . In C# I am calculating hash code from coordinate using bitwise operation XOR.

But javascript convert all operands in bitwise operation to integer but i need convert double precision to integer before apply bitwise in c# way (binnary). In reflector i see this that c# calculate hash code fro double like this and I need this function in javascript as fast as can be.

public override unsafe int GetHashCode() //from System.Double
{
    double num = this;
    if (num == 0.0)
    {
        return 0;
    }
    long num2 = *((long*) &num);
    return (((int) num2) ^ ((int) (num2 >> 32)));
}

Example:

var rotation = function (n) {
    n = (n >> 1) | ((n & 0x001) << 31);
    return n;
}

var x: number = 1;
var y: number = 5;

var hash = x ^ rotation(y); // result is -2147483645

var x1: number = 1.1;
var y1: number = 5;

var hash1 = x1 ^ rotation(y1); // result is -2147483645

Example result is not correct hash == hash1

Example 2: Using to string there is correct result but calculate Hash from string is to complicate and I thing is not fast enough.

    var rotation = function (n) {
        n = (n >> 1) | ((n & 0x001) << 31);
        return n;
    }

     var GetHashCodeString = function(str: string): number {
        var hash = 0, i, l, ch;
        if (str.length == 0) return hash;
        for (i = 0, l = str.length; i < l; i++) {
            ch = str.charCodeAt(i);
            hash = ((hash << 5) - hash) + ch;
            hash |= 0; // Convert to 32bit integer
        }
        return hash;
     }

    var x: number = 1;
    var y: number = 5;

    var hash = GetHashCodeString(x.toString()) ^ rotation(GetHashCodeString(y.toString()));
    //result is -2147483605
    var x1: number = 1.1;
    var y1: number = 5;

    var hash1 = GetHashCodeString(x1.toString()) ^ rotation(GetHashCodeString(y1.toString()));
   //result is -2147435090

Example2 result is correct hash != hash1

Is there some faster way than converting number to string than calculate hash from each character? Because my object is very large and it will take lot of time and operation in this way ...

I try do it using TypedArrays but yet I am not successful.

Thanks very much for your help

like image 994
Ivan Mjartan Avatar asked Sep 28 '22 11:09

Ivan Mjartan


2 Answers

Hi there I tried use TypedArrays to calculate Hash code from number and the result is interesting. In IE the performance 4x better in Chrome 2x in FireFox this approach is equal to string version ...

var GetHashCodeNumber = function (n: number): number {
         //create 8 byte array buffer number in js is 64bit 
         var arr = new ArrayBuffer(8);

         //create view to array buffer
         var dv = new DataView(arr);

         //set number to buffer as 64 bit float  
         dv.setFloat64(0, n);

         //now get first 32 bit from array and convert it to integer
         // from offset 0
         var c = dv.getInt32(0);

         //now get next 32 bit from array and convert it to integer 
         //from offset 4 
         var d = dv.getInt32(4);

         //XOR first end second integer numbers 
         return c ^ d;
     }

I think this can be useful for someone

EDIT: using one buffer and DataView is faster !

like image 114
Ivan Mjartan Avatar answered Nov 02 '22 06:11

Ivan Mjartan


Here is a faster way to do this in JavaScript.

const kBuf = new ArrayBuffer(8);
const kBufAsF64 = new Float64Array(kBuf);
const kBufAsI32 = new Int32Array(kBuf);

function hashNumber(n) {
  // Remove this `if` if you want 0 and -0 to hash to different values.
  if (~~n === n) {
    return ~~n;
  }
  kBufAsF64[0] = n;
  return kBufAsI32[0] ^ kBufAsI32[1];
}

It's 250x faster than the DataView approach: see benchmark.

like image 35
glebm Avatar answered Nov 02 '22 05:11

glebm