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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines