OLD | NEW |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-present eyeo GmbH | 3 * Copyright (C) 2006-present eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
13 * | 13 * |
14 * You should have received a copy of the GNU General Public License | 14 * You should have received a copy of the GNU General Public License |
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
16 */ | 16 */ |
17 | 17 |
18 #pragma once | 18 #pragma once |
19 | 19 |
20 #include <cstddef> | 20 #include <cstddef> |
| 21 #include <type_traits> |
21 | 22 |
22 #include "Map.h" | 23 #include "Map.h" |
23 #include "String.h" | 24 #include "String.h" |
24 | 25 |
| 26 namespace { |
| 27 size_t stringHash(const String& key) |
| 28 { |
| 29 // FNV-1a hash function |
| 30 size_t result = 2166136261; |
| 31 for (size_t i = 0; i < key.length(); i++) |
| 32 result = (result ^ key[i]) * 16777619; |
| 33 return result; |
| 34 } |
| 35 } |
| 36 |
| 37 struct StringHash |
| 38 { |
| 39 size_t operator()(const String& key) const |
| 40 { |
| 41 return stringHash(key); |
| 42 } |
| 43 }; |
| 44 |
25 namespace StringMap_internal | 45 namespace StringMap_internal |
26 { | 46 { |
| 47 template<typename Key, |
| 48 class = typename std::enable_if<std::is_base_of<String, Key>::value>::type> |
27 struct StringSetEntry | 49 struct StringSetEntry |
28 { | 50 { |
29 typedef DependentString key_type; | 51 typedef Key key_type; |
30 typedef const String& key_type_cref; | 52 typedef const String& key_type_cref; |
31 typedef size_t size_type; | 53 typedef size_t size_type; |
32 | 54 |
33 key_type first; | 55 key_type first; |
34 | 56 |
35 StringSetEntry(key_type_cref key = key_type()) | 57 StringSetEntry(key_type_cref key = key_type()) |
36 { | 58 { |
37 if (!key.is_invalid()) | 59 if (!key.is_invalid()) |
38 first.reset(key); | 60 first.reset(key); |
39 } | 61 } |
(...skipping 13 matching lines...) Expand all Loading... |
53 return first.is_deleted(); | 75 return first.is_deleted(); |
54 } | 76 } |
55 | 77 |
56 void erase() | 78 void erase() |
57 { | 79 { |
58 first.erase(); | 80 first.erase(); |
59 } | 81 } |
60 | 82 |
61 static size_type hash(key_type_cref key) | 83 static size_type hash(key_type_cref key) |
62 { | 84 { |
63 // FNV-1a hash function | 85 return stringHash(key); |
64 size_type result = 2166136261; | |
65 for (String::size_type i = 0; i < key.length(); i++) | |
66 result = (result ^ key[i]) * 16777619; | |
67 return result; | |
68 } | 86 } |
69 }; | 87 }; |
70 | 88 |
71 template<typename Value> | 89 template<typename Key, typename Value> |
72 struct StringMapEntry : StringSetEntry | 90 struct StringMapEntry : StringSetEntry<Key> |
73 { | 91 { |
74 typedef StringSetEntry super; | 92 typedef StringSetEntry<Key> super; |
75 typedef Value value_type; | 93 typedef Value value_type; |
76 | 94 |
77 value_type second; | 95 value_type second; |
78 | 96 |
79 StringMapEntry(key_type_cref key = DependentString(), | 97 StringMapEntry(typename super::key_type_cref key = Key(), |
80 value_type value = value_type()) | 98 value_type value = value_type()) |
81 : super(key), second(value) | 99 : super(key), second(value) |
82 { | 100 { |
83 } | 101 } |
84 | 102 |
85 void erase() | 103 void erase() |
86 { | 104 { |
87 super::erase(); | 105 super::erase(); |
88 second = value_type(); | 106 second = value_type(); |
89 } | 107 } |
90 }; | 108 }; |
91 } | 109 } |
92 | 110 |
93 using StringSet = Set<StringMap_internal::StringSetEntry>; | 111 using StringSet = Set<StringMap_internal::StringSetEntry<DependentString>>; |
94 | 112 |
95 template<typename Value> | 113 template<typename Value> |
96 using StringMap = Map<StringMap_internal::StringMapEntry<Value>>; | 114 using StringMap = Map<StringMap_internal::StringMapEntry<DependentString, Value>
>; |
| 115 template<typename Value> |
| 116 using OwnedStringMap = Map<StringMap_internal::StringMapEntry<OwnedString, Value
>>; |
OLD | NEW |