00001 #ifndef HASH_MAP_H 00002 #define HASH_MAP_H 00003 00004 //-------------------------------------------------------------------- 00005 // 00006 // This file is part of PEACE. 00007 // 00008 // PEACE is free software: you can redistribute it and/or modify it 00009 // under the terms of the GNU General Public License as published by 00010 // the Free Software Foundation, either version 3 of the License, or 00011 // (at your option) any later version. 00012 // 00013 // PEACE is distributed in the hope that it will be useful, but 00014 // WITHOUT ANY WARRANTY; without even the implied warranty of 00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 // General Public License for more details. 00017 // 00018 // You should have received a copy of the GNU General Public License 00019 // along with PEACE. If not, see <http://www.gnu.org/licenses/>. 00020 // 00021 // Miami University makes no representations or warranties about the 00022 // suitability of the software, either express or implied, including 00023 // but not limited to the implied warranties of merchantability, 00024 // fitness for a particular purpose, or non-infringement. Miami 00025 // University shall not be liable for any damages suffered by licensee 00026 // as a result of using, result of using, modifying or distributing 00027 // this software or its derivatives. 00028 // 00029 // By using or copying this Software, Licensee agrees to abide by the 00030 // intellectual property laws, and all other applicable laws of the 00031 // U.S., and the terms of GNU General Public License (version 3). 00032 // 00033 // Authors: Dhananjai M. Rao raodm@muohio.edu 00034 // 00035 //--------------------------------------------------------------------- 00036 00037 #include <cstring> 00038 #include <string> 00039 #include <memory> 00040 00041 #ifndef _WINDOWS 00042 00043 // The following compile guard is needed so that the hash map can be 00044 // used for a test when running configure (because when configure is 00045 // running, there is no config.h) 00046 #ifdef REGULAR_COMPILE 00047 #include "config.h" 00048 #endif 00049 00050 #ifdef HAVE_TR1_UNORDERED_MAP 00051 00052 // We must be compiling with gcc 4.2+ in which hash map has been moved 00053 // to unordered_map in preperation for the Cx0 standard compliance. So 00054 // use the latest and greatest standard 00055 #include <tr1/unordered_map> 00056 #define GLIBC_NAMESPACE std::tr1 00057 #define HashMap std::tr1::unordered_map 00058 #define Hash std::tr1::hash 00059 00060 #else // Not gcc 4.2+ (but linux) 00061 00062 #ifdef HAVE_EXT_HASH_MAP 00063 // Use standard hash map from the extended name space. With GCC 3.2 00064 // and above the hash map data structure has been moded to a extended 00065 // directory under a non-std namespace. The following defines 00066 // attempts to put the hash map in the standard context making it much 00067 // easier to work with. 00068 #include <ext/hash_map> 00069 #define GLIBC_NAMESPACE __gnu_cxx 00070 #define HashMap __gnu_cxx::hash_map 00071 #define Hash __gnu_cxx::hash 00072 00073 #else 00074 // No TR1 and no EXT. Hopefully HAS_HASH_MAP is true 00075 #include <hash_map> 00076 #define GLIBC_NAMESPACE std 00077 #define HashMap std::hash_map 00078 #define Hash std::hash 00079 00080 #endif 00081 00082 #endif 00083 00084 // Hasher for std::string. This hash function is needed to use std::string 00085 // objects as key value in hash_map only in older version of compilers. 00086 // Check and provide a backwards compatible hasher if needed. 00087 #ifdef NEED_STRING_HASHER 00088 struct StringHasher { 00089 inline size_t operator()(const std::string& s) const { 00090 Hash<const char*> hasher; 00091 return hasher(s.c_str()); 00092 00093 } 00094 }; 00095 #else 00096 // Use default system-provided string hasher 00097 #define StringHasher Hash<std::string> 00098 #endif 00099 00100 /** \typedef hash_map<std::string, int, StringHasher> StringIntMap 00101 00102 \brief A typedef for a hash map whose key is std::string and 00103 contains integers. 00104 00105 The following typedef provides a short cut for using a hash map 00106 whose key is a std::string and contains integers. 00107 */ 00108 typedef HashMap< std::string, int, StringHasher > StringIntMap; 00109 00110 #else // Windows code path begins 00111 00112 #include <hash_map> 00113 00114 // In windows the following mappings are used. 00115 #define HashMap stdext::hash_map 00116 #define Hash stdext::hash 00117 00118 /** String comparison structure for const char *. 00119 00120 The following structure essentially provides the comparison 00121 operator needed by the hash_map for comparing hash_key values. 00122 This structure specifically provides comparison for hash_map's 00123 whose key values are C strings. 00124 */ 00125 struct LessString { 00126 inline bool operator()(const std::string& s1, const std::string& s2) const { 00127 return s1.compare(s2) < 0; 00128 } 00129 }; 00130 00131 /** \typedef A hash_map<std::string, int> 00132 00133 A typedef for a hash map whose key is std::string and contains 00134 integers. 00135 00136 The following typedef provides a short cut for using a hash map 00137 whose key is a std::string and contains integers. 00138 */ 00139 typedef HashMap<std::string, int, stdext::hash_compare<std::string, LessString> > StringIntMap; 00140 00141 #endif 00142 00143 /** String comparison structure for HashMap. 00144 00145 The following structure essentially provides the functor for 00146 comparison operator needed by the hash_map for comparing hash_key 00147 values. This structure specifically provides comparison for 00148 hash_maps' whose key values are standard c strings. 00149 */ 00150 struct EqualStr { 00151 bool operator()(const char* s1, const char* s2) const { 00152 return strcmp(s1, s2) == 0; 00153 } 00154 }; 00155 00156 /** Integer comparison structure for HashMap. 00157 00158 The following structure essentially provides the functor for 00159 comparion operator needed by the hash_map for comparing hash_key 00160 values. This structure specifically provides comparison for 00161 hash_map's whose key values are standard integers. 00162 */ 00163 struct EqualInteger { 00164 inline bool operator()(const int i1, const int i2) const { 00165 return (i1 == i2); 00166 } 00167 }; 00168 00169 #ifdef ICC 00170 namespace __gnu_cxx { 00171 template<> struct hash<std::string> { 00172 inline size_t operator()(const std::string& str) const { 00173 return hash<const char*>()(str.c_str()); 00174 } 00175 }; 00176 } 00177 #endif 00178 00179 #endif