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

Delta Between Two Patch Sets: compiled/StringMap.h

Issue 29572731: Issue 5141 - Generalize Map class to allow non-strings as keys (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore
Left Patch Set: Fixed key initialization issue Created Oct. 17, 2017, 11:45 a.m.
Right Patch Set: Addressed remaining nits Created Dec. 4, 2017, 6:28 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « compiled/Map.h ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 struct StringSetEntry 27 struct StringSetEntry
28 { 28 {
29 typedef String key_type; 29 typedef DependentString key_type;
30 typedef const String& key_type_cref;
30 typedef size_t size_type; 31 typedef size_t size_type;
31 32
32 DependentString first; 33 key_type first;
sergei 2017/10/17 12:58:06 Actually, the type of `first` should be key_type,
Wladimir Palant 2017/12/04 13:44:15 Done.
33 34
34 StringSetEntry(const key_type& key = DependentString()) 35 StringSetEntry(key_type_cref key = key_type())
35 { 36 {
36 if (!key.is_invalid()) 37 if (!key.is_invalid())
37 first.reset(key); 38 first.reset(key);
sergei 2017/10/17 12:58:06 Why is this trick required and `:first(key)` canno
Wladimir Palant 2017/12/04 13:44:15 That's because DepedentString() constructor will c
sergei 2017/12/04 14:26:09 I have added a ticket for that because it seems a
38 } 39 }
39 40
40 bool equals(const key_type& other) const 41 bool equals(key_type_cref other) const
41 { 42 {
42 return first.equals(other); 43 return first.equals(other);
43 } 44 }
44 45
45 bool is_invalid() const 46 bool is_invalid() const
46 { 47 {
47 return first.is_invalid(); 48 return first.is_invalid();
48 } 49 }
49 50
50 bool is_deleted() const 51 bool is_deleted() const
51 { 52 {
52 return first.is_deleted(); 53 return first.is_deleted();
53 } 54 }
54 55
55 void erase() 56 void erase()
56 { 57 {
57 first.erase(); 58 first.erase();
58 } 59 }
59 60
60 static size_type hash(const key_type& key) 61 static size_type hash(key_type_cref key)
61 { 62 {
62 // FNV-1a hash function 63 // FNV-1a hash function
63 size_type result = 2166136261; 64 size_type result = 2166136261;
64 for (String::size_type i = 0; i < key.length(); i++) 65 for (String::size_type i = 0; i < key.length(); i++)
65 result = (result ^ key[i]) * 16777619; 66 result = (result ^ key[i]) * 16777619;
66 return result; 67 return result;
67 } 68 }
68 }; 69 };
69 70
70 template<typename Value> 71 template<typename Value>
71 struct StringMapEntry : StringSetEntry 72 struct StringMapEntry : StringSetEntry
72 { 73 {
73 typedef StringSetEntry super; 74 typedef StringSetEntry super;
74 typedef Value value_type; 75 typedef Value value_type;
75 76
76 Value second; 77 value_type second;
77 78
78 StringMapEntry(const key_type& key = DependentString(), 79 StringMapEntry(key_type_cref key = DependentString(),
79 value_type value = value_type()) 80 value_type value = value_type())
80 : super(key), second(value) 81 : super(key), second(value)
81 { 82 {
82 } 83 }
83 84
84 void erase() 85 void erase()
85 { 86 {
86 super::erase(); 87 super::erase();
87 second = value_type(); 88 second = value_type();
88 } 89 }
89 }; 90 };
90 } 91 }
91 92
92 using StringSet = Set<StringMap_internal::StringSetEntry>; 93 using StringSet = Set<StringMap_internal::StringSetEntry>;
93 94
94 template<typename Value> 95 template<typename Value>
95 using StringMap = Map<StringMap_internal::StringMapEntry<Value>>; 96 using StringMap = Map<StringMap_internal::StringMapEntry<Value>>;
LEFTRIGHT

Powered by Google App Engine
This is Rietveld