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
, std::pair
<KeyType
, MappedType
> > {
38 typedef std::pair
<KeyType
, MappedType
> ValueType
;
40 typedef HashTableConstKeysIterator
<HashTableType
, KeyType
, MappedType
> Keys
;
41 typedef HashTableConstValuesIterator
<HashTableType
, KeyType
, MappedType
> Values
;
43 HashTableConstIteratorAdapter(const typename
HashTableType::const_iterator
& impl
) : m_impl(impl
) {}
45 const ValueType
* get() const { return (const ValueType
*)m_impl
.get(); }
46 const ValueType
& operator*() const { return *get(); }
47 const ValueType
* operator->() const { return get(); }
49 HashTableConstIteratorAdapter
& operator++() { ++m_impl
; return *this; }
50 // postfix ++ intentionally omitted
52 Keys
keys() { return Keys(*this); }
53 Values
values() { return Values(*this); }
55 typename
HashTableType::const_iterator m_impl
;
58 template<typename HashTableType
, typename KeyType
, typename MappedType
> struct HashTableIteratorAdapter
<HashTableType
, std::pair
<KeyType
, MappedType
> > {
60 typedef std::pair
<KeyType
, MappedType
> ValueType
;
62 typedef HashTableKeysIterator
<HashTableType
, KeyType
, MappedType
> Keys
;
63 typedef HashTableValuesIterator
<HashTableType
, KeyType
, MappedType
> Values
;
65 HashTableIteratorAdapter(const typename
HashTableType::iterator
& impl
) : m_impl(impl
) {}
67 ValueType
* get() const { return (ValueType
*)m_impl
.get(); }
68 ValueType
& operator*() const { return *get(); }
69 ValueType
* operator->() const { return get(); }
71 HashTableIteratorAdapter
& operator++() { ++m_impl
; return *this; }
72 // postfix ++ intentionally omitted
74 operator HashTableConstIteratorAdapter
<HashTableType
, ValueType
>() {
75 typename
HashTableType::const_iterator i
= m_impl
;
79 Keys
keys() { return Keys(*this); }
80 Values
values() { return Values(*this); }
82 typename
HashTableType::iterator m_impl
;
85 template<typename HashTableType
, typename KeyType
, typename MappedType
> struct HashTableConstKeysIterator
{
87 typedef HashTableConstIteratorAdapter
<HashTableType
, std::pair
<KeyType
, MappedType
> > ConstIterator
;
90 HashTableConstKeysIterator(const ConstIterator
& impl
) : m_impl(impl
) {}
92 const KeyType
* get() const { return &(m_impl
.get()->first
); }
93 const KeyType
& operator*() const { return *get(); }
94 const KeyType
* operator->() const { return get(); }
96 HashTableConstKeysIterator
& operator++() { ++m_impl
; return *this; }
97 // postfix ++ intentionally omitted
102 template<typename HashTableType
, typename KeyType
, typename MappedType
> struct HashTableConstValuesIterator
{
104 typedef HashTableConstIteratorAdapter
<HashTableType
, std::pair
<KeyType
, MappedType
> > ConstIterator
;
107 HashTableConstValuesIterator(const ConstIterator
& impl
) : m_impl(impl
) {}
109 const MappedType
* get() const { return &(m_impl
.get()->second
); }
110 const MappedType
& operator*() const { return *get(); }
111 const MappedType
* operator->() const { return get(); }
113 HashTableConstValuesIterator
& operator++() { ++m_impl
; return *this; }
114 // postfix ++ intentionally omitted
116 ConstIterator m_impl
;
119 template<typename HashTableType
, typename KeyType
, typename MappedType
> struct HashTableKeysIterator
{
121 typedef HashTableIteratorAdapter
<HashTableType
, std::pair
<KeyType
, MappedType
> > Iterator
;
122 typedef HashTableConstIteratorAdapter
<HashTableType
, std::pair
<KeyType
, MappedType
> > ConstIterator
;
125 HashTableKeysIterator(const Iterator
& impl
) : m_impl(impl
) {}
127 KeyType
* get() const { return &(m_impl
.get()->first
); }
128 KeyType
& operator*() const { return *get(); }
129 KeyType
* operator->() const { return get(); }
131 HashTableKeysIterator
& operator++() { ++m_impl
; return *this; }
132 // postfix ++ intentionally omitted
134 operator HashTableConstKeysIterator
<HashTableType
, KeyType
, MappedType
>() {
135 ConstIterator i
= m_impl
;
142 template<typename HashTableType
, typename KeyType
, typename MappedType
> struct HashTableValuesIterator
{
144 typedef HashTableIteratorAdapter
<HashTableType
, std::pair
<KeyType
, MappedType
> > Iterator
;
145 typedef HashTableConstIteratorAdapter
<HashTableType
, std::pair
<KeyType
, MappedType
> > ConstIterator
;
148 HashTableValuesIterator(const Iterator
& impl
) : m_impl(impl
) {}
150 MappedType
* get() const { return &(m_impl
.get()->second
); }
151 MappedType
& operator*() const { return *get(); }
152 MappedType
* operator->() const { return get(); }
154 HashTableValuesIterator
& operator++() { ++m_impl
; return *this; }
155 // postfix ++ intentionally omitted
157 operator HashTableConstValuesIterator
<HashTableType
, KeyType
, MappedType
>() {
158 ConstIterator i
= m_impl
;
165 template<typename T
, typename U
, typename V
>
166 inline bool operator==(const HashTableConstKeysIterator
<T
, U
, V
>& a
, const HashTableConstKeysIterator
<T
, U
, V
>& b
)
168 return a
.m_impl
== b
.m_impl
;
171 template<typename T
, typename U
, typename V
>
172 inline bool operator!=(const HashTableConstKeysIterator
<T
, U
, V
>& a
, const HashTableConstKeysIterator
<T
, U
, V
>& b
)
174 return a
.m_impl
!= b
.m_impl
;
177 template<typename T
, typename U
, typename V
>
178 inline bool operator==(const HashTableConstValuesIterator
<T
, U
, V
>& a
, const HashTableConstValuesIterator
<T
, U
, V
>& b
)
180 return a
.m_impl
== b
.m_impl
;
183 template<typename T
, typename U
, typename V
>
184 inline bool operator!=(const HashTableConstValuesIterator
<T
, U
, V
>& a
, const HashTableConstValuesIterator
<T
, U
, V
>& b
)
186 return a
.m_impl
!= b
.m_impl
;
189 template<typename T
, typename U
, typename V
>
190 inline bool operator==(const HashTableKeysIterator
<T
, U
, V
>& a
, const HashTableKeysIterator
<T
, U
, V
>& b
)
192 return a
.m_impl
== b
.m_impl
;
195 template<typename T
, typename U
, typename V
>
196 inline bool operator!=(const HashTableKeysIterator
<T
, U
, V
>& a
, const HashTableKeysIterator
<T
, U
, V
>& b
)
198 return a
.m_impl
!= b
.m_impl
;
201 template<typename T
, typename U
, typename V
>
202 inline bool operator==(const HashTableValuesIterator
<T
, U
, V
>& a
, const HashTableValuesIterator
<T
, U
, V
>& b
)
204 return a
.m_impl
== b
.m_impl
;
207 template<typename T
, typename U
, typename V
>
208 inline bool operator!=(const HashTableValuesIterator
<T
, U
, V
>& a
, const HashTableValuesIterator
<T
, U
, V
>& b
)
210 return a
.m_impl
!= b
.m_impl
;
216 #endif // WTF_HashIterators_h