java BitSet

                                  

一. Bitset 基础

Bitset,也就是位图,由于可以用非常紧凑的格式来表示给定范围的连续数据而经常出现在各种算法设计中。上面的图来自c++库中bitset的一张图

基本原理是,用1位来表示一个数据是否出现过,0为没有出现过,1表示出现过。使用用的时候既可根据某一个是否为0表示此数是否出现过。

一个1G的空间,有 8*1024*1024*1024=8.58*10^9bit,也就是可以表示85亿个不同的数。


常见的应用是那些需要对海量数据进行一些统计工作的时候,比如日志分析等。

面试题中也常出现,比如:统计40亿个数据中没有出现的数据,将40亿个不同数据进行排序等。

又如:现在有1千万个随机数,随机数的范围在1到1亿之间。现在要求写出一种算法,将1到1亿之间没有在随机数中的数求出来(百度)。

programming pearls上也有一个关于使用bitset来查找电话号码的题目。


Bitmap的常见扩展,是用2位或者更多为来表示此数字的更多信息,比如出现了多少次等。


二. java中bitset的实现

Bitset这种结构虽然简单,实现的时候也有一些细节需要主要。其中的关键是一些位操作,比如如何将指定位进行反转、设置、查询指定位的状态(0或者1)等。
本文,分析一下java中bitset的实现,抛砖引玉,希望给那些需要自己设计位图结构的需要的程序员有所启发。

Bitmap的基本操作有:
  1. 初始化一个bitset,指定大小。
  2. 清空bitset。
  3. 反转某一指定位。
  4. 设置某一指定位。
  5. 获取某一位的状态。
  6. 当前bitset的bit总位数。

1. 声明

在java中,bitset的实现,位于java.util这个包中,从jdk 1.0就引入了这个数据结构。在多个jdk的演变中,bitset也不断演变。本文参照的是jdk 7.0 源代码中的实现。
声明如下:
  1. package java.util;  
  2.   
  3. import java.io.*;  
  4. import java.nio.ByteBuffer;  
  5. import java.nio.ByteOrder;  
  6. import java.nio.LongBuffer;  
  7.   
  8. public class BitSet implements Cloneable, java.io.Serializable {、  
  1.   private long[] words;  
  2. ....  
  3. ....  

同时我们也看到使用long数组来作为内部存储结构。这个决定了,Bitset至少为一个long的大小。下面的构造函数中也会有所体现。

2. 初始化函数

  1. <pre name="code" class="java"public BitSet() {  
  2.         initWords(BITS_PER_WORD);  
  3.         sizeIsSticky = false;  
  4.     }  
  5.   
  6.     public BitSet(int nbits) {  
  7.         // nbits can't be negative; size 0 is OK  
  8.         if (nbits < 0)  
  9.             throw new NegativeArraySizeException("nbits < 0: " + nbits);  
  10.   
  11.         initWords(nbits);  
  12.     private void initWords(int nbits) {  
  13.         words = new long[wordIndex(nbits-1) + 1];  
  14.     }  
  15.     private static int wordIndex(int bitIndex) {  
  16.         return bitIndex >> ADDRESS_BITS_PER_WORD;  
  17.     }  
  18.     private final static int ADDRESS_BITS_PER_WORD = 6;  
  19.     private final static int BITS_PER_WORD = 1 << ADDRESS_BITS_PER_WORD;</pre><br>  
  20. 两个构造函数,分别是一个指定了初始大小,一个没指定。如果没指定,我们可以看到默认的初始大小为, 2^6 = 64-1=63 bit. 我们知道java中long的大小就是8个字节,也就是8*8=64bit。也就是说,bitset默认的是一个long整形的大小。初始化函数指定了必要的大小。<br>  
  21. <strong>注意:</strong>如果指定了bitset的初始化大小,那么会把他规整到一个大于或者等于这个数字的64的整倍数。比如64位,bitset的大小是1long,而65位时,bitset大小是2long,即128位。做这么一个规定,主要是为了内存对齐,同时避免考虑到不要处理特殊情况,简化程序。  
  22. <pre></pre>  
  23. <pre></pre>  
  24. <pre></pre>  
  25. <pre></pre>  

3. 清空bitset

a. 清空所有的bit位,即全部置0。通过循环方式来以此以此置0。 如果是c语言,使用memset会不会快点?
  1. public void clear() {  
  2.     while (wordsInUse > 0)  
  3.         words[--wordsInUse] = 0;  
  4. }  
b. 清空某一位
  1. public void clear(int bitIndex) {  
  2.      if (bitIndex < 0)  
  3.          throw new IndexOutOfBoundsException("bitIndex < 0: " + bitIndex);  
  4.   
  5.      int wordIndex = wordIndex(bitIndex);  
  6.      if (wordIndex >= wordsInUse)  
  7.          return;  
  8.   
  9.      words[wordIndex] &= ~(1L << bitIndex);  
  10.   
  11.      recalculateWordsInUse();  
  12.      checkInvariants();  
  13.  }  

第一行是参数检查,如果bitIndex小于0,则抛参数非法异常。后面执行的是bitset中操作中经典的两步曲:a. 找到对应的long  b. 操作对应的位。
a. 找到对应的long。 这行语句是  int wordIndex = wordIndex(bitIndex);  
b. 操作对应的位。常见的位操作是通过与特定的mask进行逻辑运算来实现的。因此,首先获取 mask(掩码)。
    对于 clear某一位来说,它需要的掩码是指定位为0,其余位为1,然后与对应的long进行&运算。
   ~(1L << bitIndex);  即获取mask
  words[wordIndex] &= ; 执行相应的运算。
注意:这里的参数检查,对负数index跑出异常,对超出大小的index,不做任何操作,直接返回。具体的原因,有待进一步思考。
c. 清空指定范围的那些bits
  1. /** 
  2.     * Sets the bits from the specified {@code fromIndex} (inclusive) to the 
  3.     * specified {@code toIndex} (exclusive) to {@code false}. 
  4.     * 
  5.     * @param  fromIndex index of the first bit to be cleared 
  6.     * @param  toIndex index after the last bit to be cleared 
  7.     * @throws IndexOutOfBoundsException if {@code fromIndex} is negative, 
  8.     *         or {@code toIndex} is negative, or {@code fromIndex} is 
  9.     *         larger than {@code toIndex} 
  10.     * @since  1.4 
  11.     */  
  12.    public void clear(int fromIndex, int toIndex) {  
  13.        checkRange(fromIndex, toIndex);  
  14.   
  15.        if (fromIndex == toIndex)  
  16.            return;  
  17.   
  18.        int startWordIndex = wordIndex(fromIndex);  
  19.        if (startWordIndex >= wordsInUse)  
  20.            return;  
  21.   
  22.        int endWordIndex = wordIndex(toIndex - 1);  
  23.        if (endWordIndex >= wordsInUse) {  
  24.            toIndex = length();  
  25.            endWordIndex = wordsInUse - 1;  
  26.        }  
  27.   
  28.        long firstWordMask = WORD_MASK << fromIndex;  
  29.        long lastWordMask  = WORD_MASK >>> -toIndex;  
  30.        if (startWordIndex == endWordIndex) {  
  31.            // Case 1: One word  
  32.            words[startWordIndex] &= ~(firstWordMask & lastWordMask);  
  33.        } else {  
  34.            // Case 2: Multiple words  
  35.            // Handle first word  
  36.            words[startWordIndex] &= ~firstWordMask;  
  37.   
  38.            // Handle intermediate words, if any  
  39.            for (int i = startWordIndex+1; i < endWordIndex; i++)  
  40.                words[i] = 0;  
  41.   
  42.            // Handle last word  
  43.            words[endWordIndex] &= ~lastWordMask;  
  44.        }  
  45.   
  46.        recalculateWordsInUse();  
  47.        checkInvariants();  
  48.    }  
方法是将这个范围分成三块,startword; interval words; stopword。
其中startword,只要将从start位到该word结束位全部置0;interval words则是这些long的所有bits全部置0;而stopword这是从起始位置到指定的结束位全部置0。
而特殊情形则是没有startword和stopword是同一个long。
具体的实现,参照代码,是分别作出两个mask,对startword和stopword进行操作。

4. 重要的两个内部检查函数

从上面的代码,可以看到每个函授结尾都会有两个函数,如下:
 recalculateWordsInUse();
 checkInvariants();
这两个函数,是对bitset的内部状态进行维护和检查的函数。细看实现既可明白其中原理:
  1. /** 
  2.      * Sets the field wordsInUse to the logical size in words of the bit set. 
  3.      * WARNING:This method assumes that the number of words actually in use is 
  4.      * less than or equal to the current value of wordsInUse! 
  5.      */  
  6.     private void recalculateWordsInUse() {  
  7.         // Traverse the bitset until a used word is found  
  8.         int i;  
  9.         for (i = wordsInUse-1; i >= 0; i--)  
  10.             if (words[i] != 0)  
  11.                 break;  
  12.   
  13.         wordsInUse = i+1// The new logical size  
  14.     }  

wordsInUse 是检查当前的long数组中,实际使用的long的个数,即long[wordsInUse-1]是当前最后一个存储有有效bit的long。这个值是用于保存bitset有效大小的。
  1. /** 
  2.  * Every public method must preserve these invariants. 
  3.  */  
  4. private void checkInvariants() {  
  5.     assert(wordsInUse == 0 || words[wordsInUse - 1] != 0);  
  6.     assert(wordsInUse >= 0 && wordsInUse <= words.length);  
  7.     assert(wordsInUse == words.length || words[wordsInUse] == 0);  
  8. }  

checkInvariants 可以看出是检查内部状态,尤其是wordsInUse是否合法的函数。

5. 反转某一个指定位

反转,就是1变成0,0变成1,是一个与1的xor操作。
  1. /** 
  2.     * Sets the bit at the specified index to the complement of its 
  3.     * current value. 
  4.     * 
  5.     * @param  bitIndex the index of the bit to flip 
  6.     * @throws IndexOutOfBoundsException if the specified index is negative 
  7.     * @since  1.4 
  8.     */  
  9.    public void flip(int bitIndex) {  
  10.        if (bitIndex < 0)  
  11.            throw new IndexOutOfBoundsException("bitIndex < 0: " + bitIndex);  
  12.   
  13.        int wordIndex = wordIndex(bitIndex);  
  14.        expandTo(wordIndex);  
  15.   
  16.        words[wordIndex] ^= (1L << bitIndex);  
  17.   
  18.        recalculateWordsInUse();  
  19.        checkInvariants();  
  20.    }  

反转的基本操作也是两步,找到对应的long, 获取mask并与指定的位进行xor操作。
 int wordIndex = wordIndex(bitIndex);
 words[wordIndex] ^= (1L << bitIndex);
我们注意到在进行操作之前,执行了一个函数 expandTo(wordIndex); 这个函数是确保bitset中有对应的这个long。如果没有的话,就对bitset中的long数组进行扩展。扩展的策略,是将当前的空间翻一倍。
代码如下:
  1. /** 
  2.     * Ensures that the BitSet can accommodate a given wordIndex, 
  3.     * temporarily violating the invariants.  The caller must 
  4.     * restore the invariants before returning to the user, 
  5.     * possibly using recalculateWordsInUse(). 
  6.     * @param wordIndex the index to be accommodated. 
  7.     */  
  8.    private void expandTo(int wordIndex) {  
  9.        int wordsRequired = wordIndex+1;  
  10.        if (wordsInUse < wordsRequired) {  
  11.            ensureCapacity(wordsRequired);  
  12.            wordsInUse = wordsRequired;  
  13.        }  
  14.    }  
  15.    /** 
  16.     * Ensures that the BitSet can hold enough words. 
  17.     * @param wordsRequired the minimum acceptable number of words. 
  18.     */  
  19.    private void ensureCapacity(int wordsRequired) {  
  20.        if (words.length < wordsRequired) {  
  21.            // Allocate larger of doubled size or required size  
  22.            int request = Math.max(2 * words.length, wordsRequired);  
  23.            words = Arrays.copyOf(words, request);  
  24.            sizeIsSticky = false;  
  25.        }  
  26.    }  

同样,也提供了一个指定区间的反转,实现方案与clear基本相同。代码如下:
  1. public void flip(int fromIndex, int toIndex) {  
  2.        checkRange(fromIndex, toIndex);  
  3.   
  4.        if (fromIndex == toIndex)  
  5.            return;  
  6.   
  7.        int startWordIndex = wordIndex(fromIndex);  
  8.        int endWordIndex   = wordIndex(toIndex - 1);  
  9.        expandTo(endWordIndex);  
  10.   
  11.        long firstWordMask = WORD_MASK << fromIndex;  
  12.        long lastWordMask  = WORD_MASK >>> -toIndex;  
  13.        if (startWordIndex == endWordIndex) {  
  14.            // Case 1: One word  
  15.            words[startWordIndex] ^= (firstWordMask & lastWordMask);  
  16.        } else {  
  17.            // Case 2: Multiple words  
  18.            // Handle first word  
  19.            words[startWordIndex] ^= firstWordMask;  
  20.   
  21.            // Handle intermediate words, if any  
  22.            for (int i = startWordIndex+1; i < endWordIndex; i++)  
  23.                words[i] ^= WORD_MASK;  
  24.   
  25.            // Handle last word  
  26.            words[endWordIndex] ^= lastWordMask;  
  27.        }  
  28.   
  29.        recalculateWordsInUse();  
  30.        checkInvariants();  
  31.    }  

6. 设置某一指定位 (or 操作)

  1. /**  
  2.      * Sets the bit at the specified index to {@code true}. 
  3.      * 
  4.      * @param  bitIndex a bit index 
  5.      * @throws IndexOutOfBoundsException if the specified index is negative 
  6.      * @since  JDK1.0 
  7.      */  
  8.     public void set(int bitIndex) {  
  9.         if (bitIndex < 0)  
  10.             throw new IndexOutOfBoundsException("bitIndex < 0: " + bitIndex);  
  11.   
  12.         int wordIndex = wordIndex(bitIndex);  
  13.         expandTo(wordIndex);  
  14.   
  15.         words[wordIndex] |= (1L << bitIndex); // Restores invariants  
  16.   
  17.         checkInvariants();  
  18.     }  

思路与flip是一样的,只是执行的是与1的or操作。
同时jdk中提供了,具体设置成0或1的操作,以及设置某一区间的操作。
  1. public void set(int bitIndex, boolean value) {  
  2.       if (value)  
  3.           set(bitIndex);  
  4.       else  
  5.           clear(bitIndex);  
  6.   }  

7. 获取某一位置的状态

  1. /** 
  2.    * Returns the value of the bit with the specified index. The value 
  3.    * is {@code true} if the bit with the index {@code bitIndex} 
  4.    * is currently set in this {@code BitSet}; otherwise, the result 
  5.    * is {@code false}. 
  6.    * 
  7.    * @param  bitIndex   the bit index 
  8.    * @return the value of the bit with the specified index 
  9.    * @throws IndexOutOfBoundsException if the specified index is negative 
  10.    */  
  11.   public boolean get(int bitIndex) {  
  12.       if (bitIndex < 0)  
  13.           throw new IndexOutOfBoundsException("bitIndex < 0: " + bitIndex);  
  14.   
  15.       checkInvariants();  
  16.   
  17.       int wordIndex = wordIndex(bitIndex);  
  18.       return (wordIndex < wordsInUse)  
  19.           && ((words[wordIndex] & (1L << bitIndex)) != 0);  
  20.   }  

同样的两步走,这里的位操作时&。可以看到,如果指定的bit不存在的话,返回的是false,即没有设置。
jdk同时提供了一个获取指定区间的bitset的方法。当然这里的返回值会是一个bitset,是一个仅仅包含需要查询位的bitset。注意这里的大小也仅仅是刚刚能够容纳必须的位(当然,规整到long的整数倍)。代码如下:
  1. public BitSet get(int fromIndex, int toIndex) {  
  2.         checkRange(fromIndex, toIndex);  
  3.   
  4.         checkInvariants();  
  5.   
  6.         int len = length();  
  7.   
  8.         // If no set bits in range return empty bitset  
  9.         if (len <= fromIndex || fromIndex == toIndex)  
  10.             return new BitSet(0);  
  11.   
  12.         // An optimization  
  13.         if (toIndex > len)  
  14.             toIndex = len;  
  15.   
  16.         BitSet result = new BitSet(toIndex - fromIndex);  
  17.         int targetWords = wordIndex(toIndex - fromIndex - 1) + 1;  
  18.         int sourceIndex = wordIndex(fromIndex);  
  19.         boolean wordAligned = ((fromIndex & BIT_INDEX_MASK) == 0);  
  20.   
  21.         // Process all words but the last word  
  22.         for (int i = 0; i < targetWords - 1; i++, sourceIndex++)  
  23.             result.words[i] = wordAligned ? words[sourceIndex] :  
  24.                 (words[sourceIndex] >>> fromIndex) |  
  25.                 (words[sourceIndex+1] << -fromIndex);  
  26.   
  27.         // Process the last word  
  28.         long lastWordMask = WORD_MASK >>> -toIndex;  
  29.         result.words[targetWords - 1] =  
  30.             ((toIndex-1) & BIT_INDEX_MASK) < (fromIndex & BIT_INDEX_MASK)  
  31.             ? /* straddles source words */  
  32.             ((words[sourceIndex] >>> fromIndex) |  
  33.              (words[sourceIndex+1] & lastWordMask) << -fromIndex)  
  34.             :  
  35.             ((words[sourceIndex] & lastWordMask) >>> fromIndex);  
  36.   
  37.         // Set wordsInUse correctly  
  38.         result.wordsInUse = targetWords;  
  39.         result.recalculateWordsInUse();  
  40.         result.checkInvariants();  
  41.   
  42.         return result;  
  43.     }  

这里有一个tricky的操作,即fromIndex的那个bit会存在返回bitset的第0个位置,以此类推。如果fromIndex不是word对齐的话,那么返回的bitset的第一个word将会包含fromIndex所在word的从fromIndex开始的到fromIndex+1开始的的那几位(总共加起来是一个word的大小)。
其中>>>是无符号位想右边移位的操作符。

8. 获取当前bitset总bit的大小

  1. /** 
  2.    * Returns the "logical size" of this {@code BitSet}: the index of 
  3.    * the highest set bit in the {@code BitSet} plus one. Returns zero 
  4.    * if the {@code BitSet} contains no set bits. 
  5.    * 
  6.    * @return the logical size of this {@code BitSet} 
  7.    * @since  1.2 
  8.    */  
  9.   public int length() {  
  10.       if (wordsInUse == 0)  
  11.           return 0;  
  12.   
  13.       return BITS_PER_WORD * (wordsInUse - 1) +  
  14.           (BITS_PER_WORD - Long.numberOfLeadingZeros(words[wordsInUse - 1]));  
  15.   }  

9. hashcode

hashcode是一个非常重要的属性,可以用来表明一个数据结构的特征。bitset的hashcode是用下面的方式实现的:
  1. /** 
  2.     * Returns the hash code value for this bit set. The hash code depends 
  3.     * Note that the hash code changes if the set of bits is altered. 
  4.     * 
  5.     * @return the hash code value for this bit set 
  6.     */  
  7.    public int hashCode() {  
  8.        long h = 1234;  
  9.        for (int i = wordsInUse; --i >= 0; )  
  10.            h ^= words[i] * (i + 1);  
  11.   
  12.        return (int)((h >> 32) ^ h);  
  13.    }  

这个hashcode同时考虑了没给word以及word的位置。当有bit的状态发生变化时,hashcode会随之改变。   

三 bitset使用

bitset的使用非常简单,只要对需要的操作调用对应的函数即可。
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值