1 |
/* Keeping a unique copy of strings. |
/* Keep a unique copy of strings. |
2 |
|
|
3 |
Copyright (C) 2002 Free Software Foundation, Inc. |
Copyright (C) 2002 Free Software Foundation, Inc. |
4 |
|
|
5 |
This file is part of Bison, the GNU Compiler Compiler. |
This file is part of Bison, the GNU Compiler Compiler. |
41 |
`-------------------------------------*/ |
`-------------------------------------*/ |
42 |
|
|
43 |
uniqstr |
uniqstr |
44 |
uniqstr_new (const char *s) |
uniqstr_new (char const *s) |
45 |
{ |
{ |
46 |
uniqstr res = hash_lookup (uniqstrs_table, s); |
uniqstr res = hash_lookup (uniqstrs_table, s); |
47 |
if (!res) |
if (!res) |
59 |
`------------------------------*/ |
`------------------------------*/ |
60 |
|
|
61 |
void |
void |
62 |
uniqstr_assert (const char *s) |
uniqstr_assert (char const *s) |
63 |
{ |
{ |
64 |
if (!hash_lookup (uniqstrs_table, s)) |
if (!hash_lookup (uniqstrs_table, s)) |
65 |
{ |
{ |
73 |
| Print the uniqstr. | |
| Print the uniqstr. | |
74 |
`--------------------*/ |
`--------------------*/ |
75 |
|
|
76 |
static bool |
static inline bool |
77 |
uniqstr_print (const uniqstr s) |
uniqstr_print (uniqstr s) |
78 |
{ |
{ |
79 |
fprintf (stderr, "%s\n", s); |
fprintf (stderr, "%s\n", s); |
80 |
return true; |
return true; |
81 |
} |
} |
82 |
|
|
83 |
|
static bool |
84 |
|
uniqstr_print_processor (void *s, void *null ATTRIBUTE_UNUSED) |
85 |
|
{ |
86 |
|
return uniqstr_print (s); |
87 |
|
} |
88 |
|
|
89 |
|
|
90 |
/*-----------------------. |
/*-----------------------. |
91 |
| A uniqstr hash table. | |
| A uniqstr hash table. | |
92 |
`-----------------------*/ |
`-----------------------*/ |
93 |
|
|
94 |
static bool |
static bool |
95 |
hash_compare_uniqstr (const uniqstr m1, const uniqstr m2) |
hash_compare_uniqstr (void const *m1, void const *m2) |
96 |
{ |
{ |
97 |
return strcmp (m1, m2) == 0; |
return strcmp (m1, m2) == 0; |
98 |
} |
} |
99 |
|
|
100 |
static unsigned int |
static unsigned int |
101 |
hash_uniqstr (const uniqstr m, unsigned int tablesize) |
hash_uniqstr (void const *m, unsigned int tablesize) |
102 |
{ |
{ |
103 |
return hash_string (m, tablesize); |
return hash_string (m, tablesize); |
104 |
} |
} |
105 |
|
|
|
/* A function to apply to each symbol. */ |
|
|
typedef bool (*uniqstr_processor) (const uniqstr); |
|
|
|
|
106 |
/*----------------------------. |
/*----------------------------. |
107 |
| Create the uniqstrs table. | |
| Create the uniqstrs table. | |
108 |
`----------------------------*/ |
`----------------------------*/ |
112 |
{ |
{ |
113 |
uniqstrs_table = hash_initialize (HT_INITIAL_CAPACITY, |
uniqstrs_table = hash_initialize (HT_INITIAL_CAPACITY, |
114 |
NULL, |
NULL, |
115 |
(Hash_hasher) hash_uniqstr, |
hash_uniqstr, |
116 |
(Hash_comparator) hash_compare_uniqstr, |
hash_compare_uniqstr, |
117 |
(Hash_data_freer) free); |
free); |
118 |
} |
} |
119 |
|
|
120 |
|
|
123 |
`-------------------------------------*/ |
`-------------------------------------*/ |
124 |
|
|
125 |
static void |
static void |
126 |
uniqstrs_do (uniqstr_processor processor, void *processor_data) |
uniqstrs_do (Hash_processor processor, void *processor_data) |
127 |
{ |
{ |
128 |
hash_do_for_each (uniqstrs_table, |
hash_do_for_each (uniqstrs_table, processor, processor_data); |
|
(Hash_processor) processor, |
|
|
processor_data); |
|
129 |
} |
} |
130 |
|
|
131 |
|
|
136 |
void |
void |
137 |
uniqstrs_print (void) |
uniqstrs_print (void) |
138 |
{ |
{ |
139 |
uniqstrs_do (uniqstr_print, NULL); |
uniqstrs_do (uniqstr_print_processor, NULL); |
140 |
} |
} |
141 |
|
|
142 |
|
|