def ip2Long(ip: String): Long = {
val fragments = ip.split("[.]")
var ipNum = 0L
for (i <- 0 until fragments.length){
ipNum = fragments(i).toLong | ipNum << 8L
}
ipNum
}
二分法查找:
def binarySearch(arr: Array[(String, String, String, String)], ip: Long): Int =
{ var l = 0
var h = arr.length - 1
while (l <= h) {
var m = (l + h) / 2
if ((ip >= arr(m)._1.toLong) && (ip <= arr(m)._2.toLong))
{
return m
} else if (ip < arr(m)._1.toLong)
{
h = m - 1
}
else
{
l = m + 1
}
}
-1
}