Ticket #8749: map_iterator2.patch

File map_iterator2.patch, 3.3 KB (added by fingolfin, 16 years ago)
  • common/hashmap.h

     
    8585 */
    8686template <class Key, class Val, class HashFunc = Hash<Key>, class EqualFunc = EqualTo<Key> >
    8787class HashMap {
    88         friend class const_iterator;
    8988private:
    9089#if defined (PALMOS_MODE)
    9190public:
     
    9695        struct Node {
    9796                const Key _key;
    9897                Val _value;
    99                 Node(const Key &key) : _key(key) {}
     98                Node(const Key &key) : _key(key), _value() {}
    10099        };
    101100
    102101        Node **_arr;    // hashtable of size arrsize.
     
    117116        int lookupAndCreateIfMissing(const Key &key);
    118117        void expand_array(uint newsize);
    119118
    120 public:
    121         class const_iterator {
    122                 typedef const HashMap<Key, Val, HashFunc, EqualFunc> * hashmap_t;
     119        template <class NodeType>
     120        class Iterator {
     121                typedef const HashMap<Key, Val, HashFunc, EqualFunc> *hashmap_t;
    123122                friend class HashMap<Key, Val, HashFunc, EqualFunc>;
    124123        protected:
    125124                uint _idx;
    126125                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) {}
    128127
    129                 const Node *deref() const {
     128                NodeType *deref() const {
    130129                        assert(_hashmap != 0);
    131                         Node *node = _hashmap->_arr[_idx];
     130                        NodeType *node = _hashmap->_arr[_idx];
    132131                        assert(node != 0);
    133132                        return node;
    134133                }
    135134
    136135        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) {}
    138139
    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 ++() {
    144147                        assert(_hashmap);
    145148                        do {
    146149                                _idx++;
     
    150153
    151154                        return *this;
    152155                }
     156
     157                Iterator operator ++(int) {
     158                        Iterator old = *this;
     159                        operator ++();
     160                        return old;
     161                }
    153162        };
    154163
     164public:
     165        typedef Iterator<Node> iterator;
     166        typedef Iterator<const Node> const_iterator;
     167
    155168        HashMap();
    156169        HashMap(const HM_t& map);
    157170        ~HashMap();
     
    183196
    184197        uint size() const { return _nele; }
    185198
     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
    186211        const_iterator  begin() const {
    187212                // Find and return the first non-empty entry
    188213                for (uint ctr = 0; ctr < _arrsize; ++ctr) {
     
    195220                return const_iterator((uint)-1, this);
    196221        }
    197222
     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
    198230        const_iterator  find(const Key &key) const {
    199231                uint ctr = lookup(key);
    200232                if (_arr[ctr])