2 /* Copyright (C) 1989-2014 Free Software Foundation, Inc.
3 Written by James Clark (jjc@jclark.com)
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 3 of the License, or
10 (at your option) any later version.
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
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 #include "dictionary.h"
24 // is `p' a good size for a hash table
26 static int is_good_size(unsigned int p)
28 const unsigned int SMALL = 10;
30 for (i = 2; i <= p/2; i++)
33 for (i = 0x100; i != 0; i <<= 8)
34 if (i % p <= SMALL || i % p > p - SMALL)
39 dictionary::dictionary(int n) : size(n), used(0), threshold(0.5), factor(1.5)
41 table = new association[n];
44 // see Knuth, Sorting and Searching, p518, Algorithm L
45 // we can't use double-hashing because we want a remove function
47 void *dictionary::lookup(symbol s, void *v)
50 for (i = int(s.hash() % size);
52 i == 0 ? i = size - 1: --i)
53 if (s == table[i].s) {
55 void *temp = table[i].v;
67 if ((double)used/(double)size >= threshold || used + 1 >= size) {
69 size = int(size*factor);
70 while (!is_good_size(size))
72 association *old_table = table;
73 table = new association[size];
75 for (i = 0; i < old_size; i++)
76 if (old_table[i].v != 0)
77 (void)lookup(old_table[i].s, old_table[i].v);
83 void *dictionary::lookup(const char *p)
85 symbol s(p, MUST_ALREADY_EXIST);
92 // see Knuth, Sorting and Searching, p527, Algorithm R
94 void *dictionary::remove(symbol s)
96 // this relies on the fact that we are using linear probing
98 for (i = int(s.hash() % size);
99 table[i].v != 0 && s != table[i].s;
100 i == 0 ? i = size - 1: --i)
102 void *p = table[i].v;
103 while (table[i].v != 0) {
113 r = int(table[i].s.hash() % size);
114 } while ((i <= r && r < j) || (r < j && j < i) || (j < i && i <= r));
122 dictionary_iterator::dictionary_iterator(dictionary &d) : dict(&d), i(0)
126 int dictionary_iterator::get(symbol *sp, void **vp)
128 for (; i < dict->size; i++)
129 if (dict->table[i].v) {
130 *sp = dict->table[i].s;
131 *vp = dict->table[i].v;
138 object_dictionary_iterator::object_dictionary_iterator(object_dictionary &od)
143 object::object() : rcount(0)
151 void object::add_reference()
156 void object::remove_reference()
162 object_dictionary::object_dictionary(int n) : d(n)
166 object *object_dictionary::lookup(symbol nm)
168 return (object *)d.lookup(nm);
171 void object_dictionary::define(symbol nm, object *obj)
173 obj->add_reference();
174 obj = (object *)d.lookup(nm, obj);
176 obj->remove_reference();
179 void object_dictionary::rename(symbol oldnm, symbol newnm)
181 object *obj = (object *)d.remove(oldnm);
183 obj = (object *)d.lookup(newnm, obj);
185 obj->remove_reference();
189 void object_dictionary::remove(symbol nm)
191 object *obj = (object *)d.remove(nm);
193 obj->remove_reference();
196 // Return non-zero if oldnm was defined.
198 int object_dictionary::alias(symbol newnm, symbol oldnm)
200 object *obj = (object *)d.lookup(oldnm);
202 obj->add_reference();
203 obj = (object *)d.lookup(newnm, obj);
205 obj->remove_reference();