File indexing completed on 2024-05-12 15:43:38

0001 /*
0002  * Copyright (C) 2007 Apple Inc.  All rights reserved.
0003  *
0004  * Redistribution and use in source and binary forms, with or without
0005  * modification, are permitted provided that the following conditions
0006  * are met:
0007  * 1. Redistributions of source code must retain the above copyright
0008  *    notice, this list of conditions and the following disclaimer.
0009  * 2. Redistributions in binary form must reproduce the above copyright
0010  *    notice, this list of conditions and the following disclaimer in the
0011  *    documentation and/or other materials provided with the distribution.
0012  *
0013  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
0014  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0015  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
0016  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
0017  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
0018  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
0019  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
0020  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
0021  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0022  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
0023  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0024  */
0025 
0026 #ifndef WTF_HashIterators_h
0027 #define WTF_HashIterators_h
0028 
0029 namespace WTF
0030 {
0031 
0032 template<typename HashTableType, typename KeyType, typename MappedType> struct HashTableConstKeysIterator;
0033 template<typename HashTableType, typename KeyType, typename MappedType> struct HashTableConstValuesIterator;
0034 template<typename HashTableType, typename KeyType, typename MappedType> struct HashTableKeysIterator;
0035 template<typename HashTableType, typename KeyType, typename MappedType> struct HashTableValuesIterator;
0036 
0037 template<typename HashTableType, typename KeyType, typename MappedType> struct HashTableConstIteratorAdapter<HashTableType, std::pair<KeyType, MappedType> > {
0038 private:
0039     typedef std::pair<KeyType, MappedType> ValueType;
0040 public:
0041     typedef HashTableConstKeysIterator<HashTableType, KeyType, MappedType> Keys;
0042     typedef HashTableConstValuesIterator<HashTableType, KeyType, MappedType> Values;
0043 
0044     HashTableConstIteratorAdapter(const typename HashTableType::const_iterator &impl) : m_impl(impl) {}
0045 
0046     const ValueType *get() const
0047     {
0048         return (const ValueType *)m_impl.get();
0049     }
0050     const ValueType &operator*() const
0051     {
0052         return *get();
0053     }
0054     const ValueType *operator->() const
0055     {
0056         return get();
0057     }
0058 
0059     HashTableConstIteratorAdapter &operator++()
0060     {
0061         ++m_impl;
0062         return *this;
0063     }
0064     // postfix ++ intentionally omitted
0065 
0066     Keys keys()
0067     {
0068         return Keys(*this);
0069     }
0070     Values values()
0071     {
0072         return Values(*this);
0073     }
0074 
0075     typename HashTableType::const_iterator m_impl;
0076 };
0077 
0078 template<typename HashTableType, typename KeyType, typename MappedType> struct HashTableIteratorAdapter<HashTableType, std::pair<KeyType, MappedType> > {
0079 private:
0080     typedef std::pair<KeyType, MappedType> ValueType;
0081 public:
0082     typedef HashTableKeysIterator<HashTableType, KeyType, MappedType> Keys;
0083     typedef HashTableValuesIterator<HashTableType, KeyType, MappedType> Values;
0084 
0085     HashTableIteratorAdapter(const typename HashTableType::iterator &impl) : m_impl(impl) {}
0086 
0087     ValueType *get() const
0088     {
0089         return (ValueType *)m_impl.get();
0090     }
0091     ValueType &operator*() const
0092     {
0093         return *get();
0094     }
0095     ValueType *operator->() const
0096     {
0097         return get();
0098     }
0099 
0100     HashTableIteratorAdapter &operator++()
0101     {
0102         ++m_impl;
0103         return *this;
0104     }
0105     // postfix ++ intentionally omitted
0106 
0107     operator HashTableConstIteratorAdapter<HashTableType, ValueType>()
0108     {
0109         typename HashTableType::const_iterator i = m_impl;
0110         return i;
0111     }
0112 
0113     Keys keys()
0114     {
0115         return Keys(*this);
0116     }
0117     Values values()
0118     {
0119         return Values(*this);
0120     }
0121 
0122     typename HashTableType::iterator m_impl;
0123 };
0124 
0125 template<typename HashTableType, typename KeyType, typename MappedType> struct HashTableConstKeysIterator {
0126 private:
0127     typedef HashTableConstIteratorAdapter<HashTableType, std::pair<KeyType, MappedType> > ConstIterator;
0128 
0129 public:
0130     HashTableConstKeysIterator(const ConstIterator &impl) : m_impl(impl) {}
0131 
0132     const KeyType *get() const
0133     {
0134         return &(m_impl.get()->first);
0135     }
0136     const KeyType &operator*() const
0137     {
0138         return *get();
0139     }
0140     const KeyType *operator->() const
0141     {
0142         return get();
0143     }
0144 
0145     HashTableConstKeysIterator &operator++()
0146     {
0147         ++m_impl;
0148         return *this;
0149     }
0150     // postfix ++ intentionally omitted
0151 
0152     ConstIterator m_impl;
0153 };
0154 
0155 template<typename HashTableType, typename KeyType, typename MappedType> struct HashTableConstValuesIterator {
0156 private:
0157     typedef HashTableConstIteratorAdapter<HashTableType, std::pair<KeyType, MappedType> > ConstIterator;
0158 
0159 public:
0160     HashTableConstValuesIterator(const ConstIterator &impl) : m_impl(impl) {}
0161 
0162     const MappedType *get() const
0163     {
0164         return &(m_impl.get()->second);
0165     }
0166     const MappedType &operator*() const
0167     {
0168         return *get();
0169     }
0170     const MappedType *operator->() const
0171     {
0172         return get();
0173     }
0174 
0175     HashTableConstValuesIterator &operator++()
0176     {
0177         ++m_impl;
0178         return *this;
0179     }
0180     // postfix ++ intentionally omitted
0181 
0182     ConstIterator m_impl;
0183 };
0184 
0185 template<typename HashTableType, typename KeyType, typename MappedType> struct HashTableKeysIterator {
0186 private:
0187     typedef HashTableIteratorAdapter<HashTableType, std::pair<KeyType, MappedType> > Iterator;
0188     typedef HashTableConstIteratorAdapter<HashTableType, std::pair<KeyType, MappedType> > ConstIterator;
0189 
0190 public:
0191     HashTableKeysIterator(const Iterator &impl) : m_impl(impl) {}
0192 
0193     KeyType *get() const
0194     {
0195         return &(m_impl.get()->first);
0196     }
0197     KeyType &operator*() const
0198     {
0199         return *get();
0200     }
0201     KeyType *operator->() const
0202     {
0203         return get();
0204     }
0205 
0206     HashTableKeysIterator &operator++()
0207     {
0208         ++m_impl;
0209         return *this;
0210     }
0211     // postfix ++ intentionally omitted
0212 
0213     operator HashTableConstKeysIterator<HashTableType, KeyType, MappedType>()
0214     {
0215         ConstIterator i = m_impl;
0216         return i;
0217     }
0218 
0219     Iterator m_impl;
0220 };
0221 
0222 template<typename HashTableType, typename KeyType, typename MappedType> struct HashTableValuesIterator {
0223 private:
0224     typedef HashTableIteratorAdapter<HashTableType, std::pair<KeyType, MappedType> > Iterator;
0225     typedef HashTableConstIteratorAdapter<HashTableType, std::pair<KeyType, MappedType> > ConstIterator;
0226 
0227 public:
0228     HashTableValuesIterator(const Iterator &impl) : m_impl(impl) {}
0229 
0230     MappedType *get() const
0231     {
0232         return &(m_impl.get()->second);
0233     }
0234     MappedType &operator*() const
0235     {
0236         return *get();
0237     }
0238     MappedType *operator->() const
0239     {
0240         return get();
0241     }
0242 
0243     HashTableValuesIterator &operator++()
0244     {
0245         ++m_impl;
0246         return *this;
0247     }
0248     // postfix ++ intentionally omitted
0249 
0250     operator HashTableConstValuesIterator<HashTableType, KeyType, MappedType>()
0251     {
0252         ConstIterator i = m_impl;
0253         return i;
0254     }
0255 
0256     Iterator m_impl;
0257 };
0258 
0259 template<typename T, typename U, typename V>
0260 inline bool operator==(const HashTableConstKeysIterator<T, U, V> &a, const HashTableConstKeysIterator<T, U, V> &b)
0261 {
0262     return a.m_impl == b.m_impl;
0263 }
0264 
0265 template<typename T, typename U, typename V>
0266 inline bool operator!=(const HashTableConstKeysIterator<T, U, V> &a, const HashTableConstKeysIterator<T, U, V> &b)
0267 {
0268     return a.m_impl != b.m_impl;
0269 }
0270 
0271 template<typename T, typename U, typename V>
0272 inline bool operator==(const HashTableConstValuesIterator<T, U, V> &a, const HashTableConstValuesIterator<T, U, V> &b)
0273 {
0274     return a.m_impl == b.m_impl;
0275 }
0276 
0277 template<typename T, typename U, typename V>
0278 inline bool operator!=(const HashTableConstValuesIterator<T, U, V> &a, const HashTableConstValuesIterator<T, U, V> &b)
0279 {
0280     return a.m_impl != b.m_impl;
0281 }
0282 
0283 template<typename T, typename U, typename V>
0284 inline bool operator==(const HashTableKeysIterator<T, U, V> &a, const HashTableKeysIterator<T, U, V> &b)
0285 {
0286     return a.m_impl == b.m_impl;
0287 }
0288 
0289 template<typename T, typename U, typename V>
0290 inline bool operator!=(const HashTableKeysIterator<T, U, V> &a, const HashTableKeysIterator<T, U, V> &b)
0291 {
0292     return a.m_impl != b.m_impl;
0293 }
0294 
0295 template<typename T, typename U, typename V>
0296 inline bool operator==(const HashTableValuesIterator<T, U, V> &a, const HashTableValuesIterator<T, U, V> &b)
0297 {
0298     return a.m_impl == b.m_impl;
0299 }
0300 
0301 template<typename T, typename U, typename V>
0302 inline bool operator!=(const HashTableValuesIterator<T, U, V> &a, const HashTableValuesIterator<T, U, V> &b)
0303 {
0304     return a.m_impl != b.m_impl;
0305 }
0306 
0307 } // namespace WTF
0308 
0309 #endif // WTF_HashIterators_h