home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / GCC / GERLIB_DEV08B.LHA / gerlib / libg++ / gperf / src / hash-table.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-12  |  3.1 KB  |  88 lines

  1. /* Hash table for checking keyword links.  Implemented using double hashing.
  2.    Copyright (C) 1989 Free Software Foundation, Inc.
  3.    written by Douglas C. Schmidt (schmidt@ics.uci.edu)
  4.  
  5. This file is part of GNU GPERF.
  6.  
  7. GNU GPERF 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. the Free Software Foundation; either version 1, or (at your option)
  10. any later version.
  11.  
  12. GNU GPERF is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with GNU GPERF; see the file COPYING.  If not, write to
  19. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. #include <stdio.h>
  22. #include <std.h>
  23. #include <builtin.h>
  24. #include "hash-table.h"
  25. #include "options.h"
  26. #include "trace.h"
  27.  
  28. #define NIL(TYPE) (TYPE *)0
  29.  
  30. /* The size of the hash table is always the smallest power of 2 >= the size
  31.    indicated by the user.  This allows several optimizations, including
  32.    the use of double hashing and elimination of the mod instruction.
  33.    Note that the size had better be larger than the number of items
  34.    in the hash table, else there's trouble!!!  Note that the memory
  35.    for the hash table is allocated *outside* the intialization routine.
  36.    This compromises information hiding somewhat, but greatly reduces
  37.    memory fragmentation, since we can now use alloca! */
  38.  
  39. Hash_Table::Hash_Table (List_Node **table_ptr, int s): 
  40.      collisions (0), size (s), table (table_ptr)
  41. {
  42.   T (Trace t ("Hash_Table::Hash_Table");)
  43.   memset ((char *) table, 0, size * sizeof *table);
  44. }
  45.  
  46. Hash_Table::~Hash_Table (void) 
  47.   T (Trace t ("Hash_Table::~Hash_Table");)
  48.   if (option[DEBUG])
  49.     {
  50.       int field_width = option.get_max_keysig_size ();
  51.  
  52.       fprintf (stderr, "\ndumping the hash table\ntotal available table slots = %d, total bytes = %d, total collisions = %d\n"
  53.                "location, %*s, keyword\n", size, size * sizeof *table, collisions, field_width, "keysig");
  54.     
  55.       for (int i = size - 1; i >= 0; i--)
  56.         if (table[i])
  57.           fprintf (stderr, "%8d, %*s, %s\n",
  58.                    i, field_width, table[i]->char_set, table[i]->key);
  59.  
  60.       fprintf (stderr, "\nend dumping hash table\n\n",
  61.                collisions);
  62.     }
  63. }
  64.  
  65. /* If the ITEM is already in the hash table return the item found
  66.    in the table.  Otherwise inserts the ITEM, and returns FALSE.
  67.    Uses double hashing. */
  68.  
  69. List_Node *
  70. Hash_Table::operator() (List_Node *item, int ignore_length) 
  71. {
  72.   T (Trace t ("Hash_Table::operator()");)
  73.   unsigned hash_val  = hashpjw (item->char_set);
  74.   int      probe     = hash_val & size - 1;
  75.   int      increment = (hash_val ^ item->length | 1) & size - 1;
  76.   
  77.   while (table[probe]
  78.          && (strcmp (table[probe]->char_set, item->char_set)
  79.              || (!ignore_length && table[probe]->length != item->length)))
  80.     {
  81.       collisions++;
  82.       probe = probe + increment & size - 1;
  83.     }
  84.  
  85.   return table[probe] ? table[probe] : (table[probe] = item, NIL (List_Node));
  86. }
  87.