00001 /* 00002 * hash_string.h : part of the Mace toolkit for building distributed systems 00003 * 00004 * Copyright (c) 2007, Charles Killian, Dejan Kostic, Ryan Braud, James W. Anderson, John Fisher-Ogden, Calvin Hubble, Duy Nguyen, Justin Burke, David Oppenheimer, Amin Vahdat, Adolfo Rodriguez, Sooraj Bhat 00005 * All rights reserved. 00006 * 00007 * Redistribution and use in source and binary forms, with or without 00008 * modification, are permitted provided that the following conditions are met: 00009 * 00010 * * Redistributions of source code must retain the above copyright 00011 * notice, this list of conditions and the following disclaimer. 00012 * * Redistributions in binary form must reproduce the above copyright 00013 * notice, this list of conditions and the following disclaimer in 00014 * the documentation and/or other materials provided with the 00015 * distribution. 00016 * * Neither the names of Duke University nor The University of 00017 * California, San Diego, nor the names of the authors or contributors 00018 * may be used to endorse or promote products derived from 00019 * this software without specific prior written permission. 00020 * 00021 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00022 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00023 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00024 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 00025 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00026 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 00027 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00028 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 00029 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 00030 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00031 * 00032 * ----END-OF-LEGAL-STUFF---- */ 00033 #include <sys/types.h> 00034 #include <cstring> 00035 #include <string> 00036 00042 #if __GNUC__ >= 3 00043 #include <ext/hash_map> 00044 #else 00045 #include <hash_map> 00046 #endif 00047 00048 #ifndef HASH_STRING_H 00049 #define HASH_STRING_H 00050 00058 template<class T> 00059 struct hash_bytes { 00060 size_t operator() (const T __x) const { 00061 unsigned long __h = 0; 00062 char* __s = (char*)&__x; 00063 for (unsigned int i = 0 ; i < sizeof(T); i++) { 00064 __h = 31 * __h + __s[i]; 00065 } 00066 return size_t(__h); 00067 } 00068 }; 00069 00077 struct hash_string { 00078 size_t operator() (const std::string& __x) const { 00079 unsigned long __h = 0; 00080 const char* __s = __x.data(); 00081 for (unsigned int i = 0 ; i < __x.size(); i++) { 00082 __h = 31 * __h + __s[i]; 00083 } 00084 return size_t(__h); 00085 } 00086 }; 00087 00089 struct ltstring { 00090 bool operator()(const std::string& s1, const std::string& s2) const 00091 { 00092 return strcmp(s1.c_str(), s2.c_str()) < 0; 00093 } 00094 }; 00095 00097 struct gtstring { 00098 bool operator()(const std::string& s1, const std::string& s2) const 00099 { 00100 return strcmp(s1.c_str(), s2.c_str()) > 0; 00101 } 00102 }; 00103 00104 class HashString { 00105 public: 00106 static size_t hash(const std::string& x) { 00107 static hash_string hasher; 00108 return hasher(x); 00109 } 00110 }; 00111 00112 // struct hash_string { 00113 // size_t operator() (const std::string& __s) const { 00114 // return __stl_hash_string(__s.c_str()); 00115 // } 00116 // }; 00117 00118 #endif // HASH_STRING_H