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: Added OwnedStringMap to address ownership of keys Created Nov. 25, 2017, 3:22 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
« no previous file with comments | « compiled/ElemHide.cpp ('k') | compiled/String.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
142 } 142 }
143 } 143 }
144 144
145 void resize(size_type bucketCount) 145 void resize(size_type bucketCount)
146 { 146 {
147 std::unique_ptr<entry_type[]> oldBuckets(std::move(mBuckets)); 147 std::unique_ptr<entry_type[]> oldBuckets(std::move(mBuckets));
148 size_type oldCount = mBucketCount; 148 size_type oldCount = mBucketCount;
149 149
150 mEntryCount = 0; 150 mEntryCount = 0;
151 mBucketCount = bucketCount; 151 mBucketCount = bucketCount;
152 mBuckets.reset(new entry_type[mBucketCount]); 152 allocate();
153 // Working around https://github.com/waywardmonkeys/emscripten-trace-colle ctor/issues/2 here
154 annotate_address(reinterpret_cast<size_type*>(mBuckets.get()) - 1, "Hash t able buffer");
155 153
156 // Copy old entries into the new buffer 154 // Copy old entries into the new buffer
157 for (size_type i = 0; i < oldCount; i++) 155 for (size_type i = 0; i < oldCount; i++)
158 { 156 {
159 entry_type& entry = oldBuckets[i]; 157 entry_type& entry = oldBuckets[i];
160 if (!entry.is_invalid() && !entry.is_deleted()) 158 if (!entry.is_invalid() && !entry.is_deleted())
161 { 159 {
162 *find_bucket(entry.first) = entry; 160 *find_bucket(entry.first) = entry;
163 mEntryCount++; 161 mEntryCount++;
164 } 162 }
(...skipping 11 matching lines...) Expand all
176 } 174 }
177 mEntryCount++; 175 mEntryCount++;
178 #if defined(DEBUG) 176 #if defined(DEBUG)
179 mInsertCounter++; 177 mInsertCounter++;
180 #endif 178 #endif
181 } 179 }
182 *existing = entry; 180 *existing = entry;
183 return existing; 181 return existing;
184 } 182 }
185 183
184 void allocate()
185 {
186 mBuckets.reset(new entry_type[mBucketCount]);
187 // Working around https://github.com/waywardmonkeys/emscripten-trace-colle ctor/issues/2 here
188 annotate_address(reinterpret_cast<size_type*>(mBuckets.get()) - 1, "Hash t able buffer");
189 }
190
186 public: 191 public:
187 explicit HashContainer(size_type expectedEntries = 0) 192 explicit HashContainer(size_type expectedEntries = 0)
188 : mEntryCount(0) 193 : mEntryCount(0)
189 { 194 {
190 expectedEntries = ceil(expectedEntries / LOAD_FACTOR); 195 expectedEntries = ceil(expectedEntries / LOAD_FACTOR);
191 mBucketCount = MIN_BUCKETS; 196 mBucketCount = MIN_BUCKETS;
192 while (mBucketCount < expectedEntries) 197 while (mBucketCount < expectedEntries)
193 mBucketCount <<= 1; 198 mBucketCount <<= 1;
194 199
195 mBuckets.reset(new entry_type[mBucketCount]); 200 allocate();
196 // Working around https://github.com/waywardmonkeys/emscripten-trace-colle ctor/issues/2 here 201 }
197 annotate_address(reinterpret_cast<size_type*>(mBuckets.get()) - 1, "Hash t able buffer"); 202
203 void clear()
204 {
205 mEntryCount = 0;
206 allocate();
198 } 207 }
199 208
200 void insert(const entry_type& entry) 209 void insert(const entry_type& entry)
201 { 210 {
202 assign(find_bucket(entry.first), entry); 211 assign(find_bucket(entry.first), entry);
203 } 212 }
204 213
205 bool erase(const key_type& key) 214 bool erase(const key_type& key)
206 { 215 {
207 entry_type* entry = find_bucket(key); 216 entry_type* entry = find_bucket(key);
(...skipping 12 matching lines...) Expand all
220 const_iterator begin() const 229 const_iterator begin() const
221 { 230 {
222 return const_iterator(&mBuckets[0], &mBuckets[mBucketCount]); 231 return const_iterator(&mBuckets[0], &mBuckets[mBucketCount]);
223 } 232 }
224 233
225 const_iterator end() const 234 const_iterator end() const
226 { 235 {
227 return const_iterator(&mBuckets[mBucketCount], &mBuckets[mBucketCount]); 236 return const_iterator(&mBuckets[mBucketCount], &mBuckets[mBucketCount]);
228 } 237 }
229 238
239 bool empty() const
240 {
241 return mEntryCount == 0;
242 }
243
230 size_type size() const 244 size_type size() const
231 { 245 {
232 return mEntryCount; 246 return mEntryCount;
233 } 247 }
234 }; 248 };
235 249
236 template<typename Entry> 250 template<typename Entry>
237 struct MapReference : public HashContainerReference<Entry> 251 struct MapReference : public HashContainerReference<Entry>
238 { 252 {
239 typedef HashContainerReference<Entry> super; 253 typedef HashContainerReference<Entry> super;
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
314 const_reference find(const key_type& key) const 328 const_reference find(const key_type& key) const
315 { 329 {
316 return super::find(key); 330 return super::find(key);
317 } 331 }
318 332
319 reference find(const key_type& key) 333 reference find(const key_type& key)
320 { 334 {
321 return reference(this, key, super::find_bucket(key)); 335 return reference(this, key, super::find_bucket(key));
322 } 336 }
323 }; 337 };
OLDNEW
« no previous file with comments | « compiled/ElemHide.cpp ('k') | compiled/String.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld