标签:bsp ddl def ring amp var class 大数据 范围
一:IP转数字,用于比大小,用在求IP段范围中
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 }
二:二分法 查找某一IP段所属范围
def binarySearch(lines: Array[(Long, Long, String)], ip: Long) : Int = { var low = 0 var high = lines.length - 1 while (low <= high) { val middle = (low + high) / 2 if ((ip >= lines(middle)._1) && (ip <= lines(middle)._2)) return middle if (ip < lines(middle)._1) high = middle - 1 else { low = middle + 1 } } -1 }
标签:bsp ddl def ring amp var class 大数据 范围
原文地址:https://www.cnblogs.com/bsyblog/p/9345105.html