Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: compiled/StringMap.h

Issue 29587914: Issue 5142 - Convert Element Hiding to C++ (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Created Oct. 25, 2017, 1:07 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
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 class StringHash
hub 2017/10/25 01:19:38 this is needed for the unordered_map<>. Otherwise
26 {
27 public:
28 size_t operator()(const String& key) const
29 {
30 // FNV-1a hash function
31 size_t result = 2166136261;
32 for (size_t i = 0; i < key.length(); i++)
33 result = (result ^ key[i]) * 16777619;
34 return result;
35 }
36 };
37
25 namespace StringMap_internal 38 namespace StringMap_internal
26 { 39 {
27 struct StringSetEntry 40 struct StringSetEntry
28 { 41 {
29 typedef String key_type; 42 typedef String key_type;
30 typedef size_t size_type; 43 typedef size_t size_type;
31 44
32 DependentString first; 45 DependentString first;
46 static StringHash hashFunctor;
33 47
34 StringSetEntry(const key_type& key = DependentString()) 48 StringSetEntry(const key_type& key = DependentString())
35 { 49 {
36 if (!key.is_invalid()) 50 if (!key.is_invalid())
37 first.reset(key); 51 first.reset(key);
38 } 52 }
39 53
40 bool equals(const key_type& other) const 54 bool equals(const key_type& other) const
41 { 55 {
42 return first.equals(other); 56 return first.equals(other);
43 } 57 }
44 58
45 bool is_invalid() const 59 bool is_invalid() const
46 { 60 {
47 return first.is_invalid(); 61 return first.is_invalid();
48 } 62 }
49 63
50 bool is_deleted() const 64 bool is_deleted() const
51 { 65 {
52 return first.is_deleted(); 66 return first.is_deleted();
53 } 67 }
54 68
55 void erase() 69 void erase()
56 { 70 {
57 first.erase(); 71 first.erase();
58 } 72 }
59 73
60 static size_type hash(const key_type& key) 74 static size_type hash(const key_type& key)
61 { 75 {
62 // FNV-1a hash function 76 return hashFunctor(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 } 77 }
68 }; 78 };
69 79
70 template<typename Value> 80 template<typename Value>
71 struct StringMapEntry : StringSetEntry 81 struct StringMapEntry : StringSetEntry
72 { 82 {
73 typedef StringSetEntry super; 83 typedef StringSetEntry super;
74 typedef Value value_type; 84 typedef Value value_type;
75 85
76 Value second; 86 Value second;
77 87
78 StringMapEntry(const key_type& key = DependentString(), 88 StringMapEntry(const key_type& key = DependentString(),
79 value_type value = value_type()) 89 value_type value = value_type())
80 : super(key), second(value) 90 : super(key), second(value)
81 { 91 {
82 } 92 }
83 93
84 void erase() 94 void erase()
85 { 95 {
86 super::erase(); 96 super::erase();
87 second = value_type(); 97 second = value_type();
88 } 98 }
89 }; 99 };
90 } 100 }
91 101
92 using StringSet = Set<StringMap_internal::StringSetEntry>; 102 using StringSet = Set<StringMap_internal::StringSetEntry>;
93 103
94 template<typename Value> 104 template<typename Value>
95 using StringMap = Map<StringMap_internal::StringMapEntry<Value>>; 105 using StringMap = Map<StringMap_internal::StringMapEntry<Value>>;
OLDNEW

Powered by Google App Engine
This is Rietveld