Ticket #8749: map_iterator2.patch
File map_iterator2.patch, 3.3 KB (added by , 16 years ago) |
---|
-
common/hashmap.h
85 85 */ 86 86 template <class Key, class Val, class HashFunc = Hash<Key>, class EqualFunc = EqualTo<Key> > 87 87 class HashMap { 88 friend class const_iterator;89 88 private: 90 89 #if defined (PALMOS_MODE) 91 90 public: … … 96 95 struct Node { 97 96 const Key _key; 98 97 Val _value; 99 Node(const Key &key) : _key(key) {}98 Node(const Key &key) : _key(key), _value() {} 100 99 }; 101 100 102 101 Node **_arr; // hashtable of size arrsize. … … 117 116 int lookupAndCreateIfMissing(const Key &key); 118 117 void expand_array(uint newsize); 119 118 120 public: 121 class const_iterator {122 typedef const HashMap<Key, Val, HashFunc, EqualFunc> * 119 template <class NodeType> 120 class Iterator { 121 typedef const HashMap<Key, Val, HashFunc, EqualFunc> *hashmap_t; 123 122 friend class HashMap<Key, Val, HashFunc, EqualFunc>; 124 123 protected: 125 124 uint _idx; 126 125 hashmap_t _hashmap; 127 const_iterator(uint idx, hashmap_t hashmap) : _idx(idx), _hashmap(hashmap) {}126 Iterator(uint idx, hashmap_t hashmap) : _idx(idx), _hashmap(hashmap) {} 128 127 129 const Node *deref() const {128 NodeType *deref() const { 130 129 assert(_hashmap != 0); 131 Node *node = _hashmap->_arr[_idx];130 NodeType *node = _hashmap->_arr[_idx]; 132 131 assert(node != 0); 133 132 return node; 134 133 } 135 134 136 135 public: 137 const_iterator() : _idx(0), _hashmap(0) {} 136 Iterator() : _idx(0), _hashmap(0) {} 137 // HACK: to allow non const/const begin, end and find to work 138 Iterator(const Iterator<Node> &iter) : _idx(iter._idx), _hashmap(iter._hashmap) {} 138 139 139 const Node &operator *() const { return *deref(); } 140 const Node *operator->() const { return deref(); } 141 bool operator ==(const const_iterator &iter) const { return _idx == iter._idx && _hashmap == iter._hashmap; } 142 bool operator !=(const const_iterator &iter) const { return !(*this == iter); } 143 const_iterator operator ++() { 140 NodeType &operator *() const { return *deref(); } 141 NodeType *operator->() const { return deref(); } 142 143 bool operator ==(const Iterator &iter) const { return _idx == iter._idx && _hashmap == iter._hashmap; } 144 bool operator !=(const Iterator &iter) const { return !(*this == iter); } 145 146 Iterator &operator ++() { 144 147 assert(_hashmap); 145 148 do { 146 149 _idx++; … … 150 153 151 154 return *this; 152 155 } 156 157 Iterator operator ++(int) { 158 Iterator old = *this; 159 operator ++(); 160 return old; 161 } 153 162 }; 154 163 164 public: 165 typedef Iterator<Node> iterator; 166 typedef Iterator<const Node> const_iterator; 167 155 168 HashMap(); 156 169 HashMap(const HM_t& map); 157 170 ~HashMap(); … … 183 196 184 197 uint size() const { return _nele; } 185 198 199 iterator begin() { 200 // Find and return the _key non-empty entry 201 for (uint ctr = 0; ctr < _arrsize; ++ctr) { 202 if (_arr[ctr]) 203 return iterator(ctr, this); 204 } 205 return end(); 206 } 207 iterator end() { 208 return iterator((uint)-1, this); 209 } 210 186 211 const_iterator begin() const { 187 212 // Find and return the first non-empty entry 188 213 for (uint ctr = 0; ctr < _arrsize; ++ctr) { … … 195 220 return const_iterator((uint)-1, this); 196 221 } 197 222 223 iterator find(const Key &key) { 224 uint ctr = lookup(key); 225 if (_arr[ctr]) 226 return iterator(ctr, this); 227 return end(); 228 } 229 198 230 const_iterator find(const Key &key) const { 199 231 uint ctr = lookup(key); 200 232 if (_arr[ctr])