ScummVM API documentation
string_types.h
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 modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (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, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef AGS_SHARED_UTIL_STRING_TYPES_H
23 #define AGS_SHARED_UTIL_STRING_TYPES_H
24 
25 #include "ags/lib/std/map.h"
26 #include "ags/lib/std/vector.h"
27 #include "ags/shared/util/string.h"
28 #include "common/hash-str.h"
29 
30 namespace AGS3 {
31 namespace FNV {
32 
33 const uint32_t PRIME_NUMBER = 2166136261U;
34 const uint32_t SECONDARY_NUMBER = 16777619U;
35 
36 inline size_t Hash(const char *data, const size_t len) {
37  uint32_t hash = PRIME_NUMBER;
38  for (size_t i = 0; i < len; ++i)
39  hash = (SECONDARY_NUMBER * hash) ^ (uint8_t)(data[i]);
40  return hash;
41 }
42 
43 inline size_t Hash_LowerCase(const char *data, const size_t len) {
44  uint32_t hash = PRIME_NUMBER;
45  for (size_t i = 0; i < len; ++i)
46  hash = (SECONDARY_NUMBER * hash) ^ (uint8_t)(tolower(data[i]));
47  return hash;
48 }
49 
50 } // namespace FNV
51 } // namespace AGS3
52 
53 namespace AGS3 {
54 
56  bool operator()(const ::AGS3::AGS::Shared::String &x, const ::AGS3::AGS::Shared::String &y) const {
57  return x.Compare(y) == 0;
58  }
59 };
60 
62  uint operator()(const ::AGS3::AGS::Shared::String &x) const {
63  return Common::hashit(x.GetCStr());
64  }
65 };
66 
68  bool operator()(const ::AGS3::AGS::Shared::String &x, const ::AGS3::AGS::Shared::String &y) const {
69  return x.CompareNoCase(y) == 0;
70  }
71 };
72 
74  uint operator()(const ::AGS3::AGS::Shared::String &x) const {
75  return Common::hashit_lower(x.GetCStr());
76  }
77 };
78 
80  bool operator()(const ::AGS3::AGS::Shared::String &x, const ::AGS3::AGS::Shared::String &y) const {
81  return x.CompareNoCase(y) < 0;
82  }
83 };
84 
85 } // namespace AGS3
86 
87 namespace Common {
88 
89 // Specalization of the Hash functor for String objects.
90 // We do case sensitve hashing here, because that is what
91 // the default EqualTo is compatible with. If one wants to use
92 // case insensitve hashing, then only because one wants to use
93 // IgnoreCase_EqualTo, and then one has to specify a custom
94 // hash anyway.
95 template<>
96 struct Hash<AGS3::AGS::Shared::String> {
97  uint operator()(const AGS3::AGS::Shared::String &s) const {
98  return Common::hashit(s.GetCStr());
99  }
100 };
101 
102 } // namespace Common
103 
104 namespace AGS3 {
105 namespace AGS {
106 namespace Shared {
107 
108 typedef std::vector<String> StringV;
109 typedef std::unordered_map<String, String> StringMap;
110 typedef std::unordered_map<String, String, IgnoreCase_Hash, IgnoreCase_EqualTo> StringIMap;
111 
112 } // namespace Shared
113 } // namespace AGS
114 } // namespace AGS3
115 
116 #endif
Definition: achievements_tables.h:27
Definition: string_types.h:55
Definition: string_types.h:67
Definition: string_types.h:79
Definition: func.h:527
Definition: algorithm.h:29
Definition: string.h:62
Definition: string_types.h:61
Definition: ags.h:40
Definition: string_types.h:73