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