bddBdd.h

Go to the documentation of this file.
00001 /*
00002  * CrocoPat is a tool for relational programming.
00003  * This file is part of CrocoPat. 
00004  *
00005  * Copyright (C) 2002-2008  Dirk Beyer
00006  *
00007  * CrocoPat is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public License
00009  * as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * CrocoPat is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public License
00018  * along with CrocoPat; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020  *
00021  * Please find the GNU Lesser General Public License in file
00022  * License_LGPL.txt or at http://www.gnu.org/licenses/lgpl.txt
00023  *
00024  * Author:
00025  * Dirk Beyer (firstname.lastname@sfu.ca)
00026  * Simon Fraser University
00027  *
00028  * With contributions of: Andreas Noack, Michael Vogel
00029  */
00030 
00031 #ifndef _bddBdd_h
00032 #define _bddBdd_h
00033 
00034 #include "relObject.h"
00035 
00036 #include <vector>
00037 #include <set>
00038 #include <map>
00039 #include <cassert>
00040 
00042 struct bddNode 
00043 {
00045   unsigned mark:1;
00047   unsigned var:31; 
00049   unsigned low;    
00051   unsigned high;
00053   unsigned next;
00054 };
00055 
00057 struct bddBinEntry 
00058 {  
00060   unsigned op;     
00062   unsigned root1;
00064   unsigned root2;
00066   unsigned result;
00067 };
00068 
00070 struct bddStatEntry 
00071 {
00073   unsigned root;
00075   double result;  
00076 };
00077 
00080 struct bddGraphNode
00081 {
00082   unsigned id;
00083   unsigned var;
00084   unsigned low;
00085   unsigned high;
00086 };
00087 
00090 class bddBdd : private relObject
00091 {
00092 private: // Constants.
00093 
00095   enum { mComplement = 1, mRenameVars, mExists,
00096          mUnite, mIntersect, mSetContains };
00097 
00098 private: // Static attributes.
00099 
00103   static bddNode* mNodes;
00105   static unsigned mMaxNodeNr;
00108   static unsigned mFree;
00109 
00113   static multiset<unsigned> mExtRefs;
00114 
00119   static unsigned* mUniqueHash;
00121   static unsigned mUniqueHBitNr;
00123   static bddBinEntry* mBinCache;
00125   static unsigned mBinCBitNr;
00127   static bddStatEntry* mStatCache;
00129   static unsigned mStatCBitNr;
00130 
00133   static unsigned mRenameCnt; // = 0;
00134 
00135 private: // Private static methods.
00136 
00139   static inline unsigned 
00140   hash(unsigned p1, unsigned pHashBitNr);
00141   static inline unsigned 
00142   hash(unsigned p1, unsigned p2, unsigned p3, unsigned pHashBitNr);
00144   static inline void 
00145   normalize(unsigned& p1, unsigned& p2);
00146 
00148   static void 
00149   mark(unsigned pRoot);
00152   static void 
00153   unMark(unsigned pRoot);
00154 
00158   static void 
00159   gc();
00160 
00164   static unsigned 
00165   insert(unsigned pVar, unsigned pLow, unsigned pHigh);
00166 
00168   bool
00169   testVars_(unsigned pRoot, 
00170             unsigned pVarIdFirst, 
00171             unsigned pVarIdLast);
00175   static double 
00176   getTupleNr_(unsigned pRoot, unsigned pVar, unsigned pMaxVar);
00178   static unsigned 
00179   getTuple_(unsigned pRoot, unsigned pVar, unsigned pMaxVar);
00182   static unsigned 
00183   getNodeNr_(unsigned pRoot);
00185   static void
00186   getNodesPerVarId_(unsigned pRoot, 
00187                     map<unsigned, unsigned>& pBddNodesPerVar);
00189   static void
00190   getGraph_(unsigned pRoot, 
00191             multimap<unsigned,bddGraphNode>& pGraph);
00193   static void 
00194   print_(ostream& pS, unsigned pRoot);
00195   
00197   static bool 
00198   setContains_(unsigned pRoot1, unsigned pRoot2);
00199 
00202   static unsigned 
00203   complement_(unsigned pRoot);
00204   static unsigned 
00205   unite_(unsigned pRoot1, unsigned pRoot2);
00206   static unsigned 
00207   intersect_(unsigned pRoot1, unsigned pRoot2);
00208   static unsigned 
00209   exists_(unsigned pRoot, unsigned pVar);
00210   static unsigned 
00211   renameVars_(unsigned pRoot, 
00212               unsigned pFirst, 
00213               unsigned pLast, 
00214               int      pOffset);
00215 
00216 public: // Public static methods.
00217 
00225   static void
00226   init(unsigned pMaxNodeNr, 
00227     unsigned pUniqueHBitNr, unsigned pBinCBitNr, unsigned pStatCBitNr);
00230   static void done ();
00231 
00233   static unsigned 
00234   getReachNodeNr();
00236   static unsigned 
00237   getExtRefNr();
00238 
00240   static void 
00241   analyseUniqueHash ();
00242 
00243 private: // Attributes.
00244 
00246   unsigned mRoot;
00247 
00248 private: // Private methods.
00249 
00251   inline void decRef() 
00252   {
00253     if(mRoot != 0 && mRoot != 1)
00254     {
00255       multiset<unsigned>::iterator lDelete = mExtRefs.find(mRoot);
00256       // Otherwise error in external BDD references.
00257       assert(lDelete != mExtRefs.end());
00258       mExtRefs.erase(lDelete);
00259     }
00260   }
00261 
00263   inline void incRef() 
00264   {
00265     if(mRoot != 0 && mRoot != 1)
00266     {
00267       mExtRefs.insert(mRoot);
00268     }
00269   }
00270 
00271 public: // Constructors and destructor.
00272 
00274   bddBdd(unsigned pRoot = 0) 
00275   {
00276     mRoot = pRoot;
00277     incRef();
00278   }
00279 
00281   bddBdd(const bddBdd& pBdd) 
00282   {
00283     mRoot = pBdd.mRoot;
00284     incRef();
00285   }
00286 
00288   bddBdd (unsigned pVarId, bool pValue);
00289   static unsigned 
00290   bddBdd_(unsigned pVarId, bool pValue);
00291 
00294   bddBdd (unsigned pVarId, unsigned pBitNr, unsigned pValue);
00295   static unsigned 
00296   bddBdd_(unsigned pVarId, unsigned pBitNr, unsigned pValue);
00297 
00299   bddBdd (unsigned pVarId1, unsigned pVarId2);
00300   static unsigned 
00301   bddBdd_(unsigned pVarId1, unsigned pVarId2);
00302   
00303   ~bddBdd() 
00304   {
00305     decRef();
00306   }
00307 
00309   bddBdd& 
00310   operator=(const bddBdd& aBdd);
00311 
00315   static bddBdd
00316   mkLessEqual (unsigned pVarId, unsigned pBitNr, unsigned pValue);
00317   static unsigned 
00318   mkLessEqual_(unsigned pVarId, unsigned pBitNr, unsigned pValue);
00319 
00320 public: // Accessors.
00321 
00324   bool
00325   testVars(unsigned pVarIdFirst, unsigned pVarIdLast)
00326   {
00327     bool lResult = testVars_(mRoot, pVarIdFirst, pVarIdLast);
00328     unMark(mRoot);
00329     return lResult;
00330   }
00331 
00335   double
00336   getTupleNr(unsigned pMinVar, unsigned pMaxVar) const
00337   { return getTupleNr_(mRoot, pMinVar, pMaxVar); }
00338 
00340   unsigned
00341   getTuple(unsigned pMinVar, unsigned pMaxVar) const
00342   {
00343     assert(!isEmpty());
00344     return getTuple_(mRoot, pMinVar, pMaxVar); 
00345   }
00346 
00348   unsigned 
00349   getNodeNr() const;
00350 
00353   unsigned
00354   getFreeNodeNr() const;
00355 
00357   unsigned
00358   getMaxNodeNr() const
00359   { return mMaxNodeNr; }
00360 
00362   map<unsigned, unsigned>
00363   getNodesPerVarId() const
00364   { map<unsigned, unsigned> lBddNodesPerVar;
00365     getNodesPerVarId_(mRoot, lBddNodesPerVar);
00366     unMark(mRoot);
00367     return lBddNodesPerVar;  }
00368 
00370   void
00371   getGraph(multimap<unsigned,bddGraphNode>& pGraph) const 
00372   { getGraph_(mRoot, pGraph);
00373     unMark(mRoot);  }
00374 
00376   void 
00377   print(ostream& pS) const
00378   { print_(pS, mRoot);
00379     cout << endl;  }
00380   
00381 public: // Service methods.
00382 
00384   bool 
00385   setEqual(const bddBdd& pBdd) const
00386   { return mRoot == pBdd.mRoot; }
00387   
00389   bool 
00390   setContains(const bddBdd& pBdd) const
00391   { return setContains_(mRoot, pBdd.mRoot); }
00392 
00394   bool 
00395   isEmpty() const
00396   { return mRoot == 0; }
00397 
00398 
00402 
00404   void 
00405   complement();
00406 
00408   void 
00409   unite(const bddBdd& pBdd);
00410 
00412   void 
00413   intersect(const bddBdd& pBdd);
00414 
00416   void 
00417   exists(unsigned pVar);
00418 
00423   void 
00424   renameVars(unsigned pFirst, unsigned pLast, int pOffset);
00425 };
00426 
00427 #endif

Generated on Fri Jun 6 22:21:09 2008 for CrocoPat by  doxygen 1.5.1