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 | Return TRUE iff S is a struniq.  | 
  56 `---------------------------------*/ 
  59 struniq_assert_p (const char *s
) 
  61   if (!hash_lookup (struniqs_table
, s
)) 
  63       error (0, 0, "not a struniq: %s", quotearg (s
)); 
  73 /*--------------------. 
  74 | Print the struniq.  | 
  75 `--------------------*/ 
  78 struniq_print (const struniq_t s
) 
  80   fprintf (stderr
, "%s\n", s
); 
  85 /*-----------------------. 
  86 | A struniq hash table.  | 
  87 `-----------------------*/ 
  90 hash_compare_struniq_t (const struniq_t m1
, const struniq_t m2
) 
  92   return strcmp (m1
, m2
) == 0; 
  96 hash_struniq_t (const struniq_t m
, unsigned int tablesize
) 
  98   return hash_string (m
, tablesize
); 
 101 /* A function to apply to each symbol. */ 
 102 typedef bool (*struniq_processor_t
) (const struniq_t
); 
 104 /*----------------------------. 
 105 | Create the struniqs table.  | 
 106 `----------------------------*/ 
 111   struniqs_table 
= hash_initialize (HT_INITIAL_CAPACITY
, 
 113                                     (Hash_hasher
) hash_struniq_t
, 
 114                                     (Hash_comparator
) hash_compare_struniq_t
, 
 115                                     (Hash_data_freer
) free
); 
 119 /*-------------------------------------. 
 120 | Perform a task on all the struniqs.  | 
 121 `-------------------------------------*/ 
 124 struniqs_do (struniq_processor_t processor
, void *processor_data
) 
 126   hash_do_for_each (struniqs_table
, 
 127                     (Hash_processor
) processor
, 
 137 struniqs_print (void) 
 139   struniqs_do (struniq_print
, NULL
); 
 143 /*--------------------. 
 144 | Free the struniqs.  | 
 145 `--------------------*/ 
 150   hash_free (struniqs_table
);