|
BitCode.java
|
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\ // PLEASE DO NOT EDIT WITHOUT THE EXPLICIT CONSENT OF THE AUTHOR! \\ //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
This class extends the class java.util.BitSet
with a few methods not provided by that class; mainly, static methods
for Boolean operations that reuse one of their arguments whenever
possible.
|
package hlt.osf.util;
import java.util.BitSet;
import hlt.language.util.IntIterator;
import hlt.osf.base.Sort;
import hlt.osf.exec.Context;
public class BitCode extends BitSet
{
| Constructs a new bit code; (all bits set to false). |
public BitCode ()
{
super();
}
Constructs a new bit code whose initial size is large enough to
explicitly represent bits with indices in the range 0 through
nbits-1; (all bits set to false).
|
public BitCode (int nbits)
{
super(nbits);
}
/* ************************************************************************ */
| This is used for the "set" character when displaying this as a string. It is '1' by default. |
private static char _ON = '1';
| This is used for the "not set" character when displaying this as a string. It is '0' by default. |
private static char _OFF = '0';
| This sets the "set" character when displaying this as a string to the specified character. |
public static void setOnChar (char c)
{
_ON = c;
}
| This sets the "not set" character when displaying this as a string to the specified character. |
public static void setOffChar (char c)
{
_OFF = c;
}
/* ************************************************************************ */
| This is used as a temporary reusable BitCode for avoiding unecessary multiple cloning. |
private static BitCode _TEMP = new BitCode(); /* ************************************************************************ */
| Returns true iff the set of set bits in this code is contained in that of the specified code. That is, iff (this and code) == this. |
public boolean isContainedIn (BitCode code)
{
_TEMP.clear(); // reset all bits to 0
_TEMP.or(this); // copy this into it
_TEMP.and(code); // intersect it with code
return this.equals(_TEMP);
}
| Returns true iff this code is strictly contained in the specified code. |
public boolean isStrictlyContainedIn (BitCode code)
{
return this.equals(code) ? false : this.isContainedIn(code);
}
| Returns true iff this code is unrelated to the specified code. |
public boolean isUnrelatedTo (BitCode code)
{
return !this.isContainedIn(code) && !code.isContainedIn(this) ;
}
/* ************************************************************************ */
| This is true iff this bitset may not be modified by the 3 boolean static operations 'not', 'and', and 'or' defined below. |
private boolean _isLocked = false;
| Returns true iff this bit code is locked - which means that it will not be modified by the 3 boolean static dyadic operations 'not', 'and', and 'or' defined below. |
public boolean isLocked ()
{
return _isLocked;
}
| Locks this bit code - which means that it will not be modified by the 3 boolean static dyadic operations 'not', 'and', and 'or' defined below. |
public BitCode lock ()
{
_isLocked = true;
return this;
}
| Unlocks this bit code - which means that it may be modified by the 3 boolean static dyadic operations 'not', 'and', and 'or' defined below. |
public BitCode unlock ()
{
_isLocked = false;
return this;
}
/* ************************************************************************ */
| Adds the specified index to this BitCode (i.e., it sets it to true), and returns this BitCode. |
public BitCode add (int index) throws LockedBitCodeException
{
if (_isLocked)
throw new LockedBitCodeException("Cannot add to a locked BitCode");
set(index);
return this;
}
/* ************************************************************************ */
| Removes the specified index from this BitCode (i.e., it sets it to false), and returns this BitCode. |
public BitCode remove (int index) throws LockedBitCodeException
{
if (_isLocked)
throw new LockedBitCodeException("Cannot remove from a locked BitCode");
set(index,false);
return this;
}
/* ************************************************************************ */
| Removes all the true bits from this BitCode (i.e., all set to false), and returns this BitCode. |
public BitCode erase () throws LockedBitCodeException
{
if (_isLocked)
throw new LockedBitCodeException("Cannot clear a locked BitCode");
super.clear();
return this;
}
/* ************************************************************************ */
| Returns a bit code that is the logical not of its bit code argument up to the sort code size. The bit code argument is left unchanged if locked. If the bit code argument is not locked, the returned bit code is the argument modified in place. Otherwise, a new bit code is returned with its bits set to their appropriate values. |
public static BitCode not (BitCode c)
{
if (!c.isLocked())
{
c.flip(0,Sort.codeSize());
return c;
}
BitCode newcode = c.copy();
newcode.flip(0,Sort.codeSize());
return newcode;
}
/* ************************************************************************ */
| Returns a bit code that is the logical and of its arguments, which are left unchanged if locked. If the first argument is not locked, the returned bit code is the first argument modified in place. Otherwise, if the second argument is not locked, the returned bit code is the second argument modified in place. Otherwise, a new bit code is returned with its bits set to their appropriate values. |
public static BitCode and (BitCode c1, BitCode c2)
{
if (!c1.isLocked())
{
c1.and(c2);
return c1;
}
if (!c2.isLocked())
{
c2.and(c1);
return c2;
}
BitCode newcode = c1.copy();
newcode.and(c2);
return newcode;
}
/* ************************************************************************ */
| Returns a bit code that is the logical or of its arguments, which are left unchanged if locked. If the first argument is not locked, the returned bit code is the first argument modified in place. Otherwise, if the second argument is not locked, the returned bit code is the second argument modified in place. Otherwise, a new bit code is returned with its bits set to their appropriate values. |
public static BitCode or (BitCode c1, BitCode c2)
{
if (!c1.isLocked())
{
c1.or(c2);
return c1;
}
if (!c2.isLocked())
{
c2.or(c1);
return c2;
}
BitCode newcode = c1.copy();
newcode.or(c2);
return newcode;
}
/* ************************************************************************ */
| Returns a new unlocked BitCode that is an equal copy of this one. |
public BitCode copy ()
{
if (Context.isTracing())
Context.tallyCodeCopy();
return ((BitCode)super.clone()).unlock();
}
/* ************************************************************************ */
| Returns this bit code of length equal to the sort code size as a string of ON and OFF characters (which default to 0 and 1). All bits at index higher or equal to that size are always false. These can be set to different characters using the static methods setOnChar and setOffChar. |
public String toBitString ()
{
String s = "";
for (int i=Sort.codeSize(); i-->0;)
s += get(i) ? _ON : _OFF;
return s;
}
| Returns this bit code as a bit string followed by a marker whether it is locked or not. |
public String toString ()
{
return toBitString()+" "+(_isLocked ? "[]" : "][");
}
/* ************************************************************************ */
public IntIterator iterator ()
{
return new BitCodeIterator(this);
}
private static class BitCodeIterator implements IntIterator
{
private BitCode _code;
private boolean _getNext = true;
private int _next = -1;
BitCodeIterator (BitCode code)
{
_code = code;
}
public final boolean hasNext ()
{
if (_getNext)
{
_next = _code.nextSetBit(_next+1);
_getNext = false;
}
return _next >= 0;
}
public final int next ()
{
_getNext = true;
return _next;
}
}
}
This file was generated on Wed Jan 30 13:26:29 CET 2013 from file BitCode.java
by the hlt.language.tools.Hilite Java tool written by Hassan Aït-Kaci