2 /* Copyright (C) 1989, 1990 Free Software Foundation, Inc.
3 Written by James Clark (jjc@jclark.uucp)
5 This file is part of groff.
7 groff is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 1, or (at your option) any later
12 groff is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License along
18 with groff; see the file LICENSE. If not, write to the Free Software
19 Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
25 #define PTABLE(T) name2(T,_ptable)
26 #define PASSOC(T) name2(T,_passoc)
27 #define PTABLE_ITERATOR(T) name2(T,_ptable_iterator)
29 extern int next_ptable_size(int);
30 extern unsigned hash_string(const char *);
32 #define declare_ptable(T) \
42 class PTABLE_ITERATOR(T) { \
46 PTABLE_ITERATOR(T)(PTABLE(T) *); \
47 int next(const char **, T **); \
54 enum { FULL_NUM = 2, FULL_DEN = 3, INITIAL_SIZE = 17 }; \
58 void define(const char *, T *); \
59 T *lookup(const char *); \
60 friend class PTABLE_ITERATOR(T); \
64 #define implement_ptable(T) \
66 PASSOC(T)::PASSOC(T)() \
71 PTABLE(T)::PTABLE(T)() \
73 v = new PASSOC(T)[size = INITIAL_SIZE]; \
77 PTABLE(T)::~PTABLE(T)() \
79 for (int i = 0; i < size; i++) { \
85 void PTABLE(T)::define(const char *key, T *val) \
88 int h = hash_string(key); \
89 for (int n = h % size; \
91 n = (n == 0 ? size - 1 : n - 1)) \
92 if (strcmp(v[n].key, key) == 0) { \
99 if (used*FULL_DEN >= size*FULL_NUM) { \
100 PASSOC(T) *oldv = v; \
101 int old_size = size; \
102 size = next_ptable_size(size); \
103 v = new PASSOC(T)[size]; \
104 for (int i = 0; i < old_size; i++) \
105 if (oldv[i].key != 0) { \
106 if (oldv[i].val == 0) \
107 delete oldv[i].key; \
109 for (int j = hash_string(oldv[i].key) % size; \
111 j = (j == 0 ? size - 1 : j - 1)) \
113 v[j].key = oldv[i].key; \
114 v[j].val = oldv[i].val; \
119 n = (n == 0 ? size - 1 : n - 1)) \
123 char *temp = new char[strlen(key)+1]; \
130 T *PTABLE(T)::lookup(const char *key) \
133 for (int n = hash_string(key) % size; \
135 n = (n == 0 ? size - 1 : n - 1)) \
136 if (strcmp(v[n].key, key) == 0) \
141 PTABLE_ITERATOR(T)::PTABLE_ITERATOR(T)(PTABLE(T) *t) \
146 int PTABLE_ITERATOR(T)::next(const char **keyp, T **valp) \
148 int size = p->size; \
149 PASSOC(T) *v = p->v; \
150 for (; i < size; i++) \
151 if (v[i].key != 0) { \