constdualmap.h

00001 /*
00002  * All of the documentation and software included in the
00003  * Alchemy Software is copyrighted by Stanley Kok, Parag
00004  * Singla, Matthew Richardson, Pedro Domingos, Marc
00005  * Sumner, Hoifung Poon, Daniel Lowd, and Jue Wang.
00006  * 
00007  * Copyright [2004-09] Stanley Kok, Parag Singla, Matthew
00008  * Richardson, Pedro Domingos, Marc Sumner, Hoifung
00009  * Poon, Daniel Lowd, and Jue Wang. All rights reserved.
00010  * 
00011  * Contact: Pedro Domingos, University of Washington
00012  * (pedrod@cs.washington.edu).
00013  * 
00014  * Redistribution and use in source and binary forms, with
00015  * or without modification, are permitted provided that
00016  * the following conditions are met:
00017  * 
00018  * 1. Redistributions of source code must retain the above
00019  * copyright notice, this list of conditions and the
00020  * following disclaimer.
00021  * 
00022  * 2. Redistributions in binary form must reproduce the
00023  * above copyright notice, this list of conditions and the
00024  * following disclaimer in the documentation and/or other
00025  * materials provided with the distribution.
00026  * 
00027  * 3. All advertising materials mentioning features or use
00028  * of this software must display the following
00029  * acknowledgment: "This product includes software
00030  * developed by Stanley Kok, Parag Singla, Matthew
00031  * Richardson, Pedro Domingos, Marc Sumner, Hoifung
00032  * Poon, Daniel Lowd, and Jue Wang in the Department of
00033  * Computer Science and Engineering at the University of
00034  * Washington".
00035  * 
00036  * 4. Your publications acknowledge the use or
00037  * contribution made by the Software to your research
00038  * using the following citation(s): 
00039  * Stanley Kok, Parag Singla, Matthew Richardson and
00040  * Pedro Domingos (2005). "The Alchemy System for
00041  * Statistical Relational AI", Technical Report,
00042  * Department of Computer Science and Engineering,
00043  * University of Washington, Seattle, WA.
00044  * http://alchemy.cs.washington.edu.
00045  * 
00046  * 5. Neither the name of the University of Washington nor
00047  * the names of its contributors may be used to endorse or
00048  * promote products derived from this software without
00049  * specific prior written permission.
00050  * 
00051  * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY OF WASHINGTON
00052  * AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
00053  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00054  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00055  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY
00056  * OF WASHINGTON OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
00057  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00058  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00059  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00060  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
00061  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00062  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00063  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
00064  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00065  * 
00066  */
00067 #ifndef CONSTDUALMAP_H_JUN_21_2005
00068 #define CONSTDUALMAP_H_JUN_21_2005
00069 
00070 
00071 #include <limits>
00072 #include <ext/hash_map>
00073 //using namespace __gnu_cxx;
00074 #include "array.h"
00075 #include "strint.h"
00076 
00077 
00078   // StrInt stores the constant name & its type id
00079 typedef hash_map<const StrInt*, int, HashStrInt, EqualStrInt> StrIntToIntMap;
00080 
00081 
00082   // Maps int to StrInt* and vice versa.
00083 class ConstDualMap
00084 {  
00085  public:
00086   ConstDualMap()  
00087   {
00088     intToStrIntArr_ = new Array<const StrInt*>;
00089     strIntToIntMap_ = new StrIntToIntMap;
00090   }
00091 
00092 
00093   ~ConstDualMap() 
00094   {
00095     for (int i = 0; i < intToStrIntArr_->size(); i++)
00096       delete (*intToStrIntArr_)[i];
00097     delete intToStrIntArr_;
00098     delete strIntToIntMap_;
00099   }
00100 
00101 
00102     // Returns const char* corresponding to i or NULL if there is no such char*.
00103     // The returned const char* should not be deleted.
00104   const char* getStr(const int& i)
00105   { 
00106     if (0 <= i && i < intToStrIntArr_->size())
00107       return (*intToStrIntArr_)[i]->str_;
00108     return NULL;
00109   }
00110 
00111 
00112     // Returns the ints_ of StrInt corresponding to i; returns NULL if there is
00113     // no such StrInt. Should not be deleted by caller.
00114   Array<int>* getInt2(const int& i)
00115   { 
00116     if (0 <= i && i < intToStrIntArr_->size())
00117       return (*intToStrIntArr_)[i]->ints_;
00118     return NULL;
00119   }
00120 
00121     // Returns the ints_ of StrInt corresponding to i; returns NULL if there is
00122     // no such StrInt. Caller should delete s if required, but not returned Array.
00123   Array<int>* getInt2(const char* const & s)
00124   { 
00125     int i = getInt(s);
00126     if (i < 0) return NULL;
00127     return getInt2(i);
00128   }
00129 
00130 
00131     // Returns int corresponding to str or -1 if there is no such str.
00132     // Caller should delete s if required.
00133     // Making this function const causes the compiler to complain.
00134   int getInt(const char* const & s)
00135   {
00136     StrInt str(s);
00137     StrIntToIntMap::iterator it;
00138     if ((it=strIntToIntMap_->find(&str)) == strIntToIntMap_->end())
00139       return -1;
00140     return (*it).second;
00141   }
00142 
00143 
00144     // Returns corresponding int (which increases by one each time addType() is 
00145     // called), or -1 is str has been added before.
00146     // Caller should delete s if required.
00147   int insert(const char* const & s, const int& ii)
00148   {
00149       // See if string already present
00150     Array<int>* oldInts = getInt2(s);
00151     if (oldInts != NULL)
00152     {
00153       if (!oldInts->contains(ii))
00154         oldInts->append(ii);
00155       StrInt str(s);
00156       StrIntToIntMap::iterator it;
00157       it = strIntToIntMap_->find(&str);
00158       return (*it).second;
00159     }
00160 
00161     StrInt* strInt = new StrInt(s,ii);
00162 
00163     StrIntToIntMap::iterator it;
00164     if ((it=strIntToIntMap_->find(strInt)) != strIntToIntMap_->end())
00165     {
00166       cout << "Warning: In ConstDualMap::insert(), tried to insert duplicate " 
00167            << strInt->str_ << ", prev id " << (*it).second << endl;
00168       delete strInt;
00169       return -1;
00170     }
00171     
00172     if (((int)intToStrIntArr_->size()) >= numeric_limits<int>::max()) 
00173     {
00174       cout << "Error: In ConstDualMap::insert(), reach int max limit when "
00175            << "inserting " << strInt->str_ << endl;
00176       delete strInt;
00177       exit(-1);
00178     }
00179 
00180     intToStrIntArr_->append(strInt);
00181     int i = intToStrIntArr_->size()-1;
00182     (*strIntToIntMap_)[strInt] = i;
00183     return i;
00184   }
00185 
00186   
00187   int getNumInt() const  { return intToStrIntArr_->size(); }
00188 
00189     // Caller should not delete the returned Array<StrInt*>*.
00190   const Array<const StrInt*>* getIntToStrIntArr() const  
00191   { 
00192     return intToStrIntArr_; 
00193   }
00194 
00195     // Caller should delete the returned Array<const char*>* but not its 
00196     // contents
00197   const Array<const char*>* getIntToStrArr() const  
00198   { 
00199     Array<const char*>* a = new Array<const char*>;
00200     for (int i = 0; i < intToStrIntArr_->size(); i++)
00201       a->append((*intToStrIntArr_)[i]->str_);
00202     return a; 
00203   }
00204 
00205 
00206     // Caller should delete the returned Array<int>*.
00207 /*
00208   const Array<int>* getIntToInt2Arr() const  
00209   { 
00210     Array<int>* a = new Array<int>;
00211     for (int i = 0; i < intToStrIntArr_->size(); i++)
00212       a->append((*intToStrIntArr_)[i]->int_);
00213     return a; 
00214   }
00215 */  
00216   void compress() { intToStrIntArr_->compress(); }
00217  
00218  private:
00219   Array<const StrInt*>* intToStrIntArr_;
00220   StrIntToIntMap* strIntToIntMap_;
00221 };
00222 
00223 #endif

Generated on Sun Jun 7 11:55:21 2009 for Alchemy by  doxygen 1.5.1