1 |
/* Macro table manager for Bison, |
/* Macro table manager for Bison, |
2 |
Copyright 2001 Free Software Foundation, Inc. |
Copyright (C) 2001, 2002 Free Software Foundation, Inc. |
3 |
|
|
4 |
This file is part of Bison, the GNU Compiler Compiler. |
This file is part of Bison, the GNU Compiler Compiler. |
5 |
|
|
24 |
#include "muscle_tab.h" |
#include "muscle_tab.h" |
25 |
#include "getargs.h" |
#include "getargs.h" |
26 |
|
|
27 |
struct hash_table muscle_table; |
/* Initial capacity of muscles hash table. */ |
28 |
|
#define HT_INITIAL_CAPACITY 257 |
29 |
|
|
30 |
static unsigned long |
struct hash_table *muscle_table = NULL; |
|
mhash1 (const void *item) |
|
|
{ |
|
|
return_STRING_HASH_1 (((const muscle_entry_t *) item)->key); |
|
|
} |
|
31 |
|
|
32 |
static unsigned long |
static bool |
33 |
mhash2 (const void *item) |
hash_compare_muscles (void const *x, void const *y) |
34 |
{ |
{ |
35 |
return_STRING_HASH_2 (((const muscle_entry_t *) item)->key); |
const muscle_entry_t *m1 = x; |
36 |
|
const muscle_entry_t *m2 = y; |
37 |
|
return strcmp (m1->key, m2->key) ? FALSE : TRUE; |
38 |
} |
} |
39 |
|
|
40 |
static int |
static unsigned int |
41 |
mcmp (const void *x, const void *y) |
hash_muscle (const void *x, unsigned int tablesize) |
42 |
{ |
{ |
43 |
return strcmp (((const muscle_entry_t*) x)->key, |
const muscle_entry_t *m = x; |
44 |
((const muscle_entry_t *) y)->key); |
return hash_string (m->key, tablesize); |
45 |
} |
} |
46 |
|
|
47 |
void |
void |
48 |
muscle_init (void) |
muscle_init (void) |
49 |
{ |
{ |
50 |
hash_init (&muscle_table, MTABSIZE, &mhash1, &mhash2, &mcmp); |
muscle_table = hash_initialize (HT_INITIAL_CAPACITY, NULL, hash_muscle, |
51 |
|
hash_compare_muscles, NULL); |
52 |
|
|
53 |
/* Version and input file. */ |
/* Version and input file. */ |
54 |
muscle_insert ("version", VERSION); |
muscle_insert ("version", VERSION); |
72 |
void |
void |
73 |
muscle_insert (const char *key, const char *value) |
muscle_insert (const char *key, const char *value) |
74 |
{ |
{ |
75 |
muscle_entry_t *pair = XMALLOC (muscle_entry_t, 1); |
muscle_entry_t pair = { key, NULL }; |
76 |
pair->key = key; |
muscle_entry_t *entry = hash_lookup (muscle_table, &pair); |
77 |
pair->value = value; |
|
78 |
hash_insert (&muscle_table, pair); |
if (!entry) |
79 |
|
{ |
80 |
|
/* First insertion in the hash. */ |
81 |
|
entry = XMALLOC (muscle_entry_t, 1); |
82 |
|
entry->key = key; |
83 |
|
hash_insert (muscle_table, entry); |
84 |
|
} |
85 |
|
entry->value = value; |
86 |
} |
} |
87 |
|
|
88 |
const char* |
const char* |
89 |
muscle_find (const char *key) |
muscle_find (const char *key) |
90 |
{ |
{ |
91 |
muscle_entry_t pair = { key, 0 }; |
muscle_entry_t pair = { key, NULL }; |
92 |
muscle_entry_t *result = hash_find_item (&muscle_table, &pair); |
muscle_entry_t *result = hash_lookup (muscle_table, &pair); |
93 |
return result ? result->value : 0; |
return result ? result->value : NULL; |
94 |
} |
} |