Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(190)

Side by Side Diff: third_party/WebKit/Source/wtf/HashMap.h

Issue 1764973002: WTF::HashTable: Implement move semantics for keys and values. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2005, 2006, 2007, 2008, 2011 Apple Inc. All rights reserved. 2 * Copyright (C) 2005, 2006, 2007, 2008, 2011 Apple Inc. All rights reserved.
3 * 3 *
4 * This library is free software; you can redistribute it and/or 4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public 5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either 6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version. 7 * version 2 of the License, or (at your option) any later version.
8 * 8 *
9 * This library is distributed in the hope that it will be useful, 9 * This library 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 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
120 const HashMapValuesProxy& values() const { return static_cast<const HashMapV aluesProxy&>(*this); } 120 const HashMapValuesProxy& values() const { return static_cast<const HashMapV aluesProxy&>(*this); }
121 121
122 iterator find(KeyPeekInType); 122 iterator find(KeyPeekInType);
123 const_iterator find(KeyPeekInType) const; 123 const_iterator find(KeyPeekInType) const;
124 bool contains(KeyPeekInType) const; 124 bool contains(KeyPeekInType) const;
125 MappedPeekType get(KeyPeekInType) const; 125 MappedPeekType get(KeyPeekInType) const;
126 126
127 // replaces value but not key if key is already present return value is a 127 // replaces value but not key if key is already present return value is a
128 // pair of the iterator to the key location, and a boolean that's true if a 128 // pair of the iterator to the key location, and a boolean that's true if a
129 // new value was actually added 129 // new value was actually added
130 AddResult set(KeyPeekInType, MappedPassInType); 130 template <typename IncomingKeyType, typename IncomingMappedType>
131 AddResult set(IncomingKeyType&&, IncomingMappedType&&);
131 132
132 // does nothing if key is already present return value is a pair of the 133 // does nothing if key is already present return value is a pair of the
133 // iterator to the key location, and a boolean that's true if a new value 134 // iterator to the key location, and a boolean that's true if a new value
134 // was actually added 135 // was actually added
135 AddResult add(KeyPeekInType, MappedPassInType); 136 template <typename IncomingKeyType, typename IncomingMappedType>
137 AddResult add(IncomingKeyType&&, IncomingMappedType&&);
136 138
137 void remove(KeyPeekInType); 139 void remove(KeyPeekInType);
138 void remove(iterator); 140 void remove(iterator);
139 void clear(); 141 void clear();
140 template <typename Collection> 142 template <typename Collection>
141 void removeAll(const Collection& toBeRemoved) { WTF::removeAll(*this, toBeRe moved); } 143 void removeAll(const Collection& toBeRemoved) { WTF::removeAll(*this, toBeRe moved); }
142 144
143 MappedPassOutType take(KeyPeekInType); // efficient combination of get with remove 145 MappedPassOutType take(KeyPeekInType); // efficient combination of get with remove
144 146
145 // An alternate version of find() that finds the object by hashing and 147 // An alternate version of find() that finds the object by hashing and
(...skipping 13 matching lines...) Expand all
159 // static bool equal(const ValueType&, const T&); 161 // static bool equal(const ValueType&, const T&);
160 // static translate(ValueType&, const T&, unsigned hashCode); 162 // static translate(ValueType&, const T&, unsigned hashCode);
161 template <typename HashTranslator, typename T> AddResult add(const T&, Mappe dPassInType); 163 template <typename HashTranslator, typename T> AddResult add(const T&, Mappe dPassInType);
162 164
163 static bool isValidKey(KeyPeekInType); 165 static bool isValidKey(KeyPeekInType);
164 166
165 template <typename VisitorDispatcher> 167 template <typename VisitorDispatcher>
166 void trace(VisitorDispatcher visitor) { m_impl.trace(visitor); } 168 void trace(VisitorDispatcher visitor) { m_impl.trace(visitor); }
167 169
168 private: 170 private:
169 AddResult inlineAdd(KeyPeekInType, MappedPassInReferenceType); 171 template <typename IncomingKeyType, typename IncomingMappedType>
172 AddResult inlineAdd(IncomingKeyType&&, IncomingMappedType&&);
170 173
171 HashTableType m_impl; 174 HashTableType m_impl;
172 }; 175 };
173 176
174 template <typename KeyArg, typename MappedArg, typename HashArg, typename KeyTra itsArg, typename MappedTraitsArg, typename Allocator> 177 template <typename KeyArg, typename MappedArg, typename HashArg, typename KeyTra itsArg, typename MappedTraitsArg, typename Allocator>
175 class HashMap<KeyArg, MappedArg, HashArg, KeyTraitsArg, MappedTraitsArg, Allocat or>::HashMapKeysProxy : 178 class HashMap<KeyArg, MappedArg, HashArg, KeyTraitsArg, MappedTraitsArg, Allocat or>::HashMapKeysProxy :
176 private HashMap<KeyArg, MappedArg, HashArg, KeyTraitsArg, MappedTraitsArg, A llocator> { 179 private HashMap<KeyArg, MappedArg, HashArg, KeyTraitsArg, MappedTraitsArg, A llocator> {
177 DISALLOW_NEW(); 180 DISALLOW_NEW();
178 public: 181 public:
179 typedef HashMap<KeyArg, MappedArg, HashArg, KeyTraitsArg, MappedTraitsArg, A llocator> HashMapType; 182 typedef HashMap<KeyArg, MappedArg, HashArg, KeyTraitsArg, MappedTraitsArg, A llocator> HashMapType;
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
257 { 260 {
258 return isHashTraitsEmptyValue<KeyTraits>(value.key); 261 return isHashTraitsEmptyValue<KeyTraits>(value.key);
259 } 262 }
260 }; 263 };
261 264
262 template <typename ValueTraits, typename HashFunctions> 265 template <typename ValueTraits, typename HashFunctions>
263 struct HashMapTranslator { 266 struct HashMapTranslator {
264 STATIC_ONLY(HashMapTranslator); 267 STATIC_ONLY(HashMapTranslator);
265 template <typename T> static unsigned hash(const T& key) { return HashFuncti ons::hash(key); } 268 template <typename T> static unsigned hash(const T& key) { return HashFuncti ons::hash(key); }
266 template <typename T, typename U> static bool equal(const T& a, const U& b) { return HashFunctions::equal(a, b); } 269 template <typename T, typename U> static bool equal(const T& a, const U& b) { return HashFunctions::equal(a, b); }
267 template <typename T, typename U, typename V> static void translate(T& locat ion, const U& key, const V& mapped) 270 template <typename T, typename U, typename V> static void translate(T& locat ion, U&& key, V&& mapped)
268 { 271 {
269 location.key = key; 272 location.key = std::forward<U>(key);
270 ValueTraits::ValueTraits::store(mapped, location.value); 273 ValueTraits::ValueTraits::store(std::forward<V>(mapped), location.value) ;
271 } 274 }
272 }; 275 };
273 276
274 template <typename ValueTraits, typename Translator> 277 template <typename ValueTraits, typename Translator>
275 struct HashMapTranslatorAdapter { 278 struct HashMapTranslatorAdapter {
276 STATIC_ONLY(HashMapTranslatorAdapter); 279 STATIC_ONLY(HashMapTranslatorAdapter);
277 template <typename T> static unsigned hash(const T& key) { return Translator ::hash(key); } 280 template <typename T> static unsigned hash(const T& key) { return Translator ::hash(key); }
278 template <typename T, typename U> static bool equal(const T& a, const U& b) { return Translator::equal(a, b); } 281 template <typename T, typename U> static bool equal(const T& a, const U& b) { return Translator::equal(a, b); }
279 template <typename T, typename U, typename V> static void translate(T& locat ion, const U& key, const V& mapped, unsigned hashCode) 282 template <typename T, typename U, typename V> static void translate(T& locat ion, U&& key, V&& mapped, unsigned hashCode)
280 { 283 {
281 Translator::translate(location.key, key, hashCode); 284 Translator::translate(location.key, std::forward<U>(key), hashCode);
282 ValueTraits::ValueTraits::store(mapped, location.value); 285 ValueTraits::ValueTraits::store(std::forward<V>(mapped), location.value) ;
283 } 286 }
284 }; 287 };
285 288
286 template <typename T, typename U, typename V, typename W, typename X, typename Y > 289 template <typename T, typename U, typename V, typename W, typename X, typename Y >
287 inline unsigned HashMap<T, U, V, W, X, Y>::size() const 290 inline unsigned HashMap<T, U, V, W, X, Y>::size() const
288 { 291 {
289 return m_impl.size(); 292 return m_impl.size();
290 } 293 }
291 294
292 template <typename T, typename U, typename V, typename W, typename X, typename Y > 295 template <typename T, typename U, typename V, typename W, typename X, typename Y >
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
361 364
362 template <typename T, typename U, typename V, typename W, typename X, typename Y > 365 template <typename T, typename U, typename V, typename W, typename X, typename Y >
363 template <typename HashTranslator, typename TYPE> 366 template <typename HashTranslator, typename TYPE>
364 inline bool 367 inline bool
365 HashMap<T, U, V, W, X, Y>::contains(const TYPE& value) const 368 HashMap<T, U, V, W, X, Y>::contains(const TYPE& value) const
366 { 369 {
367 return m_impl.template contains<HashMapTranslatorAdapter<ValueTraits, HashTr anslator>>(value); 370 return m_impl.template contains<HashMapTranslatorAdapter<ValueTraits, HashTr anslator>>(value);
368 } 371 }
369 372
370 template <typename T, typename U, typename V, typename W, typename X, typename Y > 373 template <typename T, typename U, typename V, typename W, typename X, typename Y >
374 template <typename IncomingKeyType, typename IncomingMappedType>
371 typename HashMap<T, U, V, W, X, Y>::AddResult 375 typename HashMap<T, U, V, W, X, Y>::AddResult
372 HashMap<T, U, V, W, X, Y>::inlineAdd(KeyPeekInType key, MappedPassInReferenceTyp e mapped) 376 HashMap<T, U, V, W, X, Y>::inlineAdd(IncomingKeyType&& key, IncomingMappedType&& mapped)
Mikhail 2016/03/04 12:58:02 think it's worth switching to 'auto HashMap<T, U,
Yuta Kitamura 2016/03/04 13:55:37 That syntax is "to be discussed" section in chromi
373 { 377 {
374 return m_impl.template add<HashMapTranslator<ValueTraits, HashFunctions>>(ke y, mapped); 378 return m_impl.template add<HashMapTranslator<ValueTraits, HashFunctions>>(st d::forward<IncomingKeyType>(key), std::forward<IncomingMappedType>(mapped));
375 } 379 }
376 380
377 template <typename T, typename U, typename V, typename W, typename X, typename Y > 381 template <typename T, typename U, typename V, typename W, typename X, typename Y >
382 template <typename IncomingKeyType, typename IncomingMappedType>
378 typename HashMap<T, U, V, W, X, Y>::AddResult 383 typename HashMap<T, U, V, W, X, Y>::AddResult
379 HashMap<T, U, V, W, X, Y>::set(KeyPeekInType key, MappedPassInType mapped) 384 HashMap<T, U, V, W, X, Y>::set(IncomingKeyType&& key, IncomingMappedType&& mappe d)
380 { 385 {
381 AddResult result = inlineAdd(key, mapped); 386 AddResult result = inlineAdd(std::forward<IncomingKeyType>(key), std::forwar d<IncomingMappedType>(mapped));
382 if (!result.isNewEntry) { 387 if (!result.isNewEntry) {
383 // The inlineAdd call above found an existing hash table entry; we need 388 // The inlineAdd call above found an existing hash table entry; we need
384 // to set the mapped value. 389 // to set the mapped value.
385 MappedTraits::store(mapped, result.storedValue->value); 390 //
391 // It's safe to call std::forward again, because |mapped| isn't moved if there's an existing entry.
392 MappedTraits::store(std::forward<IncomingMappedType>(mapped), result.sto redValue->value);
386 } 393 }
387 return result; 394 return result;
388 } 395 }
389 396
390 template <typename T, typename U, typename V, typename W, typename X, typename Y > 397 template <typename T, typename U, typename V, typename W, typename X, typename Y >
391 template <typename HashTranslator, typename TYPE> 398 template <typename HashTranslator, typename TYPE>
392 typename HashMap<T, U, V, W, X, Y>::AddResult 399 typename HashMap<T, U, V, W, X, Y>::AddResult
393 HashMap<T, U, V, W, X, Y>::add(const TYPE& key, MappedPassInType value) 400 HashMap<T, U, V, W, X, Y>::add(const TYPE& key, MappedPassInType value)
394 { 401 {
395 return m_impl.template addPassingHashCode<HashMapTranslatorAdapter<ValueTrai ts, HashTranslator>>(key, value); 402 return m_impl.template addPassingHashCode<HashMapTranslatorAdapter<ValueTrai ts, HashTranslator>>(key, value);
396 } 403 }
397 404
398 template <typename T, typename U, typename V, typename W, typename X, typename Y > 405 template <typename T, typename U, typename V, typename W, typename X, typename Y >
406 template <typename IncomingKeyType, typename IncomingMappedType>
399 typename HashMap<T, U, V, W, X, Y>::AddResult 407 typename HashMap<T, U, V, W, X, Y>::AddResult
400 HashMap<T, U, V, W, X, Y>::add(KeyPeekInType key, MappedPassInType mapped) 408 HashMap<T, U, V, W, X, Y>::add(IncomingKeyType&& key, IncomingMappedType&& mappe d)
401 { 409 {
402 return inlineAdd(key, mapped); 410 return inlineAdd(std::forward<IncomingKeyType>(key), std::forward<IncomingMa ppedType>(mapped));
403 } 411 }
404 412
405 template <typename T, typename U, typename V, typename W, typename X, typename Y > 413 template <typename T, typename U, typename V, typename W, typename X, typename Y >
406 typename HashMap<T, U, V, W, X, Y>::MappedPeekType 414 typename HashMap<T, U, V, W, X, Y>::MappedPeekType
407 HashMap<T, U, V, W, X, Y>::get(KeyPeekInType key) const 415 HashMap<T, U, V, W, X, Y>::get(KeyPeekInType key) const
408 { 416 {
409 ValueType* entry = const_cast<HashTableType&>(m_impl).lookup(key); 417 ValueType* entry = const_cast<HashTableType&>(m_impl).lookup(key);
410 if (!entry) 418 if (!entry)
411 return MappedTraits::peek(MappedTraits::emptyValue()); 419 return MappedTraits::peek(MappedTraits::emptyValue());
412 return MappedTraits::peek(entry->value); 420 return MappedTraits::peek(entry->value);
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
516 STATIC_ONLY(NeedsTracing); 524 STATIC_ONLY(NeedsTracing);
517 static const bool value = false; 525 static const bool value = false;
518 }; 526 };
519 #endif 527 #endif
520 528
521 } // namespace WTF 529 } // namespace WTF
522 530
523 using WTF::HashMap; 531 using WTF::HashMap;
524 532
525 #endif // WTF_HashMap_h 533 #endif // WTF_HashMap_h
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698