tclbdd

Check-in [d889c00657]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:add Restrict - no tests yet
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:d889c0065705c08e6ec9d482e5d8a3bf521150e4
User & Date: kennykb 2013-11-30 01:30:10
Context
2013-11-30
04:28
Added missing bddInt.h (whoops\!). Added code for AllSat - no Tcl support yet, so entirely untested. check-in: b11e4e2f87 user: kennykb tags: trunk
01:30
add Restrict - no tests yet check-in: d889c00657 user: kennykb tags: trunk
2013-11-29
16:16
sort methods correctly check-in: 37bd59506a user: kennykb tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to generic/bdd.c.

848
849
850
851
852
853
854
















































855
856
857
858
859
860
861
...
869
870
871
872
873
874
875

876
877
878
879
880
881
882
...
883
884
885
886
887
888
889
890
891
892



893
894
895
896
897
898
899
...
938
939
940
941
942
943
944


945
946
947
948
949
950
951
...
953
954
955
956
957
958
959

960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985




986
987
988
989
990
991
992
...
997
998
999
1000
1001
1002
1003








































































































1004
1005
1006
1007
1008
1009
1010
{
    return BDD_MakeBead(sysPtr, n, 1, 0);
}
 
/*
 *-----------------------------------------------------------------------------
 *
















































 * BDD_Negate --
 *
 *	Computes the negation of a BDD.
 *
 * Results:
 *	Returns a BDD which is true where the given BDD is false, and
 *	vice versa.
................................................................................
    BDD_System* sysPtr,		/* System of BDD's */
    BDD_BeadIndex u)		/* BDD to negate */
{
    Tcl_HashEntry* entryPtr;	/* Hash entry for precomputed negation */
    int newFlag;		/* Flag == 1 if no precomputed negation found */
    Bead* uPtr = sysPtr->beads+u;
				/* Pointer to the bead data */

    BDD_BeadIndex result;	/* Resulting BDD */

    /*
     * Handle constants
     */

    entryPtr = Tcl_CreateHashEntry(H, u, &newFlag);
................................................................................
    if (!newFlag) {
	result = (BDD_BeadIndex) Tcl_GetHashValue(entryPtr);
	++sysPtr->beads[result].refCount;
    } else if (u <= 1) {
	result = !u;
	++sysPtr->beads[result].refCount;
    } else {
	result = BDD_MakeBead(sysPtr, uPtr->level,
			      Negate(H, sysPtr, uPtr->low),
			      Negate(H, sysPtr, uPtr->high));



    }
    Tcl_SetHashValue(entryPtr, (ClientData) result);
    return result;
}
BDD_BeadIndex
BDD_Negate(
    BDD_System* sysPtr,		/* System of BDD's */
................................................................................
    Bead* u1Ptr = beads + u1;	/* Pointer to the left-hand bead */
    Bead* u2Ptr = beads + u2;	/* Pointer to the right-hand bead */
    BDD_VariableIndex level;	/* Level of the result */
    BDD_BeadIndex low1, high1;	/* Low and high transitions of the 
				 * left-hand bead */
    BDD_BeadIndex low2, high2;	/* Low and high transitions of the
				 * right-hand bead*/


    int newFlag;		/* Flag==1 if the output is a new bead */
    BDD_BeadIndex result;	/* Return value */
    Tcl_HashEntry* entry;	/* Pointer to the entry in the
				 * cache of beads for this operation */

    u[0] = u1;
    u[1] = u2;
................................................................................
    if (!newFlag) {
	result = (BDD_BeadIndex) Tcl_GetHashValue(entry);
	++sysPtr->beads[result].refCount;
    } else if (u1 <= 1 && u2 <= 1) {
	unsigned int i = ((unsigned int)u1 << 1) + (unsigned int)u2;
	result = ((op>>i) & 1);
	++sysPtr->beads[result].refCount;

    } else if (u1Ptr->level == u2Ptr->level) {
	level = u1Ptr->level;
	low1 = u1Ptr->low;
	high1 = u1Ptr->high;
	low2 = u2Ptr->low;
	high2 = u2Ptr->high;
	result = BDD_MakeBead(sysPtr,
			      level,
			      Apply(sysPtr, G, op, low1, low2),
			      Apply(sysPtr, G, op, high1, high2));
    } else if (u1Ptr->level < u2Ptr->level) {
	level = u1Ptr->level;
	low1 = u1Ptr->low;
	high1 = u1Ptr->high;
	result = BDD_MakeBead(sysPtr,
			      level,
			      Apply(sysPtr, G, op, low1, u2),
			      Apply(sysPtr, G, op, high1, u2));
    } else /* u1Ptr->level > u2Ptr->level */ {
	level = u2Ptr->level;
	low2 = u2Ptr->low;
	high2 = u2Ptr->high;
	result = BDD_MakeBead(sysPtr,
			      level,
			      Apply(sysPtr, G, op, u1, low2),
			      Apply(sysPtr, G, op, u1, high2));




    }
    Tcl_SetHashValue(entry, result);
    return result;
}

BDD_BeadIndex
BDD_Apply(
................................................................................
{
    Tcl_HashTable G;
    Tcl_InitCustomHashTable(&G, TCL_CUSTOM_TYPE_KEYS, &Bead2KeyType);
    BDD_BeadIndex entry = Apply(sysPtr, &G, op, u1, u2);
    Tcl_DeleteHashTable(&G);
    return entry;
}








































































































 
/*
 *-----------------------------------------------------------------------------
 *
 * BDD_SatCount --
 *
 *	Counts the number of variable assignments that satisfy the expression







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







>







 







<
|
|
>
>
>







 







>
>







 







>
|
|
|
|
|
|
<
<
|
|
|
|
|
|
<
<
|
|
|
|
|
|
<
<
|
|
>
>
>
>







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
...
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
...
932
933
934
935
936
937
938

939
940
941
942
943
944
945
946
947
948
949
950
...
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
....
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019


1020
1021
1022
1023
1024
1025


1026
1027
1028
1029
1030
1031


1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
....
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
{
    return BDD_MakeBead(sysPtr, n, 1, 0);
}
 
/*
 *-----------------------------------------------------------------------------
 *
 * BDD_Literal --
 *
 *	Tests if a BDD represents a single (possibly negated) literal
 *	and returns informatin about the literal if so.
 *
 * Results:
 *	Returns 1 if the BDD represents a literal and 0 if it does not.
 *
 * Side effects:
 *	If assignPtr is not NULL, sets *assignPtr to the variable index
 *	and value of the literal. A value of 1 is not negated; a value
 *	of 0 is negated.
 *
 *-----------------------------------------------------------------------------
 */

int
BDD_Literal(
    BDD_System* sysPtr,		/* System of BDD's */
    BDD_BeadIndex expr,		/* Expression to test */
    BDD_ValueAssignment* assignPtr)
				/* Description of the literal */
{
    /*
     * Constants are not literals
     */
    if (expr <= 1) {
	return 0;
    }

    /*
     * Literals have both next stats constant
     */
    Bead* beadPtr = sysPtr->beads + expr;
    if (beadPtr->high > 1 || beadPtr->low > 1) {
	return 0;
    }

    if (assignPtr) {
	assignPtr->var = beadPtr->level;
	assignPtr->value = beadPtr->high;
    }
    return 1;
}
 
/*
 *-----------------------------------------------------------------------------
 *
 * BDD_Negate --
 *
 *	Computes the negation of a BDD.
 *
 * Results:
 *	Returns a BDD which is true where the given BDD is false, and
 *	vice versa.
................................................................................
    BDD_System* sysPtr,		/* System of BDD's */
    BDD_BeadIndex u)		/* BDD to negate */
{
    Tcl_HashEntry* entryPtr;	/* Hash entry for precomputed negation */
    int newFlag;		/* Flag == 1 if no precomputed negation found */
    Bead* uPtr = sysPtr->beads+u;
				/* Pointer to the bead data */
    BDD_BeadIndex l, h;		/* Low and high transitions of the result */
    BDD_BeadIndex result;	/* Resulting BDD */

    /*
     * Handle constants
     */

    entryPtr = Tcl_CreateHashEntry(H, u, &newFlag);
................................................................................
    if (!newFlag) {
	result = (BDD_BeadIndex) Tcl_GetHashValue(entryPtr);
	++sysPtr->beads[result].refCount;
    } else if (u <= 1) {
	result = !u;
	++sysPtr->beads[result].refCount;
    } else {

	l = Negate(H, sysPtr, uPtr->low);
	h = Negate(H, sysPtr, uPtr->high);
	result = BDD_MakeBead(sysPtr, uPtr->level, l, h);
	BDD_UnrefBead(sysPtr, l);
	BDD_UnrefBead(sysPtr, h);
    }
    Tcl_SetHashValue(entryPtr, (ClientData) result);
    return result;
}
BDD_BeadIndex
BDD_Negate(
    BDD_System* sysPtr,		/* System of BDD's */
................................................................................
    Bead* u1Ptr = beads + u1;	/* Pointer to the left-hand bead */
    Bead* u2Ptr = beads + u2;	/* Pointer to the right-hand bead */
    BDD_VariableIndex level;	/* Level of the result */
    BDD_BeadIndex low1, high1;	/* Low and high transitions of the 
				 * left-hand bead */
    BDD_BeadIndex low2, high2;	/* Low and high transitions of the
				 * right-hand bead*/

    BDD_BeadIndex l, h;	        /* Low and high transitions of the result */
    int newFlag;		/* Flag==1 if the output is a new bead */
    BDD_BeadIndex result;	/* Return value */
    Tcl_HashEntry* entry;	/* Pointer to the entry in the
				 * cache of beads for this operation */

    u[0] = u1;
    u[1] = u2;
................................................................................
    if (!newFlag) {
	result = (BDD_BeadIndex) Tcl_GetHashValue(entry);
	++sysPtr->beads[result].refCount;
    } else if (u1 <= 1 && u2 <= 1) {
	unsigned int i = ((unsigned int)u1 << 1) + (unsigned int)u2;
	result = ((op>>i) & 1);
	++sysPtr->beads[result].refCount;
    } else {
	if (u1Ptr->level == u2Ptr->level) {
	    level = u1Ptr->level;
	    low1 = u1Ptr->low;
	    high1 = u1Ptr->high;
	    low2 = u2Ptr->low;
	    high2 = u2Ptr->high;


	    l = Apply(sysPtr, G, op, low1, low2);
	    h = Apply(sysPtr, G, op, high1, high2);
	} else if (u1Ptr->level < u2Ptr->level) {
	    level = u1Ptr->level;
	    low1 = u1Ptr->low;
	    high1 = u1Ptr->high;


	    l = Apply(sysPtr, G, op, low1, u2);
	    h = Apply(sysPtr, G, op, high1, u2);
	} else /* u1Ptr->level > u2Ptr->level */ {
	    level = u2Ptr->level;
	    low2 = u2Ptr->low;
	    high2 = u2Ptr->high;


	    l = Apply(sysPtr, G, op, u1, low2);
	    h = Apply(sysPtr, G, op, u1, high2);
	}
	result = BDD_MakeBead(sysPtr, level, l, h);
	BDD_UnrefBead(sysPtr, l);
	BDD_UnrefBead(sysPtr, h);
    }
    Tcl_SetHashValue(entry, result);
    return result;
}

BDD_BeadIndex
BDD_Apply(
................................................................................
{
    Tcl_HashTable G;
    Tcl_InitCustomHashTable(&G, TCL_CUSTOM_TYPE_KEYS, &Bead2KeyType);
    BDD_BeadIndex entry = Apply(sysPtr, &G, op, u1, u2);
    Tcl_DeleteHashTable(&G);
    return entry;
}
 
/*
 *-----------------------------------------------------------------------------
 *
 * BDD_Restrict --
 *
 *	Simplifies a BDD by restricting the values of certain variables
 *	to 0 or 1.
 *
 * Results:
 *	Returns the restricted BDD. Refcount must be decremented by
 *	the caller when the caller is done with the BDD.
 *
 * Notes:
 *	The restrictions must be listed in ascending order by variable
 *	number.
 *
 *-----------------------------------------------------------------------------
 */
static BDD_BeadIndex
Restrict(
    Tcl_HashTable* H,		/* Hash table of precomputed values */
    BDD_System* sysPtr,		/* System of BDD's */
    BDD_BeadIndex u,		/* BDD to restrict */
    const BDD_ValueAssignment r[], 
				/* Restrictions to apply */
    BDD_VariableIndex n)	/* Number of restrictions */
{
    int newFlag;		/* Flag = 1 if we have not seen u yet */
    Tcl_HashEntry* entryPtr;	/* Hash entry for u in precomputed values */
    BDD_BeadIndex l, h;		/* low and high branches of the resulting BDD */
    BDD_BeadIndex result;	/* Returned BDD */
    
    /* Handle tautologies and empty restriction sets */

    if (n == 0 || u <= 1) {
	++sysPtr->beads[u].refCount;
	return u;
    }

    /* Has this value been computed already? */

    entryPtr = Tcl_CreateHashEntry(H, u, &newFlag);
    if (newFlag) {
	Tcl_SetHashValue(entryPtr, 0);
    }
    BDD_BeadIndex cached = (BDD_BeadIndex) Tcl_GetHashValue(entryPtr);
    if (cached != 0) {
	++sysPtr->beads[cached].refCount;
	return cached;
    }

    /*
     * Value is not cached. Handle the first variable of the restriction
     */
    BDD_BeadIndex rvar = r[0].var;
    BDD_BeadIndex uvar = sysPtr->beads[u].level;
    if (rvar < uvar) {
	/*
	 * r[0] is an irrelevant variable in u
	 */
	result = Restrict(H, sysPtr, u, r+1, n-1);
    } else if (rvar == uvar) {
	/*
	 * r[0] appears in u. Bind it.
	 */
	if (r[0].value) {
	    result = Restrict(H, sysPtr, sysPtr->beads[u].high, r+1, n-1);
	} else {
	    result = Restrict(H, sysPtr, sysPtr->beads[u].low, r+1, n-1);
	}
    } else /* rvar > uvar */ {
	/*
	 * u's first variable is unrestricted. Apply the restriction to both
	 * successors of u, and make a bead for the restricted expression.
	 */
	l = Restrict(H, sysPtr, sysPtr->beads[u].low, r, n);
	h = Restrict(H, sysPtr, sysPtr->beads[u].high, r, n);
	result = BDD_MakeBead(sysPtr, uvar, l, h);
	BDD_UnrefBead(sysPtr, l);
	BDD_UnrefBead(sysPtr, h);
    }

    /*
     * Cache the result
     */
    Tcl_SetHashValue(entryPtr, (ClientData) result);
    return result;
}
BDD_BeadIndex
BDD_Restrict(
    BDD_System* sysPtr,		/* System of BDD's */
    BDD_BeadIndex u,		/* BDD to restrict */
    const BDD_ValueAssignment r[],
				/* Restrictions to apply */
    BDD_VariableIndex n)	/* Count of restrictions */
{
    Tcl_HashTable H;		/* Cache of partial results */
    BDD_BeadIndex result;	/* Bead index of the result */
    Tcl_InitHashTable(&H, TCL_ONE_WORD_KEYS);
    result = Restrict(&H, sysPtr, u, r, n);
    Tcl_DeleteHashTable(&H);
    return result;
}
 
/*
 *-----------------------------------------------------------------------------
 *
 * BDD_SatCount --
 *
 *	Counts the number of variable assignments that satisfy the expression

Changes to generic/bdd.h.

66
67
68
69
70
71
72









73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89






90
91
92




93
94
95
96
97
98
99
				 * half */
typedef unsigned int BDD_VariableIndex;
				/* Since the size of BDD's often is
				 * exponential in the number of variables,
				 * it's hard to imagine more than 
				 * an 'unsigned int' worth. */










#define BDDAPI /* TODO: work out the export gubbins */

extern BDDAPI BDD_System* BDD_NewSystem(BDD_BeadIndex n);
extern BDDAPI void BDD_SetVariableCount(BDD_System* sysPtr,
					BDD_VariableIndex n);
extern BDDAPI BDD_VariableIndex BDD_GetVariableCount(BDD_System* sysPtr);
extern BDDAPI BDD_BeadIndex BDD_NthVariable(BDD_System* sysPtr,
					    BDD_VariableIndex n);
extern BDDAPI BDD_BeadIndex BDD_NotNthVariable(BDD_System* sysPtr,
					       BDD_VariableIndex n);
extern BDDAPI BDD_BeadIndex BDD_MakeBead(BDD_System* sysPtr,
					 BDD_VariableIndex level,
					 BDD_BeadIndex low,
					 BDD_BeadIndex high);
extern BDDAPI void BDD_IncrBeadRefCount(BDD_System* sysPtr, 
					BDD_BeadIndex bead);
extern BDDAPI void BDD_UnrefBead(BDD_System* sysPtr, BDD_BeadIndex bead);






extern BDDAPI BDD_BeadIndex BDD_Negate(BDD_System* sysPtr, BDD_BeadIndex u);
extern BDDAPI BDD_BeadIndex BDD_Apply(BDD_System* sysPtr, BDD_BinOp op,
				      BDD_BeadIndex u1, BDD_BeadIndex u2);




extern BDDAPI int BDD_SatCount(BDD_System* sysPtr, BDD_BeadIndex x,
			       mp_int* count);
extern BDDAPI int BDD_Dump(Tcl_Interp*, Tcl_Obj*, BDD_System*, BDD_BeadIndex);
extern BDDAPI void BDD_DeleteSystem(BDD_System* sysPtr);


#endif /* not BDD_H_INCLUDED */







>
>
>
>
>
>
>
>
>






<
<
<
<







>
>
>
>
>
>



>
>
>
>







66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87




88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
				 * half */
typedef unsigned int BDD_VariableIndex;
				/* Since the size of BDD's often is
				 * exponential in the number of variables,
				 * it's hard to imagine more than 
				 * an 'unsigned int' worth. */

/*
 * Value assignment: used in making conjunctions and restrictions,
 * and in reporting out satisfying expressions.
 */
typedef struct BDD_ValueAssignment {
    BDD_VariableIndex var;	/* Variable */
    int value;			/* Boolean value */
} BDD_ValueAssignment;

#define BDDAPI /* TODO: work out the export gubbins */

extern BDDAPI BDD_System* BDD_NewSystem(BDD_BeadIndex n);
extern BDDAPI void BDD_SetVariableCount(BDD_System* sysPtr,
					BDD_VariableIndex n);
extern BDDAPI BDD_VariableIndex BDD_GetVariableCount(BDD_System* sysPtr);




extern BDDAPI BDD_BeadIndex BDD_MakeBead(BDD_System* sysPtr,
					 BDD_VariableIndex level,
					 BDD_BeadIndex low,
					 BDD_BeadIndex high);
extern BDDAPI void BDD_IncrBeadRefCount(BDD_System* sysPtr, 
					BDD_BeadIndex bead);
extern BDDAPI void BDD_UnrefBead(BDD_System* sysPtr, BDD_BeadIndex bead);
extern BDDAPI BDD_BeadIndex BDD_NthVariable(BDD_System* sysPtr,
					    BDD_VariableIndex n);
extern BDDAPI BDD_BeadIndex BDD_NotNthVariable(BDD_System* sysPtr,
					       BDD_VariableIndex n);
extern BDDAPI int BDD_Literal(BDD_System* sysPtr, BDD_BeadIndex expr,
			      BDD_ValueAssignment* assignPtr);
extern BDDAPI BDD_BeadIndex BDD_Negate(BDD_System* sysPtr, BDD_BeadIndex u);
extern BDDAPI BDD_BeadIndex BDD_Apply(BDD_System* sysPtr, BDD_BinOp op,
				      BDD_BeadIndex u1, BDD_BeadIndex u2);
extern BDDAPI BDD_BeadIndex BDD_Restrict(BDD_System* sysPtr,
					 BDD_BeadIndex u,
					 const BDD_ValueAssignment r[],
					 BDD_VariableIndex n);
extern BDDAPI int BDD_SatCount(BDD_System* sysPtr, BDD_BeadIndex x,
			       mp_int* count);
extern BDDAPI int BDD_Dump(Tcl_Interp*, Tcl_Obj*, BDD_System*, BDD_BeadIndex);
extern BDDAPI void BDD_DeleteSystem(BDD_System* sysPtr);


#endif /* not BDD_H_INCLUDED */

Changes to generic/tclBdd.c.

111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
...
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
...
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
...
881
882
883
884
885
886
887


























































































888
889
890
891
892
893
894
			       int, Tcl_Obj* const[]);
static int BddSystemNegateMethod(ClientData, Tcl_Interp*, Tcl_ObjectContext,
				 int, Tcl_Obj* const[]);
static int BddSystemNotnthvarMethod(ClientData, Tcl_Interp*, Tcl_ObjectContext,
				    int, Tcl_Obj* const[]);
static int BddSystemNthvarMethod(ClientData, Tcl_Interp*, Tcl_ObjectContext,
				 int, Tcl_Obj* const[]);
/* not yet there
static int BddSystemRestrictMethod(ClientData, Tcl_Interp*, Tcl_ObjectContext,
				   int, Tcl_Obj* const[]);
*/
static int BddSystemSatcountMethod(ClientData, Tcl_Interp*, Tcl_ObjectContext,
				   int, Tcl_Obj* const[]);
static int BddSystemUnsetMethod(ClientData, Tcl_Interp*, Tcl_ObjectContext,
				int, Tcl_Obj* const[]);
static int CloneBddSystemObject(Tcl_Interp*, ClientData, ClientData*);
static int CloneMethod(Tcl_Interp*, ClientData, ClientData*);
static void DeleteBddSystemData(BddSystemData*);
................................................................................
const static Tcl_MethodType BddSystemNthvarMethodType = {
    TCL_OO_METHOD_VERSION_CURRENT, /* version */
    "nthvar",			   /* name */
    BddSystemNthvarMethod,	   /* callProc */
    DeleteMethod,		   /* method delete proc */
    CloneMethod			   /* method clone proc */
};
#if 0
/* not yet there */
const static Tcl_MethodType BddSystemRestrictMethodType = {
    TCL_OO_METHOD_VERSION_CURRENT, /* version */
    "restrict",			   /* name */
    BddSystemRestrictMethod,	   /* callProc */
    DeleteMethod,		   /* method delete proc */
    CloneMethod			   /* method clone proc */
};
#endif
const static Tcl_MethodType BddSystemSatcountMethodType = {
    TCL_OO_METHOD_VERSION_CURRENT, /* version */
    "satcount",			   /* name */
    BddSystemSatcountMethod,	   /* callProc */
    DeleteMethod,		   /* method delete proc */
    CloneMethod			   /* method clone proc */
};
................................................................................
    { "constant",  &BddSystemConstantMethodType,  NULL },
    { "copy",      &BddSystemCopyMethodType,      NULL },
    { "dump",      &BddSystemDumpMethodType,      NULL },
    { "nand",      &BddSystemBinopMethodType,     (ClientData) BDD_BINOP_NAND },
    { "nor",       &BddSystemBinopMethodType,     (ClientData) BDD_BINOP_NOR },
    { "notnthvar", &BddSystemNotnthvarMethodType, NULL },
    { "nthvar",    &BddSystemNthvarMethodType,    NULL },
    /* not yet there
    { "restrict",  &BddSystemRestrictMethodType,  NULL },
    */
    { "satcount",  &BddSystemSatcountMethodType,  NULL },
    { "unset",     &BddSystemUnsetMethodType,     NULL },
    { "|",         &BddSystemBinopMethodType,     (ClientData) BDD_BINOP_OR },
    { "~",         &BddSystemNegateMethodType,    NULL },
    { NULL,	   NULL,                         NULL }
};
 
................................................................................
	return TCL_ERROR;
    }
    beadIndex = BDD_NotNthVariable(sdata->system, (BDD_VariableIndex) varNum);
    SetNamedExpression(sdata, objv[skipped], beadIndex);
    BDD_UnrefBead(sdata->system, beadIndex);
    return TCL_OK;
}


























































































 
/*
 *-----------------------------------------------------------------------------
 *
 * BddSystemSatcountMethod --
 *
 *	Computes and returns the number of satisfying assignments for







<


<







 







<
<







<







 







<

<







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







111
112
113
114
115
116
117

118
119

120
121
122
123
124
125
126
...
202
203
204
205
206
207
208


209
210
211
212
213
214
215

216
217
218
219
220
221
222
...
245
246
247
248
249
250
251

252

253
254
255
256
257
258
259
...
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
			       int, Tcl_Obj* const[]);
static int BddSystemNegateMethod(ClientData, Tcl_Interp*, Tcl_ObjectContext,
				 int, Tcl_Obj* const[]);
static int BddSystemNotnthvarMethod(ClientData, Tcl_Interp*, Tcl_ObjectContext,
				    int, Tcl_Obj* const[]);
static int BddSystemNthvarMethod(ClientData, Tcl_Interp*, Tcl_ObjectContext,
				 int, Tcl_Obj* const[]);

static int BddSystemRestrictMethod(ClientData, Tcl_Interp*, Tcl_ObjectContext,
				   int, Tcl_Obj* const[]);

static int BddSystemSatcountMethod(ClientData, Tcl_Interp*, Tcl_ObjectContext,
				   int, Tcl_Obj* const[]);
static int BddSystemUnsetMethod(ClientData, Tcl_Interp*, Tcl_ObjectContext,
				int, Tcl_Obj* const[]);
static int CloneBddSystemObject(Tcl_Interp*, ClientData, ClientData*);
static int CloneMethod(Tcl_Interp*, ClientData, ClientData*);
static void DeleteBddSystemData(BddSystemData*);
................................................................................
const static Tcl_MethodType BddSystemNthvarMethodType = {
    TCL_OO_METHOD_VERSION_CURRENT, /* version */
    "nthvar",			   /* name */
    BddSystemNthvarMethod,	   /* callProc */
    DeleteMethod,		   /* method delete proc */
    CloneMethod			   /* method clone proc */
};


const static Tcl_MethodType BddSystemRestrictMethodType = {
    TCL_OO_METHOD_VERSION_CURRENT, /* version */
    "restrict",			   /* name */
    BddSystemRestrictMethod,	   /* callProc */
    DeleteMethod,		   /* method delete proc */
    CloneMethod			   /* method clone proc */
};

const static Tcl_MethodType BddSystemSatcountMethodType = {
    TCL_OO_METHOD_VERSION_CURRENT, /* version */
    "satcount",			   /* name */
    BddSystemSatcountMethod,	   /* callProc */
    DeleteMethod,		   /* method delete proc */
    CloneMethod			   /* method clone proc */
};
................................................................................
    { "constant",  &BddSystemConstantMethodType,  NULL },
    { "copy",      &BddSystemCopyMethodType,      NULL },
    { "dump",      &BddSystemDumpMethodType,      NULL },
    { "nand",      &BddSystemBinopMethodType,     (ClientData) BDD_BINOP_NAND },
    { "nor",       &BddSystemBinopMethodType,     (ClientData) BDD_BINOP_NOR },
    { "notnthvar", &BddSystemNotnthvarMethodType, NULL },
    { "nthvar",    &BddSystemNthvarMethodType,    NULL },

    { "restrict",  &BddSystemRestrictMethodType,  NULL },

    { "satcount",  &BddSystemSatcountMethodType,  NULL },
    { "unset",     &BddSystemUnsetMethodType,     NULL },
    { "|",         &BddSystemBinopMethodType,     (ClientData) BDD_BINOP_OR },
    { "~",         &BddSystemNegateMethodType,    NULL },
    { NULL,	   NULL,                         NULL }
};
 
................................................................................
	return TCL_ERROR;
    }
    beadIndex = BDD_NotNthVariable(sdata->system, (BDD_VariableIndex) varNum);
    SetNamedExpression(sdata, objv[skipped], beadIndex);
    BDD_UnrefBead(sdata->system, beadIndex);
    return TCL_OK;
}
 
/*
 *-----------------------------------------------------------------------------
 *
 * BddSystemRestrictMethod --
 *
 *	Computes a BDD restricted to a particular set of variable values
 *
 * Usage:
 *	$system restrict name expr r1 r2 ...
 *
 * Parameters:
 *	name - The name to assign
 *	expr - The expression to restrict
 *	r1 r2 ... - Restrictions. The restrictions must be named expressions
 *                  resulting from 'nthvar' or 'notnthvar' (or other
 *                  expressions representing a single, possibly negated,
 *		    literal.
 *
 * Results:
 *	None
 *
 * Side effects:
 *	Assigns the negation of the given variable to the new name
 *
 *-----------------------------------------------------------------------------
 */

static int
BddSystemRestrictMethod(
    ClientData clientData,	/* Operation to perform */
    Tcl_Interp* interp,		/* Tcl interpreter */
    Tcl_ObjectContext ctx,	/* Object context */
    int objc,			/* Parameter count */
    Tcl_Obj *const objv[])	/* Parameter vector */
{
    Tcl_Object thisObject = Tcl_ObjectContextObject(ctx);
				/* The current object */
    BddSystemData* sdata = (BddSystemData*)
	Tcl_ObjectGetMetadata(thisObject, &BddSystemDataType);
				/* The current system of expressions */
    int skipped = Tcl_ObjectContextSkippedArgs(ctx);
				/* The number of args used in method dispatch */
    BDD_BeadIndex expr;		/* The expression to restrict */
    BDD_VariableIndex nRestrictions;
				/* How many restrictions? */
    BDD_ValueAssignment* restrictions;
    int i;
    BDD_VariableIndex j;
    BDD_BeadIndex literalIndex;
    Tcl_Obj* errorMessage;
    BDD_BeadIndex result;
    
    /* Check syntax */

    if (objc < skipped+2) {
	Tcl_WrongNumArgs(interp, skipped, objv, "name expr ?restriction?...");
	return TCL_ERROR;
    }
    if (FindNamedExpression(interp, sdata, objv[skipped+1],
			    &expr) != TCL_OK) {
	return TCL_ERROR;
    }
    if (objc >= skipped+3) {
	nRestrictions = objc-skipped-2;
	restrictions = (BDD_ValueAssignment*)
	    ckalloc(nRestrictions * sizeof(BDD_ValueAssignment));;
	for (i = skipped+2, j = 0; i < objc; ++i, ++j) {
	    /* TODO - Refactor into FindLiteral and FindLiteralList */
	    if (FindNamedExpression(interp, sdata, objv[i],
				    &literalIndex) != TCL_OK) {
		ckfree(restrictions);
		return TCL_ERROR;
	    }
	    if (!BDD_Literal(sdata->system, literalIndex, restrictions+j)) {
		errorMessage = Tcl_ObjPrintf("%s is not a literal",
					     Tcl_GetString(objv[i]));
		Tcl_SetObjResult(interp, errorMessage);
		Tcl_SetErrorCode(interp, "BDD", "NotLiteral",
				 Tcl_GetString(objv[i]));
		ckfree(restrictions);
		return TCL_ERROR;
	    }
	}
	result = BDD_Restrict(sdata->system, expr, restrictions, nRestrictions);
	SetNamedExpression(sdata, objv[skipped], result);
	BDD_UnrefBead(sdata->system, result);
    }
    return TCL_OK;
}
 
/*
 *-----------------------------------------------------------------------------
 *
 * BddSystemSatcountMethod --
 *
 *	Computes and returns the number of satisfying assignments for

Changes to tests/bdd.test.

829
830
831
832
833
834
835




















836
837
838
839
840
841
842
	list [dict size [sys dump r0]] [format %llx [sys satcount r0]]
    }
    -cleanup {
	rename sys {}
    }
    -result {257 80000000000000000000000000000000}
}





















cleanupTests
return

# Local Variables:
# mode: tcl
# End:







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
	list [dict size [sys dump r0]] [format %llx [sys satcount r0]]
    }
    -cleanup {
	rename sys {}
    }
    -result {257 80000000000000000000000000000000}
}

test bdd-13.n {restrict - reduce to constant} {*}{
    -setup {
	bdd::system create sys
	sys nthvar a 0
	sys nthvar b 1
	sys nthvar c 2
    }
    -body {
	sys & r1 a b
	sys & r1 r1 c
	sys restrict r1 r1 a b
	sys restrict r2 r1 c
	sys beadindex r2
    }
    -cleanup {
	rename sys {}
    }
    -result 1
}

cleanupTests
return

# Local Variables:
# mode: tcl
# End: