1.BitMap
BitMap:用1bit 代替一个整数;一种hash的形式,能够是是实现O(1) 查找,以空间换时间的折衷方案;
BitMap.java
public class BitMap {
private long bits;
private int[] map;
public static final long SHIFT = 5;
public static final long MASK = ((1 << SHIFT) -1);
public BitMap(long nbits) {
bits = nbits + 1;
int size = (int) (((bits >>> SHIFT) + 1) & 0x0ffffffffL);
map = new int[size];
for (int i = 0; i < size; i++) {
map[i] = 0;
}
}
public long size() {
return bits;
}
public void set(long index) {
int offset = (int) ((index >>> SHIFT) & 0x0ffffffffL);
map[offset] |= (1 << (index & MASK));
}
public void clear(long index) {
int offset = (int) ((index >>> SHIFT) & 0x0ffffffffL);
map[offset] &= ~(1 << (index & MASK));
}
public boolean isSet(long index) {
int offset = (int) ((index >>> SHIFT) & 0x0ffffffffL);
int off = (int) (index & MASK);
return ((map[offset] >>> off) & 0x01) == 0x1;
}
public static void main(String[] args) {
System.out.println(MASK);
long size = 1L << 32;
System.out.println(size);
BitMap bitMap = new BitMap(size);
System.out.println("bitMap size: " + bitMap.size());
for (long i = 0; i <= bitMap.size(); ++i) {
if (bitMap.isSet(i)) {
System.out.println("Error:@ " + i);
}
}
System.err.println("xxxx");
for (long i = 0; i <= bitMap.size(); i++) {
bitMap.set(i);
if (!bitMap.isSet(i)) {
System.out.println("not set:@ " + i);
}
bitMap.clear(i);
}
System.err.println("xxxx");
for (long i = 0; i <= bitMap.size(); ++i) {
if (bitMap.isSet(i)) {
System.out.println("Error:@ " + i);
}
}
}
}
IpTable.java
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;
public class IpTable {
private BitMap table;
private long ipNum;
private IpTable() {
}
public static IpTable getIpTable(String ipTableFile) {
IpTable ipTable = new IpTable();
ipTable.table = new BitMap(1L << 32);
ipTable.ipNum = 0;
try {
File file = new File(ipTableFile);
InputStreamReader reader = new InputStreamReader(new FileInputStream(file));
BufferedReader br = new BufferedReader(reader);
String line = "";
while ((line = br.readLine()) != null) {
long ip = ipToLong(line);
ipTable.addIp(ip);
}
br.close();
reader.close();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return ipTable;
}
public static long ipToLong(String ip) {
String[] ip_bytes = ip.split("\\.");
return ((Integer.parseInt(ip_bytes[0]) << 24) |
(Integer.parseInt(ip_bytes[1]) << 16) |
(Integer.parseInt(ip_bytes[2]) << 8) |
(Integer.parseInt(ip_bytes[3]) << 0)) & 0x0ffffffffL;
}
public void addIp(long ip) {
if (!table.isSet(ip)) {
ipNum++;
}
table.set(ip);
}
public void removeIp(long ip) {
if (table.isSet(ip)) {
ipNum--;
}
table.clear(ip);
}
public boolean hasIp(long ip) {
return table.isSet(ip);
}
public long getValidIpNum() {
return ipNum;
}
public static void main(String[] args) {
IpTable ipTable = IpTable.getIpTable("ip.txt");
System.out.println("ipTable has valid ip num: " + ipTable.getValidIpNum());
String ip = "192.168.128.10";
if (ipTable.hasIp(IpTable.ipToLong(ip))) {
System.out.println("ip table has ip: " + ip);
}
}
}