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