… | |
… | |
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 | |
38 | int arch_cmp = 0; /* How many strcmp's */ |
|
|
39 | int arch_search = 0; /* How many searches */ |
|
|
40 | int arch_init; /* True if doing arch initialization */ |
34 | int 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 |
52 | typedef std::tr1::unordered_map |
45 | typedef 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 | |
62 | static HT ht; |
54 | static HT ht (5000); |
63 | #else |
|
|
64 | static 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 | |
|
|
331 | void |
|
|
332 | arch_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 | |
|
|
342 | void |
|
|
343 | clear_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 | |
|
|
352 | void |
318 | void |
353 | init_archetable (void) |
319 | init_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 | |
… | |
… | |
366 | free_all_archs (void) |
332 | free_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 | |
|
|
680 | archetype * |
644 | archetype * |
681 | archetype::find (const char *name) |
645 | archetype::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 | */ |
725 | void |
661 | void |
726 | archetype::hash_add () |
662 | archetype::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 | |
751 | void |
667 | void |
752 | archetype::hash_del () |
668 | archetype::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 | |
|
|
782 | archetype * |
677 | archetype * |
783 | type_to_archetype (int type) |
678 | type_to_archetype (int type) |
784 | { |
679 | { |
785 | archetype *at; |
680 | archetype *at; |
786 | |
681 | |