1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #ifndef COMMON_HASH_STR_H
24 #define COMMON_HASH_STR_H
25 
26 #include "common/hashmap.h"
27 #include "common/str.h"
28 
29 namespace Common {
30 
31 uint hashit(const char *str);
32 uint hashit_lower(const char *str); // Generate a hash based on the lowercase version of the string
hashit(const String & str)33 inline uint hashit(const String &str) { return hashit(str.c_str()); }
hashit_lower(const String & str)34 inline uint hashit_lower(const String &str) { return hashit_lower(str.c_str()); }
35 
36 // FIXME: The following functors obviously are not consistently named
37 
38 struct CaseSensitiveString_EqualTo {
operatorCaseSensitiveString_EqualTo39 	bool operator()(const String& x, const String& y) const { return x.equals(y); }
40 };
41 
42 struct CaseSensitiveString_Hash {
operatorCaseSensitiveString_Hash43 	uint operator()(const String& x) const { return hashit(x.c_str()); }
44 };
45 
46 
47 struct IgnoreCase_EqualTo {
operatorIgnoreCase_EqualTo48 	bool operator()(const String& x, const String& y) const { return x.equalsIgnoreCase(y); }
49 };
50 
51 struct IgnoreCase_Hash {
operatorIgnoreCase_Hash52 	uint operator()(const String& x) const { return hashit_lower(x.c_str()); }
53 };
54 
55 // Specalization of the Hash functor for String objects.
56 // We do case sensitve hashing here, because that is what
57 // the default EqualTo is compatible with. If one wants to use
58 // case insensitve hashing, then only because one wants to use
59 // IgnoreCase_EqualTo, and then one has to specify a custom
60 // hash anyway.
61 template<>
62 struct Hash<String> {
63 	uint operator()(const String& s) const {
64 		return hashit(s.c_str());
65 	}
66 };
67 
68 template<>
69 struct Hash<const char *> {
70 	uint operator()(const char *s) const {
71 		return hashit(s);
72 	}
73 };
74 
75 // String map -- by default case insensitive
76 typedef HashMap<String, String, IgnoreCase_Hash, IgnoreCase_EqualTo> StringMap;
77 
78 } // End of namespace Common
79 
80 #endif
81