LEFT | RIGHT |
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 | 21 |
22 #include "base.h" | 22 #include "base.h" |
23 #include "Map.h" | 23 #include "Map.h" |
24 #include "String.h" | 24 #include "String.h" |
25 | 25 |
26 ABP_NS_BEGIN | 26 ABP_NS_BEGIN |
| 27 namespace StringMap_internal |
| 28 { |
| 29 inline size_t stringHash(const String& key) |
| 30 { |
| 31 // FNV-1a hash function |
| 32 size_t result = 2166136261; |
| 33 for (size_t i = 0; i < key.length(); i++) |
| 34 result = (result ^ key[i]) * 16777619; |
| 35 return result; |
| 36 } |
| 37 } |
| 38 |
| 39 struct StringHash |
| 40 { |
| 41 size_t operator()(const String& key) const |
| 42 { |
| 43 return StringMap_internal::stringHash(key); |
| 44 } |
| 45 }; |
27 | 46 |
28 namespace StringMap_internal | 47 namespace StringMap_internal |
29 { | 48 { |
| 49 template<typename Key, |
| 50 class = typename std::enable_if<std::is_base_of<String, Key>::value>::type> |
30 struct StringSetEntry | 51 struct StringSetEntry |
31 { | 52 { |
32 typedef DependentString key_type; | 53 typedef Key key_type; |
33 typedef const String& key_type_cref; | 54 typedef const String& key_type_cref; |
34 typedef size_t size_type; | 55 typedef size_t size_type; |
35 | 56 |
36 key_type first; | 57 key_type first; |
37 | 58 |
38 StringSetEntry(key_type_cref key = key_type()) | 59 StringSetEntry(key_type_cref key = key_type()) |
39 { | 60 { |
40 if (!key.is_invalid()) | 61 if (!key.is_invalid()) |
41 first.reset(key); | 62 first.reset(key); |
42 } | 63 } |
(...skipping 13 matching lines...) Expand all Loading... |
56 return first.is_deleted(); | 77 return first.is_deleted(); |
57 } | 78 } |
58 | 79 |
59 void erase() | 80 void erase() |
60 { | 81 { |
61 first.erase(); | 82 first.erase(); |
62 } | 83 } |
63 | 84 |
64 static size_type hash(key_type_cref key) | 85 static size_type hash(key_type_cref key) |
65 { | 86 { |
66 // FNV-1a hash function | 87 return stringHash(key); |
67 size_type result = 2166136261; | |
68 for (String::size_type i = 0; i < key.length(); i++) | |
69 result = (result ^ key[i]) * 16777619; | |
70 return result; | |
71 } | 88 } |
72 }; | 89 }; |
73 | 90 |
74 template<typename Value> | 91 template<typename Key, typename Value> |
75 struct StringMapEntry : StringSetEntry | 92 struct StringMapEntry : StringSetEntry<Key> |
76 { | 93 { |
77 typedef StringSetEntry super; | 94 typedef StringSetEntry<Key> super; |
78 typedef Value value_type; | 95 typedef Value value_type; |
79 | 96 |
80 value_type second; | 97 value_type second; |
81 | 98 |
82 StringMapEntry(key_type_cref key = DependentString(), | 99 StringMapEntry(typename super::key_type_cref key = Key(), |
83 value_type value = value_type()) | 100 value_type value = value_type()) |
84 : super(key), second(value) | 101 : super(key), second(std::move(value)) |
85 { | 102 { |
86 } | 103 } |
87 | 104 |
88 void erase() | 105 void erase() |
89 { | 106 { |
90 super::erase(); | 107 super::erase(); |
91 second = value_type(); | 108 second = value_type(); |
92 } | 109 } |
93 }; | 110 }; |
94 } | 111 } |
95 | 112 |
96 using StringSet = Set<StringMap_internal::StringSetEntry>; | 113 using StringSet = Set<StringMap_internal::StringSetEntry<DependentString>>; |
97 | 114 |
98 template<typename Value> | 115 template<typename Value> |
99 using StringMap = Map<StringMap_internal::StringMapEntry<Value>>; | 116 using StringMap = Map<StringMap_internal::StringMapEntry<DependentString, Value>
>; |
100 | 117 template<typename Value> |
101 ABP_NS_END | 118 using OwnedStringMap = Map<StringMap_internal::StringMapEntry<OwnedString, Value
>>; |
| 119 ABP_NS_END |
LEFT | RIGHT |