java 中的集合(六) BitSet源码分析

首先需要说明的是,BitSet并不属于集合框架,没有实现Collection或Map接口。但因为其与List有一定类似性,所以这里一并列拿出来研究。

BitSet类实现了一个按需增长的位向量。每个位都有一个boolean值,用非负的整数将BitSet的位编入索引。可以对每个编入索引的位进行查找、设置或者清除。默认情况下,所有位的初始值都是false。BitSet非线程安全,在单线程情况下使用。

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(8个byte,对应long型)。所以,当我们传进129作为参数的时候,我们会申请一个long[(129-1)>>6+1]也就是long[3]的数组。BitSet是通过一个整数来表示一定的bit位。

BitSet还有一些比较重要的方法

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型数组中的哪一个元素,其次决定是否需要扩容(expandTo方法,下文再介绍),最后进行修改。可以看到修改的代码是这样的:

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

需要注意的是,long类型的移位会模除64。例如对long类型的值左移65位,实际是左移了65%64=1位。这样就定位到了long型数据的对应位置,相当于对应位置的原值与1进行或运算。注意:这里忽视了long型数据本身的值,只关心各个位置上的bit,所以也不需要考虑long型数据的符号问题。另外一个需要注意的问题,由于bitIndex是从0开始计数的,bitIndex表示的实际是第bitIndex+1位数据,同时由于1L本身就表示第1位,所以左移bitIndex位,可对应到bitIndex+1的位置。

与set方法相对的一个方法是clear方法,两者大同小异:

/**
        * 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();  
       }  

关于上文说到的扩容:

   /**
         * 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表示的是BitSet中的long型数组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的大小变为原来的两倍(如果仍然不够,则将大小直接变为需要的长度,这与Vector很像)。然后复制数组。最后将wordsInUse设置为wordsRequired。

与之相对的,clear方法虽然不可能涉及到扩容操作,但也需要判断是否越界:

 if (wordIndex >= wordsInUse)  
           return;   

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语句说明,只有当wordIndex不越界,并且对应位置上的bit是1的时候,我们才说值是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(实际是192位),设置了第23,45,67位为true,那么其逻辑大小就是68(从0开始计数)。其中numberOfLeadingZeros方法是输出二进制字符串左边开始0的个数(由于long型数据左边是高位,实际上是BitSet中靠后的bit),也就是最后一个long型数据所代表的bit中,最后一个1后面的bit数,用64减去这个值,在加上前面的long型数据个数乘以64,就可以得到逻辑大小。(值得一提的是,BitSet中的第0位实际存在第1个long型数据的第1位,后面的以此类推)


参考地址:http://blog.csdn.net/wxwzy738/article/details/8879423

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在C++的STL实现由一个bitset类模板,其用法如下: std::bitset bs; 也就是说,这个bs只能支持64位以内的位存储和操作;bs一旦定义就不能动态增长了。本资源附件实现了一个动态Bitset,和标准bitset兼容。 /** @defgroup Bitset Bitset位集类 * @{ */ //根据std::bitset改写,函数意义和std::bitset保持一致 class CORE_API Bitset: public Serializable { typedef typename uint32_t _Ty; static const int _Bitsperword = (CHAR_BIT * sizeof(_Ty)); _Ty * _Array; //最低位放在[0]位置,每位的默认值为0 int _Bits;//最大有效的Bit个数 private: int calculateWords()const; void tidy(_Ty _Wordval = 0); void trim(); _Ty getWord(size_t _Wpos)const; public: //默认构造 Bitset(); //传入最大的位数,每位默认是0 Bitset(int nBits); virtual ~Bitset(); //直接整数转化成二进制,赋值给Bitset,最高低放在[0]位置 Bitset(unsigned long long _Val); //拷贝构造函数 Bitset(const Bitset & b); Bitset(const char * str); Bitset(const std::string & str, size_t _Pos, size_t _Count); public: size_t size()const; //返回设置为1的位数 size_t count() const; bool subscript(size_t _Pos) const; bool get(size_t pos) const; //设置指定位置为0或1,true表示1,false表示0,如果pos大于数组长度,则自动扩展 void set(size_t _Pos, bool _Val = true); //将位数组转换成整数,最低位放在[0]位置 //例如数组存放的1011,则返回13,而不是返回11 unsigned long long to_ullong() const; bool test(size_t _Pos) const; bool any() const; bool none() const; bool all() const; std::string to_string() const; public: //直接整数转化成二进制,赋值给Bitset,最高位放在[0]位置 Bitset& operator = (const Bitset& b); //直接整数转化成二进制,赋值给Bitset,最高位放在[0]位置 Bitset& operator = (unsigned long long ull); //返回指定位置的值,如果pos大于位数组长度,自动拓展 bool operator [] (const size_t pos); //测试两个Bitset是否相等 bool operator == (const Bitset & b); bool operator != (const Bitset & b); Bitset operator<>(size_t _Pos) const; bool operator > (const Bitset & c)const; bool operator < (const Bitset & c)const; Bitset& operator &=(const Bitset& _Right); Bitset& operator|=(const Bitset& _Right); Bitset& operator^=(const Bitset& _Right); Bitset& operator<>=(size_t _Pos); public: Bitset& flip(size_t _Pos); Bitset& flip(); //将高位与低位互相,如数组存放的是1011,则本函数执行后为1101 Bitset& reverse(); //返回左边n位,构成新的Bitset Bitset left(size_t n) const; //返回右边n位,构成新的Bitset Bitset right(size_t n) const; //判断b包含的位数组是否是本类的位数组的自串,如果是返回开始位置 size_t find (const Bitset & b) const; size_t find(unsigned long long & b) const; size_t find(const char * b) const; size_t find(const std::string & b) const; //判断本类的位数组是否是b的前缀 bool is_prefix(unsigned long long & b) const; bool is_prefix(const char * b) const; bool is_prefix(const std::string & b) const; bool is_prefix(const Bitset & b) const; void clear(); void resize(size_t newSize); void reset(const unsigned char * flags, size_t s); void reset(unsigned long long _Val); void reset(const char * _Str); void reset(const std::string & _Str, size_t _Pos, size_t _Count); //左移动n位,返回新的Bitset //extendBits=false "1101" 左移动2位 "0100"; //extendBits=true "1101" 左移动2位 "110100"; Bitset leftShift(size_t n,bool extendBits=false)const; //右移动n位,返回新的Bitset //extendBits=false "1101" 右移动2位 "0011"; //extendBits=true "1101" 右移动2位 "001101"; Bitset rightShift(size_t n, bool extendBits = false) const; public: virtual uint32_t getByteArraySize(); // returns the size of the required byte array. virtual void loadFromByteArray(const unsigned char * data); // load this object using the byte array. virtual void storeToByteArray(unsigned char ** data, uint32_t& length) ; // store this object in the byte array. };

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值