⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bitset.java

📁 antlr最新版本V3源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* [The "BSD licence"] Copyright (c) 2005-2006 Terence Parr All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright    notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright    notice, this list of conditions and the following disclaimer in the    documentation and/or other materials provided with the distribution. 3. The name of the author may not be used to endorse or promote products    derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/package org.antlr.misc;import org.antlr.analysis.Label;import org.antlr.tool.Grammar;import java.util.Collection;import java.util.Iterator;import java.util.List;import java.util.Map;/**A BitSet to replace java.util.BitSet. * * Primary differences are that most set operators return new sets * as opposed to oring and anding "in place".  Further, a number of * operations were added.  I cannot contain a BitSet because there * is no way to access the internal bits (which I need for speed) * and, because it is final, I cannot subclass to add functionality. * Consider defining set degree.  Without access to the bits, I must * call a method n times to test the ith bit...ack! * * Also seems like or() from util is wrong when size of incoming set is bigger * than this.bits.length. * * @author Terence Parr */public class BitSet implements IntSet, Cloneable {    protected final static int BITS = 64;    // number of bits / long    protected final static int LOG_BITS = 6; // 2^6 == 64    /* We will often need to do a mod operator (i mod nbits).  Its     * turns out that, for powers of two, this mod operation is     * same as (i & (nbits-1)).  Since mod is slow, we use a     * precomputed mod mask to do the mod instead.     */    protected final static int MOD_MASK = BITS - 1;    /** The actual data bits */    protected long bits[];    /** Construct a bitset of size one word (64 bits) */    public BitSet() {        this(BITS);    }    /** Construction from a static array of longs */    public BitSet(long[] bits_) {        bits = bits_;    }    /** Construct a bitset given the size     * @param nbits The size of the bitset in bits     */    public BitSet(int nbits) {        bits = new long[((nbits - 1) >> LOG_BITS) + 1];    }    /** or this element into this set (grow as necessary to accommodate) */    public void add(int el) {        //System.out.println("add("+el+")");        int n = wordNumber(el);        //System.out.println("word number is "+n);        //System.out.println("bits.length "+bits.length);        if (n >= bits.length) {            growToInclude(el);        }        bits[n] |= bitMask(el);    }    public void addAll(IntSet set) {        if ( set instanceof BitSet ) {            this.orInPlace((BitSet)set);        }		else if ( set instanceof IntervalSet ) {			IntervalSet other = (IntervalSet)set;			// walk set and add each interval			for (Iterator iter = other.intervals.iterator(); iter.hasNext();) {				Interval I = (Interval) iter.next();				this.orInPlace(BitSet.range(I.a,I.b));			}		}		else {			throw new IllegalArgumentException("can't add "+											   set.getClass().getName()+											   " to BitSet");		}    }	public void addAll(int[] elements) {		if ( elements==null ) {			return;		}		for (int i = 0; i < elements.length; i++) {			int e = elements[i];			add(e);		}	}	public void addAll(List elements) {		if ( elements==null ) {			return;		}		for (int i = 0; i < elements.size(); i++) {			Object o = elements.get(i);			if ( !(o instanceof Integer) ) {				throw new IllegalArgumentException();			}			Integer eI = (Integer)o;			add(eI.intValue());		}	}    public IntSet and(IntSet a) {        BitSet s = (BitSet)this.clone();        s.andInPlace((BitSet)a);        return s;    }    public void andInPlace(BitSet a) {        int min = Math.min(bits.length, a.bits.length);        for (int i = min - 1; i >= 0; i--) {            bits[i] &= a.bits[i];        }        // clear all bits in this not present in a (if this bigger than a).        for (int i = min; i < bits.length; i++) {            bits[i] = 0;        }    }    private final static long bitMask(int bitNumber) {        int bitPosition = bitNumber & MOD_MASK; // bitNumber mod BITS        return 1L << bitPosition;    }    public void clear() {        for (int i = bits.length - 1; i >= 0; i--) {            bits[i] = 0;        }    }    public void clear(int el) {        int n = wordNumber(el);        if (n >= bits.length) {	// grow as necessary to accommodate            growToInclude(el);        }        bits[n] &= ~bitMask(el);    }    public Object clone() {        BitSet s;        try {            s = (BitSet)super.clone();            s.bits = new long[bits.length];            System.arraycopy(bits, 0, s.bits, 0, bits.length);        }        catch (CloneNotSupportedException e) {            throw new InternalError();        }        return s;    }    public int size() {        int deg = 0;        for (int i = bits.length - 1; i >= 0; i--) {            long word = bits[i];            if (word != 0L) {                for (int bit = BITS - 1; bit >= 0; bit--) {                    if ((word & (1L << bit)) != 0) {                        deg++;                    }                }            }        }        return deg;    }    public boolean equals(Object other) {        if ( other == null || !(other instanceof BitSet) ) {            return false;        }        BitSet otherSet = (BitSet)other;        int n = Math.min(this.bits.length, otherSet.bits.length);        // for any bits in common, compare        for (int i=0; i<n; i++) {            if (this.bits[i] != otherSet.bits[i]) {                return false;            }        }        // make sure any extra bits are off        if (this.bits.length > n) {            for (int i = n+1; i<this.bits.length; i++) {                if (this.bits[i] != 0) {                    return false;                }            }        }        else if (otherSet.bits.length > n) {            for (int i = n+1; i<otherSet.bits.length; i++) {                if (otherSet.bits[i] != 0) {                    return false;                }            }        }        return true;    }    /**     * Grows the set to a larger number of bits.     * @param bit element that must fit in set     */    public void growToInclude(int bit) {        int newSize = Math.max(bits.length << 1, numWordsToHold(bit));        long newbits[] = new long[newSize];        System.arraycopy(bits, 0, newbits, 0, bits.length);        bits = newbits;    }    public boolean member(int el) {        int n = wordNumber(el);        if (n >= bits.length) return false;        return (bits[n] & bitMask(el)) != 0;    }    /** Get the first element you find and return it.  Return Label.INVALID     *  otherwise.     */    public int getSingleElement() {        for (int i = 0; i < (bits.length << LOG_BITS); i++) {            if (member(i)) {                return i;            }        }        return Label.INVALID;    }    public boolean isNil() {        for (int i = bits.length - 1; i >= 0; i--) {            if (bits[i] != 0) return false;        }        return true;    }    public IntSet complement() {        BitSet s = (BitSet)this.clone();        s.notInPlace();        return s;    }    public IntSet complement(IntSet set) {		if ( set==null ) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -