Hashtable/map with an object as a key

posted 15 Sep 2011, 00:26 by Masahiro Takatsuka
To efficiently store 3D float array, I tried to use a hashtable [new Array() or new Object()]:

var hash = new Array(); (or new Object())
var key = new SomeObject();
var value = 3.4.5;
if (!hash[key])
    hash[key] = value;

However,  I'm dynamically configure what type of Array to be used to store 3D vectors and matrix : 

if (typeof DoubleArray != 'undefined') {
glMatrixArray = DoubleArray;
} else if (typeof Float64Array != 'undefined') {
glMatrixArray = Float64Array;
} else if (typeof FloatArray != 'undefined') {
glMatrixArray = FloatArray
} else if(typeof WebGLFloatArray != 'undefined') {
glMatrixArray = WebGLFloatArray;
} else {
glMatrixArray = Array;
}

With Safari 5.1.1 on OS X Lion, glMatrixArray == Array; and you can use the Array object as a key for the hashtable.  However, with Chrome 13.0.782.220 on OS X Lion, glMatrixArray == Float64Array and you cannot use Float64Array object as a key.  
So, I now use goog.getHashCode(obj) (from Google Closure Library) to get a unique hashcode.
Comments