1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
|
/*
* tardy - a tar post-processor
* Copyright (C) 1998, 1999, 2002 Peter Miller;
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
*
* MANIFEST: functions to manipulate symbol tables
*/
#include <error.h>
#include <fstrcmp.h>
#include <mem.h>
#include <symtab.h>
symtab::symtab()
{
chain = 0;
reap = 0;
hash_modulus = 1 << 4; /* MUST be a power of 2 */
hash_cutover = hash_modulus;
hash_split = hash_modulus - hash_cutover;
hash_cutover_mask = hash_cutover - 1;
hash_cutover_split_mask = (hash_cutover * 2) - 1;
hash_load = 0;
hash_table = (row **)mem_alloc(hash_modulus * sizeof(row *));
for (str_hash_ty j = 0; j < hash_modulus; ++j)
hash_table[j] = 0;
}
symtab::~symtab()
{
for (str_hash_ty j = 0; j < hash_modulus; ++j)
{
row **rpp = &hash_table[j];
while (*rpp)
{
row *rp = *rpp;
*rpp = rp->overflow;
if (reap)
reap(rp->data);
delete rp;
}
}
}
/*
* NAME
* split - reduce symbol table load
*
* SYNOPSIS
* void split(symtab_ty);
*
* DESCRIPTION
* The split function is used to split symbols in the bucket indicated by
* the split point. The symbols are split between that bucket and the one
* after the current end of the table.
*
* CAVEAT
* It is only sensable to do this when the symbol table load exceeds some
* reasonable threshold. A threshold of 80% is suggested.
*/
void
symtab::split()
{
/*
* get the list to be split across buckets
*/
row *p = hash_table[hash_split];
hash_table[hash_split] = 0;
/*
* increase the modulus by one
*/
hash_modulus++;
hash_table =
(row **)
mem_change_size
(
hash_table,
hash_modulus * sizeof(row *)
);
hash_table[hash_modulus - 1] = 0;
hash_split = hash_modulus - hash_cutover;
if (hash_split >= hash_cutover)
{
hash_cutover = hash_modulus;
hash_split = 0;
hash_cutover_mask = hash_cutover - 1;
hash_cutover_split_mask = (hash_cutover * 2) - 1;
}
/*
* now redistribute the list elements
*
* It is important to preserve the order of the links because
* they can be push-down stacks, and to simply add them to the
* head of the list will reverse the order of the stack!
*/
while (p)
{
row *p2 = p;
p = p2->overflow;
p2->overflow = 0;
str_hash_ty index = p2->key.hash() & hash_cutover_mask;
if (index < hash_split)
{
index =
(
p2->key.hash()
&
hash_cutover_split_mask
);
}
row **ipp;
for
(
ipp = &hash_table[index];
*ipp;
ipp = &(*ipp)->overflow
)
;
*ipp = p2;
}
}
/*
* NAME
* symtab_query - search for a variable
*
* SYNOPSIS
* int symtab_query(symtab_ty *, string_ty *key);
*
* DESCRIPTION
* The symtab_query function is used to reference a variable.
*
* RETURNS
* If the variable has been defined, the function returns a non-zero value
* and the value is returned through the 'value' pointer.
* If the variable has not been defined, it returns zero,
* and 'value' is unaltered.
*/
void *
symtab::query(const rcstring &key)
const
{
str_hash_ty index = key.hash() & hash_cutover_mask;
if (index < hash_split)
index = key.hash() & hash_cutover_split_mask;
for (row *p = hash_table[index]; p; p = p->overflow)
{
if (key == p->key)
return p->data;
}
return 0;
}
/*
* NAME
* symtab_query_fuzzy - search for a variable name
*
* SYNOPSIS
* string_ty *symtab_query_fuzzy(symtab_ty *, string_ty *key);
*
* DESCRIPTION
* The symtab_query_fuzzy function is used to search for a variable name.
*
* RETURNS
* The closest match for the variable name given.
* If no match is particularly close, it returns 0.
*/
rcstring
symtab::query_fuzzy(const rcstring &key)
const
{
rcstring best_name;
double best_weight = 0.6;
for (str_hash_ty index = 0; index < hash_modulus; ++index)
{
for (row *p = hash_table[index]; p; p = p->overflow)
{
double weight =
fmemcmp
(
key.to_c_string(),
key.length(),
p->key.to_c_string(),
p->key.length()
);
if (weight > best_weight)
best_name = p->key;
}
}
return best_name;
}
/*
* NAME
* symtab_assign - assign a variable
*
* SYNOPSIS
* void symtab_assign(symtab_ty *, string_ty *key, void *data);
*
* DESCRIPTION
* The symtab_assign function is used to assign
* a value to a given variable.
*
* CAVEAT
* The name is copied, the data is not.
*/
void
symtab::assign(const rcstring &key, void *data)
{
str_hash_ty index = key.hash() & hash_cutover_mask;
if (index < hash_split)
index = key.hash() & hash_cutover_split_mask;
row *p;
for (p = hash_table[index]; p; p = p->overflow)
{
if (key == p->key)
{
if (reap)
reap(p->data);
p->data = data;
return;
}
}
p = new row();
p->key = key;
p->overflow = hash_table[index];
p->data = data;
hash_table[index] = p;
hash_load++;
while (hash_load * 10 >= hash_modulus * 8)
split();
}
/*
* NAME
* symtab_assign_push - assign a variable
*
* SYNOPSIS
* void symtab_assign_push(symtab_ty *, string_ty *key, void *data);
*
* DESCRIPTION
* The symtab_assign function is used to assign
* a value to a given variable.
* Any previous value will be obscured until this one
* is deleted with symtab_delete.
*
* CAVEAT
* The name is copied, the data is not.
*/
void
symtab::assign_push(const rcstring &key, void *data)
{
str_hash_ty index = key.hash() & hash_cutover_mask;
if (index < hash_split)
index = key.hash() & hash_cutover_split_mask;
row *p = new row();
p->key = key;
p->overflow = hash_table[index];
p->data = data;
hash_table[index] = p;
hash_load++;
while (hash_load * 10 >= hash_modulus * 8)
split();
}
/*
* NAME
* symtab_delete - delete a variable
*
* SYNOPSIS
* void symtab_delete(string_ty *name, symtab_class_ty class);
*
* DESCRIPTION
* The symtab_delete function is used to delete variables.
*
* CAVEAT
* The name is freed, the data is reaped.
* (By default, reap does nothing.)
*/
void
symtab::remove(const rcstring &key)
{
str_hash_ty index = key.hash() & hash_cutover_mask;
if (index < hash_split)
index = key.hash() & hash_cutover_split_mask;
row **rpp = &hash_table[index];
for (;;)
{
row *rp = *rpp;
if (!rp)
break;
if (key == rp->key)
{
if (reap)
reap(rp->data);
*rpp = rp->overflow;
delete rp;
hash_load--;
break;
}
rpp = &rp->overflow;
}
}
/*
* NAME
* symtab_dump - dump id table
*
* SYNOPSIS
* void symtab_dump(symtab_ty *stp, char *caption);
*
* DESCRIPTION
* The symtab_dump function is used to dump the contents of the
* symbol table. The caption will be used to indicate why the
* symbol table was dumped.
*
* CAVEAT
* This function is only available when symbol DEBUG is defined.
*/
void
symtab::dump(const char *caption)
const
{
error("symbol table %s = {", caption);
for (str_hash_ty j = 0; j < hash_modulus; ++j)
{
for (row *p = hash_table[j]; p; p = p->overflow)
{
error
(
"key = \"%s\", data = %08lX",
p->key.to_c_string(),
(long)p->data
);
}
}
error("}");
}
void
symtab::walk(void (*func)(const symtab &, const rcstring &, void *, void *),
void *arg)
{
for (str_hash_ty j = 0; j < hash_modulus; ++j)
for (row *rp = hash_table[j]; rp; rp = rp->overflow)
func(*this, rp->key, rp->data, arg);
}
|