数据结构---位图算法


这里所说的位图并不是像素图片的位图,而是内存中连续的二进制位(bit)所组成的数据结构, 该算法主要用于对大量整数做去重和查询操作。

		System.out.println(1L <<0);
        System.out.println(1L <<1);
        System.out.println(1L <<2);
        System.out.println(1L <<3);
        System.out.println(1L <<4);
        System.out.println(1L <<5);
        System.out.println(1L <<6);
        System.out.println(1L <<64);
        System.out.println(1L <<65);
        System.out.println(1L <<67);

在这里插入图片描述
在这里插入图片描述

将数据存储到位图中

就是将Bitmap的对应位置设为1

  1. 定位到long[]数组中的对应的元素。
  2. 通过与运算修改long元素的值。
    /**
     * 定位Bitmap某一位所对应的word(long[] 类型)
     * @param bitIndex    位图的第bitIndex
     * @return
     */
    private int getWordIndex(int bitIndex){
        ///右移6位,相当于除以64
        //每一个word是一个long类型元素,对应一个64位二进制数据
        return bitIndex>>6;
    }

    /**
     * 把Bitmap某一位设置为true
     * @param bitIndex  位图的第bitIndex位
     */
    public void setBit(int bitIndex){
        if(bitIndex<0||bitIndex>size-1){
            throw new IndexOutOfBoundsException("超过bitmap的有效范围");
        }
        int wordIndex = getWordIndex(bitIndex);
        words[wordIndex]=words[wordIndex]|(1L<<bitIndex);
    }

在这里插入图片描述

查询数据是否在位图中

  1. 定位到long[]数组中的对应的元素。
  2. 判断long元素的对应位置的二进制位是否为1。
/**
     * 定位Bitmap某一位所对应的word(long[] 类型)
     * @param bitIndex    位图的第bitIndex
     * @return
     */
    private int getWordIndex(int bitIndex){
        ///右移6位,相当于除以64
        //每一个word是一个long类型元素,对应一个64位二进制数据
        return bitIndex>>6;
    }

/**
     * 判断Bitmap某一位的状态
     * @param bitIndex 位图的第bitIndex位
     * @return
     */
    public boolean getBit(int bitIndex){
        if(bitIndex<0||bitIndex>size-1){
            throw new IndexOutOfBoundsException("超过bitmap的有效范围");
        }
        //bitIndex转words数组的下标
        int wordIndex = getWordIndex(bitIndex);
        return (words[wordIndex]&(1L<<bitIndex))!=0;
    }

在这里插入图片描述

如果存在,则与运算的结果非0,返回True
否则结果为0,返回false

JAVA代码

public class MyBitmap {
    // 每一个word是一个long类型元素,对应一个64位二进制数据
    private long[] words;
    //Bitmap的位数大小
    private int size;

    public MyBitmap(int size) {
        this.size = size;
        //把Bitmap某一位所对应的word,初始化long[] words数组
        this.words = new long[getWordIndex(size-1)+1];
    }

    /**
     * 定位Bitmap某一位所对应的word(long[] 类型)
     * @param bitIndex    位图的第bitIndex
     * @return
     */
    private int getWordIndex(int bitIndex){
        ///右移6位,相当于除以64
        //每一个word是一个long类型元素,对应一个64位二进制数据
        return bitIndex>>6;
    }

    /**
     * 把Bitmap某一位设置为true
     * @param bitIndex  位图的第bitIndex位
     */
    public void setBit(int bitIndex){
        if(bitIndex<0||bitIndex>size-1){
            throw new IndexOutOfBoundsException("超过bitmap的有效范围");
        }
        int wordIndex = getWordIndex(bitIndex);
        words[wordIndex]=words[wordIndex]|(1L<<bitIndex);
    }

    /**
     * 判断Bitmap某一位的状态
     * @param bitIndex 位图的第bitIndex位
     * @return
     */
    public boolean getBit(int bitIndex){
        if(bitIndex<0||bitIndex>size-1){
            throw new IndexOutOfBoundsException("超过bitmap的有效范围");
        }
        //bitIndex转words数组的下标
        int wordIndex = getWordIndex(bitIndex);
        return (words[wordIndex]&(1L<<bitIndex))!=0;
    }

}

测试类如下:

public class testMyBitmap {
    public static void main(String[] args) {
        MyBitmap bitmap = new MyBitmap(128);
        bitmap.setBit(126);
        bitmap.setBit(75);
        System.out.println(bitmap.getBit(126));
        System.out.println(bitmap.getBit(78));

    }

}

在这里插入图片描述

查找的时间复杂度为O(1)
存储数据的空间复杂度较之前减少了1/32

问题扩展

给定 40 亿个不重复的没排过序的 unsigned int 型整数,然后再给定一个数,如何快速判断这个数是否在这 40 亿个整数当中?

40 亿个不重复整数,我们用 40 亿个 bit 来表示,初始位均为 0,那么总共需要内存:4,000,000,000b≈512M。
我们读取这 40 亿个整数,将对应的 bit 设置为 1。接着读取要查询的数,查看相应位是否为 1,如果为 1 表示存在,如果为 0 表示不存在。

总结:判断数字是否存在、判断数字是否重复的问题,位图法是一种非常高效的方法。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值