Factor our hash function used for XRC ids hash map.
authorVadim Zeitlin <vadim@wxwidgets.org>
Sun, 7 Nov 2010 19:34:05 +0000 (19:34 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Sun, 7 Nov 2010 19:34:05 +0000 (19:34 +0000)
Define the hash function in a separate function instead of duplicating it in
XRCID_Lookup() and RemoveXRCIDEntry().

The hash function is extremely simplistic and inefficient right now, it should
be replaced with wxStringHash::stringHash().

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@66065 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

src/xrc/xmlres.cpp

index 57f57de8aa5c7836ff70ed200128d84b85d79ad4..c6683a31788afaa7528b4308bc4c344542829d98 100644 (file)
@@ -2409,13 +2409,23 @@ struct XRCID_record
 
 static XRCID_record *XRCID_Records[XRCID_TABLE_SIZE] = {NULL};
 
-static int XRCID_Lookup(const char *str_id, int value_if_not_found = wxID_NONE)
+// Extremely simplistic hash function which probably ought to be replaced with
+// wxStringHash::stringHash().
+static inline unsigned XRCIdHash(const char *str_id)
 {
-    unsigned int index = 0;
+    unsigned index = 0;
 
     for (const char *c = str_id; *c != '\0'; c++) index += (unsigned int)*c;
     index %= XRCID_TABLE_SIZE;
 
+    return index;
+}
+
+static int XRCID_Lookup(const char *str_id, int value_if_not_found = wxID_NONE)
+{
+    const unsigned index = XRCIdHash(str_id);
+
+
     XRCID_record *oldrec = NULL;
     for (XRCID_record *rec = XRCID_Records[index]; rec; rec = rec->next)
     {
@@ -2619,10 +2629,7 @@ wxString wxXmlResource::FindXRCIDById(int numId)
 /* static */
 void wxIdRangeManager::RemoveXRCIDEntry(const char *str_id)
 {
-    int index = 0;
-
-    for (const char *c = str_id; *c != '\0'; c++) index += (int)*c;
-    index %= XRCID_TABLE_SIZE;
+    const unsigned index = XRCIdHash(str_id);
 
     XRCID_record **p_previousrec = &XRCID_Records[index];
     for (XRCID_record *rec = XRCID_Records[index]; rec; rec = rec->next)