All Downloads are FREE. Search and download functionalities are using the official Maven repository.

bboss.org.jgroups.util.FixedSizeBitSet Maven / Gradle / Ivy

The newest version!
package bboss.org.jgroups.util;



/**
 * Class copied from {@link java.util.BitSet}. Changes are that the FixedSizeBitSet doesn't expand, so access to it
 * doesn't need to be synchronized, plus we only need a few methods so most methods of the old class have been removed.
 * @author Bela Ban
 * @version $Id: FixedSizeBitSet.java,v 1.1 2009/11/25 08:41:17 belaban Exp $
 */
public class FixedSizeBitSet {
    /*
     * BitSets are packed into arrays of "words."  Currently a word is a long, which consists of 64 bits, requiring
     * 6 address bits. The choice of word size is determined purely by performance concerns.
     */
    private final static int ADDRESS_BITS_PER_WORD=6;
    private final static int BITS_PER_WORD=1 << ADDRESS_BITS_PER_WORD;

    /* Used to shift left or right for a partial word mask */
    private static final long WORD_MASK=0xffffffffffffffffL;

    private final long[] words;
    private final int size;




    /**
     * Creates a bit set whose initial size is the range 0 through
     * size-1. All bits are initially false.
     * @param size the initial size of the bit set (in bits).
     * @throws NegativeArraySizeException if the specified initial size is negative
     */
    public FixedSizeBitSet(int size) {
        if(size < 0) // nbits can't be negative; size 0 is OK
            throw new NegativeArraySizeException("size < 0: " + size);
        this.size=size;
        words=new long[wordIndex(size - 1) + 1];
    }

    


    /**
     * Sets the bit at the specified index to true.
     * @param index a bit index.
     * @throws IndexOutOfBoundsException if the specified index is negative.
     */
    public void set(int index) {
        if(index < 0 || index >= size)
            throw new IndexOutOfBoundsException("index: " + index);

        int wordIndex=wordIndex(index);
        words[wordIndex]|=(1L << index); // Restores invariants
    }

   

    /**
     * Sets the bit specified by the index to false.
     * @param index the index of the bit to be cleared.
     * @throws IndexOutOfBoundsException if the specified index is negative.
     */
    public void clear(int index) {
        if(index < 0 || index >= size)
            throw new IndexOutOfBoundsException("index: " + index);

        int wordIndex=wordIndex(index);
        words[wordIndex]&=~(1L << index);
    }



    /**
     * Returns the value of the bit with the specified index. The value
     * is true if the bit with the index index
     * is currently set in this bit set; otherwise, the result is false.
     * @param index the bit index.
     * @return the value of the bit with the specified index.
     * @throws IndexOutOfBoundsException if the specified index is negative.
     */
    public boolean get(int index) {
        if(index < 0 || index >= size)
            throw new IndexOutOfBoundsException("index: " + index);

        int wordIndex=wordIndex(index);
        return (words[wordIndex] & (1L << index)) != 0;
    }



    /**
     * Returns the index of the first bit that is set to true that occurs on or after
     * the specified starting index. If no such bit exists then -1 is returned.
     * 

* To iterate over the true bits in a BitSet, * use the following loop: *

*

     * for (int i = bs.nextSetBit(0); i >= 0; i = bs.nextSetBit(i+1)) {
     *     // operate on index i here
     * }
* @param fromIndex the index to start checking from (inclusive). * @return the index of the next set bit. * @throws IndexOutOfBoundsException if the specified index is negative. */ public int nextSetBit(int fromIndex) { if(fromIndex < 0) throw new IndexOutOfBoundsException("fromIndex: " + fromIndex); if(fromIndex >= size) return -1; int u=wordIndex(fromIndex); long word=words[u] & (WORD_MASK << fromIndex); while(true) { if(word != 0) return (u * BITS_PER_WORD) + Long.numberOfTrailingZeros(word); if(++u == words.length) return -1; word=words[u]; } } /** * Returns the index of the first bit that is set to false * that occurs on or after the specified starting index. * @param fromIndex the index to start checking from (inclusive). * @return the index of the next clear bit. * @throws IndexOutOfBoundsException if the specified index is negative. */ public int nextClearBit(int fromIndex) { // Neither spec nor implementation handle bitsets of maximal length. // See 4816253. if(fromIndex < 0) throw new IndexOutOfBoundsException("fromIndex: " + fromIndex); if(fromIndex >= size) return -1; int u=wordIndex(fromIndex); if(u >= words.length) return fromIndex; long word=~words[u] & (WORD_MASK << fromIndex); while(true) { if(word != 0) return (u * BITS_PER_WORD) + Long.numberOfTrailingZeros(word); if(++u == words.length) return -1; word=~words[u]; } } /** * Returns the number of bits set to true in this bit set * @return the number of bits set to true in this bit set */ public int cardinality() { int sum=0; for(int i=0; i < words.length; i++) sum+=Long.bitCount(words[i]); return sum; } public int size() {return size;} /** * Returns a string representation of this bit set. For every index * for which this BitSet contains a bit in the set * state, the decimal representation of that index is included in * the result. Such indices are listed in order from lowest to * highest, separated by ", " (a comma and a space) and * surrounded by braces, resulting in the usual mathematical * notation for a set of integers.

* Overrides the toString method of Object. *

Example: *

     * BitSet drPepper = new BitSet();
* Now drPepper.toString() returns "{}".

*

     * drPepper.set(2);
* Now drPepper.toString() returns "{2}".

*

     * drPepper.set(4);
     * drPepper.set(10);
* Now drPepper.toString() returns "{2, 4, 10}". * @return a string representation of this bit set. */ public String toString() { int numBits=(words.length > 128)? cardinality() : words.length * BITS_PER_WORD; StringBuilder b=new StringBuilder(6 * numBits + 2); b.append('{'); int i=nextSetBit(0); if(i != -1) { b.append(i); for(i=nextSetBit(i + 1); i >= 0; i=nextSetBit(i + 1)) { int endOfRun=nextClearBit(i); do { b.append(", ").append(i); } while(++i < endOfRun); } } b.append('}'); return b.toString(); } private static int wordIndex(int bitIndex) { return bitIndex >> ADDRESS_BITS_PER_WORD; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy