基于BitMap 实现IpTable

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);
      }
    }
  }

}


2. 基于BitMap实现IpTable:


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);
    }
  }

}



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值