| Left: | ||
| Right: |
| 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 | |
|
sergei
2018/01/15 15:31:19
what about making it struct?
hub
2018/01/16 02:57:39
Done.
| |
| 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 DependentString key_type; | 52 typedef Key key_type; |
| 30 typedef const String& key_type_cref; | 53 typedef const String& key_type_cref; |
| 31 typedef size_t size_type; | 54 typedef size_t size_type; |
| 32 | 55 |
| 33 key_type first; | 56 key_type first; |
| 34 | 57 |
| 35 StringSetEntry(key_type_cref key = key_type()) | 58 StringSetEntry(key_type_cref key = key_type()) |
| 36 { | 59 { |
| 37 if (!key.is_invalid()) | 60 if (!key.is_invalid()) |
| 38 first.reset(key); | 61 first.reset(key); |
| 39 } | 62 } |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 53 return first.is_deleted(); | 76 return first.is_deleted(); |
| 54 } | 77 } |
| 55 | 78 |
| 56 void erase() | 79 void erase() |
| 57 { | 80 { |
| 58 first.erase(); | 81 first.erase(); |
| 59 } | 82 } |
| 60 | 83 |
| 61 static size_type hash(key_type_cref key) | 84 static size_type hash(key_type_cref key) |
| 62 { | 85 { |
| 63 // FNV-1a hash function | 86 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 } | 87 } |
| 69 }; | 88 }; |
| 70 | 89 |
| 71 template<typename Value> | 90 template<typename Key, typename Value> |
| 72 struct StringMapEntry : StringSetEntry | 91 struct StringMapEntry : StringSetEntry<Key> |
| 73 { | 92 { |
| 74 typedef StringSetEntry super; | 93 typedef StringSetEntry<Key> super; |
| 75 typedef Value value_type; | 94 typedef Value value_type; |
| 76 | 95 |
| 77 value_type second; | 96 value_type second; |
| 78 | 97 |
| 79 StringMapEntry(key_type_cref key = DependentString(), | 98 StringMapEntry(typename super::key_type_cref key = Key(), |
| 80 value_type value = value_type()) | 99 value_type value = value_type()) |
| 81 : super(key), second(value) | 100 : super(key), second(value) |
| 82 { | 101 { |
| 83 } | 102 } |
| 84 | 103 |
| 85 void erase() | 104 void erase() |
| 86 { | 105 { |
| 87 super::erase(); | 106 super::erase(); |
| 88 second = value_type(); | 107 second = value_type(); |
| 89 } | 108 } |
| 90 }; | 109 }; |
| 91 } | 110 } |
| 92 | 111 |
| 93 using StringSet = Set<StringMap_internal::StringSetEntry>; | 112 using StringSet = Set<StringMap_internal::StringSetEntry<DependentString>>; |
| 94 | 113 |
| 95 template<typename Value> | 114 template<typename Value> |
| 96 using StringMap = Map<StringMap_internal::StringMapEntry<Value>>; | 115 using StringMap = Map<StringMap_internal::StringMapEntry<DependentString, Value> >; |
| 116 template<typename Value> | |
| 117 using OwnedStringMap = Map<StringMap_internal::StringMapEntry<OwnedString, Value >>; | |
| OLD | NEW |