Ticket #8849: hashmap.patch
File hashmap.patch, 4.3 KB (added by , 17 years ago) |
---|
-
hashmap.h
124 124 int lookupAndCreateIfMissing(const Key &key); 125 125 void expand_array(uint newsize); 126 126 127 class Iterator; 128 class ConstIterator; 129 friend class Iterator; 130 friend class ConstIterator; 127 template<class T> friend class IteratorImpl; 131 128 132 129 /** 133 130 * Simple HashMap iterator implementation. 134 131 */ 135 class Iterator { 132 template<class NodeType> 133 class IteratorImpl { 134 friend class HashMap; 135 template<class T> friend class IteratorImpl; 136 136 protected: 137 137 typedef const HashMap hashmap_t; 138 friend class HashMap;139 138 140 // Allow ConstIterator to read member vars, so that Iterators can be converted to ConstIterator141 friend class HashMap::ConstIterator;142 143 139 uint _idx; 144 140 hashmap_t *_hashmap; 145 141 146 142 protected: 147 Iterator (uint idx, hashmap_t *hashmap) : _idx(idx), _hashmap(hashmap) {}143 IteratorImpl(uint idx, hashmap_t *hashmap) : _idx(idx), _hashmap(hashmap) {} 148 144 149 Node *deref() const {145 NodeType *deref() const { 150 146 assert(_hashmap != 0); 151 147 assert(_idx < _hashmap->_arrsize); 152 148 Node *node = _hashmap->_arr[_idx]; … … 155 151 } 156 152 157 153 public: 158 Iterator() : _idx(0), _hashmap(0) {} 154 IteratorImpl() : _idx(0), _hashmap(0) {} 155 template<class T> 156 IteratorImpl(const IteratorImpl<T> &c) : _idx(c._idx), _hashmap(c._hashmap) {} 159 157 160 Node &operator *() const { return *deref(); }161 Node *operator->() const { return deref(); }158 NodeType &operator *() const { return *deref(); } 159 NodeType *operator->() const { return deref(); } 162 160 163 bool operator ==(const Iterator &iter) const { return _idx == iter._idx && _hashmap == iter._hashmap; }164 bool operator !=(const Iterator &iter) const { return !(*this == iter); }161 bool operator ==(const IteratorImpl &iter) const { return _idx == iter._idx && _hashmap == iter._hashmap; } 162 bool operator !=(const IteratorImpl &iter) const { return !(*this == iter); } 165 163 166 Iterator &operator ++() {164 IteratorImpl &operator ++() { 167 165 assert(_hashmap); 168 166 do { 169 167 _idx++; … … 174 172 return *this; 175 173 } 176 174 177 Iterator operator ++(int) {178 Iterator old = *this;175 IteratorImpl operator ++(int) { 176 IteratorImpl old = *this; 179 177 operator ++(); 180 178 return old; 181 179 } 182 180 }; 183 181 184 /**185 * Simple HashMap const iterator implementation.186 * This is almost completely identical to the normal iterator class, only187 * with some const keywords added here and there, plus a conversion188 * operator which makes it possible to transparently convert iterators to189 * const iterators.190 * It is sadly not really possible to reduce this code duplication using191 * template, unless one is willing to accept various warnings on certain192 * compilers. Note that many (most? all?) implementations of the standard193 * C++ library use a similar approach for their implementations.194 */195 class ConstIterator {196 protected:197 typedef const HashMap hashmap_t;198 friend class HashMap;199 200 uint _idx;201 hashmap_t *_hashmap;202 203 protected:204 ConstIterator(uint idx, hashmap_t *hashmap) : _idx(idx), _hashmap(hashmap) {}205 206 const Node *deref() const {207 assert(_hashmap != 0);208 assert(_idx < _hashmap->_arrsize);209 const Node *node = _hashmap->_arr[_idx];210 assert(node != 0);211 return node;212 }213 214 public:215 ConstIterator() : _idx(0), _hashmap(0) {}216 217 // Converting a non-const iterator to a const one is allowed218 ConstIterator(const Iterator &iter) : _idx(iter._idx), _hashmap(iter._hashmap) {}219 220 const Node &operator *() const { return *deref(); }221 const Node *operator->() const { return deref(); }222 223 bool operator ==(const ConstIterator &iter) const { return _idx == iter._idx && _hashmap == iter._hashmap; }224 bool operator !=(const ConstIterator &iter) const { return !(*this == iter); }225 226 ConstIterator &operator ++() {227 assert(_hashmap);228 do {229 _idx++;230 } while (_idx < _hashmap->_arrsize && _hashmap->_arr[_idx] == 0);231 if (_idx >= _hashmap->_arrsize)232 _idx = (uint)-1;233 234 return *this;235 }236 237 ConstIterator operator ++(int) {238 ConstIterator old = *this;239 operator ++();240 return old;241 }242 };243 244 182 public: 245 typedef Iterator iterator;246 typedef ConstIteratorconst_iterator;183 typedef IteratorImpl<Node> iterator; 184 typedef IteratorImpl<const Node> const_iterator; 247 185 248 186 HashMap(); 249 187 HashMap(const HM_t& map);