首页 > 编程语言 >BitSet的源码研究

BitSet的源码研究

时间:2023-06-08 21:08:19浏览次数:54  
标签:bitIndex 研究 wordIndex int 源码 BitSet words bit


这几天看Bloom Filter,因为在java中,并不能像C/C++一样直接操纵bit级别的数据,所以只能另想办法替代:

1)使用整数数组来替代;

2)使用BitSet;

BitSet实际是由“二进制位”构成的一个Vector。如果希望高效率地保存大量“开-关”信息,就应使用BitSet。它只有从尺寸的角度看才有意义;如果希望的高效率的访问,那么它的速度会比使用一些固有类型的数组慢一些。

BitSet的大小与实际申请的大小并不一定一样,BitSet的size方法打印出的大小一定是64的倍数,这与它的实际申请代码有关,假设以下面的代码实例化一个BitSet:

BitSet set =            new            BitSet(           129           );



我们来看看实际是如何申请的:申请源码如下:



/**          


                      * Creates a bit set whose initial size is large enough to explicitly          


                      * represent bits with indices in the range <code>0</code> through          


                      * <code>nbits-1</code>. All bits are initially <code>false</code>.          


                      *          


                      * @param     nbits   the initial size of the bit set.          


                      * @exception NegativeArraySizeException if the specified initial size          


                      *               is negative.          


                      */          


                      public            BitSet(           int            nbits) {          


                      // nbits can't be negative; size 0 is OK          


                      if            (nbits <            0           )          


                      throw            new            NegativeArraySizeException(           "nbits < 0: "            + nbits);          


                      


                      initWords(nbits);          


                      sizeIsSticky =            true           ;          


                      }          


                      


                      private            void            initWords(           int            nbits) {          


                      words =            new            long           [wordIndex(nbits-           1           ) +            1           ];          


                      }



实际的空间是由initWords方法控制的,在这个方法里面,我们实例化了一个long型数组,那么wordIndex又是干嘛的呢?其源码如下:



/**          


                      * Given a bit index, return word index containing it.          


                      */          


           private            static            int            wordIndex(           int            bitIndex) {          


                      return            bitIndex >> ADDRESS_BITS_PER_WORD;          


           }



这里涉及到一个常量ADDRESS_BITS_PER_WORD,先解释一下,源码中的定义如下:



private            final            static            int            ADDRESS_BITS_PER_WORD =            6           ;



那么很明显2^6=64,所以,当我们传进129作为参数的时候,我们会申请一个long[(129-1)>>6+1]也就是long[3]的数组,到此就很明白了,实际上替代办法的1)和2)是很相似的:都是通过一个整数(4个byte或者8个byte)来表示一定的bit位,之后,通过与十六位进制的数进行and,or,~等等操作进行Bit位的操作。

 

接下来讲讲其他比较重要的方法

1)set方法,源码如下:



/**          


                      * Sets the bit at the specified index to <code>true</code>.          


                      *          


                      * @param     bitIndex   a bit index.          


                      * @exception IndexOutOfBoundsException if the specified index is negative.          


                      * @since     JDK1.0          


                      */          


                      public            void            set(           int            bitIndex) {          


                      if            (bitIndex <            0           )          


                      throw            new            IndexOutOfBoundsException(           "bitIndex < 0: "            + bitIndex);          


                      


                      int            wordIndex = wordIndex(bitIndex);          


                      expandTo(wordIndex);          


                      


                      words[wordIndex] |= (1L << bitIndex);            // Restores invariants          


                      


                      checkInvariants();          


                      }



这个方法将bitIndex位上的值由false设置为true,解释如下:

我们设置的时候很明显是在改变long数组的某一个元素的值,首先需要确定的是改变哪一个元素,其次需要使用与或操作改变这个元素,在上面的代码中,首先将bitIndex>>6,这样就确定了是修改哪一个元素的值,其次这里涉及到一个expandTo方法,我们先跳过去,直接看代码:



words[wordIndex] |= (1L << bitIndex);            // Restores invariants



这里不是很好理解,要注意:需要注意的是java中的移位操作会模除位数,也就是说,long类型的移位会模除64。例如对long类型的值左移65位,实际是左移了65%64=1位。所以这行代码就等于:



int            transderBits = bitIndex %            64           ;          


           words[wordsIndex] |= (1L << transferBits);



上面这样写就很清楚了。

与之相对的一个方法是:

 



/**          


                      * Sets the bit specified by the index to <code>false</code>.          


                      *          


                      * @param     bitIndex   the index of the bit to be cleared.          


                      * @exception IndexOutOfBoundsException if the specified index is negative.          


                      * @since     JDK1.0          


                      */          


                      public            void            clear(           int            bitIndex) {          


                      if            (bitIndex <            0           )          


                      throw            new            IndexOutOfBoundsException(           "bitIndex < 0: "            + bitIndex);          


                      


                      int            wordIndex = wordIndex(bitIndex);          


                      if            (wordIndex >= wordsInUse)          


                      return           ;          


                      


                      words[wordIndex] &= ~(1L << bitIndex);          


                      


                      recalculateWordsInUse();          


                      checkInvariants();          


                      }



 

这段代码理解上与set大同小异,主要是用来设置某一位上的值为false的。

上面有个方法,顺带着解释一下:

expandTo方法:



/**          


                      * Ensures that the BitSet can accommodate a given wordIndex,          


                      * temporarily violating the invariants.  The caller must          


                      * restore the invariants before returning to the user,          


                      * possibly using recalculateWordsInUse().          


                      * @param   wordIndex the index to be accommodated.          


                      */          


                      private            void            expandTo(           int            wordIndex) {          


                      int            wordsRequired = wordIndex+           1           ;          


                      if            (wordsInUse < wordsRequired) {          


                      ensureCapacity(wordsRequired);          


                      wordsInUse = wordsRequired;          


                      }          


                      }



这里面又有个参数wordsInUse,定义如下:



/**          


                      * The number of words in the logical size of this BitSet.          


                      */          


           private            transient            int            wordsInUse =            0           ;



根据其定义解释,这个参数表示的是BitSet中的words的逻辑大小。当我们传进一个wordIndex的时候,首先需要判断这个逻辑大小与wordIndex的大小关系,如果小于它,我们就调用方法ensureCapacity:



private            void            ensureCapacity(           int            wordsRequired) {          


                      if            (words.length < wordsRequired) {          


                      // Allocate larger of doubled size or required size          


                      int            request = Math.max(           2            * words.length, wordsRequired);          


                      words = Arrays.copyOf(words, request);          


                      sizeIsSticky =            false           ;          


                      }          


                      }



也就是说将words的大小变为原来的两倍,复制数组,标志sizeIsSticky为false,这个参数的定义如下:



/**          


                      * Whether the size of "words" is user-specified.  If so, we assume          


                      * the user knows what he's doing and try harder to preserve it.          


                      */          


           private            transient            boolean            sizeIsSticky =            false           ;



执行完这个方法后,我们可以将wordsInUse设置为wordsRequired。(换句话说,BitSet具有自动扩充的功能)

 

2)get方法:



/**          


                      * Returns the value of the bit with the specified index. The value          


                      * is <code>true</code> if the bit with the index <code>bitIndex</code>          


                      * is currently set in this <code>BitSet</code>; otherwise, the result          


                      * is <code>false</code>.          


                      *          


                      * @param     bitIndex   the bit index.          


                      * @return    the value of the bit with the specified index.          


                      * @exception IndexOutOfBoundsException if the specified index is negative.          


                      */          


                      public            boolean            get(           int            bitIndex) {          


                      if            (bitIndex <            0           )          


                      throw            new            IndexOutOfBoundsException(           "bitIndex < 0: "            + bitIndex);          


                      


                      checkInvariants();          


                      


                      int            wordIndex = wordIndex(bitIndex);          


                      return            (wordIndex < wordsInUse)          


                      && ((words[wordIndex] & (1L << bitIndex)) !=            0           );          


                      }



这里主要是最后一个return语句,

 



return            (wordIndex < wordsInUse) && ((words[wordIndex] & (1L << bitIndex)) !=            0           );



 

只有当wordIndex越界,并且wordIndex上的wordIndex上的bit不为0的时候,我们才说这一位是true.

 

3)size()方法:

 



/**          


                      * Returns the number of bits of space actually in use by this          


                      * <code>BitSet</code> to represent bit values.          


                      * The maximum element in the set is the size - 1st element.          


                      *          


                      * @return  the number of bits currently in this bit set.          


                      */          


           public            int            size() {          


           return            words.length * BITS_PER_WORD;          


           }



 

这里也有一个常量,定义如下:



private            final            static            int            ADDRESS_BITS_PER_WORD =            6           ;          


           private            final            static            int            BITS_PER_WORD =            1            << ADDRESS_BITS_PER_WORD;



很明显,BITS_PER_WORD = 64,这里很重要的一点就是,如果使用size来返回BitSet数组的大小,其值一定是64的倍数,原因就在这里

 

4)与size相似的一个方法:length()源码如下:

 



/**          


                      * Returns the "logical size" of this <code>BitSet</code>: the index of          


                      * the highest set bit in the <code>BitSet</code> plus one. Returns zero          


                      * if the <code>BitSet</code> contains no set bits.          


                      *          


                      * @return  the logical size of this <code>BitSet</code>.          


                      * @since   1.2          


                      */          


                      public            int            length() {          


                      if            (wordsInUse ==            0           )          


                      return            0           ;          


                      


                      return            BITS_PER_WORD * (wordsInUse -            1           ) +          


                      (BITS_PER_WORD - Long.numberOfLeadingZeros(words[wordsInUse -            1           ]));          


                      }



 

方法虽然短小,却比较难以理解,细细分析一下:根据注释,这个方法法返回的是BitSet的逻辑大小,比如说你声明了一个129位的BitSet,设置了第23,45,67位,那么其逻辑大小就是67,也就是说逻辑大小其实是的是在你设置的所有位里面最高位的Index。

这里有一个方法,Long.numberOfLeadingZeros,网上没有很好的解释,做实验如下:



long            test =            1           ;<br>System.out.println(Long.numberOfLeadingZeros(test<<           3           ));<br>System.out.println(Long.numberOfLeadingZeros(test<<           40           ));<br>System.out.println(Long.numberOfLeadingZeros(test<<           40            | test<<           4           ));



打印结果如下:

60
23
23

也就是说,这个方法是输出一个64位二进制字符串前面0的个数的。

  

总结:

其实BitSet的源码并不复杂,只要理解其原理,对整数的移位等操作比较熟悉,细心阅读就可以理解。

标签:bitIndex,研究,wordIndex,int,源码,BitSet,words,bit
From: https://blog.51cto.com/u_16131764/6443230

相关文章

  • 得到第K个大的数算法研究
      第一种算法是最容易想到的,就是利用快速排序的思想,将一个数组分成以某一个数X为轴,左边的所有的数都比X小,而右边的数都比X大。但我快速排序不同的是,在这个算法中只考虑X的一边,而不是两边都考虑。 如果X的位置是i,那么要得到第k个数,如果k<=i,那么这个数一定在i的左边,否则在i......
  • JAVA的springboot+vue学习平台管理系统,校园在线学习管理系统,附源码+数据库+论文+PPT
    1、项目介绍在Internet高速发展的今天,我们生活的各个领域都涉及到计算机的应用,其中包括学习平台的网络应用,在外国学习平台已经是很普遍的方式,不过国内的管理平台可能还处于起步阶段。学习平台具有学习信息管理功能的选择。学习平台采用java技术,基于springboot框架,mysql数据库进行......
  • Java语言实现生产者与消费者的消息队列模型(附源码)
    Java构建生产者与消费者之间的生产关系模型,可以理解生产者生产message,存放缓存的容器,同时消费者进行消费需求的消耗,如果做一个合理的比喻的话:生产者消费者问题是线程模型中的经典问题。解决生产者/消费者问题有两种方法:一是采用某种机制保护生产者和消费者之间的同步;二是在生产者和......
  • 聊聊读研究生应该怎么权衡offer的选择(适合选择恐惧症,哈哈)
    关注微信公众号“AI学习经历分享”,回复对应关键词,获取机器学习,深度学习,Python,Java的技术干货!今天突然有时间聊聊这个读研究生offer的选择,一方面是因为当初都答应了一位朋友,但是因为种种原因和因素,鸽了这个约定,并且最近一段时间比较忙,但是我从来没有忘记,答应别人的事情一定要做到。......
  • 关于Pod中进程在节点中的研究
    最近研究OpenShiftvirtulization,各种Pod对KVM进程的封装,引发了Pod中进程到底在Node中是什么表现形势的好奇,因为对基础知识的不扎实,还是希望找个环境能仔细看看,建立起openshift4.12的环境后,首先列出某个节点上的所有的Pod[lab-user@bastion~]$ocgetpods-A--field-selec......
  • 多校园微社区交友及二手物品论坛小程序源码运营需要什么?
    在大学校园里,存在着很多的二手商品,但是由于信息资源的不流通以及传统二手商品信息交流方式的笨拙,导致了很多仍然具有一定价值或者具有非常价值的二手商品的囤积,乃至被当作废弃物处理。现在通过微信小程序的校园二手交易平台,可以方便快捷的发布和交流任何二手商品的信息,并且可以通过......
  • zabbix--CentOS7 源码安装Zabbix6 LTS版本
    环境说明#这里使用为 CentOS7.6 版本进行测试验证,ZabbixServer 采用源码包部署,数据库采用 MySQL8.0 版本,zabbix-web 使用 nginx+php 来实现。具体信息如下:软件名版本安装方式ZabbixServer6.0.3源码安装ZabbixAgent6.0.3源码安装MySQL8.0.28yum安......
  • 跟着源码学IM(十一):一套基于Netty的分布式高可用IM详细设计与实现(有源码)
    本文由will分享,个人博客zhangyaoo.github.io,原题“基于Netty的IM系统设计与实现”,有修订和重新排版。1、引言本文将要分享的是如何从零实现一套基于Netty框架的分布式高可用IM系统,它将支持长连接网关管理、单聊、群聊、聊天记录查询、离线消息存储、消息推送、心跳、分布式唯......
  • 直播小程序源码,自定义支持360度旋转的View
    直播小程序源码,自定义支持360度旋转的View自定义Touch360ImageView的代码如下: packagecom.example.myapplication;importandroid.content.Context;importandroid.content.res.TypedArray;importandroid.graphics.drawable.LevelListDrawable;importandroid.util.Attribut......
  • 视频直播网站源码,自定义气泡效果(BubbleView)
    视频直播网站源码,自定义气泡效果(BubbleView)代码如下: packagecom.example.myapplication;importandroid.content.Context;importandroid.graphics.BlurMaskFilter;importandroid.graphics.Canvas;importandroid.graphics.Color;importandroid.graphics.Paint;importandr......