WTF
HashIterators.h
Go to the documentation of this file.
00001 // -*- mode: c++; c-basic-offset: 4 -*- 00002 /* 00003 * Copyright (C) 2007 Apple Inc. All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions 00007 * are met: 00008 * 1. Redistributions of source code must retain the above copyright 00009 * notice, this list of conditions and the following disclaimer. 00010 * 2. Redistributions in binary form must reproduce the above copyright 00011 * notice, this list of conditions and the following disclaimer in the 00012 * documentation and/or other materials provided with the distribution. 00013 * 00014 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY 00015 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00016 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 00017 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR 00018 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00019 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 00020 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00021 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 00022 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00023 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00024 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00025 */ 00026 00027 #ifndef WTF_HashIterators_h 00028 #define WTF_HashIterators_h 00029 00030 namespace WTF { 00031 00032 template<typename HashTableType, typename KeyType, typename MappedType> struct HashTableConstKeysIterator; 00033 template<typename HashTableType, typename KeyType, typename MappedType> struct HashTableConstValuesIterator; 00034 template<typename HashTableType, typename KeyType, typename MappedType> struct HashTableKeysIterator; 00035 template<typename HashTableType, typename KeyType, typename MappedType> struct HashTableValuesIterator; 00036 00037 template<typename HashTableType, typename KeyType, typename MappedType> struct HashTableConstIteratorAdapter<HashTableType, std::pair<KeyType, MappedType> > { 00038 private: 00039 typedef std::pair<KeyType, MappedType> ValueType; 00040 public: 00041 typedef HashTableConstKeysIterator<HashTableType, KeyType, MappedType> Keys; 00042 typedef HashTableConstValuesIterator<HashTableType, KeyType, MappedType> Values; 00043 00044 HashTableConstIteratorAdapter(const typename HashTableType::const_iterator& impl) : m_impl(impl) {} 00045 00046 const ValueType* get() const { return (const ValueType*)m_impl.get(); } 00047 const ValueType& operator*() const { return *get(); } 00048 const ValueType* operator->() const { return get(); } 00049 00050 HashTableConstIteratorAdapter& operator++() { ++m_impl; return *this; } 00051 // postfix ++ intentionally omitted 00052 00053 Keys keys() { return Keys(*this); } 00054 Values values() { return Values(*this); } 00055 00056 typename HashTableType::const_iterator m_impl; 00057 }; 00058 00059 template<typename HashTableType, typename KeyType, typename MappedType> struct HashTableIteratorAdapter<HashTableType, std::pair<KeyType, MappedType> > { 00060 private: 00061 typedef std::pair<KeyType, MappedType> ValueType; 00062 public: 00063 typedef HashTableKeysIterator<HashTableType, KeyType, MappedType> Keys; 00064 typedef HashTableValuesIterator<HashTableType, KeyType, MappedType> Values; 00065 00066 HashTableIteratorAdapter(const typename HashTableType::iterator& impl) : m_impl(impl) {} 00067 00068 ValueType* get() const { return (ValueType*)m_impl.get(); } 00069 ValueType& operator*() const { return *get(); } 00070 ValueType* operator->() const { return get(); } 00071 00072 HashTableIteratorAdapter& operator++() { ++m_impl; return *this; } 00073 // postfix ++ intentionally omitted 00074 00075 operator HashTableConstIteratorAdapter<HashTableType, ValueType>() { 00076 typename HashTableType::const_iterator i = m_impl; 00077 return i; 00078 } 00079 00080 Keys keys() { return Keys(*this); } 00081 Values values() { return Values(*this); } 00082 00083 typename HashTableType::iterator m_impl; 00084 }; 00085 00086 template<typename HashTableType, typename KeyType, typename MappedType> struct HashTableConstKeysIterator { 00087 private: 00088 typedef HashTableConstIteratorAdapter<HashTableType, std::pair<KeyType, MappedType> > ConstIterator; 00089 00090 public: 00091 HashTableConstKeysIterator(const ConstIterator& impl) : m_impl(impl) {} 00092 00093 const KeyType* get() const { return &(m_impl.get()->first); } 00094 const KeyType& operator*() const { return *get(); } 00095 const KeyType* operator->() const { return get(); } 00096 00097 HashTableConstKeysIterator& operator++() { ++m_impl; return *this; } 00098 // postfix ++ intentionally omitted 00099 00100 ConstIterator m_impl; 00101 }; 00102 00103 template<typename HashTableType, typename KeyType, typename MappedType> struct HashTableConstValuesIterator { 00104 private: 00105 typedef HashTableConstIteratorAdapter<HashTableType, std::pair<KeyType, MappedType> > ConstIterator; 00106 00107 public: 00108 HashTableConstValuesIterator(const ConstIterator& impl) : m_impl(impl) {} 00109 00110 const MappedType* get() const { return &(m_impl.get()->second); } 00111 const MappedType& operator*() const { return *get(); } 00112 const MappedType* operator->() const { return get(); } 00113 00114 HashTableConstValuesIterator& operator++() { ++m_impl; return *this; } 00115 // postfix ++ intentionally omitted 00116 00117 ConstIterator m_impl; 00118 }; 00119 00120 template<typename HashTableType, typename KeyType, typename MappedType> struct HashTableKeysIterator { 00121 private: 00122 typedef HashTableIteratorAdapter<HashTableType, std::pair<KeyType, MappedType> > Iterator; 00123 typedef HashTableConstIteratorAdapter<HashTableType, std::pair<KeyType, MappedType> > ConstIterator; 00124 00125 public: 00126 HashTableKeysIterator(const Iterator& impl) : m_impl(impl) {} 00127 00128 KeyType* get() const { return &(m_impl.get()->first); } 00129 KeyType& operator*() const { return *get(); } 00130 KeyType* operator->() const { return get(); } 00131 00132 HashTableKeysIterator& operator++() { ++m_impl; return *this; } 00133 // postfix ++ intentionally omitted 00134 00135 operator HashTableConstKeysIterator<HashTableType, KeyType, MappedType>() { 00136 ConstIterator i = m_impl; 00137 return i; 00138 } 00139 00140 Iterator m_impl; 00141 }; 00142 00143 template<typename HashTableType, typename KeyType, typename MappedType> struct HashTableValuesIterator { 00144 private: 00145 typedef HashTableIteratorAdapter<HashTableType, std::pair<KeyType, MappedType> > Iterator; 00146 typedef HashTableConstIteratorAdapter<HashTableType, std::pair<KeyType, MappedType> > ConstIterator; 00147 00148 public: 00149 HashTableValuesIterator(const Iterator& impl) : m_impl(impl) {} 00150 00151 MappedType* get() const { return &(m_impl.get()->second); } 00152 MappedType& operator*() const { return *get(); } 00153 MappedType* operator->() const { return get(); } 00154 00155 HashTableValuesIterator& operator++() { ++m_impl; return *this; } 00156 // postfix ++ intentionally omitted 00157 00158 operator HashTableConstValuesIterator<HashTableType, KeyType, MappedType>() { 00159 ConstIterator i = m_impl; 00160 return i; 00161 } 00162 00163 Iterator m_impl; 00164 }; 00165 00166 template<typename T, typename U, typename V> 00167 inline bool operator==(const HashTableConstKeysIterator<T, U, V>& a, const HashTableConstKeysIterator<T, U, V>& b) 00168 { 00169 return a.m_impl == b.m_impl; 00170 } 00171 00172 template<typename T, typename U, typename V> 00173 inline bool operator!=(const HashTableConstKeysIterator<T, U, V>& a, const HashTableConstKeysIterator<T, U, V>& b) 00174 { 00175 return a.m_impl != b.m_impl; 00176 } 00177 00178 template<typename T, typename U, typename V> 00179 inline bool operator==(const HashTableConstValuesIterator<T, U, V>& a, const HashTableConstValuesIterator<T, U, V>& b) 00180 { 00181 return a.m_impl == b.m_impl; 00182 } 00183 00184 template<typename T, typename U, typename V> 00185 inline bool operator!=(const HashTableConstValuesIterator<T, U, V>& a, const HashTableConstValuesIterator<T, U, V>& b) 00186 { 00187 return a.m_impl != b.m_impl; 00188 } 00189 00190 template<typename T, typename U, typename V> 00191 inline bool operator==(const HashTableKeysIterator<T, U, V>& a, const HashTableKeysIterator<T, U, V>& b) 00192 { 00193 return a.m_impl == b.m_impl; 00194 } 00195 00196 template<typename T, typename U, typename V> 00197 inline bool operator!=(const HashTableKeysIterator<T, U, V>& a, const HashTableKeysIterator<T, U, V>& b) 00198 { 00199 return a.m_impl != b.m_impl; 00200 } 00201 00202 template<typename T, typename U, typename V> 00203 inline bool operator==(const HashTableValuesIterator<T, U, V>& a, const HashTableValuesIterator<T, U, V>& b) 00204 { 00205 return a.m_impl == b.m_impl; 00206 } 00207 00208 template<typename T, typename U, typename V> 00209 inline bool operator!=(const HashTableValuesIterator<T, U, V>& a, const HashTableValuesIterator<T, U, V>& b) 00210 { 00211 return a.m_impl != b.m_impl; 00212 } 00213 00214 00215 } // namespace WTF 00216 00217 #endif // WTF_HashIterators_h
KDE 4.6 API Reference