1

With lot of float[] arrays with 2d geometry I'm trying to cache those vertices lists into some keyed map.

For (read)perfromance reasons a key with long or int should be faster than a string one ?

objectmap<key,float[]> 

Is it possible to create an int/long key from values ?

keyForVertices = generateKeyFromVertices(float[]{...})

For the same values in float[] generated key should be unique per float[] values ie.

arrAKey = generate from float[]{-10,10,20,20,30,30} 
arrBKey = generate from float[]{-10,10} 
arrCKey = generate from float[]{10,-10} 

arrAKey!=arrBKey
arrBKey!=arrCKey
arrAKey!=arrCKey

**edit why java.util.Arrays.hashCode(float[]) wont work ?

Paweł
  • 1,632
  • 15
  • 25

2 Answers2

1

You can use Cantor pairing function or Szudzik method. Check this answer. For more than two numbers you can use pairing of pairing function.

Community
  • 1
  • 1
Pavel Parshin
  • 489
  • 4
  • 14
1

Any hashing or checksumming algorithm will give you a stable result (always the same answer for the same content). The quality of the algorithm will determine how many conflicts you have (different content that returns the same result). You could use the standard Java CRC32 checksummer but it really wants bytes, not floats.

I think your suggestion to use the Arrays.hashCode(float[]) will return exactly what you want (a relatively stable int deterministically computed from the content in the given array).

Be aware that if you start caching vertex arrays, you will need to be extra careful if those arrays are mutated or changed in anyway. (The cached entry will need to be invalidated, and you might end up sharing mutations you didn't mean to share.)

P.T.
  • 23,368
  • 7
  • 60
  • 92