1/*
2 * Copyright (C) 2007 Apple Inc. All rights reserved.
3 *
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 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 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
26#ifndef WTF_HashIterators_h
27#define WTF_HashIterators_h
28
29#include <iterator>
30
31namespace WTF {
32
33 template<typename HashTableType, typename KeyType, typename MappedType> struct HashTableConstKeysIterator;
34 template<typename HashTableType, typename KeyType, typename MappedType> struct HashTableConstValuesIterator;
35 template<typename HashTableType, typename KeyType, typename MappedType> struct HashTableKeysIterator;
36 template<typename HashTableType, typename KeyType, typename MappedType> struct HashTableValuesIterator;
37
38 template<typename HashTableType, typename KeyType, typename MappedType> struct HashTableConstIteratorAdapter<HashTableType, KeyValuePair<KeyType, MappedType>> : public std::iterator<std::forward_iterator_tag, KeyValuePair<KeyType, MappedType>, std::ptrdiff_t, const KeyValuePair<KeyType, MappedType>*, const KeyValuePair<KeyType, MappedType>&> {
39 private:
40 typedef KeyValuePair<KeyType, MappedType> ValueType;
41 public:
42 typedef HashTableConstKeysIterator<HashTableType, KeyType, MappedType> Keys;
43 typedef HashTableConstValuesIterator<HashTableType, KeyType, MappedType> Values;
44
45 HashTableConstIteratorAdapter() {}
46 HashTableConstIteratorAdapter(const typename HashTableType::const_iterator& impl) : m_impl(impl) {}
47
48 const ValueType* get() const { return (const ValueType*)m_impl.get(); }
49 const ValueType& operator*() const { return *get(); }
50 const ValueType* operator->() const { return get(); }
51
52 HashTableConstIteratorAdapter& operator++() { ++m_impl; return *this; }
53 // postfix ++ intentionally omitted
54
55 Keys keys() { return Keys(*this); }
56 Values values() { return Values(*this); }
57
58 typename HashTableType::const_iterator m_impl;
59 };
60
61 template<typename HashTableType, typename KeyType, typename MappedType> struct HashTableIteratorAdapter<HashTableType, KeyValuePair<KeyType, MappedType>> : public std::iterator<std::forward_iterator_tag, KeyValuePair<KeyType, MappedType>, std::ptrdiff_t, KeyValuePair<KeyType, MappedType>*, KeyValuePair<KeyType, MappedType>&> {
62 private:
63 typedef KeyValuePair<KeyType, MappedType> ValueType;
64 public:
65 typedef HashTableKeysIterator<HashTableType, KeyType, MappedType> Keys;
66 typedef HashTableValuesIterator<HashTableType, KeyType, MappedType> Values;
67
68 HashTableIteratorAdapter() {}
69 HashTableIteratorAdapter(const typename HashTableType::iterator& impl) : m_impl(impl) {}
70
71 ValueType* get() const { return (ValueType*)m_impl.get(); }
72 ValueType& operator*() const { return *get(); }
73 ValueType* operator->() const { return get(); }
74
75 HashTableIteratorAdapter& operator++() { ++m_impl; return *this; }
76 // postfix ++ intentionally omitted
77
78 operator HashTableConstIteratorAdapter<HashTableType, ValueType>() {
79 typename HashTableType::const_iterator i = m_impl;
80 return i;
81 }
82
83 Keys keys() { return Keys(*this); }
84 Values values() { return Values(*this); }
85
86 typename HashTableType::iterator m_impl;
87 };
88
89 template<typename HashTableType, typename KeyType, typename MappedType> struct HashTableConstKeysIterator : public std::iterator<std::forward_iterator_tag, KeyType, std::ptrdiff_t, const KeyType*, const KeyType&> {
90 private:
91 typedef HashTableConstIteratorAdapter<HashTableType, KeyValuePair<KeyType, MappedType>> ConstIterator;
92
93 public:
94 HashTableConstKeysIterator(const ConstIterator& impl) : m_impl(impl) {}
95
96 const KeyType* get() const { return &(m_impl.get()->key); }
97 const KeyType& operator*() const { return *get(); }
98 const KeyType* operator->() const { return get(); }
99
100 HashTableConstKeysIterator& operator++() { ++m_impl; return *this; }
101 // postfix ++ intentionally omitted
102
103 ConstIterator m_impl;
104 };
105
106 template<typename HashTableType, typename KeyType, typename MappedType> struct HashTableConstValuesIterator : public std::iterator<std::forward_iterator_tag, MappedType, std::ptrdiff_t, const MappedType*, const MappedType&> {
107 private:
108 typedef HashTableConstIteratorAdapter<HashTableType, KeyValuePair<KeyType, MappedType>> ConstIterator;
109
110 public:
111 HashTableConstValuesIterator(const ConstIterator& impl) : m_impl(impl) {}
112
113 const MappedType* get() const { return &(m_impl.get()->value); }
114 const MappedType& operator*() const { return *get(); }
115 const MappedType* operator->() const { return get(); }
116
117 HashTableConstValuesIterator& operator++() { ++m_impl; return *this; }
118 // postfix ++ intentionally omitted
119
120 ConstIterator m_impl;
121 };
122
123 template<typename HashTableType, typename KeyType, typename MappedType> struct HashTableKeysIterator : public std::iterator<std::forward_iterator_tag, KeyType, std::ptrdiff_t, KeyType*, KeyType&> {
124 private:
125 typedef HashTableIteratorAdapter<HashTableType, KeyValuePair<KeyType, MappedType>> Iterator;
126 typedef HashTableConstIteratorAdapter<HashTableType, KeyValuePair<KeyType, MappedType>> ConstIterator;
127
128 public:
129 HashTableKeysIterator(const Iterator& impl) : m_impl(impl) {}
130
131 KeyType* get() const { return &(m_impl.get()->key); }
132 KeyType& operator*() const { return *get(); }
133 KeyType* operator->() const { return get(); }
134
135 HashTableKeysIterator& operator++() { ++m_impl; return *this; }
136 // postfix ++ intentionally omitted
137
138 operator HashTableConstKeysIterator<HashTableType, KeyType, MappedType>() {
139 ConstIterator i = m_impl;
140 return i;
141 }
142
143 Iterator m_impl;
144 };
145
146 template<typename HashTableType, typename KeyType, typename MappedType> struct HashTableValuesIterator : public std::iterator<std::forward_iterator_tag, MappedType, std::ptrdiff_t, MappedType*, MappedType&> {
147 private:
148 typedef HashTableIteratorAdapter<HashTableType, KeyValuePair<KeyType, MappedType>> Iterator;
149 typedef HashTableConstIteratorAdapter<HashTableType, KeyValuePair<KeyType, MappedType>> ConstIterator;
150
151 public:
152 HashTableValuesIterator(const Iterator& impl) : m_impl(impl) {}
153
154 MappedType* get() const { return &(m_impl.get()->value); }
155 MappedType& operator*() const { return *get(); }
156 MappedType* operator->() const { return get(); }
157
158 HashTableValuesIterator& operator++() { ++m_impl; return *this; }
159 // postfix ++ intentionally omitted
160
161 operator HashTableConstValuesIterator<HashTableType, KeyType, MappedType>() {
162 ConstIterator i = m_impl;
163 return i;
164 }
165
166 Iterator m_impl;
167 };
168
169 template<typename T, typename U, typename V>
170 inline bool operator==(const HashTableConstKeysIterator<T, U, V>& a, const HashTableConstKeysIterator<T, U, V>& b)
171 {
172 return a.m_impl == b.m_impl;
173 }
174
175 template<typename T, typename U, typename V>
176 inline bool operator!=(const HashTableConstKeysIterator<T, U, V>& a, const HashTableConstKeysIterator<T, U, V>& b)
177 {
178 return a.m_impl != b.m_impl;
179 }
180
181 template<typename T, typename U, typename V>
182 inline bool operator==(const HashTableConstValuesIterator<T, U, V>& a, const HashTableConstValuesIterator<T, U, V>& b)
183 {
184 return a.m_impl == b.m_impl;
185 }
186
187 template<typename T, typename U, typename V>
188 inline bool operator!=(const HashTableConstValuesIterator<T, U, V>& a, const HashTableConstValuesIterator<T, U, V>& b)
189 {
190 return a.m_impl != b.m_impl;
191 }
192
193 template<typename T, typename U, typename V>
194 inline bool operator==(const HashTableKeysIterator<T, U, V>& a, const HashTableKeysIterator<T, U, V>& b)
195 {
196 return a.m_impl == b.m_impl;
197 }
198
199 template<typename T, typename U, typename V>
200 inline bool operator!=(const HashTableKeysIterator<T, U, V>& a, const HashTableKeysIterator<T, U, V>& b)
201 {
202 return a.m_impl != b.m_impl;
203 }
204
205 template<typename T, typename U, typename V>
206 inline bool operator==(const HashTableValuesIterator<T, U, V>& a, const HashTableValuesIterator<T, U, V>& b)
207 {
208 return a.m_impl == b.m_impl;
209 }
210
211 template<typename T, typename U, typename V>
212 inline bool operator!=(const HashTableValuesIterator<T, U, V>& a, const HashTableValuesIterator<T, U, V>& b)
213 {
214 return a.m_impl != b.m_impl;
215 }
216
217
218} // namespace WTF
219
220#endif // WTF_HashIterators_h
221