--- deliantra/server/common/arch.C 2007/01/15 21:06:18 1.37 +++ deliantra/server/common/arch.C 2007/02/02 21:50:43 1.38 @@ -28,15 +28,9 @@ #include #include -#define USE_UNORDERED_MAP 0 +#include +#include -#if USE_UNORDERED_MAP -# include -# include -#endif - -int arch_cmp = 0; /* How many strcmp's */ -int arch_search = 0; /* How many searches */ int arch_init; /* True if doing arch initialization */ /* The naming of these functions is really poor - they are all @@ -47,22 +41,17 @@ * MSW 2003-04-29 */ -#if USE_UNORDERED_MAP // the hashtable typedef std::tr1::unordered_map < - std::size_t, + const char *, arch_ptr, - std::tr1::hash, - std::equal_to, - slice_allocator< std::pair > - true, + str_hash, + str_equal, + slice_allocator< std::pair > > HT; -static HT ht; -#else -static arch_ptr arch_table[ARCHTABLE]; -#endif +static HT ht (5000); /** * GROS - This function retrieves an archetype given the name that appears @@ -324,31 +313,8 @@ } /* - * Stores debug-information about how efficient the hashtable - * used for archetypes has been in the static errmsg array. - */ - -void -arch_info (object *op) -{ - sprintf (errmsg, "%d searches and %d strcmp()'s", arch_search, arch_cmp); - new_draw_info (NDI_BLACK, 0, op, errmsg); -} - -/* - * Initialise the hashtable used by the archetypes. - */ - -void -clear_archetable (void) -{ - memset ((void *) arch_table, 0, ARCHTABLE * sizeof (archetype *)); -} - -/* * An alternative way to init the hashtable which is slower, but _works_... */ - void init_archetable (void) { @@ -356,7 +322,7 @@ LOG (llevDebug, " Setting up archetable...\n"); - for (at = first_archetype; at != NULL; at = (at->more == NULL) ? at->next : at->more) + for (at = first_archetype; at; at = at->more ? at->more : at->next) at->hash_add (); LOG (llevDebug, "done\n"); @@ -368,7 +334,7 @@ archetype *at, *next; int i = 0, f = 0; - for (at = first_archetype; at != NULL; at = next) + for (at = first_archetype; at; at = next) { if (at->more) next = at->more; @@ -559,7 +525,6 @@ object_thawer thawer (filename); - clear_archetable (); LOG (llevDebug, " arch-pass 1...\n"); first_arch_pass (thawer); LOG (llevDebug, " done\n"); @@ -676,47 +641,18 @@ * Finds, using the hashtable, which archetype matches the given name. * returns a pointer to the found archetype, otherwise NULL. */ - archetype * archetype::find (const char *name) { if (!name) return 0; -#if USE_UNORDERED_MAP - AUTODECL (i, ht.find ((size_t) name)); + AUTODECL (i, ht.find (name)); if (i == ht.end ()) return 0; else return i->second; -#endif - - archetype *at; - unsigned long index; - - index = hasharch (name, ARCHTABLE); - arch_search++; - for (;;) - { - at = arch_table[index]; - - if (at == NULL) - { - if (warn_archetypes) - LOG (llevError, "Couldn't find archetype %s\n", name); - - return NULL; - } - - arch_cmp++; - - if (!strcmp ((const char *) at->name, name)) - return at; - - if (++index >= ARCHTABLE) - index = 0; - } } /* @@ -725,60 +661,19 @@ void archetype::hash_add () { -#if USE_UNORDERED_MAP - ht.insert (std::make_pair ((size_t) (const char *) name, this)); -#else - - int index = hasharch ((const char *) name, ARCHTABLE), org_index = index; - - for (;;) - { - if (!arch_table[index]) - { - arch_table[index] = this; - break; - } - - if (++index == ARCHTABLE) - index = 0; - - if (index == org_index) - fatal (ARCHTABLE_TOO_SMALL); - } -#endif + ht.insert (std::make_pair (name, this)); } void archetype::hash_del () { -#if USE_UNORDERED_MAP -# error remove this from HT -#else - - int index = hasharch ((const char *) name, ARCHTABLE), org_index = index; - - for (;;) - { - if (arch_table[index] == this) - { - arch_table[index] = 0; - break; - } - - if (++index == ARCHTABLE) - index = 0; - - if (index == org_index) - break; - } -#endif + ht.erase (name); } /* * Returns the first archetype using the given type. * Used in treasure-generation. */ - archetype * type_to_archetype (int type) {