pion-net  4.0.9
PionHashMap.hpp
1 // -----------------------------------------------------------------------
2 // pion-common: a collection of common libraries used by the Pion Platform
3 // -----------------------------------------------------------------------
4 // Copyright (C) 2007-2008 Atomic Labs, Inc. (http://www.atomiclabs.com)
5 //
6 // Distributed under the Boost Software License, Version 1.0.
7 // See http://www.boost.org/LICENSE_1_0.txt
8 //
9 
10 #ifndef __PION_PIONHASHMAP_HEADER__
11 #define __PION_PIONHASHMAP_HEADER__
12 
13 #include <string>
14 #include <cctype>
15 #include <boost/functional/hash.hpp>
16 #include <pion/PionConfig.hpp>
17 
18 #if defined(PION_HAVE_UNORDERED_MAP)
19  #include <tr1/unordered_map>
20 #elif defined(PION_HAVE_EXT_HASH_MAP)
21  #include <ext/hash_map>
22 #elif defined(PION_HAVE_HASH_MAP)
23  #include <hash_map>
24 #endif
25 
26 
27 namespace pion { // begin namespace pion
28 
29 
30 #if defined(PION_HAVE_UNORDERED_MAP)
31  #define PION_HASH_MAP std::tr1::unordered_map
32  #define PION_HASH_MULTIMAP std::tr1::unordered_multimap
33  #define PION_HASH_STRING boost::hash<std::string>
34  #define PION_HASH(TYPE) boost::hash<TYPE>
35 #elif defined(PION_HAVE_EXT_HASH_MAP)
36  #if __GNUC__ >= 3
37  #define PION_HASH_MAP __gnu_cxx::hash_map
38  #define PION_HASH_MULTIMAP __gnu_cxx::hash_multimap
39  #else
40  #define PION_HASH_MAP hash_map
41  #define PION_HASH_MULTIMAP hash_multimap
42  #endif
43  #define PION_HASH_STRING boost::hash<std::string>
44  #define PION_HASH(TYPE) boost::hash<TYPE>
45 #elif defined(PION_HAVE_HASH_MAP)
46  #ifdef _MSC_VER
47  #define PION_HASH_MAP stdext::hash_map
48  #define PION_HASH_MULTIMAP stdext::hash_multimap
49  #define PION_HASH_STRING stdext::hash_compare<std::string, std::less<std::string> >
50  #define PION_HASH(TYPE) stdext::hash_compare<TYPE, std::less<TYPE> >
51  #else
52  #define PION_HASH_MAP hash_map
53  #define PION_HASH_MULTIMAP hash_multimap
54  #define PION_HASH_STRING boost::hash<std::string>
55  #define PION_HASH(TYPE) boost::hash<TYPE>
56  #endif
57 #endif
58 
59 
62  inline bool operator()(const std::string& str1, const std::string& str2) const {
63  if (str1.size() != str2.size())
64  return false;
65  std::string::const_iterator it1 = str1.begin();
66  std::string::const_iterator it2 = str2.begin();
67  while ( (it1!=str1.end()) && (it2!=str2.end()) ) {
68  if (tolower(*it1) != tolower(*it2))
69  return false;
70  ++it1;
71  ++it2;
72  }
73  return true;
74  }
75 };
76 
77 
80  inline unsigned long operator()(const std::string& str) const {
81  unsigned long value = 0;
82  for (std::string::const_iterator i = str.begin(); i!= str.end(); ++i)
83  value = static_cast<unsigned char>(tolower(*i)) + (value << 6) + (value << 16) - value;
84  return value;
85  }
86 };
87 
88 
91  inline bool operator()(const std::string& str1, const std::string& str2) const {
92  std::string::const_iterator it1 = str1.begin();
93  std::string::const_iterator it2 = str2.begin();
94  while ( (it1 != str1.end()) && (it2 != str2.end()) ) {
95  if (tolower(*it1) != tolower(*it2))
96  return (tolower(*it1) < tolower(*it2));
97  ++it1;
98  ++it2;
99  }
100  return (str1.size() < str2.size());
101  }
102 };
103 
104 
105 #ifdef _MSC_VER
106  struct CaseInsensitiveHashCompare : public stdext::hash_compare<std::string, CaseInsensitiveLess> {
108  // makes operator() with two arguments visible, otherwise it would be hidden by the operator() defined here
109  using stdext::hash_compare<std::string, CaseInsensitiveLess>::operator();
110 
111  inline size_t operator()(const std::string& str) const {
112  return CaseInsensitiveHash()(str);
113  }
114  };
115 #endif
116 
117 
119 #ifdef _MSC_VER
120  typedef PION_HASH_MULTIMAP<std::string, std::string, CaseInsensitiveHashCompare> StringDictionary;
121 #else
122  typedef PION_HASH_MULTIMAP<std::string, std::string, CaseInsensitiveHash, CaseInsensitiveEqual > StringDictionary;
123 #endif
124 
126 //typedef PION_HASH_MULTIMAP<std::string, std::string, PION_HASH_STRING > StringDictionary;
127 
128 
129 } // end namespace pion
130 
131 #endif
PION_HASH_MULTIMAP< std::string, std::string, CaseInsensitiveHash, CaseInsensitiveEqual > StringDictionary
data type for case-insensitive dictionary of strings
case insensitive hash function for std::string
Definition: PionHashMap.hpp:79
returns true if two strings are equal (ignoring case)
Definition: PionHashMap.hpp:61
returns true if str1 < str2 (ignoring case)
Definition: PionHashMap.hpp:90