--- deliantra/server/common/arch.C 2006/12/13 00:42:03 1.30 +++ deliantra/server/common/arch.C 2007/02/02 22:07:51 1.39 @@ -1,25 +1,26 @@ /* - CrossFire, A Multiplayer game for X-windows - - Copyright (C) 2002 Mark Wedel & Crossfire Development Team - Copyright (C) 1992 Frank Tore Johansen - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - - The authors can be reached via e-mail at -*/ + * CrossFire, A Multiplayer game for X-windows + * + * Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team + * Copyright (C) 2002 Mark Wedel & Crossfire Development Team + * Copyright (C) 1992 Frank Tore Johansen + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + * The authors can be reached via e-mail at + */ #include @@ -27,15 +28,9 @@ #include #include -#define USE_UNORDERED_MAP 0 - -#if USE_UNORDERED_MAP -# include -# include -#endif +#include +#include -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 @@ -46,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 @@ -77,17 +67,13 @@ archetype * find_archetype_by_object_name (const char *name) { - archetype *at; + shstr_cmp name_cmp (name); - if (name == NULL) - return (archetype *) NULL; + for (archetype *at = first_archetype; at; at = at->next) + if (at->clone.name == name_cmp) + return at; - for (at = first_archetype; at != NULL; at = at->next) - { - if (!strcmp (at->clone.name, name)) - return at; - } - return NULL; + return 0; } /** @@ -98,18 +84,13 @@ archetype * find_archetype_by_object_type_name (int type, const char *name) { - archetype *at; + shstr_cmp name_cmp (name); - if (name == NULL) - return NULL; - - for (at = first_archetype; at != NULL; at = at->next) - { - if (at->clone.type == type && strcmp (at->clone.name, name) == 0) - return at; - } + for (archetype *at = first_archetype; at; at = at->next) + if (at->clone.name == name_cmp && at->clone.type == type) + return at; - return NULL; + return 0; } /* This is a lot like the above function. Instead, we are trying to match @@ -120,18 +101,13 @@ object * get_archetype_by_skill_name (const char *skill, int type) { - archetype * - at; + shstr_cmp skill_cmp (skill); - if (skill == NULL) - return NULL; + for (archetype *at = first_archetype; at; at = at->next) + if (at->clone.skill == skill_cmp && (type == -1 || type == at->clone.type)) + return arch_to_object (at); - for (at = first_archetype; at != NULL; at = at->next) - { - if (((type == -1) || (type == at->clone.type)) && (!strcmp (at->clone.skill, skill))) - return arch_to_object (at); - } - return NULL; + return 0; } /* similiar to above - this returns the first archetype @@ -144,15 +120,11 @@ archetype * get_archetype_by_type_subtype (int type, int subtype) { - archetype * - at; + for (archetype *at = first_archetype; at; at = at->next) + if ((type == -1 || type == at->clone.type) && (subtype == -1 || subtype == at->clone.subtype)) + return at; - for (at = first_archetype; at != NULL; at = at->next) - { - if (((type == -1) || (type == at->clone.type)) && (subtype == -1 || subtype == at->clone.subtype)) - return at; - } - return NULL; + return 0; } /** @@ -170,7 +142,6 @@ object * get_archetype_by_object_name (const char *name) { - archetype *at; char tmpname[MAX_BUF]; int i; @@ -179,9 +150,8 @@ for (i = strlen (tmpname); i > 0; i--) { tmpname[i] = 0; - at = find_archetype_by_object_name (tmpname); - if (at != NULL) + if (archetype *at = find_archetype_by_object_name (tmpname)) return arch_to_object (at); } @@ -327,31 +297,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) { @@ -359,7 +306,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"); @@ -371,7 +318,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; @@ -389,7 +336,6 @@ archetype::archetype () { - clear_object (&clone); /* to initial state other also */ CLEAR_FLAG (&clone, FLAG_FREED); /* This shouldn't matter, since copy_to */ SET_FLAG (&clone, FLAG_REMOVED); /* doesn't copy these flags... */ } @@ -563,7 +509,6 @@ object_thawer thawer (filename); - clear_archetable (); LOG (llevDebug, " arch-pass 1...\n"); first_arch_pass (thawer); LOG (llevDebug, " done\n"); @@ -578,7 +523,8 @@ LOG (llevDebug, " loading treasure...\n"); load_treasures (); - LOG (llevDebug, " done\n arch-pass 2...\n"); + LOG (llevDebug, " done\n"); + LOG (llevDebug, " arch-pass 2...\n"); second_arch_pass (thawer); LOG (llevDebug, " done\n"); #ifdef DEBUG @@ -592,7 +538,6 @@ * Creates and returns a new object which is a copy of the given archetype. * This function returns NULL on failure. */ - object * arch_to_object (archetype *at) { @@ -618,7 +563,6 @@ * Thus get_archetype() will be guaranteed to always return * an object, and never NULL. */ - object * create_singularity (const char *name) { @@ -636,7 +580,6 @@ * Finds which archetype matches the given name, and returns a new * object containing a copy of the archetype. */ - object * get_archetype (const char *name) { @@ -682,47 +625,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; - } } /* @@ -731,60 +645,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) {