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