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 | 21 |
22 #include "Map.h" | 22 #include "Map.h" |
23 #include "String.h" | 23 #include "String.h" |
24 | 24 |
25 namespace StringMap_internal | 25 namespace StringMap_internal |
26 { | 26 { |
| 27 template<typename Key, |
| 28 class = typename std::enable_if<std::is_base_of<String, Key>::value>::type> |
27 struct StringSetEntry | 29 struct StringSetEntry |
28 { | 30 { |
29 typedef DependentString key_type; | 31 typedef Key key_type; |
30 typedef const String& key_type_cref; | 32 typedef const String& key_type_cref; |
31 typedef size_t size_type; | 33 typedef size_t size_type; |
32 | 34 |
33 key_type first; | 35 key_type first; |
34 | 36 |
35 StringSetEntry(key_type_cref key = key_type()) | 37 StringSetEntry(key_type_cref key = key_type()) |
36 { | 38 { |
37 if (!key.is_invalid()) | 39 if (!key.is_invalid()) |
38 first.reset(key); | 40 first.reset(key); |
39 } | 41 } |
(...skipping 21 matching lines...) Expand all Loading... |
61 static size_type hash(key_type_cref key) | 63 static size_type hash(key_type_cref key) |
62 { | 64 { |
63 // FNV-1a hash function | 65 // FNV-1a hash function |
64 size_type result = 2166136261; | 66 size_type result = 2166136261; |
65 for (String::size_type i = 0; i < key.length(); i++) | 67 for (String::size_type i = 0; i < key.length(); i++) |
66 result = (result ^ key[i]) * 16777619; | 68 result = (result ^ key[i]) * 16777619; |
67 return result; | 69 return result; |
68 } | 70 } |
69 }; | 71 }; |
70 | 72 |
71 template<typename Value> | 73 template<typename Key, typename Value> |
72 struct StringMapEntry : StringSetEntry | 74 struct StringMapEntry : StringSetEntry<Key> |
73 { | 75 { |
74 typedef StringSetEntry super; | 76 typedef StringSetEntry<Key> super; |
75 typedef Value value_type; | 77 typedef Value value_type; |
76 | 78 |
77 value_type second; | 79 value_type second; |
78 | 80 |
79 StringMapEntry(key_type_cref key = DependentString(), | 81 StringMapEntry(typename super::key_type_cref key = Key(), |
80 value_type value = value_type()) | 82 value_type value = value_type()) |
81 : super(key), second(value) | 83 : super(key), second(value) |
82 { | 84 { |
83 } | 85 } |
84 | 86 |
85 void erase() | 87 void erase() |
86 { | 88 { |
87 super::erase(); | 89 super::erase(); |
88 second = value_type(); | 90 second = value_type(); |
89 } | 91 } |
90 }; | 92 }; |
91 } | 93 } |
92 | 94 |
93 using StringSet = Set<StringMap_internal::StringSetEntry>; | 95 using StringSet = Set<StringMap_internal::StringSetEntry<DependentString>>; |
94 | 96 |
95 template<typename Value> | 97 template<typename Value> |
96 using StringMap = Map<StringMap_internal::StringMapEntry<Value>>; | 98 using StringMap = Map<StringMap_internal::StringMapEntry<DependentString, Value>
>; |
| 99 template<typename Value> |
| 100 using OwnedStringMap = Map<StringMap_internal::StringMapEntry<OwnedString, Value
>>; |
OLD | NEW |