ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/arch.C
(Generate patch)

Comparing deliantra/server/common/arch.C (file contents):
Revision 1.35 by root, Wed Jan 3 20:32:13 2007 UTC vs.
Revision 1.38 by root, Fri Feb 2 21:50:43 2007 UTC

1/* 1/*
2 CrossFire, A Multiplayer game for X-windows 2 * CrossFire, A Multiplayer game for X-windows
3 3 *
4 * Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team
4 Copyright (C) 2002 Mark Wedel & Crossfire Development Team 5 * Copyright (C) 2002 Mark Wedel & Crossfire Development Team
5 Copyright (C) 1992 Frank Tore Johansen 6 * Copyright (C) 1992 Frank Tore Johansen
6 7 *
7 This program is free software; you can redistribute it and/or modify 8 * This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by 9 * it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or 10 * the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version. 11 * (at your option) any later version.
11 12 *
12 This program is distributed in the hope that it will be useful, 13 * This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details. 16 * GNU General Public License for more details.
16 17 *
17 You should have received a copy of the GNU General Public License 18 * You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software 19 * along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 21 *
21 The authors can be reached via e-mail at <crossfire@schmorp.de> 22 * The authors can be reached via e-mail at <crossfire@schmorp.de>
22*/ 23 */
23 24
24#include <cassert> 25#include <cassert>
25 26
26#include <global.h> 27#include <global.h>
27#include <funcpoint.h> 28#include <funcpoint.h>
28#include <loader.h> 29#include <loader.h>
29 30
30#define USE_UNORDERED_MAP 0
31
32#if USE_UNORDERED_MAP
33# include <tr1/functional> 31#include <tr1/functional>
34# include <tr1/unordered_map> 32#include <tr1/unordered_map>
35#endif
36 33
37int arch_cmp = 0; /* How many strcmp's */
38int arch_search = 0; /* How many searches */
39int arch_init; /* True if doing arch initialization */ 34int arch_init; /* True if doing arch initialization */
40 35
41/* The naming of these functions is really poor - they are all 36/* The naming of these functions is really poor - they are all
42 * pretty much named '.._arch_...', but they may more may not 37 * pretty much named '.._arch_...', but they may more may not
43 * return archetypes. Some make the arch_to_object call, and thus 38 * return archetypes. Some make the arch_to_object call, and thus
44 * return an object. Perhaps those should be called 'archob' functions 39 * return an object. Perhaps those should be called 'archob' functions
45 * to denote they return an object derived from the archetype. 40 * to denote they return an object derived from the archetype.
46 * MSW 2003-04-29 41 * MSW 2003-04-29
47 */ 42 */
48 43
49#if USE_UNORDERED_MAP
50// the hashtable 44// the hashtable
51typedef std::tr1::unordered_map 45typedef std::tr1::unordered_map
52 < 46 <
53 std::size_t, 47 const char *,
54 arch_ptr, 48 arch_ptr,
55 std::tr1::hash<size_t>, 49 str_hash,
56 std::equal_to<size_t>, 50 str_equal,
57 slice_allocator< std::pair<const std::size_t, archetype *> > 51 slice_allocator< std::pair<const char *const, arch_ptr> >
58 true,
59 > HT; 52 > HT;
60 53
61static HT ht; 54static HT ht (5000);
62#else
63static arch_ptr arch_table[ARCHTABLE];
64#endif
65 55
66/** 56/**
67 * GROS - This function retrieves an archetype given the name that appears 57 * GROS - This function retrieves an archetype given the name that appears
68 * during the game (for example, "writing pen" instead of "stylus"). 58 * during the game (for example, "writing pen" instead of "stylus").
69 * It does not use the hashtable system, but browse the whole archlist each time. 59 * It does not use the hashtable system, but browse the whole archlist each time.
321 311
322/* init_blocksview();*/ 312/* init_blocksview();*/
323} 313}
324 314
325/* 315/*
326 * Stores debug-information about how efficient the hashtable
327 * used for archetypes has been in the static errmsg array.
328 */
329
330void
331arch_info (object *op)
332{
333 sprintf (errmsg, "%d searches and %d strcmp()'s", arch_search, arch_cmp);
334 new_draw_info (NDI_BLACK, 0, op, errmsg);
335}
336
337/*
338 * Initialise the hashtable used by the archetypes.
339 */
340
341void
342clear_archetable (void)
343{
344 memset ((void *) arch_table, 0, ARCHTABLE * sizeof (archetype *));
345}
346
347/*
348 * An alternative way to init the hashtable which is slower, but _works_... 316 * An alternative way to init the hashtable which is slower, but _works_...
349 */ 317 */
350
351void 318void
352init_archetable (void) 319init_archetable (void)
353{ 320{
354 archetype *at; 321 archetype *at;
355 322
356 LOG (llevDebug, " Setting up archetable...\n"); 323 LOG (llevDebug, " Setting up archetable...\n");
357 324
358 for (at = first_archetype; at != NULL; at = (at->more == NULL) ? at->next : at->more) 325 for (at = first_archetype; at; at = at->more ? at->more : at->next)
359 at->hash_add (); 326 at->hash_add ();
360 327
361 LOG (llevDebug, "done\n"); 328 LOG (llevDebug, "done\n");
362} 329}
363 330
365free_all_archs (void) 332free_all_archs (void)
366{ 333{
367 archetype *at, *next; 334 archetype *at, *next;
368 int i = 0, f = 0; 335 int i = 0, f = 0;
369 336
370 for (at = first_archetype; at != NULL; at = next) 337 for (at = first_archetype; at; at = next)
371 { 338 {
372 if (at->more) 339 if (at->more)
373 next = at->more; 340 next = at->more;
374 else 341 else
375 next = at->next; 342 next = at->next;
556 523
557 { 524 {
558 object_thawer 525 object_thawer
559 thawer (filename); 526 thawer (filename);
560 527
561 clear_archetable ();
562 LOG (llevDebug, " arch-pass 1...\n"); 528 LOG (llevDebug, " arch-pass 1...\n");
563 first_arch_pass (thawer); 529 first_arch_pass (thawer);
564 LOG (llevDebug, " done\n"); 530 LOG (llevDebug, " done\n");
565 } 531 }
566 532
673 639
674/* 640/*
675 * Finds, using the hashtable, which archetype matches the given name. 641 * Finds, using the hashtable, which archetype matches the given name.
676 * returns a pointer to the found archetype, otherwise NULL. 642 * returns a pointer to the found archetype, otherwise NULL.
677 */ 643 */
678
679archetype * 644archetype *
680archetype::find (const char *name) 645archetype::find (const char *name)
681{ 646{
682 if (!name) 647 if (!name)
683 return 0; 648 return 0;
684 649
685#if USE_UNORDERED_MAP
686 AUTODECL (i, ht.find ((size_t) name)); 650 AUTODECL (i, ht.find (name));
687 651
688 if (i == ht.end ()) 652 if (i == ht.end ())
689 return 0; 653 return 0;
690 else 654 else
691 return i->second; 655 return i->second;
692#endif
693
694 archetype *at;
695 unsigned long index;
696
697 index = hasharch (name, ARCHTABLE);
698 arch_search++;
699 for (;;)
700 {
701 at = arch_table[index];
702
703 if (at == NULL)
704 {
705 if (warn_archetypes)
706 LOG (llevError, "Couldn't find archetype %s\n", name);
707
708 return NULL;
709 }
710
711 arch_cmp++;
712
713 if (!strcmp ((const char *) at->name, name))
714 return at;
715
716 if (++index >= ARCHTABLE)
717 index = 0;
718 }
719} 656}
720 657
721/* 658/*
722 * Adds an archetype to the hashtable. 659 * Adds an archetype to the hashtable.
723 */ 660 */
724void 661void
725archetype::hash_add () 662archetype::hash_add ()
726{ 663{
727#if USE_UNORDERED_MAP
728 ht.insert (std::make_pair ((size_t) (const char *) name, this)); 664 ht.insert (std::make_pair (name, this));
729#else
730
731 int index = hasharch ((const char *) name, ARCHTABLE), org_index = index;
732
733 for (;;)
734 {
735 if (!arch_table[index])
736 {
737 arch_table[index] = this;
738 break;
739 }
740
741 if (++index == ARCHTABLE)
742 index = 0;
743
744 if (index == org_index)
745 fatal (ARCHTABLE_TOO_SMALL);
746 }
747#endif
748} 665}
749 666
750void 667void
751archetype::hash_del () 668archetype::hash_del ()
752{ 669{
753#if USE_UNORDERED_MAP 670 ht.erase (name);
754# error remove this from HT
755#else
756
757 int index = hasharch ((const char *) name, ARCHTABLE), org_index = index;
758
759 for (;;)
760 {
761 if (arch_table[index] == this)
762 {
763 arch_table[index] = 0;
764 break;
765 }
766
767 if (++index == ARCHTABLE)
768 index = 0;
769
770 if (index == org_index)
771 break;
772 }
773#endif
774} 671}
775 672
776/* 673/*
777 * Returns the first archetype using the given type. 674 * Returns the first archetype using the given type.
778 * Used in treasure-generation. 675 * Used in treasure-generation.
779 */ 676 */
780
781archetype * 677archetype *
782type_to_archetype (int type) 678type_to_archetype (int type)
783{ 679{
784 archetype *at; 680 archetype *at;
785 681

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines