]>
Commit | Line | Data |
---|---|---|
1 | /* Keeping a unique copy of strings. | |
2 | Copyright (C) 2002 Free Software Foundation, Inc. | |
3 | ||
4 | This file is part of Bison, the GNU Compiler Compiler. | |
5 | ||
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) | |
9 | any later version. | |
10 | ||
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. | |
15 | ||
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. */ | |
20 | ||
21 | #include "system.h" | |
22 | #include "quotearg.h" | |
23 | #include "hash.h" | |
24 | #include "struniq.h" | |
25 | ||
26 | /*-----------------------. | |
27 | | A struniq hash table. | | |
28 | `-----------------------*/ | |
29 | ||
30 | /* Initial capacity of struniq hash table. */ | |
31 | #define HT_INITIAL_CAPACITY 257 | |
32 | ||
33 | static struct hash_table *struniqs_table = NULL; | |
34 | ||
35 | /*-------------------------------------. | |
36 | | Create the struniq for S if needed. | | |
37 | `-------------------------------------*/ | |
38 | ||
39 | const struniq_t | |
40 | struniq_new (const char *s) | |
41 | { | |
42 | /* Keep the struniqs in a printable form. */ | |
43 | struniq_t res = hash_lookup (struniqs_table, | |
44 | quotearg_style (escape_quoting_style, s)); | |
45 | ||
46 | if (!res) | |
47 | { | |
48 | /* First insertion in the hash. */ | |
49 | res = xstrdup (quotearg_style (escape_quoting_style, s)); | |
50 | hash_insert (struniqs_table, res); | |
51 | } | |
52 | return res; | |
53 | } | |
54 | ||
55 | ||
56 | /*--------------------. | |
57 | | Print the struniq. | | |
58 | `--------------------*/ | |
59 | ||
60 | static bool | |
61 | struniq_print (const struniq_t s) | |
62 | { | |
63 | fprintf (stderr, "%s\n", s); | |
64 | return true; | |
65 | } | |
66 | ||
67 | \f | |
68 | /*-----------------------. | |
69 | | A struniq hash table. | | |
70 | `-----------------------*/ | |
71 | ||
72 | static bool | |
73 | hash_compare_struniq_t (const struniq_t m1, const struniq_t m2) | |
74 | { | |
75 | return strcmp (m1, m2) == 0; | |
76 | } | |
77 | ||
78 | static unsigned int | |
79 | hash_struniq_t (const struniq_t m, unsigned int tablesize) | |
80 | { | |
81 | return hash_string (m, tablesize); | |
82 | } | |
83 | ||
84 | /* A function to apply to each symbol. */ | |
85 | typedef bool (*struniq_processor_t) (const struniq_t); | |
86 | ||
87 | /*----------------------------. | |
88 | | Create the struniqs table. | | |
89 | `----------------------------*/ | |
90 | ||
91 | void | |
92 | struniqs_new (void) | |
93 | { | |
94 | struniqs_table = hash_initialize (HT_INITIAL_CAPACITY, | |
95 | NULL, | |
96 | (Hash_hasher) hash_struniq_t, | |
97 | (Hash_comparator) hash_compare_struniq_t, | |
98 | (Hash_data_freer) free); | |
99 | } | |
100 | ||
101 | ||
102 | /*-------------------------------------. | |
103 | | Perform a task on all the struniqs. | | |
104 | `-------------------------------------*/ | |
105 | ||
106 | static void | |
107 | struniqs_do (struniq_processor_t processor, void *processor_data) | |
108 | { | |
109 | hash_do_for_each (struniqs_table, | |
110 | (Hash_processor) processor, | |
111 | processor_data); | |
112 | } | |
113 | ||
114 | ||
115 | /*-----------------. | |
116 | | Print them all. | | |
117 | `-----------------*/ | |
118 | ||
119 | void | |
120 | struniqs_print (void) | |
121 | { | |
122 | struniqs_do (struniq_print, NULL); | |
123 | } | |
124 | ||
125 | ||
126 | /*--------------------. | |
127 | | Free the struniqs. | | |
128 | `--------------------*/ | |
129 | ||
130 | void | |
131 | struniqs_free (void) | |
132 | { | |
133 | hash_free (struniqs_table); | |
134 | } |