简体   繁体   English

将IP地址映射到阵列索引

[英]Mapping IP addresses to array index

I am looking for a good way to map IP addresses to indexes in an array. 我正在寻找一种将IP地址映射到数组索引的好方法。 Say I have an array of 32 size and maximum possible IP addresses which we can get as input from user will always be <= 32. I would like to map this IP addresses to some index position in the array and from thereon use this index position to refer to this IP address. 假设我有一个32个大小的数组,并且我们可以从用户输入中获得的最大可能IP地址始终为<=32。我想将此IP地址映射到数组中的某个索引位置,然后使用该索引位置引用此IP地址。

What would be the best technique for doing this. 什么是执行此操作的最佳技术。 IP address is essentially just an integer. IP地址本质上只是一个整数。 Collisions also need to be handled if two addresses map to the same index position. 如果两个地址映射到相同的索引位置,则也需要处理冲突。

ip1 => convert to some integer say i1 => index is now just i1 % 32 ip1 =>转换为整数说i1 =>索引现在只是i1%32

Any hashing technique which fits well into this use-case to convert the IP address would also be useful. 任何非常适合此用例以转换IP地址的哈希技术也将很有用。

Thanks for your help. 谢谢你的帮助。

Are you expecting any hashing algorithm reference for your problem? 您是否希望为您的问题提供任何哈希算法参考?

Since you have array size 32. 由于数组大小为32。

You can just use number of set bits in you IP address to index to the array location. 您可以仅使用IP地址中的设置位数来索引阵列位置。 But all 0 and all 1 are not used addresses so you are at loss of 2 locations. 但是全0和全1都没有使用过,因此您将丢失2个位置。 You can check using sum of all the nibbles ignoring the carry bits for your hashing. 您可以使用所有半字节的总和来检查哈希值,而忽略进位。 You can increase number of bits added based on your array size. 您可以根据阵列大小增加添加的位数。

you might also base your hash on only the 2 bytes of your IP address since the higher order bytes may be same in a single sub-net. 您也可能只将哈希基于IP地址的2个字节,因为单个子网中的高位字节可能相同。

For case of collision you would need to have a pointer array instead of a single dimensional array so you can store multiple addresses with same hash. 在发生冲突的情况下,您将需要一个指针数组而不是一维数组,以便您可以使用相同的哈希存储多个地址。

There cannot be a hash function that makes this possible. 没有哈希函数可以做到这一点。 The hash function takes the ip address only as a param, while you need knowledge of the other elements to accomplish what you want: if you have 32 positions and > 32 possible values for your elements, there must be collisions for any hashfunction. 哈希函数仅将ip地址用作参数,而您需要了解其他元素才能完成所需的工作:如果您拥有32个位置并且元素的可能值> 32,则任何哈希函数都必须存在冲突。

If this was possible, hashmap implementations would not have to consider collisions until the size would be bigger than the max size of the underlying array. 如果可能的话,哈希映射实现将不必考虑冲突,直到其大小大于基础数组的最大大小为止。 And they do consider that :) 他们确实认为:)

It might be helpfull to see how java hashmap maps a hashcode to an index of the underlying array: 查看java hashmap如何将哈希码映射到基础数组的索引可能会有所帮助:

static int  indexFor(int h, int length) {
     return h & (length-1);
 }

If you use this, keep in mind to use powers of 2 to make this effective. 如果使用此功能,请记住使用2的幂以使其生效。 Java starts with 16 and increases to 32, 64 etc. Java从16开始,并增加到32、64等。

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

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