1 /* Copyright (C) 2003,2004,2005,2006 MORIOKA Tomohiko
2 This file is part of the CONCORD Library.
4 The CONCORD Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The CONCORD Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the CONCORD Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 #include "concord-name.h"
23 struct CONCORD_HASH_TABLE_ENTRY
29 struct CONCORD_HASH_TABLE
32 CONCORD_HASH_TABLE_ENTRY *data;
35 CONCORD_HASH_TABLE* concord_make_hash_table (size_t size);
36 void concord_destroy_hash_table (CONCORD_HASH_TABLE* hash);
37 int concord_hash_c_string (const unsigned char *ptr);
40 concord_make_hash_table (size_t size)
42 CONCORD_HASH_TABLE* hash
43 = (CONCORD_HASH_TABLE*)malloc (sizeof (CONCORD_HASH_TABLE));
49 = (CONCORD_HASH_TABLE_ENTRY*) malloc (sizeof (CONCORD_HASH_TABLE_ENTRY)
51 if (hash->data == NULL)
58 memset (hash->data, 0, sizeof (CONCORD_HASH_TABLE_ENTRY) * size);
63 concord_destroy_hash_table (CONCORD_HASH_TABLE* table)
72 /* derived from hashpjw, Dragon Book P436. */
74 concord_hash_c_string (const unsigned char *ptr)
81 hash = (hash << 4) + *ptr++;
82 g = hash & 0xf0000000;
84 hash = (hash ^ (g >> 24)) ^ g;
86 return hash & 07777777777;
91 concord_make_name_table ()
93 return concord_make_hash_table (256);
97 concord_destroy_name_table (CONCORD_NAME_TABLE* table)
101 for (i = 0; i < table->size; i++)
103 CONCORD_NAME_TABLE_ENTRY entry = table->data[i];
105 if (entry.key != NULL)
107 if (entry.value != NULL)
112 concord_destroy_hash_table (table);
116 concord_name_table_put (CONCORD_NAME_TABLE* table,
117 const char *key, void *value)
120 CONCORD_NAME_TABLE_ENTRY* entry;
125 index = concord_hash_c_string ((unsigned char*)key) % table->size;
126 for (i = index; i < table->size; i++)
128 entry = &table->data[i];
129 if (entry->key == NULL)
131 size_t len = strlen (key);
133 entry->key = (unsigned char*)malloc (len + 1);
134 if (entry->key == NULL)
136 strcpy (entry->key, key);
137 entry->value = value;
140 else if (strcmp (entry->key, key) == 0)
142 entry->value = value;
146 if (concord_name_table_grow (table) == 0)
147 return concord_name_table_put (table, key, value);
152 concord_name_table_get (CONCORD_NAME_TABLE* table, const char *key)
155 CONCORD_NAME_TABLE_ENTRY entry;
160 index = concord_hash_c_string ((unsigned char*)key) % table->size;
161 for (i = index; i < table->size; i++)
163 entry = table->data[i];
164 if (entry.key == NULL)
166 else if (strcmp (entry.key, key) == 0)
173 concord_name_table_grow (CONCORD_NAME_TABLE* table)
175 CONCORD_NAME_TABLE *new_table
176 = concord_make_hash_table ( table->size * 2
177 /* - (table->size * 4 / 5) */ );
180 if (new_table == NULL)
183 for (i = 0; i < table->size; i++)
185 CONCORD_NAME_TABLE_ENTRY entry = table->data[i];
186 if ( (entry.key != NULL) && (entry.value != NULL) )
189 = concord_name_table_put (new_table, entry.key, entry.value);
192 concord_destroy_hash_table (new_table);
197 table->size = new_table->size;
198 table->data = new_table->data;