OLD | NEW |
1 /* | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
2 * Copyright (C) 2007 Apple Inc. All rights reserved. | 2 // Use of this source code is governed by a BSD-style license that can be |
3 * | 3 // found in the LICENSE file. |
4 * Redistribution and use in source and binary forms, with or without | |
5 * modification, are permitted provided that the following conditions | |
6 * are met: | |
7 * 1. Redistributions of source code must retain the above copyright | |
8 * notice, this list of conditions and the following disclaimer. | |
9 * 2. Redistributions in binary form must reproduce the above copyright | |
10 * notice, this list of conditions and the following disclaimer in the | |
11 * documentation and/or other materials provided with the distribution. | |
12 * | |
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY | |
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR | |
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | |
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
24 */ | |
25 | 4 |
26 #ifndef WTF_HashIterators_h | 5 #include "platform/wtf/HashIterators.h" |
27 #define WTF_HashIterators_h | |
28 | 6 |
29 #include "wtf/Allocator.h" | 7 // The contents of this header was moved to platform/wtf as part of |
30 | 8 // WTF migration project. See the following post for details: |
31 namespace WTF { | 9 // https://groups.google.com/a/chromium.org/d/msg/blink-dev/tLdAZCTlcAA/bYXVT8gY
CAAJ |
32 | |
33 template <typename HashTableType, typename KeyType, typename MappedType> | |
34 struct HashTableConstKeysIterator; | |
35 template <typename HashTableType, typename KeyType, typename MappedType> | |
36 struct HashTableConstValuesIterator; | |
37 template <typename HashTableType, typename KeyType, typename MappedType> | |
38 struct HashTableKeysIterator; | |
39 template <typename HashTableType, typename KeyType, typename MappedType> | |
40 struct HashTableValuesIterator; | |
41 | |
42 template <typename HashTableType, typename KeyType, typename MappedType> | |
43 struct HashTableConstIteratorAdapter<HashTableType, | |
44 KeyValuePair<KeyType, MappedType>> { | |
45 STACK_ALLOCATED(); | |
46 | |
47 private: | |
48 typedef KeyValuePair<KeyType, MappedType> ValueType; | |
49 | |
50 public: | |
51 typedef HashTableConstKeysIterator<HashTableType, KeyType, MappedType> | |
52 KeysIterator; | |
53 typedef HashTableConstValuesIterator<HashTableType, KeyType, MappedType> | |
54 ValuesIterator; | |
55 | |
56 HashTableConstIteratorAdapter() {} | |
57 HashTableConstIteratorAdapter( | |
58 const typename HashTableType::const_iterator& impl) | |
59 : m_impl(impl) {} | |
60 | |
61 const ValueType* get() const { return (const ValueType*)m_impl.get(); } | |
62 const ValueType& operator*() const { return *get(); } | |
63 const ValueType* operator->() const { return get(); } | |
64 | |
65 HashTableConstIteratorAdapter& operator++() { | |
66 ++m_impl; | |
67 return *this; | |
68 } | |
69 // postfix ++ intentionally omitted | |
70 | |
71 KeysIterator keys() { return KeysIterator(*this); } | |
72 ValuesIterator values() { return ValuesIterator(*this); } | |
73 | |
74 typename HashTableType::const_iterator m_impl; | |
75 }; | |
76 | |
77 template <typename HashTableType, typename KeyType, typename MappedType> | |
78 struct HashTableIteratorAdapter<HashTableType, | |
79 KeyValuePair<KeyType, MappedType>> { | |
80 STACK_ALLOCATED(); | |
81 | |
82 private: | |
83 typedef KeyValuePair<KeyType, MappedType> ValueType; | |
84 | |
85 public: | |
86 typedef HashTableKeysIterator<HashTableType, KeyType, MappedType> | |
87 KeysIterator; | |
88 typedef HashTableValuesIterator<HashTableType, KeyType, MappedType> | |
89 ValuesIterator; | |
90 | |
91 HashTableIteratorAdapter() {} | |
92 HashTableIteratorAdapter(const typename HashTableType::iterator& impl) | |
93 : m_impl(impl) {} | |
94 | |
95 ValueType* get() const { return (ValueType*)m_impl.get(); } | |
96 ValueType& operator*() const { return *get(); } | |
97 ValueType* operator->() const { return get(); } | |
98 | |
99 HashTableIteratorAdapter& operator++() { | |
100 ++m_impl; | |
101 return *this; | |
102 } | |
103 // postfix ++ intentionally omitted | |
104 | |
105 operator HashTableConstIteratorAdapter<HashTableType, ValueType>() { | |
106 typename HashTableType::const_iterator i = m_impl; | |
107 return i; | |
108 } | |
109 | |
110 KeysIterator keys() { return KeysIterator(*this); } | |
111 ValuesIterator values() { return ValuesIterator(*this); } | |
112 | |
113 typename HashTableType::iterator m_impl; | |
114 }; | |
115 | |
116 template <typename HashTableType, typename KeyType, typename MappedType> | |
117 struct HashTableConstKeysIterator { | |
118 STACK_ALLOCATED(); | |
119 | |
120 private: | |
121 typedef HashTableConstIteratorAdapter<HashTableType, | |
122 KeyValuePair<KeyType, MappedType>> | |
123 ConstIterator; | |
124 | |
125 public: | |
126 HashTableConstKeysIterator(const ConstIterator& impl) : m_impl(impl) {} | |
127 | |
128 const KeyType* get() const { return &(m_impl.get()->key); } | |
129 const KeyType& operator*() const { return *get(); } | |
130 const KeyType* operator->() const { return get(); } | |
131 | |
132 HashTableConstKeysIterator& operator++() { | |
133 ++m_impl; | |
134 return *this; | |
135 } | |
136 // postfix ++ intentionally omitted | |
137 | |
138 ConstIterator m_impl; | |
139 }; | |
140 | |
141 template <typename HashTableType, typename KeyType, typename MappedType> | |
142 struct HashTableConstValuesIterator { | |
143 STACK_ALLOCATED(); | |
144 | |
145 private: | |
146 typedef HashTableConstIteratorAdapter<HashTableType, | |
147 KeyValuePair<KeyType, MappedType>> | |
148 ConstIterator; | |
149 | |
150 public: | |
151 HashTableConstValuesIterator(const ConstIterator& impl) : m_impl(impl) {} | |
152 | |
153 const MappedType* get() const { return &(m_impl.get()->value); } | |
154 const MappedType& operator*() const { return *get(); } | |
155 const MappedType* operator->() const { return get(); } | |
156 | |
157 HashTableConstValuesIterator& operator++() { | |
158 ++m_impl; | |
159 return *this; | |
160 } | |
161 // postfix ++ intentionally omitted | |
162 | |
163 ConstIterator m_impl; | |
164 }; | |
165 | |
166 template <typename HashTableType, typename KeyType, typename MappedType> | |
167 struct HashTableKeysIterator { | |
168 STACK_ALLOCATED(); | |
169 | |
170 private: | |
171 typedef HashTableIteratorAdapter<HashTableType, | |
172 KeyValuePair<KeyType, MappedType>> | |
173 Iterator; | |
174 typedef HashTableConstIteratorAdapter<HashTableType, | |
175 KeyValuePair<KeyType, MappedType>> | |
176 ConstIterator; | |
177 | |
178 public: | |
179 HashTableKeysIterator(const Iterator& impl) : m_impl(impl) {} | |
180 | |
181 KeyType* get() const { return &(m_impl.get()->key); } | |
182 KeyType& operator*() const { return *get(); } | |
183 KeyType* operator->() const { return get(); } | |
184 | |
185 HashTableKeysIterator& operator++() { | |
186 ++m_impl; | |
187 return *this; | |
188 } | |
189 // postfix ++ intentionally omitted | |
190 | |
191 operator HashTableConstKeysIterator<HashTableType, KeyType, MappedType>() { | |
192 ConstIterator i = m_impl; | |
193 return i; | |
194 } | |
195 | |
196 Iterator m_impl; | |
197 }; | |
198 | |
199 template <typename HashTableType, typename KeyType, typename MappedType> | |
200 struct HashTableValuesIterator { | |
201 STACK_ALLOCATED(); | |
202 | |
203 private: | |
204 typedef HashTableIteratorAdapter<HashTableType, | |
205 KeyValuePair<KeyType, MappedType>> | |
206 Iterator; | |
207 typedef HashTableConstIteratorAdapter<HashTableType, | |
208 KeyValuePair<KeyType, MappedType>> | |
209 ConstIterator; | |
210 | |
211 public: | |
212 HashTableValuesIterator(const Iterator& impl) : m_impl(impl) {} | |
213 | |
214 MappedType* get() const { return &(m_impl.get()->value); } | |
215 MappedType& operator*() const { return *get(); } | |
216 MappedType* operator->() const { return get(); } | |
217 | |
218 HashTableValuesIterator& operator++() { | |
219 ++m_impl; | |
220 return *this; | |
221 } | |
222 // postfix ++ intentionally omitted | |
223 | |
224 operator HashTableConstValuesIterator<HashTableType, KeyType, MappedType>() { | |
225 ConstIterator i = m_impl; | |
226 return i; | |
227 } | |
228 | |
229 Iterator m_impl; | |
230 }; | |
231 | |
232 template <typename T, typename U, typename V> | |
233 inline bool operator==(const HashTableConstKeysIterator<T, U, V>& a, | |
234 const HashTableConstKeysIterator<T, U, V>& b) { | |
235 return a.m_impl == b.m_impl; | |
236 } | |
237 | |
238 template <typename T, typename U, typename V> | |
239 inline bool operator!=(const HashTableConstKeysIterator<T, U, V>& a, | |
240 const HashTableConstKeysIterator<T, U, V>& b) { | |
241 return a.m_impl != b.m_impl; | |
242 } | |
243 | |
244 template <typename T, typename U, typename V> | |
245 inline bool operator==(const HashTableConstValuesIterator<T, U, V>& a, | |
246 const HashTableConstValuesIterator<T, U, V>& b) { | |
247 return a.m_impl == b.m_impl; | |
248 } | |
249 | |
250 template <typename T, typename U, typename V> | |
251 inline bool operator!=(const HashTableConstValuesIterator<T, U, V>& a, | |
252 const HashTableConstValuesIterator<T, U, V>& b) { | |
253 return a.m_impl != b.m_impl; | |
254 } | |
255 | |
256 template <typename T, typename U, typename V> | |
257 inline bool operator==(const HashTableKeysIterator<T, U, V>& a, | |
258 const HashTableKeysIterator<T, U, V>& b) { | |
259 return a.m_impl == b.m_impl; | |
260 } | |
261 | |
262 template <typename T, typename U, typename V> | |
263 inline bool operator!=(const HashTableKeysIterator<T, U, V>& a, | |
264 const HashTableKeysIterator<T, U, V>& b) { | |
265 return a.m_impl != b.m_impl; | |
266 } | |
267 | |
268 template <typename T, typename U, typename V> | |
269 inline bool operator==(const HashTableValuesIterator<T, U, V>& a, | |
270 const HashTableValuesIterator<T, U, V>& b) { | |
271 return a.m_impl == b.m_impl; | |
272 } | |
273 | |
274 template <typename T, typename U, typename V> | |
275 inline bool operator!=(const HashTableValuesIterator<T, U, V>& a, | |
276 const HashTableValuesIterator<T, U, V>& b) { | |
277 return a.m_impl != b.m_impl; | |
278 } | |
279 | |
280 } // namespace WTF | |
281 | |
282 #endif // WTF_HashIterators_h | |
OLD | NEW |