1 /* Keeping a unique copy of strings.
2 Copyright (C) 2002 Free Software Foundation, Inc.
4 This file is part of Bison, the GNU Compiler Compiler.
6 Bison is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 Bison is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with Bison; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
29 /*-----------------------.
30 | A uniqstr hash table. |
31 `-----------------------*/
33 /* Initial capacity of uniqstr hash table. */
34 #define HT_INITIAL_CAPACITY 257
36 static struct hash_table
*uniqstrs_table
= NULL
;
38 /*-------------------------------------.
39 | Create the uniqstr for S if needed. |
40 `-------------------------------------*/
43 uniqstr_new (const char *s
)
45 uniqstr res
= hash_lookup (uniqstrs_table
, s
);
48 /* First insertion in the hash. */
50 hash_insert (uniqstrs_table
, res
);
56 /*------------------------------.
57 | Abort if S is not a uniqstr. |
58 `------------------------------*/
61 uniqstr_assert (const char *s
)
63 if (!hash_lookup (uniqstrs_table
, s
))
65 error (0, 0, "not a uniqstr: %s", quotearg (s
));
71 /*--------------------.
72 | Print the uniqstr. |
73 `--------------------*/
76 uniqstr_print (const uniqstr s
)
78 fprintf (stderr
, "%s\n", s
);
83 /*-----------------------.
84 | A uniqstr hash table. |
85 `-----------------------*/
88 hash_compare_uniqstr (const uniqstr m1
, const uniqstr m2
)
90 return strcmp (m1
, m2
) == 0;
94 hash_uniqstr (const uniqstr m
, unsigned int tablesize
)
96 return hash_string (m
, tablesize
);
99 /* A function to apply to each symbol. */
100 typedef bool (*uniqstr_processor
) (const uniqstr
);
102 /*----------------------------.
103 | Create the uniqstrs table. |
104 `----------------------------*/
109 uniqstrs_table
= hash_initialize (HT_INITIAL_CAPACITY
,
111 (Hash_hasher
) hash_uniqstr
,
112 (Hash_comparator
) hash_compare_uniqstr
,
113 (Hash_data_freer
) free
);
117 /*-------------------------------------.
118 | Perform a task on all the uniqstrs. |
119 `-------------------------------------*/
122 uniqstrs_do (uniqstr_processor processor
, void *processor_data
)
124 hash_do_for_each (uniqstrs_table
,
125 (Hash_processor
) processor
,
135 uniqstrs_print (void)
137 uniqstrs_do (uniqstr_print
, NULL
);
141 /*--------------------.
142 | Free the uniqstrs. |
143 `--------------------*/
148 hash_free (uniqstrs_table
);