1 /* Copyright (C) 2003,2004,2005,2006,2013 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
23 unsigned long concord_hash_c_string (const unsigned char *ptr);
24 unsigned long cos_hash_c_string_n (const unsigned char *ptr, size_t size);
25 unsigned long cos_symbol_hash_string (COS_String string);
28 COS_String_ent cos_string_ent_nil = { {COS_OBJECT_PREFIX_OBJECT,
29 COS_Object_Type_String,
33 COS_Symbol_ent cos_symbol_ent_nil = { {COS_OBJECT_PREFIX_OBJECT,
34 COS_Object_Type_Symbol,
39 COS_Symbol cos_Qnil = &cos_symbol_ent_nil;
42 COS_String_ent cos_string_ent_t = { {COS_OBJECT_PREFIX_OBJECT,
43 COS_Object_Type_String,
47 COS_Symbol_ent cos_symbol_ent_t = { {COS_OBJECT_PREFIX_OBJECT,
48 COS_Object_Type_Symbol,
53 COS_Symbol cos_Qt = &cos_symbol_ent_t;
56 COS_Symbol_Table cos_default_symbol_table = NULL;
60 cos_make_symbol (COS_String string)
62 COS_Symbol obj = COS_ALLOCATE_OBJECT (Symbol);
69 cos_retain_object (string);
74 cos_retain_symbol (COS_Object obj)
76 //cos_retain_object (((COS_Symbol)obj)->value);
77 //cos_retain_object (((COS_Symbol)obj)->name);
82 cos_release_symbol (COS_Object obj)
87 if ( ((COS_Symbol)obj)->value != NULL)
88 cos_release_object (((COS_Symbol)obj)->value);
90 cos_release_object (((COS_Symbol)obj)->name);
96 cos_symbol_p (COS_object obj)
98 return COS_OBJECT_SYMBOL_P (obj);
102 cos_symbol_table_intern (COS_Symbol_Table table, COS_object name)
105 COS_String key_string;
106 unsigned long i, index;
112 if (COS_OBJECT_C_STRING_P (name))
114 key_string = cos_build_string ((char*)name);
118 key_string = (COS_String)name;
120 key = key_string->data;
121 index = cos_symbol_hash_string (key_string) % table->size;
122 for (i = index; i < table->size; i++)
124 entry = table->data[i];
127 entry = cos_make_symbol (key_string);
128 cos_retain_object ((COS_object)entry);
129 table->data[i] = entry;
132 else if ( COS_OBJECT_SYMBOL_P (entry)
133 && COS_OBJECT_STRING_P (entry->name)
134 && (entry->name->size == key_string->size)
135 && (memcmp (entry->name->data,
136 key_string->data, key_string->size) == 0) )
138 cos_retain_object ((COS_object)entry);
142 if (cos_symbol_table_grow (table) == 0)
143 return cos_symbol_table_intern (table, key_string);
148 cos_symbol_name (COS_Symbol symbol)
154 COS_Symbol_Table cos_make_symbol_table_0 (size_t size);
155 void cos_destroy_symbol_table_0 (COS_Symbol_Table hash);
158 /* derived from hashpjw, Dragon Book P436. */
160 cos_hash_c_string_n (const unsigned char *ptr, size_t size)
162 unsigned long hash = 0;
165 for ( i = 0; i < size; i++ )
168 hash = (hash << 4) + ptr[i];
169 g = hash & 0xf0000000;
171 hash = (hash ^ (g >> 24)) ^ g;
173 return hash & 07777777777;
177 cos_symbol_hash_string (COS_String string)
179 return cos_hash_c_string_n (string->data, string->size);
184 cos_make_symbol_table_0 (size_t size)
186 COS_Symbol_Table hash
187 = (COS_Symbol_Table)malloc (sizeof (COS_Symbol_Table_ent));
193 = (COS_Symbol*) malloc (sizeof (COS_Symbol) * size);
194 if (hash->data == NULL)
201 memset (hash->data, 0, sizeof (COS_Symbol) * size);
206 cos_destroy_symbol_table_0 (COS_Symbol_Table table)
216 cos_make_symbol_table ()
218 return cos_make_symbol_table_0 (2 /* 256 */);
222 cos_destroy_symbol_table (COS_Symbol_Table table)
226 for (i = 0; i < table->size; i++)
228 COS_Symbol entry = table->data[i];
232 if (entry->name != NULL)
233 cos_release_object (entry->name);
234 if (entry->value != NULL)
235 cos_release_object (entry->value);
239 cos_destroy_symbol_table_0 (table);
243 cos_intern (COS_object name)
245 if (cos_default_symbol_table == NULL)
247 cos_default_symbol_table = cos_make_symbol_table();
248 cos_symbol_table_set (cos_default_symbol_table, cos_Qnil);
249 cos_symbol_table_set (cos_default_symbol_table, cos_Qt);
251 return cos_symbol_table_intern (cos_default_symbol_table, name);
255 cos_symbol_table_set (COS_Symbol_Table table, COS_Symbol symbol)
263 index = cos_symbol_hash_string (symbol->name) % table->size;
264 for (i = index; i < table->size; i++)
266 entry = table->data[i];
270 cos_retain_object ((COS_object)entry);
271 table->data[i] = entry;
274 else if (entry == symbol)
279 if (cos_symbol_table_grow (table) == 0)
280 return cos_symbol_table_set (table, symbol);
285 cos_print_symbol_table (COS_Symbol_Table table)
291 table = cos_default_symbol_table;
293 printf ("#[symbol_table %lX\tsize = %d", table->size);
294 for (i = 0; i < table->size; i++)
296 entry = table->data[i];
297 printf ("\n\t%d : ", i);
298 cos_print_object (entry);
305 cos_symbol_table_grow (COS_Symbol_Table table)
307 COS_Symbol_Table new_table
308 = cos_make_symbol_table_0 ( table->size * 2
309 /* - (table->size * 4 / 5) */ );
312 if (new_table == NULL)
315 //printf ("\n(old table is ");
316 //cos_print_symbol_table (table);
317 for (i = 0; i < table->size; i++)
319 COS_Symbol entry = table->data[i];
323 int status = cos_symbol_table_set (new_table, entry);
327 cos_destroy_symbol_table_0 (new_table);
333 table->size = new_table->size;
334 table->data = new_table->data;
336 //printf ("\t)\n(new table is ");
337 //cos_print_symbol_table (table);