2 /* Copyright (C) 1989, 1990, 1991, 1992, 2003, 2004, 2006, 2009
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 3 of the License, or
11 (at your option) any later version.
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
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 // name2(a,b) concatenates two C identifiers.
25 #ifdef TRADITIONAL_CPP
26 # define name2(a,b) a/**/b
27 #else /* not TRADITIONAL_CPP */
28 # define name2(a,b) name2x(a,b)
29 # define name2x(a,b) a ## b
30 #endif /* not TRADITIONAL_CPP */
32 // `class PTABLE(T)' is the type of a hash table mapping a string
33 // (const char *) to an object of type T.
35 // `struct PASSOC(T)' is the type of a association (pair) between a
36 // string (const char *) and an object of type T.
38 // `class PTABLE_ITERATOR(T)' is the type of an iterator iterating through a
41 // Nowadays one would use templates for this; this code predates the addition
42 // of templates to C++.
43 #define PTABLE(T) name2(T,_ptable)
44 #define PASSOC(T) name2(T,_passoc)
45 #define PTABLE_ITERATOR(T) name2(T,_ptable_iterator)
47 // itable.h declares this too
48 #ifndef NEXT_PTABLE_SIZE_DEFINED
49 # define NEXT_PTABLE_SIZE_DEFINED
50 extern unsigned next_ptable_size(unsigned); // Return the first suitable
51 // hash table size greater than the given
55 extern unsigned long hash_string(const char *); // Return a hash code of the
56 // given string. The hash function is
57 // platform dependent. */
59 // Declare the types `class PTABLE(T)', `struct PASSOC(T)', and `class
60 // PTABLE_ITERATOR(T)' for the type `T'.
61 #define declare_ptable(T) \
71 class PTABLE_ITERATOR(T) { \
75 PTABLE_ITERATOR(T)(PTABLE(T) *); /* Initialize an iterator running \
76 through the given table. */ \
77 int next(const char **, T **); /* Fetch the next pair, store the key \
78 and value in arg1 and arg2, \
79 respectively, and return 1. If \
80 there is no more pair in the \
94 PTABLE(T)(); /* Create an empty table. */ \
95 ~PTABLE(T)(); /* Delete a table, including its \
97 const char *define(const char *, T *);/* Define the value (arg2) for a key \
98 (arg1). Return the copy in the \
99 table of the key (arg1), or \
100 possibly NULL if the value (arg2) \
102 T *lookup(const char *); /* Return a pointer to the value of \
103 the given key, if found in the \
104 table, or NULL otherwise. */ \
105 T *lookupassoc(const char **); /* Return a pointer to the value of \
106 the given key, passed by reference,\
107 and replace the key argument with \
108 the copy found in the table, if \
109 the key is found in the table. \
110 Return NULL otherwise. */ \
111 friend class PTABLE_ITERATOR(T); \
115 // Keys (which are strings) are allocated and freed by PTABLE.
116 // Values must be allocated by the caller (always using new[], not new)
117 // and are freed by PTABLE.
119 // Define the implementations of the members of the types `class PTABLE(T)',
120 // `struct PASSOC(T)', `class PTABLE_ITERATOR(T)' for the type `T'.
121 #define implement_ptable(T) \
123 PASSOC(T)::PASSOC(T)() \
128 PTABLE(T)::PTABLE(T)() \
130 v = new PASSOC(T)[size = INITIAL_SIZE]; \
134 PTABLE(T)::~PTABLE(T)() \
136 for (unsigned i = 0; i < size; i++) { \
143 const char *PTABLE(T)::define(const char *key, T *val) \
146 unsigned long h = hash_string(key); \
148 for (n = unsigned(h % size); \
150 n = (n == 0 ? size - 1 : n - 1)) \
151 if (strcmp(v[n].key, key) == 0) { \
158 if (used*FULL_DEN >= size*FULL_NUM) { \
159 PASSOC(T) *oldv = v; \
160 unsigned old_size = size; \
161 size = next_ptable_size(size); \
162 v = new PASSOC(T)[size]; \
163 for (unsigned i = 0; i < old_size; i++) \
164 if (oldv[i].key != 0) { \
165 if (oldv[i].val == 0) \
166 a_delete oldv[i].key; \
169 for (j = unsigned(hash_string(oldv[i].key) % size); \
171 j = (j == 0 ? size - 1 : j - 1)) \
173 v[j].key = oldv[i].key; \
174 v[j].val = oldv[i].val; \
177 for (n = unsigned(h % size); \
179 n = (n == 0 ? size - 1 : n - 1)) \
183 char *temp = new char[strlen(key)+1]; \
191 T *PTABLE(T)::lookup(const char *key) \
194 for (unsigned n = unsigned(hash_string(key) % size); \
196 n = (n == 0 ? size - 1 : n - 1)) \
197 if (strcmp(v[n].key, key) == 0) \
202 T *PTABLE(T)::lookupassoc(const char **keyptr) \
204 const char *key = *keyptr; \
206 for (unsigned n = unsigned(hash_string(key) % size); \
208 n = (n == 0 ? size - 1 : n - 1)) \
209 if (strcmp(v[n].key, key) == 0) { \
210 *keyptr = v[n].key; \
216 PTABLE_ITERATOR(T)::PTABLE_ITERATOR(T)(PTABLE(T) *t) \
221 int PTABLE_ITERATOR(T)::next(const char **keyp, T **valp) \
223 unsigned size = p->size; \
224 PASSOC(T) *v = p->v; \
225 for (; i < size; i++) \
226 if (v[i].key != 0) { \