public BDD varRange(BigInteger lo, BigInteger hi) { if (lo.signum() < 0 || hi.compareTo(size()) >= 0 || lo.compareTo(hi) > 0) { throw new BDDException("range <" + lo + ", " + hi + "> is invalid"); } BDDFactory factory = getFactory(); BDD result = factory.zero(); int[] ivar = this.vars(); while (lo.compareTo(hi) <= 0) { BDD v = factory.one(); for (int n = ivar.Length - 1; ; n--) { if (lo.testBit(n)) { v.andWith(factory.ithVar(ivar[n])); } else { v.andWith(factory.nithVar(ivar[n])); } BigInteger mask = BigInteger.ONE.shiftLeft(n).subtract(BigInteger.ONE); if (!lo.testBit(n) && lo.or(mask).compareTo(hi) <= 0) { lo = lo.or(mask).add(BigInteger.ONE); break; } } result.orWith(v); } return(result); }
public BDD ithVar(BigInteger val) { if (val.signum() < 0 || val.compareTo(size()) >= 0) { throw new BDDException(val + " is out of range"); } BDDFactory factory = getFactory(); BDD v = factory.one(); int[] ivar = this.vars(); for (int n = 0; n < ivar.Length; n++) { if (val.testBit(0)) { v.andWith(factory.ithVar(ivar[n])); } else { v.andWith(factory.nithVar(ivar[n])); } val = val.shiftRight(1); } return(v); }
public int ensureCapacity(BigInteger range) { BigInteger calcsize = BigInteger.valueOf(2L); if (range.signum() < 0) { throw new BDDException(); } if (range.compareTo(realsize) < 0) { return(ivar.Length); } this.realsize = range.add(BigInteger.ONE); int binsize = 1; while (calcsize.compareTo(range) <= 0) { binsize++; calcsize = calcsize.shiftLeft(1); } if (ivar.Length == binsize) { return(binsize); } int[] new_ivar = new int[binsize]; System.arraycopy(ivar, 0, new_ivar, 0, ivar.Length); BDDFactory factory = getFactory(); for (int i = ivar.Length; i < new_ivar.Length; ++i) { //System.out.println("Domain "+this+" Duplicating var#"+new_ivar[i-1]); int newVar = factory.duplicateVar(new_ivar[i - 1]); factory.firstbddvar++; new_ivar[i] = newVar; //System.out.println("Domain "+this+" var#"+i+" = "+newVar); } this.ivar = new_ivar; //System.out.println("Domain "+this+" old var = "+var); this.var.free(); BDD nvar = factory.one(); for (int i = 0; i < ivar.Length; ++i) { nvar.andWith(factory.ithVar(ivar[i])); } this.var = nvar; //System.out.println("Domain "+this+" new var = "+var); return(binsize); }
public BDD buildAdd(BDDDomain that, int bits, long value) { if (bits > this.varNum() || bits > that.varNum()) { throw new BDDException("Number of bits requested (" + bits + ") is larger than domain sizes " + this.varNum() + "," + that.varNum()); } BDDFactory bdd = getFactory(); if (value == 0L) { BDD result = bdd.one(); int n; for (n = 0; n < bits; n++) { BDD b = bdd.ithVar(this.ivar[n]); b.biimpWith(bdd.ithVar(that.ivar[n])); result.andWith(b); } for (; n < Math.Max(this.varNum(), that.varNum()); n++) { BDD b = (n < this.varNum()) ? bdd.nithVar(this.ivar[n]) : bdd.one(); b.andWith((n < that.varNum()) ? bdd.nithVar(that.ivar[n]) : bdd.one()); result.andWith(b); } return(result); } int[] vars = new int[bits]; System.arraycopy(this.ivar, 0, vars, 0, vars.Length); BDDBitVector y = bdd.buildVector(vars); BDDBitVector v = bdd.constantVector(bits, value); BDDBitVector z = y.add(v); int[] thatvars = new int[bits]; System.arraycopy(that.ivar, 0, thatvars, 0, thatvars.Length); BDDBitVector x = bdd.buildVector(thatvars); BDD result = bdd.one(); int n; for (n = 0; n < x.size(); n++) { BDD b = x.bitvec[n].biimp(z.bitvec[n]); result.andWith(b); } for (; n < Math.Max(this.varNum(), that.varNum()); n++) { BDD b = (n < this.varNum()) ? bdd.nithVar(this.ivar[n]) : bdd.one(); b.andWith((n < that.varNum()) ? bdd.nithVar(that.ivar[n]) : bdd.one()); result.andWith(b); } x.free(); y.free(); z.free(); v.free(); return(result); }
/** * <p>Returns what corresponds to a disjunction of all possible values of this * domain. This is more efficient than doing ithVar(0) OR ithVar(1) ... * explicitly for all values in the domain.</p> * * <p>Compare to fdd_domain.</p> */ public BDD domain() { BDDFactory factory = getFactory(); /* Encode V<=X-1. V is the variables in 'var' and X is the domain size */ BigInteger val = size().subtract(BigInteger.ONE); BDD d = factory.one(); int[] ivar = vars(); for (int n = 0; n < this.varNum(); n++) { if (val.testBit(0)) { d.orWith(factory.nithVar(ivar[n])); } else { d.andWith(factory.nithVar(ivar[n])); } val = val.shiftRight(1); } return(d); }
/** * Builds a BDD which is true for all the possible assignments to the * variable blocks that makes the blocks equal. * * Compare to fdd_equals/fdd_equ. * * @param that * @return BDD */ public BDD buildEquals(BDDDomain that) { if (!this.size().equals(that.size())) { throw new BDDException("Size of " + this + " != size of that " + that + "( " + this.size() + " vs " + that.size() + ")"); } BDDFactory factory = getFactory(); BDD e = factory.one(); int[] this_ivar = this.vars(); int[] that_ivar = that.vars(); for (int n = 0; n < this.varNum(); n++) { BDD a = factory.ithVar(this_ivar[n]); BDD b = factory.ithVar(that_ivar[n]); a.biimpWith(b); e.andWith(a); } return(e); }