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

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

Issue 2643663002: WTF: Support std::initializer_list in HashMap. (Closed)
Patch Set: Created 3 years, 11 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
« no previous file with comments | « no previous file | third_party/WebKit/Source/wtf/HashMapTest.cpp » ('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 * 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
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details. 12 * Library General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU Library General Public License 14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to 15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA. 17 * Boston, MA 02110-1301, USA.
18 * 18 *
19 */ 19 */
20 20
21 #ifndef WTF_HashMap_h 21 #ifndef WTF_HashMap_h
22 #define WTF_HashMap_h 22 #define WTF_HashMap_h
23 23
24 #include "wtf/HashTable.h" 24 #include "wtf/HashTable.h"
25 #include "wtf/allocator/PartitionAllocator.h" 25 #include "wtf/allocator/PartitionAllocator.h"
26 #include <initializer_list>
26 27
27 namespace WTF { 28 namespace WTF {
28 29
29 template <typename KeyTraits, typename MappedTraits> 30 template <typename KeyTraits, typename MappedTraits>
30 struct HashMapValueTraits; 31 struct HashMapValueTraits;
31 32
32 struct KeyValuePairKeyExtractor { 33 struct KeyValuePairKeyExtractor {
33 STATIC_ONLY(KeyValuePairKeyExtractor); 34 STATIC_ONLY(KeyValuePairKeyExtractor);
34 template <typename T> 35 template <typename T>
35 static const typename T::KeyType& extract(const T& p) { 36 static const typename T::KeyType& extract(const T& p) {
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
81 HashMap() { 82 HashMap() {
82 static_assert(Allocator::isGarbageCollected || 83 static_assert(Allocator::isGarbageCollected ||
83 !IsPointerToGarbageCollectedType<KeyArg>::value, 84 !IsPointerToGarbageCollectedType<KeyArg>::value,
84 "Cannot put raw pointers to garbage-collected classes into " 85 "Cannot put raw pointers to garbage-collected classes into "
85 "an off-heap HashMap. Use HeapHashMap<> instead."); 86 "an off-heap HashMap. Use HeapHashMap<> instead.");
86 static_assert(Allocator::isGarbageCollected || 87 static_assert(Allocator::isGarbageCollected ||
87 !IsPointerToGarbageCollectedType<MappedArg>::value, 88 !IsPointerToGarbageCollectedType<MappedArg>::value,
88 "Cannot put raw pointers to garbage-collected classes into " 89 "Cannot put raw pointers to garbage-collected classes into "
89 "an off-heap HashMap. Use HeapHashMap<> instead."); 90 "an off-heap HashMap. Use HeapHashMap<> instead.");
90 } 91 }
92 HashMap(const HashMap&) = default;
93 HashMap& operator=(const HashMap&) = default;
94 HashMap(HashMap&&) = default;
95 HashMap& operator=(HashMap&&) = default;
96
97 // For example, HashMap<int, int>({{1, 11}, {2, 22}, {3, 33}}) will give you
98 // a HashMap containing a mapping {1 -> 11, 2 -> 22, 3 -> 33}.
99 HashMap(std::initializer_list<ValueType> elements);
100 HashMap& operator=(std::initializer_list<ValueType> elements);
91 101
92 typedef HashTableIteratorAdapter<HashTableType, ValueType> iterator; 102 typedef HashTableIteratorAdapter<HashTableType, ValueType> iterator;
93 typedef HashTableConstIteratorAdapter<HashTableType, ValueType> 103 typedef HashTableConstIteratorAdapter<HashTableType, ValueType>
94 const_iterator; 104 const_iterator;
95 typedef typename HashTableType::AddResult AddResult; 105 typedef typename HashTableType::AddResult AddResult;
96 106
97 void swap(HashMap& ref) { m_impl.swap(ref.m_impl); } 107 void swap(HashMap& ref) { m_impl.swap(ref.m_impl); }
98 108
99 unsigned size() const; 109 unsigned size() const;
100 unsigned capacity() const; 110 unsigned capacity() const;
(...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after
329 ValueTraits::ValueTraits::store(std::forward<V>(mapped), location.value); 339 ValueTraits::ValueTraits::store(std::forward<V>(mapped), location.value);
330 } 340 }
331 }; 341 };
332 342
333 template <typename T, 343 template <typename T,
334 typename U, 344 typename U,
335 typename V, 345 typename V,
336 typename W, 346 typename W,
337 typename X, 347 typename X,
338 typename Y> 348 typename Y>
349 HashMap<T, U, V, W, X, Y>::HashMap(std::initializer_list<ValueType> elements) {
350 if (elements.size())
351 m_impl.reserveCapacityForSize(elements.size());
352 for (const ValueType& element : elements)
353 add(element.key, element.value);
tzik 2017/01/18 13:36:40 Can we std::move() them?
Yuta Kitamura 2017/01/19 06:07:04 No, because std::initializer_list<T>::reference is
Yuta Kitamura 2017/01/19 06:08:08 typo: ValueType&& -> ValueType&.
354 }
355
356 template <typename T,
357 typename U,
358 typename V,
359 typename W,
360 typename X,
361 typename Y>
362 auto HashMap<T, U, V, W, X, Y>::operator=(
363 std::initializer_list<ValueType> elements) -> HashMap& {
364 *this = HashMap(std::move(elements));
365 return *this;
366 }
367
368 template <typename T,
369 typename U,
370 typename V,
371 typename W,
372 typename X,
373 typename Y>
339 inline unsigned HashMap<T, U, V, W, X, Y>::size() const { 374 inline unsigned HashMap<T, U, V, W, X, Y>::size() const {
340 return m_impl.size(); 375 return m_impl.size();
341 } 376 }
342 377
343 template <typename T, 378 template <typename T,
344 typename U, 379 typename U,
345 typename V, 380 typename V,
346 typename W, 381 typename W,
347 typename X, 382 typename X,
348 typename Y> 383 typename Y>
(...skipping 351 matching lines...) Expand 10 before | Expand all | Expand 10 after
700 iterator end = collection.end().values(); 735 iterator end = collection.end().values();
701 for (unsigned i = 0; it != end; ++it, ++i) 736 for (unsigned i = 0; it != end; ++it, ++i)
702 vector[i] = *it; 737 vector[i] = *it;
703 } 738 }
704 739
705 } // namespace WTF 740 } // namespace WTF
706 741
707 using WTF::HashMap; 742 using WTF::HashMap;
708 743
709 #endif // WTF_HashMap_h 744 #endif // WTF_HashMap_h
OLDNEW
« no previous file with comments | « no previous file | third_party/WebKit/Source/wtf/HashMapTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698