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