]> git.saurik.com Git - bison.git/blob - src/struniq.c
(struniq_assert): Now returns void, and aborts if the assertion is false.
[bison.git] / src / struniq.c
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 "error.h"
24 #include "hash.h"
25 #include "struniq.h"
26
27 /*-----------------------.
28 | A struniq hash table. |
29 `-----------------------*/
30
31 /* Initial capacity of struniq hash table. */
32 #define HT_INITIAL_CAPACITY 257
33
34 static struct hash_table *struniqs_table = NULL;
35
36 /*-------------------------------------.
37 | Create the struniq for S if needed. |
38 `-------------------------------------*/
39
40 struniq_t
41 struniq_new (const char *s)
42 {
43 struniq_t res = hash_lookup (struniqs_table, s);
44 if (!res)
45 {
46 /* First insertion in the hash. */
47 res = xstrdup (s);
48 hash_insert (struniqs_table, res);
49 }
50 return res;
51 }
52
53
54 /*------------------------------.
55 | Abort if S is not a struniq. |
56 `------------------------------*/
57
58 void
59 struniq_assert (const char *s)
60 {
61 if (!hash_lookup (struniqs_table, s))
62 {
63 error (0, 0, "not a struniq: %s", quotearg (s));
64 abort ();
65 }
66 }
67
68
69 /*--------------------.
70 | Print the struniq. |
71 `--------------------*/
72
73 static bool
74 struniq_print (const struniq_t s)
75 {
76 fprintf (stderr, "%s\n", s);
77 return true;
78 }
79
80 \f
81 /*-----------------------.
82 | A struniq hash table. |
83 `-----------------------*/
84
85 static bool
86 hash_compare_struniq_t (const struniq_t m1, const struniq_t m2)
87 {
88 return strcmp (m1, m2) == 0;
89 }
90
91 static unsigned int
92 hash_struniq_t (const struniq_t m, unsigned int tablesize)
93 {
94 return hash_string (m, tablesize);
95 }
96
97 /* A function to apply to each symbol. */
98 typedef bool (*struniq_processor_t) (const struniq_t);
99
100 /*----------------------------.
101 | Create the struniqs table. |
102 `----------------------------*/
103
104 void
105 struniqs_new (void)
106 {
107 struniqs_table = hash_initialize (HT_INITIAL_CAPACITY,
108 NULL,
109 (Hash_hasher) hash_struniq_t,
110 (Hash_comparator) hash_compare_struniq_t,
111 (Hash_data_freer) free);
112 }
113
114
115 /*-------------------------------------.
116 | Perform a task on all the struniqs. |
117 `-------------------------------------*/
118
119 static void
120 struniqs_do (struniq_processor_t processor, void *processor_data)
121 {
122 hash_do_for_each (struniqs_table,
123 (Hash_processor) processor,
124 processor_data);
125 }
126
127
128 /*-----------------.
129 | Print them all. |
130 `-----------------*/
131
132 void
133 struniqs_print (void)
134 {
135 struniqs_do (struniq_print, NULL);
136 }
137
138
139 /*--------------------.
140 | Free the struniqs. |
141 `--------------------*/
142
143 void
144 struniqs_free (void)
145 {
146 hash_free (struniqs_table);
147 }