Ticket #8749: map_iterator.patch

File map_iterator.patch, 13.8 KB (added by lordhoto, 16 years ago)

STL like iterators for Common::HashMap

  • gui/eval.cpp

     
    310310        if (includeAliases) {
    311311                AliasesMap::const_iterator itera = _aliases.find(s);
    312312                if (itera != _aliases.end())
    313                         var = &(itera->_value);
     313                        var = &(itera->second);
    314314        }
    315315
    316316        VariablesMap::const_iterator iterv = _vars.find(*var);
    317317        if (iterv != _vars.end())
    318                 return iterv->_value;
     318                return iterv->second;
    319319
    320320        return EVAL_UNDEF_VAR;
    321321}
  • gui/launcher.cpp

     
    576576        const ConfigManager::DomainMap &domains = ConfMan.getGameDomains();
    577577        ConfigManager::DomainMap::const_iterator iter = domains.begin();
    578578        for (iter = domains.begin(); iter != domains.end(); ++iter) {
    579                 String gameid(iter->_value.get("gameid"));
    580                 String description(iter->_value.get("description"));
     579                String gameid(iter->second.get("gameid"));
     580                String description(iter->second.get("description"));
    581581
    582582                if (gameid.empty())
    583                         gameid = iter->_key;
     583                        gameid = iter->first;
    584584                if (description.empty()) {
    585585                        GameDescriptor g = Base::findGame(gameid);
    586586                        if (g.contains("description"))
     
    596596#endif
    597597
    598598                if (description.empty())
    599                         description = "Unknown (target " + iter->_key + ", gameid " + gameid + ")";
     599                        description = "Unknown (target " + iter->first + ", gameid " + gameid + ")";
    600600
    601601                if (!gameid.empty() && !description.empty()) {
    602602                        // Insert the game into the launcher list
     
    605605                        while (pos < size && (scumm_stricmp(description.c_str(), l[pos].c_str()) > 0))
    606606                                pos++;
    607607                        l.insert_at(pos, description);
    608                         _domains.insert_at(pos, iter->_key);
     608                        _domains.insert_at(pos, iter->first);
    609609                }
    610610        }
    611611
     
    738738
    739739        // Copy all non-empty key/value pairs into the new domain
    740740        for (GameDescriptor::const_iterator iter = result.begin(); iter != result.end(); ++iter) {
    741                 if (!iter->_value.empty() && iter->_key != "preferredtarget")
    742                         ConfMan.set(iter->_key, iter->_value, domain);
     741                if (!iter->second.empty() && iter->first != "preferredtarget")
     742                        ConfMan.set(iter->first, iter->second, domain);
    743743        }
    744744
    745745        // TODO: Setting the description field here has the drawback
  • engines/agi/detection.cpp

     
    21332133                char name[8];
    21342134
    21352135                for (IntMap::const_iterator f = allFiles.begin(); f != allFiles.end(); ++f) {
    2136                         if (f->_key.hasSuffix("vol.0")) {
     2136                        if (f->first.hasSuffix("vol.0")) {
    21372137                                memset(name, 0, 8);
    2138                                 strncpy(name, f->_key.c_str(), MIN((uint)8, f->_key.size() > 5 ? f->_key.size() - 5 : f->_key.size()));
     2138                                strncpy(name, f->first.c_str(), MIN((uint)8, f->first.size() > 5 ? f->first.size() - 5 : f->first.size()));
    21392139
    21402140                                if (allFiles.contains("object") && allFiles.contains("words.tok") &&
    21412141                                        allFiles.contains(Common::String(name) + "dir")) {
  • common/file.cpp

     
    320320                        // Try all default directories
    321321                        StringIntMap::const_iterator x(_defaultDirectories->begin());
    322322                        for (; _handle == NULL && x != _defaultDirectories->end(); ++x) {
    323                                 _handle = fopenNoCase(filename, x->_key, modeStr);
     323                                _handle = fopenNoCase(filename, x->first, modeStr);
    324324                        }
    325325                }
    326326
     
    430430        if (_defaultDirectories) {
    431431                StringIntMap::const_iterator i(_defaultDirectories->begin());
    432432                for (; i != _defaultDirectories->end(); ++i) {
    433                         FilesystemNode file2(i->_key + filename);
     433                        FilesystemNode file2(i->first + filename);
    434434                       
    435435                        if(file2.exists())
    436436                                return !file2.isDirectory();
  • common/advancedDetector.cpp

     
    275275        printf("and its version/language/etc.:\n");
    276276
    277277        for (StringMap::const_iterator file = filesMD5.begin(); file != filesMD5.end(); ++file)
    278                 printf("  \"%s\", \"%s\", %d\n", file->_key.c_str(), file->_value.c_str(), filesSize[file->_key]);
     278                printf("  \"%s\", \"%s\", %d\n", file->first.c_str(), file->second.c_str(), filesSize[file->first]);
    279279
    280280        printf("\n");
    281281}
     
    372372        } else {
    373373                // Get the information of the requested files
    374374                for (StringSet::const_iterator file = filesList.begin(); file != filesList.end(); ++file) {
    375                         tstr = file->_key;
     375                        tstr = file->first;
    376376
    377377                        debug(3, "+ %s", tstr.c_str());
    378378                        if (!filesMD5.contains(tstr)) {
     
    380380                                        filesSize[tstr] = (int32)testFile.size();
    381381                                        testFile.close();
    382382
    383                                         if (md5_file_string(file->_key.c_str(), md5str, params.md5Bytes)) {
     383                                        if (md5_file_string(file->first.c_str(), md5str, params.md5Bytes)) {
    384384                                                filesMD5[tstr] = md5str;
    385385                                                debug(3, "> %s: %s", tstr.c_str(), md5str);
    386386                                        }
  • common/config-manager.cpp

     
    285285                if (!_domainSaveOrder.contains(kApplicationDomain))
    286286                        writeDomain(cfg_file, kApplicationDomain, _appDomain);
    287287                for (d = _gameDomains.begin(); d != _gameDomains.end(); ++d) {
    288                         if (!_domainSaveOrder.contains(d->_key))
    289                                 writeDomain(cfg_file, d->_key, d->_value);
     288                        if (!_domainSaveOrder.contains(d->first))
     289                                writeDomain(cfg_file, d->first, d->second);
    290290                }
    291291        }
    292292#endif // !__DC__
     
    312312        // Write all key/value pairs in this domain, including comments
    313313        Domain::const_iterator x;
    314314        for (x = domain.begin(); x != domain.end(); ++x) {
    315                 if (!x->_value.empty()) {
     315                if (!x->second.empty()) {
    316316                        // Write comment (if any)
    317                         if (domain.hasKVComment(x->_key)) {
    318                                 comment = domain.getKVComment(x->_key);
     317                        if (domain.hasKVComment(x->first)) {
     318                                comment = domain.getKVComment(x->first);
    319319                                stream.writeString(comment);
    320320                        }
    321321                        // Write the key/value pair
    322                         stream.writeString(x->_key);
     322                        stream.writeString(x->first);
    323323                        stream.writeByte('=');
    324                         stream.writeString(x->_value);
     324                        stream.writeString(x->second);
    325325                        stream.writeByte('\n');
    326326                }
    327327        }
     
    632632        Domain &newDom = _gameDomains[newName];
    633633        Domain::const_iterator iter;
    634634        for (iter = oldDom.begin(); iter != oldDom.end(); ++iter)
    635                 newDom[iter->_key] = iter->_value;
     635                newDom[iter->first] = iter->second;
    636636
    637637        _gameDomains.erase(oldName);
    638638}
     
    649649const String &ConfigManager::Domain::get(const String &key) const {
    650650        const_iterator iter(find(key));
    651651        if (iter != end())
    652                 return iter->_value;
     652                return iter->second;
    653653
    654654#if !(defined(PALMOS_ARM) || defined(PALMOS_DEBUG) || defined(__GP32__))
    655655        return String::emptyString;
  • 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:
     
    9493        typedef HashMap<Key, Val, HashFunc, EqualFunc> HM_t;
    9594
    9695        struct Node {
    97                 const Key _key;
    98                 Val _value;
    99                 Node(const Key &key) : _key(key) {}
     96                const Key first;
     97                Val second;
     98                Node(const Key &key) : first(key), second() {}
    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 first 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])
     
    327359                // Since we know that no key exists twice in the old table, we
    328360                // can do this slightly better than by calling lookup, since we
    329361                // don't have to call _equal().
    330                 dex = _hash(old_arr[ctr]->_key) % _arrsize;
     362                dex = _hash(old_arr[ctr]->first) % _arrsize;
    331363                while (_arr[dex] != NULL) {
    332364                        dex = (dex + 1) % _arrsize;
    333365                }
     
    349381int HashMap<Key, Val, HashFunc, EqualFunc>::lookup(const Key &key) const {
    350382        uint ctr = _hash(key) % _arrsize;
    351383
    352         while (_arr[ctr] != NULL && !_equal(_arr[ctr]->_key, key)) {
     384        while (_arr[ctr] != NULL && !_equal(_arr[ctr]->first, key)) {
    353385                ctr = (ctr + 1) % _arrsize;
    354386
    355387#ifdef DEBUG_HASH_COLLISIONS
     
    406438Val &HashMap<Key, Val, HashFunc, EqualFunc>::getVal(const Key &key) {
    407439        uint ctr = lookupAndCreateIfMissing(key);
    408440        assert(_arr[ctr] != NULL);
    409         return _arr[ctr]->_value;
     441        return _arr[ctr]->second;
    410442}
    411443
    412444template <class Key, class Val, class HashFunc, class EqualFunc>
    413445const Val &HashMap<Key, Val, HashFunc, EqualFunc>::getVal(const Key &key) const {
    414446        uint ctr = lookup(key);
    415447        if (_arr[ctr] != NULL)
    416                 return _arr[ctr]->_value;
     448                return _arr[ctr]->second;
    417449        else
    418450                return _defaultVal;
    419451}
     
    422454void HashMap<Key, Val, HashFunc, EqualFunc>::setVal(const Key &key, const Val &val) {
    423455        uint ctr = lookupAndCreateIfMissing(key);
    424456        assert(_arr[ctr] != NULL);
    425         _arr[ctr]->_value = val;
     457        _arr[ctr]->second = val;
    426458}
    427459
    428460template <class Key, class Val, class HashFunc, class EqualFunc>
     
    444476                        break;
    445477                // Compute the slot where the content of the next slot should normally be,
    446478                // assuming an empty table, and check whether we have to move it.
    447                 uint k = _hash(_arr[j]->_key) % _arrsize;
     479                uint k = _hash(_arr[j]->first) % _arrsize;
    448480                if ((j > i && (k <= i || k > j)) ||
    449481                    (j < i && (k <= i && k > j)) ) {
    450482                        _arr[i] = _arr[j];
  • base/commandLine.cpp

     
    571571        const ConfigManager::DomainMap &domains = ConfMan.getGameDomains();
    572572        ConfigManager::DomainMap::const_iterator iter = domains.begin();
    573573        for (iter = domains.begin(); iter != domains.end(); ++iter) {
    574                 Common::String name(iter->_key);
    575                 Common::String description(iter->_value.get("description"));
     574                Common::String name(iter->first);
     575                Common::String description(iter->second.get("description"));
    576576
    577577                if (description.empty()) {
    578578                        // FIXME: At this point, we should check for a "gameid" override
     
    599599        Common::ConfigManager::DomainMap::const_iterator iter = domains.begin();
    600600        int success = 0, failure = 0;
    601601        for (iter = domains.begin(); iter != domains.end(); ++iter) {
    602                 Common::String name(iter->_key);
    603                 Common::String gameid(iter->_value.get("gameid"));
    604                 Common::String path(iter->_value.get("path"));
     602                Common::String name(iter->first);
     603                Common::String gameid(iter->second.get("gameid"));
     604                Common::String path(iter->second.get("path"));
    605605                printf("Looking at target '%s', gameid '%s', path '%s' ...\n",
    606606                                name.c_str(), gameid.c_str(), path.c_str());
    607607                if (path.empty()) {
     
    739739
    740740        // Finally, store the command line settings into the config manager.
    741741        for (Common::StringMap::const_iterator x = settings.begin(); x != settings.end(); ++x) {
    742                 Common::String key(x->_key);
    743                 Common::String value(x->_value);
     742                Common::String key(x->first);
     743                Common::String value(x->second);
    744744
    745745                // Replace any "-" in the key by "_" (e.g. change "save-slot" to "save_slot").
    746746                for (Common::String::iterator c = key.begin(); c != key.end(); ++c)