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 |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
110 private: | 110 private: |
111 explicit HashContainer(const HashContainer& other); | 111 explicit HashContainer(const HashContainer& other); |
112 void operator=(const HashContainer& other); | 112 void operator=(const HashContainer& other); |
113 | 113 |
114 protected: | 114 protected: |
115 static constexpr size_type MIN_BUCKETS = 1; | 115 static constexpr size_type MIN_BUCKETS = 1; |
116 static constexpr double LOAD_FACTOR = 0.8; | 116 static constexpr double LOAD_FACTOR = 0.8; |
117 std::unique_ptr<entry_type[]> mBuckets; | 117 std::unique_ptr<entry_type[]> mBuckets; |
118 size_type mBucketCount; | 118 size_type mBucketCount; |
119 size_type mEntryCount; | 119 size_type mEntryCount; |
120 size_type mExpectedEntries; | |
Wladimir Palant
2017/10/09 08:39:46
No point recalculating bucket count if the map is
| |
120 | 121 |
121 #if defined(DEBUG) | 122 #if defined(DEBUG) |
122 size_type mInsertCounter; | 123 size_type mInsertCounter; |
123 #endif | 124 #endif |
124 | 125 |
125 explicit HashContainer(size_type expectedEntries = 0) | 126 explicit HashContainer(size_type expectedEntries = 0) |
126 : mEntryCount(0) | 127 : mEntryCount(0), mExpectedEntries(expectedEntries) |
127 { | 128 { |
128 expectedEntries = ceil(expectedEntries / LOAD_FACTOR); | 129 init(expectedEntries); |
129 mBucketCount = MIN_BUCKETS; | |
130 while (mBucketCount < expectedEntries) | |
131 mBucketCount <<= 1; | |
132 | |
133 mBuckets.reset(new entry_type[mBucketCount]); | |
134 // Working around https://github.com/waywardmonkeys/emscripten-trace-colle ctor/issues/2 here | |
135 annotate_address(reinterpret_cast<size_type*>(mBuckets.get()) - 1, "Hash t able buffer"); | |
136 } | 130 } |
137 | 131 |
138 static size_type hash(const String& str) | 132 static size_type hash(const String& str) |
139 { | 133 { |
140 // FNV-1a hash function | 134 // FNV-1a hash function |
141 size_type result = 2166136261; | 135 size_type result = 2166136261; |
142 for (String::size_type i = 0; i < str.length(); i++) | 136 for (String::size_type i = 0; i < str.length(); i++) |
143 result = (result ^ str[i]) * 16777619; | 137 result = (result ^ str[i]) * 16777619; |
144 return result; | 138 return result; |
145 } | 139 } |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
233 return const_iterator(&mBuckets[mBucketCount], &mBuckets[mBucketCount]); | 227 return const_iterator(&mBuckets[mBucketCount], &mBuckets[mBucketCount]); |
234 } | 228 } |
235 | 229 |
236 size_type size() const | 230 size_type size() const |
237 { | 231 { |
238 return mEntryCount; | 232 return mEntryCount; |
239 } | 233 } |
240 | 234 |
241 void clear() | 235 void clear() |
242 { | 236 { |
243 resize(0); | 237 mEntryCount = 0; |
238 init(mExpectedEntries); | |
Wladimir Palant
2017/10/09 08:39:45
IMHO, the shared function between constructor and
| |
239 } | |
240 | |
241 private: | |
242 void init(size_type expectedEntries) | |
243 { | |
244 expectedEntries = ceil(expectedEntries / LOAD_FACTOR); | |
245 mBucketCount = MIN_BUCKETS; | |
246 while (mBucketCount < expectedEntries) | |
247 mBucketCount <<= 1; | |
248 | |
249 mBuckets.reset(new entry_type[mBucketCount]); | |
250 // Working around https://github.com/waywardmonkeys/emscripten-trace-colle ctor/issues/2 here | |
251 annotate_address(reinterpret_cast<size_type*>(mBuckets.get()) - 1, "Hash t able buffer"); | |
244 } | 252 } |
245 }; | 253 }; |
246 | 254 |
247 struct StringSetEntry | 255 struct StringSetEntry |
248 { | 256 { |
249 StringSetEntry() {} | 257 StringSetEntry() {} |
250 StringSetEntry(const String& key) | 258 StringSetEntry(const String& key) |
251 : first(key) | 259 : first(key) |
252 { | 260 { |
253 } | 261 } |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
353 const_reference find(const String& key) const | 361 const_reference find(const String& key) const |
354 { | 362 { |
355 return super::find(key); | 363 return super::find(key); |
356 } | 364 } |
357 | 365 |
358 reference find(const String& key) | 366 reference find(const String& key) |
359 { | 367 { |
360 return reference(this, key, super::find_bucket(key)); | 368 return reference(this, key, super::find_bucket(key)); |
361 } | 369 } |
362 }; | 370 }; |
LEFT | RIGHT |