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 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
|
/* hashlib.c -- functions to manage and access hash tables for bash. */
/* Copyright (C) 1987, 1989, 1991 Free Software Foundation, Inc.
This file is part of GNU Bash, the Bourne Again SHell.
Bash 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, or (at your option) any later
version.
Bash 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 Bash; see the file COPYING. If not, write to the Free Software
Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
#include <config.h>
#include "bashansi.h"
#if defined (HAVE_UNISTD_H)
# ifdef _MINIX
# include <sys/types.h>
# endif
# include <unistd.h>
#endif
#include <stdio.h>
#include "shell.h"
#include "hashlib.h"
static void initialize_hash_table __P((HASH_TABLE *));
static BUCKET_CONTENTS *copy_bucket_array __P((BUCKET_CONTENTS *, sh_string_func_t *));
/* Zero the buckets in TABLE. */
static void
initialize_hash_table (table)
HASH_TABLE *table;
{
register int i;
for (i = 0; i < table->nbuckets; i++)
table->bucket_array[i] = (BUCKET_CONTENTS *)NULL;
}
/* Make a new hash table with BUCKETS number of buckets. Initialize
each slot in the table to NULL. */
HASH_TABLE *
make_hash_table (buckets)
int buckets;
{
HASH_TABLE *new_table;
new_table = (HASH_TABLE *)xmalloc (sizeof (HASH_TABLE));
if (buckets == 0)
buckets = DEFAULT_HASH_BUCKETS;
new_table->bucket_array =
(BUCKET_CONTENTS **)xmalloc (buckets * sizeof (BUCKET_CONTENTS *));
new_table->nbuckets = buckets;
new_table->nentries = 0;
initialize_hash_table (new_table);
return (new_table);
}
int
hash_table_nentries (table)
HASH_TABLE *table;
{
return (HASH_ENTRIES(table));
}
static BUCKET_CONTENTS *
copy_bucket_array (ba, cpdata)
BUCKET_CONTENTS *ba;
sh_string_func_t *cpdata; /* data copy function */
{
BUCKET_CONTENTS *new_bucket, *n, *e;
if (ba == 0)
return ((BUCKET_CONTENTS *)0);
for (n = (BUCKET_CONTENTS *)0, e = ba; e; e = e->next)
{
if (n == 0)
{
new_bucket = (BUCKET_CONTENTS *)xmalloc (sizeof (BUCKET_CONTENTS));
n = new_bucket;
}
else
{
n->next = (BUCKET_CONTENTS *)xmalloc (sizeof (BUCKET_CONTENTS));
n = n->next;
}
n->key = savestring (e->key);
n->data = e->data ? (cpdata ? (*cpdata) (e->data) : savestring (e->data))
: (char *)NULL;
n->times_found = e->times_found;
n->next = (BUCKET_CONTENTS *)NULL;
}
return new_bucket;
}
HASH_TABLE *
copy_hash_table (table, cpdata)
HASH_TABLE *table;
sh_string_func_t *cpdata;
{
HASH_TABLE *new_table;
int i;
if (table == 0)
return ((HASH_TABLE *)NULL);
new_table = make_hash_table (table->nbuckets);
for (i = 0; i < table->nbuckets; i++)
new_table->bucket_array[i] = copy_bucket_array (table->bucket_array[i], cpdata);
new_table->nentries = table->nentries;
return new_table;
}
/* Return the location of the bucket which should contain the data
for STRING. TABLE is a pointer to a HASH_TABLE. */
#if 0
/* A possibly better distribution may be obtained by initializing i to
~0UL and using i = (i * 31) + *string++ as the step */
#define ALL_ONES (~((unsigned long) 0))
#define BITS(h, n) ((unsigned long)(h) & ~(ALL_ONES << (n)))
#endif
int
hash_string (string, table)
const char *string;
HASH_TABLE *table;
{
register unsigned int i = 0;
while (*string)
i = (i << 2) + *string++;
#if 0
return (BITS (i, 31) % table->nbuckets);
#else
/* Rely on properties of unsigned division (unsigned/int -> unsigned) and
don't discard the upper 32 bits of the value, if present. */
return (i % table->nbuckets);
#endif
}
/* Return a pointer to the hashed item, or NULL if the item
can't be found. */
BUCKET_CONTENTS *
find_hash_item (string, table)
const char *string;
HASH_TABLE *table;
{
BUCKET_CONTENTS *list;
int which_bucket;
if (table == 0)
return (BUCKET_CONTENTS *)NULL;
which_bucket = hash_string (string, table);
for (list = table->bucket_array[which_bucket]; list; list = list->next)
{
if (STREQ (list->key, string))
{
list->times_found++;
return (list);
}
}
return (BUCKET_CONTENTS *)NULL;
}
/* Remove the item specified by STRING from the hash table TABLE.
The item removed is returned, so you can free its contents. If
the item isn't in this table NULL is returned. */
BUCKET_CONTENTS *
remove_hash_item (string, table)
const char *string;
HASH_TABLE *table;
{
int the_bucket;
BUCKET_CONTENTS *prev, *temp;
if (table == 0)
return (BUCKET_CONTENTS *)NULL;
the_bucket = hash_string (string, table);
prev = (BUCKET_CONTENTS *)NULL;
for (temp = table->bucket_array[the_bucket]; temp; temp = temp->next)
{
if (STREQ (temp->key, string))
{
if (prev)
prev->next = temp->next;
else
table->bucket_array[the_bucket] = temp->next;
table->nentries--;
return (temp);
}
prev = temp;
}
return ((BUCKET_CONTENTS *) NULL);
}
/* Create an entry for STRING, in TABLE. If the entry already
exists, then return it. */
BUCKET_CONTENTS *
add_hash_item (string, table)
char *string;
HASH_TABLE *table;
{
BUCKET_CONTENTS *item;
int bucket;
if (table == 0)
table = make_hash_table (0);
if ((item = find_hash_item (string, table)) == 0)
{
bucket = hash_string (string, table);
item = table->bucket_array[bucket];
while (item && item->next)
item = item->next;
if (item)
{
item->next = (BUCKET_CONTENTS *)xmalloc (sizeof (BUCKET_CONTENTS));
item = item->next;
}
else
{
table->bucket_array[bucket] =
(BUCKET_CONTENTS *)xmalloc (sizeof (BUCKET_CONTENTS));
item = table->bucket_array[bucket];
}
item->data = (char *)NULL;
item->next = (BUCKET_CONTENTS *)NULL;
item->key = string;
table->nentries++;
item->times_found = 0;
}
return (item);
}
/* Remove and discard all entries in TABLE. If FREE_DATA is non-null, it
is a function to call to dispose of a hash item's data. Otherwise,
free() is called. */
void
flush_hash_table (table, free_data)
HASH_TABLE *table;
sh_free_func_t *free_data;
{
int i;
register BUCKET_CONTENTS *bucket, *item;
if (table == 0)
return;
for (i = 0; i < table->nbuckets; i++)
{
bucket = table->bucket_array[i];
while (bucket)
{
item = bucket;
bucket = bucket->next;
if (free_data)
(*free_data) (item->data);
else
free (item->data);
free (item->key);
free (item);
}
table->bucket_array[i] = (BUCKET_CONTENTS *)NULL;
}
}
/* Free the hash table pointed to by TABLE. */
void
dispose_hash_table (table)
HASH_TABLE *table;
{
free (table->bucket_array);
free (table);
}
/* No longer necessary; everything uses the macro */
#if 0
/* Return the bucket_contents list of bucket BUCKET in TABLE. If
TABLE doesn't have BUCKET buckets, return NULL. */
#undef get_hash_bucket
BUCKET_CONTENTS *
get_hash_bucket (bucket, table)
int bucket;
HASH_TABLE *table;
{
if (table && bucket < table->nbuckets)
return (table->bucket_array[bucket]);
else
return (BUCKET_CONTENTS *)NULL;
}
#endif
#ifdef DEBUG
void
print_table_stats (table, name)
HASH_TABLE *table;
char *name;
{
register int slot, bcount;
register BUCKET_CONTENTS *bc;
if (name == 0)
name = "unknown hash table";
fprintf (stderr, "%s: %d buckets; %d items\n", name, table->nbuckets, table->nentries);
/* Print out a count of how many strings hashed to each bucket, so we can
see how even the distribution is. */
for (slot = 0; slot < table->nbuckets; slot++)
{
bc = get_hash_bucket (slot, table);
fprintf (stderr, "\tslot %3d: ", slot);
for (bcount = 0; bc; bc = bc->next)
bcount++;
fprintf (stderr, "%d\n", bcount);
}
}
#endif
#ifdef TEST_HASHING
#undef NULL
#include <stdio.h>
#ifndef NULL
#define NULL 0
#endif
HASH_TABLE *table, *ntable;
#define NBUCKETS 107
void *
xmalloc (bytes)
size_t bytes;
{
void *result = malloc (bytes);
if (!result)
{
fprintf (stderr, "hash: out of virtual memory\n");
abort ();
}
return (result);
}
main ()
{
char string[256];
int count = 0;
BUCKET_CONTENTS *tt;
table = make_hash_table (NBUCKETS);
for (;;)
{
char *temp_string;
if (fgets (string, sizeof (string), stdin) == 0)
break;
if (!*string)
break;
temp_string = savestring (string);
tt = add_hash_item (temp_string, table);
if (tt->times_found)
{
fprintf (stderr, "You have already added item `%s'\n", string);
free (temp_string);
}
else
{
count++;
}
}
print_table_stats (table, "hash test");
ntable = copy_hash_table (table, (sh_string_func_t *)NULL);
flush_hash_table (table, (sh_free_func_t *)NULL);
print_table_stats (ntable, "hash copy test");
exit (0);
}
#endif /* TEST_HASHING */
|