FileDocCategorySizeDatePackage
BitIntSet.javaAPI DocAndroid 1.5 API4013Wed May 06 22:41:02 BST 2009com.android.dx.util

BitIntSet

public class BitIntSet extends Object implements IntSet
A set of integers, represented by a bit set

Fields Summary
int[]
bits
also accessed in ListIntSet
Constructors Summary
public BitIntSet(int max)
Constructs an instance.

param
max the maximum value of ints in this set.

        bits = Bits.makeBitSet(max);
    
Methods Summary
public voidadd(int value)

inheritDoc

        ensureCapacity(value);
        Bits.set(bits, value, true);
    
public intelements()

inheritDoc

        return Bits.bitCount(bits);        
    
private voidensureCapacity(int value)
Ensures that the bit set has the capacity to represent the given value.

param
value >= 0 value to represent

        if (value >= Bits.getMax(bits)) {
            int[] newBits = Bits.makeBitSet(
                    Math.max(value + 1, 2 * Bits.getMax(bits)));
            System.arraycopy(bits, 0, newBits, 0, bits.length);
            bits = newBits;
        }
    
public booleanhas(int value)

inheritDoc

        return (value < Bits.getMax(bits)) && Bits.get(bits, value);    
    
public IntIteratoriterator()

inheritDoc

        return new IntIterator() {
            private int idx = Bits.findFirst(bits, 0);

            /** @inheritDoc */
            public boolean hasNext() {
                return idx >= 0;
            }

            /** @inheritDoc */
            public int next() {
                if (!hasNext()) {
                    throw new NoSuchElementException();
                }

                int ret = idx;

                idx = Bits.findFirst(bits, idx+1);

                return ret;
            }

            /** @inheritDoc */
            public void remove() {
                BitIntSet.this.remove(idx);
                idx = Bits.findFirst(bits, idx+1);
            }
        };
    
public voidmerge(IntSet other)

inheritDoc

        if (other instanceof BitIntSet) {
            BitIntSet o = (BitIntSet) other;
            ensureCapacity(Bits.getMax(o.bits) + 1);
            Bits.or(bits, o.bits);
        } else if (other instanceof ListIntSet) {
            ListIntSet o = (ListIntSet) other;
            int sz = o.ints.size();

            if (sz > 0) {
                ensureCapacity(o.ints.get(sz - 1));
            }
            for (int i = 0; i < o.ints.size(); i++) {
                Bits.set(bits, o.ints.get(i), true);
            }
        } else {
            IntIterator iter = other.iterator();
            while (iter.hasNext()) {
                add(iter.next());
            }
        }
    
public voidremove(int value)

inheritDoc

        if (value < Bits.getMax(bits)) {
            Bits.set(bits, value, false);
        }
    
public java.lang.StringtoString()

inheritDoc

        StringBuilder sb = new StringBuilder();

        sb.append('{");

        boolean first = true;
        for (int i = Bits.findFirst(bits, 0)
                ; i >= 0
                ; i = Bits.findFirst(bits, i + 1)) {
            if (!first) {
                sb.append(", ");
            }
            first = false;
            sb.append(i);
        }

        sb.append('}");

        return sb.toString();