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. */
27 /*-----------------------.
28 | A struniq hash table. |
29 `-----------------------*/
31 /* Initial capacity of struniq hash table. */
32 #define HT_INITIAL_CAPACITY 257
34 static struct hash_table
*struniqs_table
= NULL
;
36 /*-------------------------------------.
37 | Create the struniq for S if needed. |
38 `-------------------------------------*/
41 struniq_new (const char *s
)
43 struniq_t res
= hash_lookup (struniqs_table
, s
);
46 /* First insertion in the hash. */
48 hash_insert (struniqs_table
, res
);
54 /*------------------------------.
55 | Abort if S is not a struniq. |
56 `------------------------------*/
59 struniq_assert (const char *s
)
61 if (!hash_lookup (struniqs_table
, s
))
63 error (0, 0, "not a struniq: %s", quotearg (s
));
69 /*--------------------.
70 | Print the struniq. |
71 `--------------------*/
74 struniq_print (const struniq_t s
)
76 fprintf (stderr
, "%s\n", s
);
81 /*-----------------------.
82 | A struniq hash table. |
83 `-----------------------*/
86 hash_compare_struniq_t (const struniq_t m1
, const struniq_t m2
)
88 return strcmp (m1
, m2
) == 0;
92 hash_struniq_t (const struniq_t m
, unsigned int tablesize
)
94 return hash_string (m
, tablesize
);
97 /* A function to apply to each symbol. */
98 typedef bool (*struniq_processor_t
) (const struniq_t
);
100 /*----------------------------.
101 | Create the struniqs table. |
102 `----------------------------*/
107 struniqs_table
= hash_initialize (HT_INITIAL_CAPACITY
,
109 (Hash_hasher
) hash_struniq_t
,
110 (Hash_comparator
) hash_compare_struniq_t
,
111 (Hash_data_freer
) free
);
115 /*-------------------------------------.
116 | Perform a task on all the struniqs. |
117 `-------------------------------------*/
120 struniqs_do (struniq_processor_t processor
, void *processor_data
)
122 hash_do_for_each (struniqs_table
,
123 (Hash_processor
) processor
,
133 struniqs_print (void)
135 struniqs_do (struniq_print
, NULL
);
139 /*--------------------.
140 | Free the struniqs. |
141 `--------------------*/
146 hash_free (struniqs_table
);