2 /* Copyright (C) 1989, 1990, 1991, 1992, 2003, 2004, 2006
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. */
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 ITABLE(T)' is the type of a hash table mapping an integer (int >= 0)
33 // to an object of type T.
35 // `struct IASSOC(T)' is the type of a association (pair) between an integer
36 // (int >= 0) and an object of type T.
38 // `class ITABLE_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 ITABLE(T) name2(T,_itable)
44 #define IASSOC(T) name2(T,_iassoc)
45 #define ITABLE_ITERATOR(T) name2(T,_itable_iterator)
47 // ptable.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 // Declare the types `class ITABLE(T)', `struct IASSOC(T)', and `class
56 // ITABLE_ITERATOR(T)' for the type `T'.
57 #define declare_itable(T) \
67 class ITABLE_ITERATOR(T) { \
71 ITABLE_ITERATOR(T)(ITABLE(T) *); /* Initialize an iterator running \
72 through the given table. */ \
73 int next(int *, T **); /* Fetch the next pair, store the key \
74 and value in arg1 and arg2, \
75 respectively, and return 1. If \
76 there is no more pair in the \
90 ITABLE(T)(); /* Create an empty table. */ \
91 ~ITABLE(T)(); /* Delete a table, including its \
93 void define(int, T *); /* Define the value (arg2) for a key \
95 T *lookup(int); /* Return a pointer to the value of \
96 the given key, if found in the \
97 table, or NULL otherwise. */ \
98 friend class ITABLE_ITERATOR(T); \
102 // Values must be allocated by the caller (always using new[], not new)
103 // and are freed by ITABLE.
105 // Define the implementations of the members of the types `class ITABLE(T)',
106 // `struct IASSOC(T)', `class ITABLE_ITERATOR(T)' for the type `T'.
107 #define implement_itable(T) \
109 IASSOC(T)::IASSOC(T)() \
114 ITABLE(T)::ITABLE(T)() \
116 v = new IASSOC(T)[size = INITIAL_SIZE]; \
120 ITABLE(T)::~ITABLE(T)() \
122 for (unsigned i = 0; i < size; i++) \
127 void ITABLE(T)::define(int key, T *val) \
130 unsigned int h = (unsigned int)(key); \
132 for (n = unsigned(h % size); \
134 n = (n == 0 ? size - 1 : n - 1)) \
135 if (v[n].key == key) { \
142 if (used*FULL_DEN >= size*FULL_NUM) { \
143 IASSOC(T) *oldv = v; \
144 unsigned old_size = size; \
145 size = next_ptable_size(size); \
146 v = new IASSOC(T)[size]; \
147 for (unsigned i = 0; i < old_size; i++) \
148 if (oldv[i].key >= 0) { \
149 if (oldv[i].val != 0) { \
151 for (j = (unsigned int)(oldv[i].key) % size; \
153 j = (j == 0 ? size - 1 : j - 1)) \
155 v[j].key = oldv[i].key; \
156 v[j].val = oldv[i].val; \
159 for (n = unsigned(h % size); \
161 n = (n == 0 ? size - 1 : n - 1)) \
170 T *ITABLE(T)::lookup(int key) \
173 for (unsigned n = (unsigned int)key % size; \
175 n = (n == 0 ? size - 1 : n - 1)) \
176 if (v[n].key == key) \
181 ITABLE_ITERATOR(T)::ITABLE_ITERATOR(T)(ITABLE(T) *t) \
186 int ITABLE_ITERATOR(T)::next(int *keyp, T **valp) \
188 unsigned size = p->size; \
189 IASSOC(T) *v = p->v; \
190 for (; i < size; i++) \
191 if (v[i].key >= 0) { \