1 // -*- mode: c++; c-basic-offset: 4 -*-
3 * Copyright (C) 2007 Apple Inc. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #ifndef WTF_HashIterators_h
28 #define WTF_HashIterators_h
32 template<typename HashTableType
, typename KeyType
, typename MappedType
> struct HashTableConstKeysIterator
;
33 template<typename HashTableType
, typename KeyType
, typename MappedType
> struct HashTableConstValuesIterator
;
34 template<typename HashTableType
, typename KeyType
, typename MappedType
> struct HashTableKeysIterator
;
35 template<typename HashTableType
, typename KeyType
, typename MappedType
> struct HashTableValuesIterator
;
37 template<typename HashTableType
, typename KeyType
, typename MappedType
> struct HashTableConstIteratorAdapter
<HashTableType
, std::pair
<KeyType
, MappedType
> > {
39 typedef std::pair
<KeyType
, MappedType
> ValueType
;
41 typedef HashTableConstKeysIterator
<HashTableType
, KeyType
, MappedType
> Keys
;
42 typedef HashTableConstValuesIterator
<HashTableType
, KeyType
, MappedType
> Values
;
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
, std::pair
<KeyType
, MappedType
> > {
61 typedef std::pair
<KeyType
, MappedType
> ValueType
;
63 typedef HashTableKeysIterator
<HashTableType
, KeyType
, MappedType
> Keys
;
64 typedef HashTableValuesIterator
<HashTableType
, KeyType
, MappedType
> Values
;
66 HashTableIteratorAdapter(const typename
HashTableType::iterator
& impl
) : m_impl(impl
) {}
68 ValueType
* get() const { return (ValueType
*)m_impl
.get(); }
69 ValueType
& operator*() const { return *get(); }
70 ValueType
* operator->() const { return get(); }
72 HashTableIteratorAdapter
& operator++() { ++m_impl
; return *this; }
73 // postfix ++ intentionally omitted
75 operator HashTableConstIteratorAdapter
<HashTableType
, ValueType
>() {
76 typename
HashTableType::const_iterator i
= m_impl
;
80 Keys
keys() { return Keys(*this); }
81 Values
values() { return Values(*this); }
83 typename
HashTableType::iterator m_impl
;
86 template<typename HashTableType
, typename KeyType
, typename MappedType
> struct HashTableConstKeysIterator
{
88 typedef HashTableConstIteratorAdapter
<HashTableType
, std::pair
<KeyType
, MappedType
> > ConstIterator
;
91 HashTableConstKeysIterator(const ConstIterator
& impl
) : m_impl(impl
) {}
93 const KeyType
* get() const { return &(m_impl
.get()->first
); }
94 const KeyType
& operator*() const { return *get(); }
95 const KeyType
* operator->() const { return get(); }
97 HashTableConstKeysIterator
& operator++() { ++m_impl
; return *this; }
98 // postfix ++ intentionally omitted
100 ConstIterator m_impl
;
103 template<typename HashTableType
, typename KeyType
, typename MappedType
> struct HashTableConstValuesIterator
{
105 typedef HashTableConstIteratorAdapter
<HashTableType
, std::pair
<KeyType
, MappedType
> > ConstIterator
;
108 HashTableConstValuesIterator(const ConstIterator
& impl
) : m_impl(impl
) {}
110 const MappedType
* get() const { return &(m_impl
.get()->second
); }
111 const MappedType
& operator*() const { return *get(); }
112 const MappedType
* operator->() const { return get(); }
114 HashTableConstValuesIterator
& operator++() { ++m_impl
; return *this; }
115 // postfix ++ intentionally omitted
117 ConstIterator m_impl
;
120 template<typename HashTableType
, typename KeyType
, typename MappedType
> struct HashTableKeysIterator
{
122 typedef HashTableIteratorAdapter
<HashTableType
, std::pair
<KeyType
, MappedType
> > Iterator
;
123 typedef HashTableConstIteratorAdapter
<HashTableType
, std::pair
<KeyType
, MappedType
> > ConstIterator
;
126 HashTableKeysIterator(const Iterator
& impl
) : m_impl(impl
) {}
128 KeyType
* get() const { return &(m_impl
.get()->first
); }
129 KeyType
& operator*() const { return *get(); }
130 KeyType
* operator->() const { return get(); }
132 HashTableKeysIterator
& operator++() { ++m_impl
; return *this; }
133 // postfix ++ intentionally omitted
135 operator HashTableConstKeysIterator
<HashTableType
, KeyType
, MappedType
>() {
136 ConstIterator i
= m_impl
;
143 template<typename HashTableType
, typename KeyType
, typename MappedType
> struct HashTableValuesIterator
{
145 typedef HashTableIteratorAdapter
<HashTableType
, std::pair
<KeyType
, MappedType
> > Iterator
;
146 typedef HashTableConstIteratorAdapter
<HashTableType
, std::pair
<KeyType
, MappedType
> > ConstIterator
;
149 HashTableValuesIterator(const Iterator
& impl
) : m_impl(impl
) {}
151 MappedType
* get() const { return &(m_impl
.get()->second
); }
152 MappedType
& operator*() const { return *get(); }
153 MappedType
* operator->() const { return get(); }
155 HashTableValuesIterator
& operator++() { ++m_impl
; return *this; }
156 // postfix ++ intentionally omitted
158 operator HashTableConstValuesIterator
<HashTableType
, KeyType
, MappedType
>() {
159 ConstIterator i
= m_impl
;
166 template<typename T
, typename U
, typename V
>
167 inline bool operator==(const HashTableConstKeysIterator
<T
, U
, V
>& a
, const HashTableConstKeysIterator
<T
, U
, V
>& b
)
169 return a
.m_impl
== b
.m_impl
;
172 template<typename T
, typename U
, typename V
>
173 inline bool operator!=(const HashTableConstKeysIterator
<T
, U
, V
>& a
, const HashTableConstKeysIterator
<T
, U
, V
>& b
)
175 return a
.m_impl
!= b
.m_impl
;
178 template<typename T
, typename U
, typename V
>
179 inline bool operator==(const HashTableConstValuesIterator
<T
, U
, V
>& a
, const HashTableConstValuesIterator
<T
, U
, V
>& b
)
181 return a
.m_impl
== b
.m_impl
;
184 template<typename T
, typename U
, typename V
>
185 inline bool operator!=(const HashTableConstValuesIterator
<T
, U
, V
>& a
, const HashTableConstValuesIterator
<T
, U
, V
>& b
)
187 return a
.m_impl
!= b
.m_impl
;
190 template<typename T
, typename U
, typename V
>
191 inline bool operator==(const HashTableKeysIterator
<T
, U
, V
>& a
, const HashTableKeysIterator
<T
, U
, V
>& b
)
193 return a
.m_impl
== b
.m_impl
;
196 template<typename T
, typename U
, typename V
>
197 inline bool operator!=(const HashTableKeysIterator
<T
, U
, V
>& a
, const HashTableKeysIterator
<T
, U
, V
>& b
)
199 return a
.m_impl
!= b
.m_impl
;
202 template<typename T
, typename U
, typename V
>
203 inline bool operator==(const HashTableValuesIterator
<T
, U
, V
>& a
, const HashTableValuesIterator
<T
, U
, V
>& b
)
205 return a
.m_impl
== b
.m_impl
;
208 template<typename T
, typename U
, typename V
>
209 inline bool operator!=(const HashTableValuesIterator
<T
, U
, V
>& a
, const HashTableValuesIterator
<T
, U
, V
>& b
)
211 return a
.m_impl
!= b
.m_impl
;
217 #endif // WTF_HashIterators_h