[Scummvm-cvs-logs] SF.net SVN: scummvm:[53759] scummvm/trunk/engines/sword25/kernel

fingolfin at users.sourceforge.net fingolfin at users.sourceforge.net
Sun Oct 24 03:53:01 CEST 2010


Revision: 53759
          http://scummvm.svn.sourceforge.net/scummvm/?rev=53759&view=rev
Author:   fingolfin
Date:     2010-10-24 01:53:01 +0000 (Sun, 24 Oct 2010)

Log Message:
-----------
SWORD25: Replace ResourceManager's hash table by a Common::HashMap

Modified Paths:
--------------
    scummvm/trunk/engines/sword25/kernel/resmanager.cpp
    scummvm/trunk/engines/sword25/kernel/resmanager.h
    scummvm/trunk/engines/sword25/kernel/resource.cpp
    scummvm/trunk/engines/sword25/kernel/resource.h

Modified: scummvm/trunk/engines/sword25/kernel/resmanager.cpp
===================================================================
--- scummvm/trunk/engines/sword25/kernel/resmanager.cpp	2010-10-24 01:52:27 UTC (rev 53758)
+++ scummvm/trunk/engines/sword25/kernel/resmanager.cpp	2010-10-24 01:53:01 UTC (rev 53759)
@@ -64,7 +64,7 @@
 /**
  * Returns a resource by it's ordinal index. Returns NULL if any error occurs
  * Note: This method is not optimised for speed and should be used only for debugging purposes
- * @param Ord       Ordinal number of the resource. Must be between 0 and GetResourceCount() - 1.
+ * @param ord       Ordinal number of the resource. Must be between 0 and GetResourceCount() - 1.
  */
 Resource *ResourceManager::getResourceByOrdinal(int ord) const {
 	// \xDCberpr\xFCfen ob der Index Ord innerhald der Listengrenzen liegt.
@@ -231,8 +231,8 @@
 			deleteResourcesIfNecessary();
 
 			// Load the resource
-			Resource *pResource;
-			if (!(pResource = _resourceServices[i]->loadResource(fileName))) {
+			Resource *pResource = _resourceServices[i]->loadResource(fileName);
+			if (!pResource) {
 				BS_LOG_ERRORLN("Responsible service could not load resource \"%s\".", fileName.c_str());
 				return NULL;
 			}
@@ -242,7 +242,7 @@
 			pResource->_iterator = _resources.begin();
 
 			// Also store the resource in the hash table for quick lookup
-			_resourceHashTable[pResource->getFileNameHash() % HASH_TABLE_BUCKETS].push_front(pResource);
+			_resourceHashMap[pResource->getFileName()] = pResource;
 
 			return pResource;
 		}
@@ -277,15 +277,13 @@
  */
 Common::List<Resource *>::iterator ResourceManager::deleteResource(Resource *pResource) {
 	// Remove the resource from the hash table
-	_resourceHashTable[pResource->getFileNameHash() % HASH_TABLE_BUCKETS].remove(pResource);
+	_resourceHashMap.erase(pResource->_fileName);
 
-	Resource *pDummy = pResource;
-
 	// Delete the resource from the resource list
 	Common::List<Resource *>::iterator result = _resources.erase(pResource->_iterator);
 
 	// Delete the resource
-	delete(pDummy);
+	delete pResource;
 
 	// Return the iterator
 	return result;
@@ -294,21 +292,14 @@
 /**
  * Returns a pointer to a loaded resource. If any error occurs, NULL will be returned.
  * @param UniquefileName        The absolute path and filename
- * Gibt einen Pointer auf die angeforderte Resource zur\xFCck, oder NULL, wenn die Resourcen nicht geladen ist.
  */
 Resource *ResourceManager::getResource(const Common::String &uniquefileName) const {
 	// Determine whether the resource is already loaded
-	const Common::List<Resource *>& hashBucket = _resourceHashTable[Common::hashit(uniquefileName) % HASH_TABLE_BUCKETS];
-	{
-		Common::List<Resource *>::const_iterator iter = hashBucket.begin();
-		for (; iter != hashBucket.end(); ++iter) {
-			// Wenn die Resource gefunden wurde wird sie zur\xFCckgegeben.
-			if ((*iter)->getFileName() == uniquefileName)
-				return *iter;
-		}
-	}
+	ResMap::iterator it = _resourceHashMap.find(uniquefileName);
+	if (it != _resourceHashMap.end())
+		return it->_value;
 
-	// Resource wurde nicht gefunden, ist also nicht geladen
+	// Resource was not found, i.e. has not yet been loaded.
 	return NULL;
 }
 

Modified: scummvm/trunk/engines/sword25/kernel/resmanager.h
===================================================================
--- scummvm/trunk/engines/sword25/kernel/resmanager.h	2010-10-24 01:52:27 UTC (rev 53758)
+++ scummvm/trunk/engines/sword25/kernel/resmanager.h	2010-10-24 01:53:01 UTC (rev 53759)
@@ -36,6 +36,8 @@
 #define SWORD25_RESOURCEMANAGER_H
 
 #include "common/list.h"
+#include "common/hashmap.h"
+#include "common/hash-str.h"
 
 #include "sword25/kernel/common.h"
 
@@ -137,10 +139,6 @@
 	{}
 	virtual ~ResourceManager();
 
-	enum {
-		HASH_TABLE_BUCKETS = 256
-	};
-
 	/**
 	 * Moves a resource to the top of the resource list
 	 * @param pResource     The resource
@@ -169,7 +167,6 @@
 	/**
 	 * Returns a pointer to a loaded resource. If any error occurs, NULL will be returned.
 	 * @param UniqueFileName        The absolute path and filename
-	 * Gibt einen Pointer auf die angeforderte Resource zur\xFCck, oder NULL, wenn die Resourcen nicht geladen ist.
 	 */
 	Resource *getResource(const Common::String &uniqueFileName) const;
 
@@ -182,7 +179,8 @@
 	uint _maxMemoryUsage;
 	Common::Array<ResourceService *> _resourceServices;
 	Common::List<Resource *> _resources;
-	Common::List<Resource *> _resourceHashTable[HASH_TABLE_BUCKETS];
+	typedef Common::HashMap<Common::String, Resource *> ResMap;
+	ResMap _resourceHashMap;
 	bool _logCacheMiss;
 };
 

Modified: scummvm/trunk/engines/sword25/kernel/resource.cpp
===================================================================
--- scummvm/trunk/engines/sword25/kernel/resource.cpp	2010-10-24 01:52:27 UTC (rev 53758)
+++ scummvm/trunk/engines/sword25/kernel/resource.cpp	2010-10-24 01:53:01 UTC (rev 53759)
@@ -47,7 +47,6 @@
 	BS_ASSERT(pPM);
 
 	_fileName = pPM->getAbsolutePath(fileName);
-	_fileNameHash = Common::hashit(fileName);
 }
 
 void Resource::release() {

Modified: scummvm/trunk/engines/sword25/kernel/resource.h
===================================================================
--- scummvm/trunk/engines/sword25/kernel/resource.h	2010-10-24 01:52:27 UTC (rev 53758)
+++ scummvm/trunk/engines/sword25/kernel/resource.h	2010-10-24 01:53:01 UTC (rev 53759)
@@ -89,13 +89,6 @@
 	}
 
 	/**
-	 * Returns the hash of the filename of a resource
-	*/
-	uint getFileNameHash() const {
-		return _fileNameHash;
-	}
-
-	/**
 	 * Returns a resource's type
 	 */
 	uint getType() const {
@@ -107,7 +100,6 @@
 
 private:
 	Common::String _fileName;          ///< The absolute filename
-	uint _fileNameHash;      ///< The hash value of the filename
 	uint _refCount;          ///< The number of locks
 	uint _type;              ///< The type of the resource
 	Common::List<Resource *>::iterator _iterator;        ///< Points to the resource position in the LRU list


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