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