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