[Scummvm-cvs-logs] SF.net SVN: scummvm: [30493] scummvm/tags/release-0-11-0/common
sev at users.sourceforge.net
sev at users.sourceforge.net
Tue Jan 15 13:28:57 CET 2008
Revision: 30493
http://scummvm.svn.sourceforge.net/scummvm/?rev=30493&view=rev
Author: sev
Date: 2008-01-15 04:28:56 -0800 (Tue, 15 Jan 2008)
Log Message:
-----------
Backport hashmap.h changes
Modified Paths:
--------------
scummvm/tags/release-0-11-0/common/file.cpp
scummvm/tags/release-0-11-0/common/hashmap.h
scummvm/tags/release-0-11-0/common/util.cpp
Modified: scummvm/tags/release-0-11-0/common/file.cpp
===================================================================
--- scummvm/tags/release-0-11-0/common/file.cpp 2008-01-15 12:25:20 UTC (rev 30492)
+++ scummvm/tags/release-0-11-0/common/file.cpp 2008-01-15 12:28:56 UTC (rev 30493)
@@ -74,7 +74,7 @@
//#undef getc
//#undef ferror
- #include "ds-fs.h"
+ #include "backends/fs/ds/ds-fs.h"
//void std_fprintf(FILE* handle, const char* fmt, ...); // used in common/util.cpp
Modified: scummvm/tags/release-0-11-0/common/hashmap.h
===================================================================
--- scummvm/tags/release-0-11-0/common/hashmap.h 2008-01-15 12:25:20 UTC (rev 30492)
+++ scummvm/tags/release-0-11-0/common/hashmap.h 2008-01-15 12:28:56 UTC (rev 30493)
@@ -116,18 +116,26 @@
int lookupAndCreateIfMissing(const Key &key);
void expand_array(uint newsize);
- template <class NodeType>
+ /**
+ * Simple HashMap iterator implementation.
+ */
class Iterator {
- typedef const HashMap<Key, Val, HashFunc, EqualFunc> *hashmap_t;
- friend class HashMap<Key, Val, HashFunc, EqualFunc>;
+ protected:
+ typedef const HashMap hashmap_t;
+ friend class HashMap;
+
+ // Allow ConstIterator to read member vars, so that Iterators can be converted to ConstIterator
+ friend class HashMap::ConstIterator;
+
uint _idx;
- hashmap_t _hashmap;
+ hashmap_t *_hashmap;
+
protected:
- Iterator(uint idx, hashmap_t hashmap) : _idx(idx), _hashmap(hashmap) {}
+ Iterator(uint idx, hashmap_t *hashmap) : _idx(idx), _hashmap(hashmap) {}
- NodeType *deref() const {
+ Node *deref() const {
assert(_hashmap != 0);
- NodeType *node = _hashmap->_arr[_idx];
+ Node *node = _hashmap->_arr[_idx];
assert(node != 0);
return node;
}
@@ -135,13 +143,9 @@
public:
Iterator() : _idx(0), _hashmap(0) {}
- // HACK: to allow non const/const begin, end and find to work.
- friend class Iterator<const NodeType>;
- Iterator(const Iterator<Node> &iter) : _idx(iter._idx), _hashmap(iter._hashmap) {}
+ Node &operator *() const { return *deref(); }
+ Node *operator->() const { return deref(); }
- NodeType &operator *() const { return *deref(); }
- NodeType *operator->() const { return deref(); }
-
bool operator ==(const Iterator &iter) const { return _idx == iter._idx && _hashmap == iter._hashmap; }
bool operator !=(const Iterator &iter) const { return !(*this == iter); }
@@ -163,9 +167,68 @@
}
};
+ /**
+ * Simple HashMap const iterator implementation.
+ * This is almost completely identical to the normal iterator class, only
+ * with some const keywords added here and there, plus a conversion
+ * operator which makes it possible to transparently convert iterators to
+ * const iterators.
+ * It is sadly not really possible to reduce this code duplication using
+ * template, unless one is willing to accept various warnings on certain
+ * compilers. Note that many (most? all?) implementations of the standard
+ * C++ library use a similar approach for their implementations.
+ */
+ class ConstIterator {
+ protected:
+ typedef const HashMap hashmap_t;
+ friend class HashMap;
+
+ uint _idx;
+ hashmap_t *_hashmap;
+
+ protected:
+ ConstIterator(uint idx, hashmap_t *hashmap) : _idx(idx), _hashmap(hashmap) {}
+
+ const Node *deref() const {
+ assert(_hashmap != 0);
+ const Node *node = _hashmap->_arr[_idx];
+ assert(node != 0);
+ return node;
+ }
+
+ public:
+ ConstIterator() : _idx(0), _hashmap(0) {}
+
+ // Converting a non-const iterator to a const one is allowed
+ ConstIterator(const Iterator &iter) : _idx(iter._idx), _hashmap(iter._hashmap) {}
+
+ const Node &operator *() const { return *deref(); }
+ const Node *operator->() const { return deref(); }
+
+ bool operator ==(const ConstIterator &iter) const { return _idx == iter._idx && _hashmap == iter._hashmap; }
+ bool operator !=(const ConstIterator &iter) const { return !(*this == iter); }
+
+ ConstIterator &operator ++() {
+ assert(_hashmap);
+ do {
+ _idx++;
+ } while (_idx < _hashmap->_arrsize && _hashmap->_arr[_idx] == 0);
+ if (_idx >= _hashmap->_arrsize)
+ _idx = (uint)-1;
+
+ return *this;
+ }
+
+ ConstIterator operator ++(int) {
+ ConstIterator old = *this;
+ operator ++();
+ return old;
+ }
+ };
+
public:
- typedef Iterator<Node> iterator;
- typedef Iterator<const Node> const_iterator;
+ typedef Iterator iterator;
+ typedef ConstIterator const_iterator;
HashMap();
HashMap(const HM_t& map);
Modified: scummvm/tags/release-0-11-0/common/util.cpp
===================================================================
--- scummvm/tags/release-0-11-0/common/util.cpp 2008-01-15 12:25:20 UTC (rev 30492)
+++ scummvm/tags/release-0-11-0/common/util.cpp 2008-01-15 12:28:56 UTC (rev 30493)
@@ -43,7 +43,7 @@
#endif
#ifdef __DS__
- #include "ds-fs.h"
+ #include "backends/fs/ds/ds-fs.h"
#undef stderr
#undef stdout
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
More information about the Scummvm-git-logs
mailing list