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