From: Vadim Zeitlin Date: Sun, 7 Nov 2010 19:34:05 +0000 (+0000) Subject: Factor our hash function used for XRC ids hash map. X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/d807030e1eccc4ac1a7c434968fef654ea8bf38e Factor our hash function used for XRC ids hash map. 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 --- diff --git a/src/xrc/xmlres.cpp b/src/xrc/xmlres.cpp index 57f57de8aa..c6683a3178 100644 --- a/src/xrc/xmlres.cpp +++ b/src/xrc/xmlres.cpp @@ -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)