简体   繁体   English

用于为float []中的值生成相同的唯一ID /哈希的算法

[英]Algorithm for generating the same unique ID/Hash as long for values in float[]

With lot of float[] arrays with 2d geometry I'm trying to cache those vertices lists into some keyed map. 对于很多具有2d几何形状的float []数组,我试图将这些顶点列表缓存到某些键控贴图中。

For (read)perfromance reasons a key with long or int should be faster than a string one ? 出于(读取)性能原因,具有long或int的键应该比字符串之一快。

objectmap<key,float[]> 

Is it possible to create an int/long key from values ? 是否可以根据值创建int / long键?

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

For the same values in float[] generated key should be unique per float[] values ie. 对于float []中的相同值,对于每个float []值,生成的键应该是唯一的,即。

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 ? **编辑为什么java.util.Arrays.hashCode(float [])无法正常工作?

You can use Cantor pairing function or Szudzik method. 您可以使用Cantor配对功能或Szudzik方法。 Check this answer . 检查此答案 For more than two numbers you can use pairing of pairing function. 对于两个以上的数字,您可以使用配对功能。

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. 您可以使用标准的Java CRC32校验和,但它确实需要字节而不是浮点数。

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). 我认为您对使用Arrays.hashCode(float[])将完全返回您想要的内容(根据给定数组中的内容确定地计算出的相对稳定的int)。

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.) (缓存的条目将需要无效,并且您可能最终共享了您本不想共享的突变。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM