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