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

Delta Between Two Patch Sets: compiled/StringMap.h

Issue 29333474: Issue 4125 - [emscripten] Convert filter classes to C++ (Closed)
Left Patch Set: Addressed Sergei`s comments again and added some asserts Created Feb. 23, 2016, 12:30 p.m.
Right Patch Set: Addressed comments from Patch Set 28 Created March 21, 2017, 10:04 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « compiled/String.h ('k') | compiled/StringScanner.h » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 #pragma once 1 #pragma once
2 2
3 #include <cstddef> 3 #include <cstddef>
4 #include <cmath> 4 #include <cmath>
5 #include <initializer_list> 5 #include <initializer_list>
6 #include <memory> 6 #include <memory>
7 7
8 #include "String.h" 8 #include "String.h"
9 #include "debug.h" 9 #include "debug.h"
10 10
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
84 template<typename Entry> 84 template<typename Entry>
85 class HashContainer 85 class HashContainer
86 { 86 {
87 public: 87 public:
88 typedef Entry entry_type; 88 typedef Entry entry_type;
89 typedef size_t size_type; 89 typedef size_t size_type;
90 typedef HashContainerIterator<Entry> const_iterator; 90 typedef HashContainerIterator<Entry> const_iterator;
91 typedef HashContainerReference<const Entry> const_reference; 91 typedef HashContainerReference<const Entry> const_reference;
92 92
93 private: 93 private:
94 HashContainer(const HashContainer& other); 94 explicit HashContainer(const HashContainer& other);
95 void operator=(const HashContainer& other); 95 void operator=(const HashContainer& other);
96 96
97 protected: 97 protected:
98 static constexpr size_type MIN_BUCKETS = 1; 98 static constexpr size_type MIN_BUCKETS = 1;
99 static constexpr double LOAD_FACTOR = 0.8; 99 static constexpr double LOAD_FACTOR = 0.8;
100 std::unique_ptr<entry_type[]> mBuckets; 100 std::unique_ptr<entry_type[]> mBuckets;
101 size_type mBucketCount; 101 size_type mBucketCount;
102 size_type mEntryCount; 102 size_type mEntryCount;
103 103
104 #if defined(DEBUG) 104 #if defined(DEBUG)
(...skipping 25 matching lines...) Expand all
130 entry_type* find_bucket(const String& key) const 130 entry_type* find_bucket(const String& key) const
131 { 131 {
132 size_type h = hash(key); 132 size_type h = hash(key);
133 133
134 // This does quadratic probing, effectively the following formula is used: 134 // This does quadratic probing, effectively the following formula is used:
135 // pos = (hash + 1/2 i + 1/2 i ^ 2) mod bucketCount 135 // pos = (hash + 1/2 i + 1/2 i ^ 2) mod bucketCount
136 for (size_type i = 0; ; ++i) 136 for (size_type i = 0; ; ++i)
137 { 137 {
138 // mBucketCount is 2^n so (h & mBucketCount - 1) is equivalent to 138 // mBucketCount is 2^n so (h & mBucketCount - 1) is equivalent to
139 // h % mBucketCount but significantly faster. 139 // h % mBucketCount but significantly faster.
140 entry_type* entry = &mBuckets[h & mBucketCount - 1]; 140 entry_type* entry = &mBuckets[h & (mBucketCount - 1)];
141 if (entry->first.is_invalid() || entry->first.equals(key)) 141 if (entry->first.is_invalid() || entry->first.equals(key))
142 return entry; 142 return entry;
143 h += i; 143 h += i;
144 } 144 }
145 } 145 }
146 146
147 void resize(size_type bucketCount) 147 void resize(size_type bucketCount)
148 { 148 {
149 std::unique_ptr<entry_type[]> oldBuckets(std::move(mBuckets)); 149 std::unique_ptr<entry_type[]> oldBuckets(std::move(mBuckets));
150 size_type oldCount = mBucketCount; 150 size_type oldCount = mBucketCount;
151 151
152 mEntryCount = 0; 152 mEntryCount = 0;
153 mBucketCount = bucketCount; 153 mBucketCount = bucketCount;
154 mBuckets.reset(new entry_type[mBucketCount]); 154 mBuckets.reset(new entry_type[mBucketCount]);
155 // Working around https://github.com/waywardmonkeys/emscripten-trace-colle ctor/issues/2 here 155 // Working around https://github.com/waywardmonkeys/emscripten-trace-colle ctor/issues/2 here
156 annotate_address(reinterpret_cast<size_type*>(mBuckets.get()) - 1, "Hash t able buffer"); 156 annotate_address(reinterpret_cast<size_type*>(mBuckets.get()) - 1, "Hash t able buffer");
157 157
158 // Copy old entries into the new buffer 158 // Copy old entries into the new buffer
159 for (size_type i = 0; i < oldCount; i++) 159 for (size_type i = 0; i < oldCount; i++)
160 { 160 {
161 entry_type& entry = oldBuckets[i]; 161 entry_type& entry = oldBuckets[i];
162 if (!entry.first.is_invalid()) 162 if (!entry.first.is_invalid() && !entry.first.is_deleted())
163 { 163 {
164 *find_bucket(entry.first) = entry; 164 *find_bucket(entry.first) = entry;
165 mEntryCount++; 165 mEntryCount++;
166 } 166 }
167 } 167 }
168 } 168 }
169 169
170 entry_type* assign(entry_type* existing, const entry_type& entry) 170 entry_type* assign(entry_type* existing, const entry_type& entry)
171 { 171 {
172 if (existing->first.is_invalid()) 172 if (existing->first.is_invalid())
(...skipping 11 matching lines...) Expand all
184 *existing = entry; 184 *existing = entry;
185 return existing; 185 return existing;
186 } 186 }
187 187
188 public: 188 public:
189 void insert(const entry_type& entry) 189 void insert(const entry_type& entry)
190 { 190 {
191 assign(find_bucket(entry.first), entry); 191 assign(find_bucket(entry.first), entry);
192 } 192 }
193 193
194 bool erase(const String& key)
195 {
196 entry_type* entry = find_bucket(key);
197 if (entry->first.is_invalid())
198 return false;
199
200 entry->first.erase();
201 return true;
202 }
203
194 const_reference find(const String& key) const 204 const_reference find(const String& key) const
195 { 205 {
196 return const_reference(find_bucket(key)); 206 return const_reference(find_bucket(key));
197 } 207 }
198 208
199 const_iterator begin() const 209 const_iterator begin() const
200 { 210 {
201 return const_iterator(&mBuckets[0], &mBuckets[mBucketCount]); 211 return const_iterator(&mBuckets[0], &mBuckets[mBucketCount]);
202 } 212 }
203 213
(...skipping 12 matching lines...) Expand all
216 } 226 }
217 227
218 DependentString first; 228 DependentString first;
219 }; 229 };
220 230
221 template<typename T> 231 template<typename T>
222 struct StringMapEntry 232 struct StringMapEntry
223 { 233 {
224 StringMapEntry() {} 234 StringMapEntry() {}
225 StringMapEntry(const String& key) 235 StringMapEntry(const String& key)
226 : first(key) 236 : first(key), second()
227 { 237 {
228 } 238 }
229 StringMapEntry(const String& key, T value) 239 StringMapEntry(const String& key, T value)
230 : first(key), second(value) 240 : first(key), second(value)
231 { 241 {
232 } 242 }
233 243
234 DependentString first; 244 DependentString first;
235 T second; 245 T second;
236 }; 246 };
(...skipping 19 matching lines...) Expand all
256 mInsertCounter = mMap->mInsertCounter; 266 mInsertCounter = mMap->mInsertCounter;
257 mHash = mMap->hash(key); 267 mHash = mMap->hash(key);
258 #endif 268 #endif
259 } 269 }
260 270
261 void assign(const String& key, const T& value) 271 void assign(const String& key, const T& value)
262 { 272 {
263 #if defined(DEBUG) 273 #if defined(DEBUG)
264 assert(mInsertCounter == mMap->mInsertCounter, 274 assert(mInsertCounter == mMap->mInsertCounter,
265 u"There should be no insert operations performed between map.find() an d assign()"_str); 275 u"There should be no insert operations performed between map.find() an d assign()"_str);
266 assert(mHash == mMap->hash(key), 276 assert(mHash == mMap->hash(key),
sergei 2016/02/23 15:07:36 It might be better to compare keys instead of hash
Wladimir Palant 2016/02/23 21:35:00 a) We cannot really keep a reference to the key si
267 u"The keys used in map.find() and assign() should be identical"_str); 277 u"The keys used in map.find() and assign() should be identical"_str);
268 #endif 278 #endif
269 279
270 mMap->assign(this->mEntry, entry_type(key, value)); 280 mMap->assign(this->mEntry, entry_type(key, value));
271 } 281 }
272 }; 282 };
273 } 283 }
274 284
275 class StringSet 285 class StringSet
276 : public StringMap_internal::HashContainer<StringMap_internal::StringSetEntry> 286 : public StringMap_internal::HashContainer<StringMap_internal::StringSetEntry>
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
316 const_reference find(const String& key) const 326 const_reference find(const String& key) const
317 { 327 {
318 return super::find(key); 328 return super::find(key);
319 } 329 }
320 330
321 reference find(const String& key) 331 reference find(const String& key)
322 { 332 {
323 return reference(this, key, super::find_bucket(key)); 333 return reference(this, key, super::find_bucket(key));
324 } 334 }
325 }; 335 };
LEFTRIGHT

Powered by Google App Engine
This is Rietveld