1 /**CFile***********************************************************************
3 FileName [cuddAddFind.c]
7 Synopsis [Functions to find maximum and minimum in an ADD and to
10 Description [External procedures included in this module:
12 <li> Cudd_addFindMax()
13 <li> Cudd_addFindMin()
16 Static functions included in this module:
21 Author [Fabio Somenzi]
23 Copyright [Copyright (c) 1995-2004, Regents of the University of Colorado
27 Redistribution and use in source and binary forms, with or without
28 modification, are permitted provided that the following conditions
31 Redistributions of source code must retain the above copyright
32 notice, this list of conditions and the following disclaimer.
34 Redistributions in binary form must reproduce the above copyright
35 notice, this list of conditions and the following disclaimer in the
36 documentation and/or other materials provided with the distribution.
38 Neither the name of the University of Colorado nor the names of its
39 contributors may be used to endorse or promote products derived from
40 this software without specific prior written permission.
42 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
43 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
44 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
45 FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
46 COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
47 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
48 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
49 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
50 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
52 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
53 POSSIBILITY OF SUCH DAMAGE.]
55 ******************************************************************************/
60 /*---------------------------------------------------------------------------*/
61 /* Constant declarations */
62 /*---------------------------------------------------------------------------*/
65 /*---------------------------------------------------------------------------*/
66 /* Stucture declarations */
67 /*---------------------------------------------------------------------------*/
70 /*---------------------------------------------------------------------------*/
71 /* Type declarations */
72 /*---------------------------------------------------------------------------*/
75 /*---------------------------------------------------------------------------*/
76 /* Variable declarations */
77 /*---------------------------------------------------------------------------*/
80 static char rcsid
[] DD_UNUSED
= "$Id: cuddAddFind.c,v 1.8 2004/08/13 18:04:45 fabio Exp $";
84 /*---------------------------------------------------------------------------*/
85 /* Macro declarations */
86 /*---------------------------------------------------------------------------*/
92 /**AutomaticStart*************************************************************/
94 /*---------------------------------------------------------------------------*/
95 /* Static function prototypes */
96 /*---------------------------------------------------------------------------*/
98 static DdNode
* addDoIthBit (DdManager
*dd
, DdNode
*f
, DdNode
*index
);
100 /**AutomaticEnd***************************************************************/
106 /*---------------------------------------------------------------------------*/
107 /* Definition of exported functions */
108 /*---------------------------------------------------------------------------*/
110 /**Function********************************************************************
112 Synopsis [Finds the maximum discriminant of f.]
114 Description [Returns a pointer to a constant ADD.]
118 ******************************************************************************/
127 if (cuddIsConstant(f
)) {
131 res
= cuddCacheLookup1(dd
,Cudd_addFindMax
,f
);
136 t
= Cudd_addFindMax(dd
,cuddT(f
));
137 if (t
== DD_PLUS_INFINITY(dd
)) return(t
);
139 e
= Cudd_addFindMax(dd
,cuddE(f
));
141 res
= (cuddV(t
) >= cuddV(e
)) ? t
: e
;
143 cuddCacheInsert1(dd
,Cudd_addFindMax
,f
,res
);
147 } /* end of Cudd_addFindMax */
150 /**Function********************************************************************
152 Synopsis [Finds the minimum discriminant of f.]
154 Description [Returns a pointer to a constant ADD.]
158 ******************************************************************************/
167 if (cuddIsConstant(f
)) {
171 res
= cuddCacheLookup1(dd
,Cudd_addFindMin
,f
);
176 t
= Cudd_addFindMin(dd
,cuddT(f
));
177 if (t
== DD_MINUS_INFINITY(dd
)) return(t
);
179 e
= Cudd_addFindMin(dd
,cuddE(f
));
181 res
= (cuddV(t
) <= cuddV(e
)) ? t
: e
;
183 cuddCacheInsert1(dd
,Cudd_addFindMin
,f
,res
);
187 } /* end of Cudd_addFindMin */
190 /**Function********************************************************************
192 Synopsis [Extracts the i-th bit from an ADD.]
194 Description [Produces an ADD from another ADD by replacing all
195 discriminants whose i-th bit is equal to 1 with 1, and all other
196 discriminants with 0. The i-th bit refers to the integer
197 representation of the leaf value. If the value is has a fractional
198 part, it is ignored. Repeated calls to this procedure allow one to
199 transform an integer-valued ADD into an array of ADDs, one for each
200 bit of the leaf values. Returns a pointer to the resulting ADD if
201 successful; NULL otherwise.]
205 SeeAlso [Cudd_addBddIthBit]
207 ******************************************************************************/
217 /* Use a constant node to remember the bit, so that we can use the
220 index
= cuddUniqueConst(dd
,(CUDD_VALUE_TYPE
) bit
);
221 if (index
== NULL
) return(NULL
);
226 res
= addDoIthBit(dd
, f
, index
);
227 } while (dd
->reordered
== 1);
230 Cudd_RecursiveDeref(dd
, index
);
234 Cudd_RecursiveDeref(dd
, index
);
238 } /* end of Cudd_addIthBit */
241 /*---------------------------------------------------------------------------*/
242 /* Definition of internal functions */
243 /*---------------------------------------------------------------------------*/
246 /*---------------------------------------------------------------------------*/
247 /* Definition of static functions */
248 /*---------------------------------------------------------------------------*/
251 /**Function********************************************************************
253 Synopsis [Performs the recursive step for Cudd_addIthBit.]
255 Description [Performs the recursive step for Cudd_addIthBit.
256 Returns a pointer to the BDD if successful; NULL otherwise.]
262 ******************************************************************************/
275 /* Check terminal case. */
276 if (cuddIsConstant(f
)) {
277 mask
= 1 << ((int) cuddV(index
));
278 value
= (int) cuddV(f
);
279 return((value
& mask
) == 0 ? DD_ZERO(dd
) : DD_ONE(dd
));
283 res
= cuddCacheLookup2(dd
,addDoIthBit
,f
,index
);
284 if (res
!= NULL
) return(res
);
286 /* Recursive step. */
288 fv
= cuddT(f
); fvn
= cuddE(f
);
290 T
= addDoIthBit(dd
,fv
,index
);
291 if (T
== NULL
) return(NULL
);
294 E
= addDoIthBit(dd
,fvn
,index
);
296 Cudd_RecursiveDeref(dd
, T
);
301 res
= (T
== E
) ? T
: cuddUniqueInter(dd
,v
,T
,E
);
303 Cudd_RecursiveDeref(dd
, T
);
304 Cudd_RecursiveDeref(dd
, E
);
311 cuddCacheInsert2(dd
,addDoIthBit
,f
,index
,res
);
315 } /* end of addDoIthBit */