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

Unified 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | third_party/WebKit/Source/wtf/HashMapTest.cpp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: third_party/WebKit/Source/wtf/HashMap.h
diff --git a/third_party/WebKit/Source/wtf/HashMap.h b/third_party/WebKit/Source/wtf/HashMap.h
index 3ac16620a3a1ed34715764bf689032fc8d29d46e..f81c66f413dd29a0046b005191a1853d58a188ea 100644
--- a/third_party/WebKit/Source/wtf/HashMap.h
+++ b/third_party/WebKit/Source/wtf/HashMap.h
@@ -23,6 +23,7 @@
#include "wtf/HashTable.h"
#include "wtf/allocator/PartitionAllocator.h"
+#include <initializer_list>
namespace WTF {
@@ -88,6 +89,15 @@ class HashMap {
"Cannot put raw pointers to garbage-collected classes into "
"an off-heap HashMap. Use HeapHashMap<> instead.");
}
+ HashMap(const HashMap&) = default;
+ HashMap& operator=(const HashMap&) = default;
+ HashMap(HashMap&&) = default;
+ HashMap& operator=(HashMap&&) = default;
+
+ // For example, HashMap<int, int>({{1, 11}, {2, 22}, {3, 33}}) will give you
+ // a HashMap containing a mapping {1 -> 11, 2 -> 22, 3 -> 33}.
+ HashMap(std::initializer_list<ValueType> elements);
+ HashMap& operator=(std::initializer_list<ValueType> elements);
typedef HashTableIteratorAdapter<HashTableType, ValueType> iterator;
typedef HashTableConstIteratorAdapter<HashTableType, ValueType>
@@ -336,6 +346,31 @@ template <typename T,
typename W,
typename X,
typename Y>
+HashMap<T, U, V, W, X, Y>::HashMap(std::initializer_list<ValueType> elements) {
+ if (elements.size())
+ m_impl.reserveCapacityForSize(elements.size());
+ for (const ValueType& element : elements)
+ 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&.
+}
+
+template <typename T,
+ typename U,
+ typename V,
+ typename W,
+ typename X,
+ typename Y>
+auto HashMap<T, U, V, W, X, Y>::operator=(
+ std::initializer_list<ValueType> elements) -> HashMap& {
+ *this = HashMap(std::move(elements));
+ return *this;
+}
+
+template <typename T,
+ typename U,
+ typename V,
+ typename W,
+ typename X,
+ typename Y>
inline unsigned HashMap<T, U, V, W, X, Y>::size() const {
return m_impl.size();
}
« 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