2 * Copyright (C) 2007 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
26 #ifndef WTF_HashIterators_h
27 #define WTF_HashIterators_h
31 template<typename HashTableType
, typename KeyType
, typename MappedType
> struct HashTableConstKeysIterator
;
32 template<typename HashTableType
, typename KeyType
, typename MappedType
> struct HashTableConstValuesIterator
;
33 template<typename HashTableType
, typename KeyType
, typename MappedType
> struct HashTableKeysIterator
;
34 template<typename HashTableType
, typename KeyType
, typename MappedType
> struct HashTableValuesIterator
;
36 template<typename HashTableType
, typename KeyType
, typename MappedType
> struct HashTableConstIteratorAdapter
<HashTableType
, KeyValuePair
<KeyType
, MappedType
>> {
38 typedef KeyValuePair
<KeyType
, MappedType
> ValueType
;
40 typedef HashTableConstKeysIterator
<HashTableType
, KeyType
, MappedType
> Keys
;
41 typedef HashTableConstValuesIterator
<HashTableType
, KeyType
, MappedType
> Values
;
43 HashTableConstIteratorAdapter() {}
44 HashTableConstIteratorAdapter(const typename
HashTableType::const_iterator
& impl
) : m_impl(impl
) {}
46 const ValueType
* get() const { return (const ValueType
*)m_impl
.get(); }
47 const ValueType
& operator*() const { return *get(); }
48 const ValueType
* operator->() const { return get(); }
50 HashTableConstIteratorAdapter
& operator++() { ++m_impl
; return *this; }
51 // postfix ++ intentionally omitted
53 Keys
keys() { return Keys(*this); }
54 Values
values() { return Values(*this); }
56 typename
HashTableType::const_iterator m_impl
;
59 template<typename HashTableType
, typename KeyType
, typename MappedType
> struct HashTableIteratorAdapter
<HashTableType
, KeyValuePair
<KeyType
, MappedType
>> {
61 typedef KeyValuePair
<KeyType
, MappedType
> ValueType
;
63 typedef HashTableKeysIterator
<HashTableType
, KeyType
, MappedType
> Keys
;
64 typedef HashTableValuesIterator
<HashTableType
, KeyType
, MappedType
> Values
;
66 HashTableIteratorAdapter() {}
67 HashTableIteratorAdapter(const typename
HashTableType::iterator
& impl
) : m_impl(impl
) {}
69 ValueType
* get() const { return (ValueType
*)m_impl
.get(); }
70 ValueType
& operator*() const { return *get(); }
71 ValueType
* operator->() const { return get(); }
73 HashTableIteratorAdapter
& operator++() { ++m_impl
; return *this; }
74 // postfix ++ intentionally omitted
76 operator HashTableConstIteratorAdapter
<HashTableType
, ValueType
>() {
77 typename
HashTableType::const_iterator i
= m_impl
;
81 Keys
keys() { return Keys(*this); }
82 Values
values() { return Values(*this); }
84 typename
HashTableType::iterator m_impl
;
87 template<typename HashTableType
, typename KeyType
, typename MappedType
> struct HashTableConstKeysIterator
{
89 typedef HashTableConstIteratorAdapter
<HashTableType
, KeyValuePair
<KeyType
, MappedType
>> ConstIterator
;
92 HashTableConstKeysIterator(const ConstIterator
& impl
) : m_impl(impl
) {}
94 const KeyType
* get() const { return &(m_impl
.get()->key
); }
95 const KeyType
& operator*() const { return *get(); }
96 const KeyType
* operator->() const { return get(); }
98 HashTableConstKeysIterator
& operator++() { ++m_impl
; return *this; }
99 // postfix ++ intentionally omitted
101 ConstIterator m_impl
;
104 template<typename HashTableType
, typename KeyType
, typename MappedType
> struct HashTableConstValuesIterator
{
106 typedef HashTableConstIteratorAdapter
<HashTableType
, KeyValuePair
<KeyType
, MappedType
>> ConstIterator
;
109 HashTableConstValuesIterator(const ConstIterator
& impl
) : m_impl(impl
) {}
111 const MappedType
* get() const { return &(m_impl
.get()->value
); }
112 const MappedType
& operator*() const { return *get(); }
113 const MappedType
* operator->() const { return get(); }
115 HashTableConstValuesIterator
& operator++() { ++m_impl
; return *this; }
116 // postfix ++ intentionally omitted
118 ConstIterator m_impl
;
121 template<typename HashTableType
, typename KeyType
, typename MappedType
> struct HashTableKeysIterator
{
123 typedef HashTableIteratorAdapter
<HashTableType
, KeyValuePair
<KeyType
, MappedType
>> Iterator
;
124 typedef HashTableConstIteratorAdapter
<HashTableType
, KeyValuePair
<KeyType
, MappedType
>> ConstIterator
;
127 HashTableKeysIterator(const Iterator
& impl
) : m_impl(impl
) {}
129 KeyType
* get() const { return &(m_impl
.get()->key
); }
130 KeyType
& operator*() const { return *get(); }
131 KeyType
* operator->() const { return get(); }
133 HashTableKeysIterator
& operator++() { ++m_impl
; return *this; }
134 // postfix ++ intentionally omitted
136 operator HashTableConstKeysIterator
<HashTableType
, KeyType
, MappedType
>() {
137 ConstIterator i
= m_impl
;
144 template<typename HashTableType
, typename KeyType
, typename MappedType
> struct HashTableValuesIterator
{
146 typedef HashTableIteratorAdapter
<HashTableType
, KeyValuePair
<KeyType
, MappedType
>> Iterator
;
147 typedef HashTableConstIteratorAdapter
<HashTableType
, KeyValuePair
<KeyType
, MappedType
>> ConstIterator
;
150 HashTableValuesIterator(const Iterator
& impl
) : m_impl(impl
) {}
152 MappedType
* get() const { return &(m_impl
.get()->value
); }
153 MappedType
& operator*() const { return *get(); }
154 MappedType
* operator->() const { return get(); }
156 HashTableValuesIterator
& operator++() { ++m_impl
; return *this; }
157 // postfix ++ intentionally omitted
159 operator HashTableConstValuesIterator
<HashTableType
, KeyType
, MappedType
>() {
160 ConstIterator i
= m_impl
;
167 template<typename T
, typename U
, typename V
>
168 inline bool operator==(const HashTableConstKeysIterator
<T
, U
, V
>& a
, const HashTableConstKeysIterator
<T
, U
, V
>& b
)
170 return a
.m_impl
== b
.m_impl
;
173 template<typename T
, typename U
, typename V
>
174 inline bool operator!=(const HashTableConstKeysIterator
<T
, U
, V
>& a
, const HashTableConstKeysIterator
<T
, U
, V
>& b
)
176 return a
.m_impl
!= b
.m_impl
;
179 template<typename T
, typename U
, typename V
>
180 inline bool operator==(const HashTableConstValuesIterator
<T
, U
, V
>& a
, const HashTableConstValuesIterator
<T
, U
, V
>& b
)
182 return a
.m_impl
== b
.m_impl
;
185 template<typename T
, typename U
, typename V
>
186 inline bool operator!=(const HashTableConstValuesIterator
<T
, U
, V
>& a
, const HashTableConstValuesIterator
<T
, U
, V
>& b
)
188 return a
.m_impl
!= b
.m_impl
;
191 template<typename T
, typename U
, typename V
>
192 inline bool operator==(const HashTableKeysIterator
<T
, U
, V
>& a
, const HashTableKeysIterator
<T
, U
, V
>& b
)
194 return a
.m_impl
== b
.m_impl
;
197 template<typename T
, typename U
, typename V
>
198 inline bool operator!=(const HashTableKeysIterator
<T
, U
, V
>& a
, const HashTableKeysIterator
<T
, U
, V
>& b
)
200 return a
.m_impl
!= b
.m_impl
;
203 template<typename T
, typename U
, typename V
>
204 inline bool operator==(const HashTableValuesIterator
<T
, U
, V
>& a
, const HashTableValuesIterator
<T
, U
, V
>& b
)
206 return a
.m_impl
== b
.m_impl
;
209 template<typename T
, typename U
, typename V
>
210 inline bool operator!=(const HashTableValuesIterator
<T
, U
, V
>& a
, const HashTableValuesIterator
<T
, U
, V
>& b
)
212 return a
.m_impl
!= b
.m_impl
;
218 #endif // WTF_HashIterators_h