/* Hash tables.
- Copyright (C) 2000, 2001, 2003 Free Software Foundation, Inc.
+ Copyright (C) 2000, 2001, 2003, 2004 Free Software Foundation, Inc.
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
{
size_t n = len;
unsigned int r = 0;
-#define HASHSTEP(r, c) ((r) * 67 + ((c) - 113));
while (n--)
- r = HASHSTEP (r, *str++);
+ r = HT_HASHSTEP (r, *str++);
- return r + len;
-#undef HASHSTEP
+ return HT_HASHFINISH (r, len);
}
/* Initialize an identifier hashtable. */
obstack_alignment_mask (&table->stack) = 0;
table->entries = xcalloc (nslots, sizeof (hashnode));
+ table->entries_owned = true;
table->nslots = nslots;
return table;
}
ht_destroy (hash_table *table)
{
obstack_free (&table->stack, NULL);
- free (table->entries);
+ if (table->entries_owned)
+ free (table->entries);
free (table);
}
ht_lookup (hash_table *table, const unsigned char *str, size_t len,
enum ht_lookup_option insert)
{
- unsigned int hash = calc_hash (str, len);
+ return ht_lookup_with_hash (table, str, len, calc_hash (str, len),
+ insert);
+}
+
+hashnode
+ht_lookup_with_hash (hash_table *table, const unsigned char *str,
+ size_t len, unsigned int hash,
+ enum ht_lookup_option insert)
+{
unsigned int hash2;
unsigned int index;
size_t sizemask;
}
while (++p < limit);
- free (table->entries);
+ if (table->entries_owned)
+ free (table->entries);
+ table->entries_owned = true;
table->entries = nentries;
table->nslots = size;
}
while (++p < limit);
}
+/* Restore the hash table. */
+void
+ht_load (hash_table *ht, hashnode *entries,
+ unsigned int nslots, unsigned int nelements,
+ bool own)
+{
+ if (ht->entries_owned)
+ free (ht->entries);
+ ht->entries = entries;
+ ht->nslots = nslots;
+ ht->nelements = nelements;
+ ht->entries_owned = own;
+}
+
/* Dump allocation statistics to stderr. */
void
ht_dump_statistics (hash_table *table)
{
size_t nelts, nids, overhead, headers;
- size_t total_bytes, longest, sum_of_squares;
- double exp_len, exp_len2, exp2_len;
+ size_t total_bytes, longest;
+ double sum_of_squares, exp_len, exp_len2, exp2_len;
hashnode *p, *limit;
#define SCALE(x) ((unsigned long) ((x) < 1024*10 \
size_t n = HT_LEN (*p);
total_bytes += n;
- sum_of_squares += n * n;
+ sum_of_squares += (double) n * n;
if (n > longest)
longest = n;
nids++;