Left: | ||
Right: |
LEFT | RIGHT |
---|---|
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; |
33 | 34 |
34 StringSetEntry() {} | 35 StringSetEntry(key_type_cref key = key_type()) |
35 StringSetEntry(const key_type& key) | |
36 : first(key) | |
37 { | 36 { |
37 if (!key.is_invalid()) | |
38 first.reset(key); | |
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() | 79 StringMapEntry(key_type_cref key = DependentString(), |
79 : StringSetEntry(), second() | 80 value_type value = value_type()) |
80 { | 81 : super(key), second(value) |
81 } | |
82 StringMapEntry(const key_type& key) | |
83 : StringSetEntry(key), second() | |
84 { | |
85 } | |
86 StringMapEntry(const key_type& key, value_type value) | |
87 : StringSetEntry(key), second(value) | |
88 { | 82 { |
89 } | 83 } |
90 | 84 |
91 void erase() | 85 void erase() |
92 { | 86 { |
93 super::erase(); | 87 super::erase(); |
94 second = value_type(); | 88 second = value_type(); |
Wladimir Palant
2017/10/10 14:33:59
This is a logic change. Previously, StringMap::era
sergei
2017/10/11 10:03:21
The new version looks better.
| |
95 } | 89 } |
96 }; | 90 }; |
97 } | 91 } |
98 | 92 |
99 class StringSet : public Set<StringMap_internal::StringSetEntry> | 93 using StringSet = Set<StringMap_internal::StringSetEntry>; |
100 { | |
101 }; | |
102 | 94 |
103 template<typename Value> | 95 template<typename Value> |
104 class StringMap : public Map<StringMap_internal::StringMapEntry<Value>, Value> | 96 using StringMap = Map<StringMap_internal::StringMapEntry<Value>>; |
105 { | |
106 public: | |
107 typedef Map<StringMap_internal::StringMapEntry<Value>, Value> super; | |
108 typedef typename super::entry_type entry_type; | |
109 typedef typename super::size_type size_type; | |
110 | |
111 explicit StringMap(size_type expectedEntries = 0) | |
112 : super(expectedEntries) | |
113 { | |
114 } | |
115 | |
116 StringMap(std::initializer_list<entry_type> list) | |
117 : super(list.size()) | |
118 { | |
119 for (const auto& item : list) | |
120 super::insert(item); | |
121 } | |
122 }; | |
LEFT | RIGHT |