OLD | NEW |
(Empty) | |
| 1 /* Keep a unique copy of strings. |
| 2 |
| 3 Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc. |
| 4 |
| 5 This file is part of Bison, the GNU Compiler Compiler. |
| 6 |
| 7 This program is free software: you can redistribute it and/or modify |
| 8 it under the terms of the GNU General Public License as published by |
| 9 the Free Software Foundation, either version 3 of the License, or |
| 10 (at your option) any later version. |
| 11 |
| 12 This program is distributed in the hope that it will be useful, |
| 13 but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 GNU General Public License for more details. |
| 16 |
| 17 You should have received a copy of the GNU General Public License |
| 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
| 19 |
| 20 #include <config.h> |
| 21 #include "system.h" |
| 22 |
| 23 #include <error.h> |
| 24 #include <hash.h> |
| 25 #include <quotearg.h> |
| 26 |
| 27 #include "uniqstr.h" |
| 28 |
| 29 /*-----------------------. |
| 30 | A uniqstr hash table. | |
| 31 `-----------------------*/ |
| 32 |
| 33 /* Initial capacity of uniqstr hash table. */ |
| 34 #define HT_INITIAL_CAPACITY 257 |
| 35 |
| 36 static struct hash_table *uniqstrs_table = NULL; |
| 37 |
| 38 /*-------------------------------------. |
| 39 | Create the uniqstr for S if needed. | |
| 40 `-------------------------------------*/ |
| 41 |
| 42 uniqstr |
| 43 uniqstr_new (char const *str) |
| 44 { |
| 45 uniqstr res = hash_lookup (uniqstrs_table, str); |
| 46 if (!res) |
| 47 { |
| 48 /* First insertion in the hash. */ |
| 49 res = xstrdup (str); |
| 50 hash_insert (uniqstrs_table, res); |
| 51 } |
| 52 return res; |
| 53 } |
| 54 |
| 55 |
| 56 /*------------------------------. |
| 57 | Abort if S is not a uniqstr. | |
| 58 `------------------------------*/ |
| 59 |
| 60 void |
| 61 uniqstr_assert (char const *str) |
| 62 { |
| 63 if (!hash_lookup (uniqstrs_table, str)) |
| 64 { |
| 65 error (0, 0, |
| 66 "not a uniqstr: %s", quotearg (str)); |
| 67 abort (); |
| 68 } |
| 69 } |
| 70 |
| 71 |
| 72 /*--------------------. |
| 73 | Print the uniqstr. | |
| 74 `--------------------*/ |
| 75 |
| 76 static inline bool |
| 77 uniqstr_print (uniqstr ustr) |
| 78 { |
| 79 fprintf (stderr, "%s\n", ustr); |
| 80 return true; |
| 81 } |
| 82 |
| 83 static bool |
| 84 uniqstr_print_processor (void *ustr, void *null ATTRIBUTE_UNUSED) |
| 85 { |
| 86 return uniqstr_print (ustr); |
| 87 } |
| 88 |
| 89 |
| 90 /*-----------------------. |
| 91 | A uniqstr hash table. | |
| 92 `-----------------------*/ |
| 93 |
| 94 static bool |
| 95 hash_compare_uniqstr (void const *m1, void const *m2) |
| 96 { |
| 97 return strcmp (m1, m2) == 0; |
| 98 } |
| 99 |
| 100 static size_t |
| 101 hash_uniqstr (void const *m, size_t tablesize) |
| 102 { |
| 103 return hash_string (m, tablesize); |
| 104 } |
| 105 |
| 106 /*----------------------------. |
| 107 | Create the uniqstrs table. | |
| 108 `----------------------------*/ |
| 109 |
| 110 void |
| 111 uniqstrs_new (void) |
| 112 { |
| 113 uniqstrs_table = hash_initialize (HT_INITIAL_CAPACITY, |
| 114 NULL, |
| 115 hash_uniqstr, |
| 116 hash_compare_uniqstr, |
| 117 free); |
| 118 } |
| 119 |
| 120 |
| 121 /*-------------------------------------. |
| 122 | Perform a task on all the uniqstrs. | |
| 123 `-------------------------------------*/ |
| 124 |
| 125 static void |
| 126 uniqstrs_do (Hash_processor processor, void *processor_data) |
| 127 { |
| 128 hash_do_for_each (uniqstrs_table, processor, processor_data); |
| 129 } |
| 130 |
| 131 |
| 132 /*-----------------. |
| 133 | Print them all. | |
| 134 `-----------------*/ |
| 135 |
| 136 void |
| 137 uniqstrs_print (void) |
| 138 { |
| 139 uniqstrs_do (uniqstr_print_processor, NULL); |
| 140 } |
| 141 |
| 142 |
| 143 /*--------------------. |
| 144 | Free the uniqstrs. | |
| 145 `--------------------*/ |
| 146 |
| 147 void |
| 148 uniqstrs_free (void) |
| 149 { |
| 150 hash_free (uniqstrs_table); |
| 151 } |
OLD | NEW |