[Scummvm-cvs-logs] SF.net SVN: scummvm: [25135] scummvm/trunk/common/hashmap.h
fingolfin at users.sourceforge.net
fingolfin at users.sourceforge.net
Sun Jan 21 01:06:51 CET 2007
Revision: 25135
http://scummvm.svn.sourceforge.net/scummvm/?rev=25135&view=rev
Author: fingolfin
Date: 2007-01-20 16:06:50 -0800 (Sat, 20 Jan 2007)
Log Message:
-----------
Added some new HashMap methods: lookupAndCreateIfMissing (internal only), setVal and getVal (which actually is just the old queryVal renamed for consistency)
Modified Paths:
--------------
scummvm/trunk/common/hashmap.h
Modified: scummvm/trunk/common/hashmap.h
===================================================================
--- scummvm/trunk/common/hashmap.h 2007-01-20 21:27:57 UTC (rev 25134)
+++ scummvm/trunk/common/hashmap.h 2007-01-21 00:06:50 UTC (rev 25135)
@@ -110,6 +110,7 @@
void assign(const HM_t& map);
int lookup(const Key &key) const;
+ int lookupAndCreateIfMissing(const Key &key);
void expand_array(uint newsize);
public:
@@ -164,8 +165,10 @@
Val &operator [](const Key &key);
const Val &operator [](const Key &key) const;
- const Val &queryVal(const Key &key) const;
+ const Val &getVal(const Key &key) const;
+ void setVal(const Key &key, const Val &val);
+
void clear(bool shrinkArray = 0);
size_t erase(const Key &key);
@@ -352,15 +355,9 @@
}
template <class Key, class Val, class HashFunc, class EqualFunc>
-bool HashMap<Key, Val, HashFunc, EqualFunc>::contains(const Key &key) const {
+int HashMap<Key, Val, HashFunc, EqualFunc>::lookupAndCreateIfMissing(const Key &key) {
uint ctr = lookup(key);
- return (_arr[ctr] != NULL);
-}
-template <class Key, class Val, class HashFunc, class EqualFunc>
-Val &HashMap<Key, Val, HashFunc, EqualFunc>::operator [](const Key &key) {
- uint ctr = lookup(key);
-
if (_arr[ctr] == NULL) {
_arr[ctr] = new Node(key);
_nele++;
@@ -372,22 +369,43 @@
}
}
+ return ctr;
+}
+
+
+template <class Key, class Val, class HashFunc, class EqualFunc>
+bool HashMap<Key, Val, HashFunc, EqualFunc>::contains(const Key &key) const {
+ uint ctr = lookup(key);
+ return (_arr[ctr] != NULL);
+}
+
+template <class Key, class Val, class HashFunc, class EqualFunc>
+Val &HashMap<Key, Val, HashFunc, EqualFunc>::operator [](const Key &key) {
+ uint ctr = lookupAndCreateIfMissing(key);
+ assert(_arr[ctr] != NULL);
return _arr[ctr]->_value;
}
template <class Key, class Val, class HashFunc, class EqualFunc>
const Val &HashMap<Key, Val, HashFunc, EqualFunc>::operator [](const Key &key) const {
- return queryVal(key);
+ return getVal(key);
}
template <class Key, class Val, class HashFunc, class EqualFunc>
-const Val &HashMap<Key, Val, HashFunc, EqualFunc>::queryVal(const Key &key) const {
+const Val &HashMap<Key, Val, HashFunc, EqualFunc>::getVal(const Key &key) const {
uint ctr = lookup(key);
assert(_arr[ctr] != NULL);
return _arr[ctr]->_value;
}
template <class Key, class Val, class HashFunc, class EqualFunc>
+void HashMap<Key, Val, HashFunc, EqualFunc>::setVal(const Key &key, const Val &val) {
+ uint ctr = lookupAndCreateIfMissing(key);
+ assert(_arr[ctr] != NULL);
+ _arr[ctr]->_value = val;
+}
+
+template <class Key, class Val, class HashFunc, class EqualFunc>
size_t HashMap<Key, Val, HashFunc, EqualFunc>::erase(const Key &key) {
// This is based on code in the Wikipedia article on Hash tables.
uint i = lookup(key);
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