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

Side by Side Diff: compiled/Map.h

Issue 29587914: Issue 5142 - Convert Element Hiding to C++ (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Rebased on master. Created Dec. 5, 2017, 6:01 p.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
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after
139 } 139 }
140 } 140 }
141 141
142 void resize(size_type bucketCount) 142 void resize(size_type bucketCount)
143 { 143 {
144 std::unique_ptr<entry_type[]> oldBuckets(std::move(mBuckets)); 144 std::unique_ptr<entry_type[]> oldBuckets(std::move(mBuckets));
145 size_type oldCount = mBucketCount; 145 size_type oldCount = mBucketCount;
146 146
147 mEntryCount = 0; 147 mEntryCount = 0;
148 mBucketCount = bucketCount; 148 mBucketCount = bucketCount;
149 mBuckets.reset(new entry_type[mBucketCount]); 149 allocate();
150 // Working around https://github.com/waywardmonkeys/emscripten-trace-colle ctor/issues/2 here
151 annotate_address(reinterpret_cast<size_type*>(mBuckets.get()) - 1, "Hash t able buffer");
152 150
153 // Copy old entries into the new buffer 151 // Copy old entries into the new buffer
154 for (size_type i = 0; i < oldCount; i++) 152 for (size_type i = 0; i < oldCount; i++)
155 { 153 {
156 entry_type& entry = oldBuckets[i]; 154 entry_type& entry = oldBuckets[i];
157 if (!entry.is_invalid() && !entry.is_deleted()) 155 if (!entry.is_invalid() && !entry.is_deleted())
158 { 156 {
159 *find_bucket(entry.first) = entry; 157 *find_bucket(entry.first) = entry;
160 mEntryCount++; 158 mEntryCount++;
161 } 159 }
(...skipping 11 matching lines...) Expand all
173 } 171 }
174 mEntryCount++; 172 mEntryCount++;
175 #if defined(DEBUG) 173 #if defined(DEBUG)
176 mInsertCounter++; 174 mInsertCounter++;
177 #endif 175 #endif
178 } 176 }
179 *existing = entry; 177 *existing = entry;
180 return existing; 178 return existing;
181 } 179 }
182 180
181 void allocate()
182 {
183 mBuckets.reset(new entry_type[mBucketCount]);
184 // Working around https://github.com/waywardmonkeys/emscripten-trace-colle ctor/issues/2 here
185 annotate_address(reinterpret_cast<size_type*>(mBuckets.get()) - 1, "Hash t able buffer");
186 }
187
183 public: 188 public:
184 explicit HashContainer(size_type expectedEntries = 0) 189 explicit HashContainer(size_type expectedEntries = 0)
185 : mEntryCount(0) 190 : mEntryCount(0)
186 { 191 {
187 expectedEntries = ceil(expectedEntries / LOAD_FACTOR); 192 expectedEntries = ceil(expectedEntries / LOAD_FACTOR);
188 mBucketCount = MIN_BUCKETS; 193 mBucketCount = MIN_BUCKETS;
189 while (mBucketCount < expectedEntries) 194 while (mBucketCount < expectedEntries)
190 mBucketCount <<= 1; 195 mBucketCount <<= 1;
191 196
192 mBuckets.reset(new entry_type[mBucketCount]); 197 allocate();
193 // Working around https://github.com/waywardmonkeys/emscripten-trace-colle ctor/issues/2 here 198 }
194 annotate_address(reinterpret_cast<size_type*>(mBuckets.get()) - 1, "Hash t able buffer"); 199
200 void clear()
201 {
202 mEntryCount = 0;
203 allocate();
195 } 204 }
196 205
197 void insert(const entry_type& entry) 206 void insert(const entry_type& entry)
198 { 207 {
199 assign(find_bucket(entry.first), entry); 208 assign(find_bucket(entry.first), entry);
200 } 209 }
201 210
202 bool erase(key_type_cref key) 211 bool erase(key_type_cref key)
203 { 212 {
204 entry_type* entry = find_bucket(key); 213 entry_type* entry = find_bucket(key);
(...skipping 12 matching lines...) Expand all
217 const_iterator begin() const 226 const_iterator begin() const
218 { 227 {
219 return const_iterator(&mBuckets[0], &mBuckets[mBucketCount]); 228 return const_iterator(&mBuckets[0], &mBuckets[mBucketCount]);
220 } 229 }
221 230
222 const_iterator end() const 231 const_iterator end() const
223 { 232 {
224 return const_iterator(&mBuckets[mBucketCount], &mBuckets[mBucketCount]); 233 return const_iterator(&mBuckets[mBucketCount], &mBuckets[mBucketCount]);
225 } 234 }
226 235
236 bool empty() const
sergei 2018/01/15 15:31:19 What about renaming of `empty` and `size` methods
hub 2018/01/16 02:57:39 I'm willing to not add empty() and use size() == 0
sergei 2018/01/16 16:43:58 As I see it's really used as !((filter.GetDomains
237 {
238 return mEntryCount == 0;
239 }
240
227 size_type size() const 241 size_type size() const
228 { 242 {
229 return mEntryCount; 243 return mEntryCount;
230 } 244 }
231 }; 245 };
232 246
233 template<typename Entry> 247 template<typename Entry>
234 struct MapReference : public HashContainerReference<Entry> 248 struct MapReference : public HashContainerReference<Entry>
235 { 249 {
236 typedef HashContainerReference<Entry> super; 250 typedef HashContainerReference<Entry> super;
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
305 const_reference find(key_type_cref key) const 319 const_reference find(key_type_cref key) const
306 { 320 {
307 return super::find(key); 321 return super::find(key);
308 } 322 }
309 323
310 reference find(key_type_cref key) 324 reference find(key_type_cref key)
311 { 325 {
312 return reference(this, key, super::find_bucket(key)); 326 return reference(this, key, super::find_bucket(key));
313 } 327 }
314 }; 328 };
OLDNEW

Powered by Google App Engine
This is Rietveld