-/* Hash.c -- Where hashing for bash is done. */
+/* hashlib.c -- functions to manage and access hash tables for bash. */
/* Copyright (C) 1987, 1989, 1991 Free Software Foundation, Inc.
with Bash; see the file COPYING. If not, write to the Free Software
Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
-/* There appears to be library functions for this stuff, but it seems like
- a lot of overhead, so I just implemented this hashing stuff on my own. */
+#include "config.h"
#if defined (HAVE_STRING_H)
# include <string.h>
# include "ansi_stdlib.h"
#endif /* HAVE_STDLIB_H */
-#include "shell.h"
-#include "hash.h"
-
-HASH_TABLE *hashed_filenames;
+#if defined (HAVE_UNISTD_H)
+# include <unistd.h>
+#endif
-#define FILENAME_HASH_BUCKETS 107
+#include "shell.h"
+#include "hashlib.h"
/* Zero the buckets in TABLE. */
static void
make_hash_table (buckets)
int buckets;
{
- HASH_TABLE *new_table = (HASH_TABLE *)xmalloc (sizeof (HASH_TABLE));
+ HASH_TABLE *new_table;
+ new_table = (HASH_TABLE *)xmalloc (sizeof (HASH_TABLE));
if (buckets == 0)
buckets = DEFAULT_HASH_BUCKETS;
return (new_table);
}
-#if 0
-/* UNUSED */
-/* Create the hash table for filenames that we use in the shell. */
-initialize_hashed_filenames ()
-{
- hashed_filenames = make_hash_table (FILENAME_HASH_BUCKETS);
-}
-#endif
-
/* Return the location of the bucket which should contain the data
for STRING. TABLE is a pointer to a HASH_TABLE. */
BUCKET_CONTENTS *list;
int which_bucket;
- if (!table)
+ if (table == 0)
return (BUCKET_CONTENTS *)NULL;
which_bucket = hash_string (string, table);
- list = table->bucket_array[which_bucket];
-
- while (list)
+ for (list = table->bucket_array[which_bucket]; list; list = list->next)
{
if (STREQ (list->key, string))
{
list->times_found++;
return (list);
}
- else list = list->next;
}
return (BUCKET_CONTENTS *)NULL;
}
int the_bucket;
BUCKET_CONTENTS *prev, *temp;
- if (!table)
+ if (table == 0)
return (BUCKET_CONTENTS *)NULL;
the_bucket = hash_string (string, table);
prev = (BUCKET_CONTENTS *)NULL;
- temp = table->bucket_array[the_bucket];
-
- while (temp)
+ for (temp = table->bucket_array[the_bucket]; temp; temp = temp->next)
{
if (STREQ (temp->key, string))
{
return (temp);
}
prev = temp;
- temp = temp->next;
}
return ((BUCKET_CONTENTS *) NULL);
}
HASH_TABLE *table;
{
BUCKET_CONTENTS *item;
+ int bucket;
- if (!table)
+ if (table == 0)
table = make_hash_table (0);
if ((item = find_hash_item (string, table)) == 0)
{
- int bucket = hash_string (string, table);
+ bucket = hash_string (string, table);
item = table->bucket_array[bucket];
while (item && item->next)
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;
+ VFunction *free_data;
+{
+ int i;
+ register BUCKET_CONTENTS *bucket, *item;
+
+ 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;
+ }
+}
+
/* Return the bucket_contents list of bucket BUCKET in TABLE. If
TABLE doesn't have BUCKET buckets, return NULL. */
#undef get_hash_bucket
char *result = (char *)malloc (bytes);
if (!result)
{
- fprintf (stderr, "Out of memory!");
+ fprintf (stderr, "hash: out of virtual memory\n");
abort ();
}
return (result);
BUCKET_CONTENTS *tt;
table = make_hash_table (NBUCKETS);
-
+
for (;;)
{
char *temp_string;
count++;
}
}
-
+
printf ("You have entered %d (%d) items. The distribution is:\n",
table->nentries, count);
{
int bcount;
register BUCKET_CONTENTS *list = get_hash_bucket (count, table);
-
+
printf ("slot %3d: ", count);
bcount = 0;
}
#endif /* TEST_HASHING */
-\f
-/*
- * Local variables:
- * compile-command: "gcc -g -DTEST_HASHING -o hash hash.c"
- * end:
- */