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 |
(...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 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
230 | 224 |
231 const_iterator end() const | 225 const_iterator end() const |
232 { | 226 { |
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 } |
234 | |
235 void clear() | |
236 { | |
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"); | |
252 } | |
240 }; | 253 }; |
241 | 254 |
242 struct StringSetEntry | 255 struct StringSetEntry |
243 { | 256 { |
244 StringSetEntry() {} | 257 StringSetEntry() {} |
245 StringSetEntry(const String& key) | 258 StringSetEntry(const String& key) |
246 : first(key) | 259 : first(key) |
247 { | 260 { |
248 } | 261 } |
249 | 262 |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
348 const_reference find(const String& key) const | 361 const_reference find(const String& key) const |
349 { | 362 { |
350 return super::find(key); | 363 return super::find(key); |
351 } | 364 } |
352 | 365 |
353 reference find(const String& key) | 366 reference find(const String& key) |
354 { | 367 { |
355 return reference(this, key, super::find_bucket(key)); | 368 return reference(this, key, super::find_bucket(key)); |
356 } | 369 } |
357 }; | 370 }; |
OLD | NEW |