2 /* Copyright (C) 1989, 1990, 1991, 1992, 2002, 2004
3 Free Software Foundation, Inc.
4 Written by James Clark (jjc@jclark.com)
6 This file is part of groff.
8 groff is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
13 groff is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License along
19 with groff; see the file COPYING. If not, write to the Free Software
20 Foundation, 51 Franklin St - Fifth Floor, Boston, MA 02110-1301, USA. */
28 const char **symbol::table = 0;
29 int symbol::table_used = 0;
30 int symbol::table_size = 0;
31 char *symbol::block = 0;
32 int symbol::block_size = 0;
34 const symbol NULL_SYMBOL;
35 const symbol EMPTY_SYMBOL("");
41 const int BLOCK_SIZE = 1024;
42 // the table will increase in size as necessary
43 // the size will be chosen from the following array
44 // add some more if you want
45 static const unsigned int table_sizes[] = {
46 101, 503, 1009, 2003, 3001, 4001, 5003, 10007, 20011, 40009, 80021,
47 160001, 500009, 1000003, 1500007, 2000003, 0
49 const double FULL_MAX = 0.3; // don't let the table get more than this full
51 static unsigned int hash_string(const char *p)
53 // compute a hash code; this assumes 32-bit unsigned ints
54 // see p436 of Compilers by Aho, Sethi & Ullman
55 // give special treatment to two-character names
56 unsigned int hc = 0, g;
62 for (; *p != 0; p++) {
65 if ((g = (hc & 0xf0000000)) == 0) {
75 // Tell compiler that a variable is intentionally unused.
76 inline void unused(void *) { }
78 symbol::symbol(const char *p, int how)
89 table_size = table_sizes[0];
90 table = (const char **)new char*[table_size];
91 for (int i = 0; i < table_size; i++)
95 unsigned int hc = hash_string(p);
97 for (pp = table + hc % table_size;
99 (pp == table ? pp = table + table_size - 1 : --pp))
100 if (strcmp(p, *pp) == 0) {
104 if (how == MUST_ALREADY_EXIST) {
108 if (table_used >= table_size - 1 || table_used >= table_size*FULL_MAX) {
109 const char **old_table = table;
110 unsigned int old_table_size = table_size;
112 for (i = 1; table_sizes[i] <= old_table_size; i++)
113 if (table_sizes[i] == 0)
114 fatal("too many symbols");
115 table_size = table_sizes[i];
117 table = (const char **)new char*[table_size];
118 for (i = 0; i < table_size; i++)
120 for (pp = old_table + old_table_size - 1;
123 symbol temp(*pp, 1); /* insert it into the new table */
127 for (pp = table + hc % table_size;
129 (pp == table ? pp = table + table_size - 1 : --pp))
133 if (how == DONT_STORE) {
137 int len = strlen(p)+1;
138 if (block == 0 || block_size < len) {
139 block_size = len > BLOCK_SIZE ? len : BLOCK_SIZE;
140 block = new char [block_size];
142 (void)strcpy(block, p);
149 symbol concat(symbol s1, symbol s2)
151 char *buf = new char [strlen(s1.contents()) + strlen(s2.contents()) + 1];
152 strcpy(buf, s1.contents());
153 strcat(buf, s2.contents());
159 symbol default_symbol("default");