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

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

Issue 1998543003: WTF: Add support for std::initializer_list in HashSet. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 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/HashSetTest.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_HashSet_h 21 #ifndef WTF_HashSet_h
22 #define WTF_HashSet_h 22 #define WTF_HashSet_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 struct IdentityExtractor; 30 struct IdentityExtractor;
30 31
31 // Note: empty or deleted values are not allowed, using them may lead to 32 // Note: empty or deleted values are not allowed, using them may lead to
32 // undefined behavior. For pointer valuess this means that null pointers are 33 // undefined behavior. For pointer valuess this means that null pointers are
33 // not allowed unless you supply custom traits. 34 // not allowed unless you supply custom traits.
34 template < 35 template <
35 typename ValueArg, 36 typename ValueArg,
(...skipping 12 matching lines...) Expand all
48 49
49 private: 50 private:
50 typedef HashTable<ValueType, ValueType, IdentityExtractor, 51 typedef HashTable<ValueType, ValueType, IdentityExtractor,
51 HashFunctions, ValueTraits, ValueTraits, Allocator> HashTableType; 52 HashFunctions, ValueTraits, ValueTraits, Allocator> HashTableType;
52 53
53 public: 54 public:
54 typedef HashTableConstIteratorAdapter<HashTableType, ValueTraits> iterator; 55 typedef HashTableConstIteratorAdapter<HashTableType, ValueTraits> iterator;
55 typedef HashTableConstIteratorAdapter<HashTableType, ValueTraits> const_iter ator; 56 typedef HashTableConstIteratorAdapter<HashTableType, ValueTraits> const_iter ator;
56 typedef typename HashTableType::AddResult AddResult; 57 typedef typename HashTableType::AddResult AddResult;
57 58
59 HashSet() = default;
60 HashSet(const HashSet&) = default;
61 HashSet& operator=(const HashSet&) = default;
62 HashSet(HashSet&&) = default;
63 HashSet& operator=(HashSet&&) = default;
64
65 HashSet(std::initializer_list<ValueType> elements);
66 HashSet& operator=(std::initializer_list<ValueType> elements);
67
58 void swap(HashSet& ref) 68 void swap(HashSet& ref)
59 { 69 {
60 m_impl.swap(ref.m_impl); 70 m_impl.swap(ref.m_impl);
61 } 71 }
62 72
63 unsigned size() const; 73 unsigned size() const;
64 unsigned capacity() const; 74 unsigned capacity() const;
65 bool isEmpty() const; 75 bool isEmpty() const;
66 76
67 void reserveCapacityForSize(unsigned size) 77 void reserveCapacityForSize(unsigned size)
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
124 struct HashSetTranslatorAdapter { 134 struct HashSetTranslatorAdapter {
125 STATIC_ONLY(HashSetTranslatorAdapter); 135 STATIC_ONLY(HashSetTranslatorAdapter);
126 template <typename T> static unsigned hash(const T& key) { return Translator ::hash(key); } 136 template <typename T> static unsigned hash(const T& key) { return Translator ::hash(key); }
127 template <typename T, typename U> static bool equal(const T& a, const U& b) { return Translator::equal(a, b); } 137 template <typename T, typename U> static bool equal(const T& a, const U& b) { return Translator::equal(a, b); }
128 template <typename T, typename U, typename V> static void translate(T& locat ion, U&& key, const V&, unsigned hashCode) 138 template <typename T, typename U, typename V> static void translate(T& locat ion, U&& key, const V&, unsigned hashCode)
129 { 139 {
130 Translator::translate(location, std::forward<U>(key), hashCode); 140 Translator::translate(location, std::forward<U>(key), hashCode);
131 } 141 }
132 }; 142 };
133 143
144 template <typename Value, typename HashFunctions, typename Traits, typename Allo cator>
145 HashSet<Value, HashFunctions, Traits, Allocator>::HashSet(std::initializer_list< ValueType> elements)
146 {
tzik 2016/05/19 08:59:57 Can we reserveCapacity here?
Yuta Kitamura 2016/05/19 09:04:21 Done.
147 for (const auto& element : elements)
148 add(element);
149 }
150
151 template <typename Value, typename HashFunctions, typename Traits, typename Allo cator>
152 auto HashSet<Value, HashFunctions, Traits, Allocator>::operator=(std::initialize r_list<ValueType> elements) -> HashSet&
153 {
154 clear();
155 for (const auto& element : elements)
156 add(element);
157 return *this;
158 }
159
134 template <typename T, typename U, typename V, typename W> 160 template <typename T, typename U, typename V, typename W>
135 inline unsigned HashSet<T, U, V, W>::size() const 161 inline unsigned HashSet<T, U, V, W>::size() const
136 { 162 {
137 return m_impl.size(); 163 return m_impl.size();
138 } 164 }
139 165
140 template <typename T, typename U, typename V, typename W> 166 template <typename T, typename U, typename V, typename W>
141 inline unsigned HashSet<T, U, V, W>::capacity() const 167 inline unsigned HashSet<T, U, V, W>::capacity() const
142 { 168 {
143 return m_impl.capacity(); 169 return m_impl.capacity();
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
261 iterator end = collection.end(); 287 iterator end = collection.end();
262 for (unsigned i = 0; it != end; ++it, ++i) 288 for (unsigned i = 0; it != end; ++it, ++i)
263 vector[i] = *it; 289 vector[i] = *it;
264 } 290 }
265 291
266 } // namespace WTF 292 } // namespace WTF
267 293
268 using WTF::HashSet; 294 using WTF::HashSet;
269 295
270 #endif // WTF_HashSet_h 296 #endif // WTF_HashSet_h
OLDNEW
« no previous file with comments | « no previous file | third_party/WebKit/Source/wtf/HashSetTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698