bddSymTab.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 _bddSymTab_h_
00032 #define _bddSymTab_h_
00033 
00034 #include "reprNUMBER.h"
00035 #include "relObject.h"
00036 
00037 #include <string>
00038 #include <map>
00039 #include <set>
00040 #include <vector>
00041 #include <cassert>
00042 using namespace std;
00043 
00044 
00060 class bddSymTab : private relObject
00061 {
00062 private: // Attributes.
00063 
00067   unsigned mBitNr;
00068 
00072   map<string, unsigned> mAttributes;
00074   map<unsigned, string> mPositions;
00075 
00080   map<string, unsigned> mAttributeNumbers;
00082   vector<string> mAttributeValues;
00083 
00085   set<string> mIsQuoted;
00086 
00087 private:
00089   void operator,(const bddSymTab&);
00090   void operator=(const bddSymTab&);
00091   bddSymTab(const bddSymTab&);
00092 
00093 public: // Constructors.
00094   
00095   bddSymTab()
00096   {}
00097 
00098   ~bddSymTab()
00099   {}  
00100   
00101 
00102 public: // Initializers.
00103 
00106   void
00107   initValueUniverse(set<string>& pValueUniverse) 
00108   {
00109     if (pValueUniverse.size() <= 1) {
00110       mBitNr = 1;        // We need at least one bit for technical reasons.
00111     } 
00112     else {
00113       // Compute bit width (mBitNr).
00114       mBitNr = 0;
00115       unsigned lMaxValueCopy = pValueUniverse.size() - 1;
00116       while (lMaxValueCopy > 0)
00117       {
00118         lMaxValueCopy /= 2;
00119         ++mBitNr;
00120       }
00121     }
00122     
00123     // Prepare value-encoding mappings. Sort.
00124     mAttributeValues.clear();
00125     for( set<string>::const_iterator
00126            lIt = pValueUniverse.begin();
00127          lIt != pValueUniverse.end();
00128          ++lIt)
00129     {
00130       mAttributeNumbers[*lIt] = mAttributeValues.size();
00131       mAttributeValues.push_back(*lIt);
00132     }   
00133   }
00134   
00135 
00136 public: // Accessors.
00137 
00139   unsigned
00140   getBitNr() const 
00141   { return mBitNr; }
00142 
00144   unsigned
00145   getUniverseSize() const
00146   { return mAttributeValues.size(); }
00147 
00148   void
00149   setQuoted(const string pAttribute)
00150   { mIsQuoted.insert(pAttribute); }
00151 
00152   bool
00153   isQuoted(const string pAttribute) const
00154   { return mIsQuoted.find(pAttribute) != mIsQuoted.end(); }
00155 
00157   bool
00158   isValueGood(const string& pAttributeValue) const
00159   {
00160     return mAttributeNumbers.find(pAttributeValue) != mAttributeNumbers.end();
00161   }
00162 
00163   unsigned
00164   getAttributePos(const string& pAttribute) const
00165   {
00166     map<string, unsigned>::const_iterator it = mAttributes.find(pAttribute);
00167     // Otherwise access for invalid attribute.
00168     assert(it != mAttributes.end());
00169     return it->second * getBitNr();
00170   }
00171 
00174   unsigned
00175   getValueNum(const string& pAttributeValue) const
00176   {
00177     map<string, unsigned>::const_iterator
00178       lIt = mAttributeNumbers.find(pAttributeValue);
00179     // Value not found in symbol table.
00180     assert(lIt != mAttributeNumbers.end());
00181     return lIt->second;
00182   }
00183 
00184   string
00185   getAttributeValue(unsigned pNum) const
00186   {
00187     assert(pNum < mAttributeValues.size());
00188     return mAttributeValues[pNum];
00189   }
00190 
00191 public: // Service methods.
00192   
00195   void
00196   addAttribute(const string& pAttribute)
00197   {
00198     assert(pAttribute != "");           // Avoid empty string as name.
00199     // Add only if new.
00200     if( mAttributes.find(pAttribute) == mAttributes.end() ) {
00201       unsigned lAttrNum = mAttributes.size();
00202       // Ensure that lAttrNum is new.
00203       while (mPositions.find(lAttrNum) != mPositions.end()) {
00204         if (lAttrNum < UINT_MAX) {
00205           ++lAttrNum;
00206         } else {
00207           cerr << "Error: Maximum number of attributes exceeded." 
00208                << endl;
00209           exit(EXIT_FAILURE);
00210         }
00211       }
00212       mAttributes[pAttribute] = lAttrNum;
00213       mPositions[lAttrNum] = pAttribute;
00214     }
00215   }
00216 
00219   void
00220   removeAttribute(const string& pAttribute) 
00221   {
00222     assert(mAttributes.find(pAttribute) != mAttributes.end());
00223     mPositions.erase(mAttributes[pAttribute]);
00224     mAttributes.erase(pAttribute);
00225   }
00226 
00230   void
00231   removeUserAttributes(const char pAttributePrefix)
00232   {
00233     vector<string> lToRemove;
00234     // Collect attributes to remove.
00235     for(map<unsigned, string>::const_iterator lIt = mPositions.begin();
00236         lIt != mPositions.end();
00237         ++lIt)
00238     {
00239       if( (lIt->second)[0] != pAttributePrefix ) 
00240       {
00241         lToRemove.push_back(lIt->second);
00242       }
00243     }
00244     // Actually remove the collected attributes.
00245     for(vector<string>::const_iterator lIt = lToRemove.begin();
00246         lIt != lToRemove.end();
00247         ++lIt)
00248     {
00249       removeAttribute(*lIt);
00250     }
00251   }  
00252 
00253   const map<unsigned,string>
00254   computeVariableOrder(const set<string>& pAttributes) const
00255   { 
00256     map<unsigned,string> result;
00257     for (set<string>::const_iterator it = pAttributes.begin();  
00258          it != pAttributes.end();  
00259          ++it) 
00260     {
00261       result[getAttributePos(*it)] = *it;
00262     }
00263     return result;
00264   }
00265   
00266 public: // IO.
00267 
00268   void printValueNames(ostream& pS) const
00269   {
00270     pS << "VALUES -> NUMBERS :" << endl
00271        << "{" << endl;
00272     for (map<string, unsigned>::const_iterator 
00273          lValueIt = mAttributeNumbers.begin();
00274          lValueIt != mAttributeNumbers.end();
00275          ++lValueIt)
00276     {
00277       pS << "(\"" << lValueIt->first << "\" -> " 
00278          << lValueIt->second << "), ";
00279     }
00280     pS << "}" << endl;
00281 
00282     // Reverse mapping.
00283     pS << "NUMBERS -> VALUES :" << endl
00284        << "{" << endl;
00285     for (vector<string>::const_iterator 
00286            lValueIt = mAttributeValues.begin();
00287            lValueIt != mAttributeValues.end();
00288            ++lValueIt)
00289     {
00290       pS << "(" << lValueIt - mAttributeValues.begin() << " -> \""
00291          << *lValueIt << "\"), ";
00292     }
00293     pS << "}" << endl;
00294   }
00295 
00296 };
00297 
00298 #endif // _bddSymTab_h_

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