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. */
26 /*-----------------------.
27 | A struniq hash table. |
28 `-----------------------*/
30 /* Initial capacity of struniq hash table. */
31 #define HT_INITIAL_CAPACITY 257
33 static struct hash_table
*struniqs_table
= NULL
;
35 /*-------------------------------------.
36 | Create the struniq for S if needed. |
37 `-------------------------------------*/
40 struniq_new (const char *s
)
42 /* Keep the struniqs in a printable form. */
43 struniq_t res
= hash_lookup (struniqs_table
,
44 quotearg_style (escape_quoting_style
, s
));
48 /* First insertion in the hash. */
49 res
= xstrdup (quotearg_style (escape_quoting_style
, s
));
50 hash_insert (struniqs_table
, res
);
56 /*--------------------.
57 | Print the struniq. |
58 `--------------------*/
61 struniq_print (const struniq_t s
)
63 fprintf (stderr
, "%s\n", s
);
68 /*-----------------------.
69 | A struniq hash table. |
70 `-----------------------*/
73 hash_compare_struniq_t (const struniq_t m1
, const struniq_t m2
)
75 return strcmp (m1
, m2
) == 0;
79 hash_struniq_t (const struniq_t m
, unsigned int tablesize
)
81 return hash_string (m
, tablesize
);
84 /* A function to apply to each symbol. */
85 typedef bool (*struniq_processor_t
) (const struniq_t
);
87 /*----------------------------.
88 | Create the struniqs table. |
89 `----------------------------*/
94 struniqs_table
= hash_initialize (HT_INITIAL_CAPACITY
,
96 (Hash_hasher
) hash_struniq_t
,
97 (Hash_comparator
) hash_compare_struniq_t
,
98 (Hash_data_freer
) free
);
102 /*-------------------------------------.
103 | Perform a task on all the struniqs. |
104 `-------------------------------------*/
107 struniqs_do (struniq_processor_t processor
, void *processor_data
)
109 hash_do_for_each (struniqs_table
,
110 (Hash_processor
) processor
,
120 struniqs_print (void)
122 struniqs_do (struniq_print
, NULL
);
126 /*--------------------.
127 | Free the struniqs. |
128 `--------------------*/
133 hash_free (struniqs_table
);