aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMax Horn2008-01-13 14:44:29 +0000
committerMax Horn2008-01-13 14:44:29 +0000
commit79e1579cb3119a70677f7c7c158c5e7686b65f6d (patch)
tree70928c5923c588cbe3b83249771f0f2b9b411850
parent9e5e73eb9a5deab58ff9f3128372bceaa6aacfbe (diff)
downloadscummvm-rg350-79e1579cb3119a70677f7c7c158c5e7686b65f6d.tar.gz
scummvm-rg350-79e1579cb3119a70677f7c7c158c5e7686b65f6d.tar.bz2
scummvm-rg350-79e1579cb3119a70677f7c7c158c5e7686b65f6d.zip
Fix warnings HashMap causes on some compilers (notably, GCC 3.x used for various cross compilers)
svn-id: r30472
-rw-r--r--common/hashmap.h93
1 files changed, 78 insertions, 15 deletions
diff --git a/common/hashmap.h b/common/hashmap.h
index 579f0f9bfb..d5e2cdcaf3 100644
--- a/common/hashmap.h
+++ b/common/hashmap.h
@@ -116,18 +116,26 @@ public:
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,12 +143,8 @@ public:
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) {}
-
- NodeType &operator *() const { return *deref(); }
- NodeType *operator->() const { return deref(); }
+ Node &operator *() const { return *deref(); }
+ Node *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 @@ public:
}
};
+ /**
+ * 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);