/// <summary> /// And function for FDD's. /// </summary> /// <param name="u1">left side of the expression</param> /// <param name="u2">right side of the expression</param> /// <returns>a new BDD where u1 and u2 are bitwise and'ed</returns> public static Bdd And(Fdd u1, Fdd u2) { int u1Size = Size(u1); int u2Size = Size(u2); int MinSize = Min(u1Size, u2Size); Bdd result = new Bdd(false); for (int i = 0; i < MinSize; i++) { result = Kernel.Or(result, Kernel.And(new Bdd(u1.Var + i), new Bdd(u2.Var + i))); } if (u1Size != u2Size) { if (u1Size > u2Size) //you reach this state when 2 Fdds does not have the same number { //of bits. What happens is then the smallest get buffered with 0's for (int i = MinSize; i < u1Size; i++) { result = Kernel.Or(result, Kernel.And(new Bdd(u1.Var + i), new Bdd(false))); } } else { for (int i = MinSize; i < u2Size; i++) { result = Kernel.Or(result, Kernel.And(new Bdd(false), new Bdd(u2.Var + i))); } } } return(result); }
/// <summary> /// restricts an FDD out of an expression, by giving it an integer value. /// </summary> /// <param name="root">the root of the tree the Fdd is a part of</param> /// <param name="fdd">the Fdd to restrict</param> /// <param name="restrictValue">the given ristrict value, must be between 0 and maxvalue of the Fdd.</param> /// <returns>the restricted Bdd</returns> public static Bdd Restrict(Bdd root, Fdd fdd, int restrictValue) { bool[] B = ToBitPattern(restrictValue); for (int i = 0; i < B.Length; i++) { root = Kernel.Restrict(root, fdd.Var + i, B[i]); } RestrictDictionary.Add(fdd.Var, restrictValue); return(root); }
/// <summary> /// Enforces maxvalues by disallowing values normally room for with the size of bits in an fdd /// Value is normally expected to be the same as used in the constructor of the fdd. /// Note: (powers of 2) -1) does not need to be constrained: 7, 255 etc. /// </summary> /// <param name="fdd">The fdd to constrain</param> /// <param name="value">The values to enforce</param> public static Bdd EnforceMaxValue(Fdd fdd, int value) { Bdd result; //Always allow all bits where most significant bit isn't set result = Kernel.Not(new Bdd(fdd.Var)); int start = (int)Math.Pow(2, Size(fdd) - 1); //lowest number with most significant bit set for (int i = start; i <= value; i++) { result = Kernel.Or(result, Equal(fdd, i)); } return(result); }
/// <summary> /// Creates a hard binding sort of like an fdd restrict. /// Difference is that the binding will persist no matter /// what changes is later added. /// </summary> /// <param name="fdd">the Fdd to create a binding on</param> /// <param name="value">the constant number that is assigned to the Fdd.</param> /// <returns></returns> public static Bdd Equal(Fdd fdd, int value) { if (Log2(value) > FddDictionary[fdd.Var]) //value larger than fdd can hold. { return(new Bdd(false)); } bool[] b = ToBitPattern(value, Size(fdd)); Bdd result = new Bdd(true); for (int i = 0; i < b.Length; i++) { if (b[i]) { result = Kernel.And(result, new Bdd(fdd.Var + i)); } else { result = Kernel.And(result, Kernel.Not(new Bdd(fdd.Var + i))); } } return(result); }
/// <summary> /// returns the bit-size of a given FDD. /// </summary> /// <param name="fdd">The FDD to get the size of</param> /// <returns></returns> public static int Size(Fdd fdd) { return(FddDictionary[fdd.Var]); }
/// <summary> /// Compares Fdd's based on their position in the FDD array /// </summary> /// <param name="fdd">The FDD to compare the current FDD to</param> /// <returns>true if the FDD's have the same index, else false</returns> public bool Equals(Fdd fdd) { return(this.GetHashCode() == fdd.GetHashCode()); //GetHashCode is unique }