#include "config.h"
#include "system.h"
#include "symtab.h"
static unsigned int calc_hash (const unsigned char *, size_t);
static void ht_expand (hash_table *);
static double approx_sqrt (double);
static unsigned int
calc_hash (const unsigned char *str, size_t len)
{
size_t n = len;
unsigned int r = 0;
while (n--)
r = HT_HASHSTEP (r, *str++);
return HT_HASHFINISH (r, len);
}
hash_table *
ht_create (unsigned int order)
{
unsigned int nslots = 1 << order;
hash_table *table;
table = xcalloc (1, sizeof (hash_table));
_obstack_begin (&table->stack, 0, 0,
(void *(*) (long)) xmalloc,
(void (*) (void *)) free);
obstack_alignment_mask (&table->stack) = 0;
table->entries = xcalloc (nslots, sizeof (hashnode));
table->entries_owned = true;
table->nslots = nslots;
return table;
}
void
ht_destroy (hash_table *table)
{
obstack_free (&table->stack, NULL);
if (table->entries_owned)
free (table->entries);
free (table);
}
hashnode
ht_lookup (hash_table *table, const unsigned char *str, size_t len,
enum ht_lookup_option insert)
{
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;
hashnode node;
sizemask = table->nslots - 1;
index = hash & sizemask;
table->searches++;
node = table->entries[index];
if (node != NULL)
{
if (node->hash_value == hash
&& HT_LEN (node) == (unsigned int) len
&& !memcmp (HT_STR (node), str, len))
{
if (insert == HT_ALLOCED)
obstack_free (&table->stack, (void *) str);
return node;
}
hash2 = ((hash * 17) & sizemask) | 1;
for (;;)
{
table->collisions++;
index = (index + hash2) & sizemask;
node = table->entries[index];
if (node == NULL)
break;
if (node->hash_value == hash
&& HT_LEN (node) == (unsigned int) len
&& !memcmp (HT_STR (node), str, len))
{
if (insert == HT_ALLOCED)
obstack_free (&table->stack, (void *) str);
return node;
}
}
}
if (insert == HT_NO_INSERT)
return NULL;
node = (*table->alloc_node) (table);
table->entries[index] = node;
HT_LEN (node) = (unsigned int) len;
node->hash_value = hash;
if (insert == HT_ALLOC)
HT_STR (node) = obstack_copy0 (&table->stack, str, len);
else
HT_STR (node) = str;
if (++table->nelements * 4 >= table->nslots * 3)
ht_expand (table);
return node;
}
static void
ht_expand (hash_table *table)
{
hashnode *nentries, *p, *limit;
unsigned int size, sizemask;
size = table->nslots * 2;
nentries = xcalloc (size, sizeof (hashnode));
sizemask = size - 1;
p = table->entries;
limit = p + table->nslots;
do
if (*p)
{
unsigned int index, hash, hash2;
hash = (*p)->hash_value;
index = hash & sizemask;
if (nentries[index])
{
hash2 = ((hash * 17) & sizemask) | 1;
do
{
index = (index + hash2) & sizemask;
}
while (nentries[index]);
}
nentries[index] = *p;
}
while (++p < limit);
if (table->entries_owned)
free (table->entries);
table->entries_owned = true;
table->entries = nentries;
table->nslots = size;
}
void
ht_forall (hash_table *table, ht_cb cb, const void *v)
{
hashnode *p, *limit;
p = table->entries;
limit = p + table->nslots;
do
if (*p)
{
if ((*cb) (table->pfile, *p, v) == 0)
break;
}
while (++p < limit);
}
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;
}
void
ht_dump_statistics (hash_table *table)
{
size_t nelts, nids, overhead, headers;
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 \
? (x) \
: ((x) < 1024*1024*10 \
? (x) / 1024 \
: (x) / (1024*1024))))
#define LABEL(x) ((x) < 1024*10 ? ' ' : ((x) < 1024*1024*10 ? 'k' : 'M'))
total_bytes = longest = sum_of_squares = nids = 0;
p = table->entries;
limit = p + table->nslots;
do
if (*p)
{
size_t n = HT_LEN (*p);
total_bytes += n;
sum_of_squares += (double) n * n;
if (n > longest)
longest = n;
nids++;
}
while (++p < limit);
nelts = table->nelements;
overhead = obstack_memory_used (&table->stack) - total_bytes;
headers = table->nslots * sizeof (hashnode);
fprintf (stderr, "\nString pool\nentries\t\t%lu\n",
(unsigned long) nelts);
fprintf (stderr, "identifiers\t%lu (%.2f%%)\n",
(unsigned long) nids, nids * 100.0 / nelts);
fprintf (stderr, "slots\t\t%lu\n",
(unsigned long) table->nslots);
fprintf (stderr, "bytes\t\t%lu%c (%lu%c overhead)\n",
SCALE (total_bytes), LABEL (total_bytes),
SCALE (overhead), LABEL (overhead));
fprintf (stderr, "table size\t%lu%c\n",
SCALE (headers), LABEL (headers));
exp_len = (double)total_bytes / (double)nelts;
exp2_len = exp_len * exp_len;
exp_len2 = (double) sum_of_squares / (double) nelts;
fprintf (stderr, "coll/search\t%.4f\n",
(double) table->collisions / (double) table->searches);
fprintf (stderr, "ins/search\t%.4f\n",
(double) nelts / (double) table->searches);
fprintf (stderr, "avg. entry\t%.2f bytes (+/- %.2f)\n",
exp_len, approx_sqrt (exp_len2 - exp2_len));
fprintf (stderr, "longest entry\t%lu\n",
(unsigned long) longest);
#undef SCALE
#undef LABEL
}
static double
approx_sqrt (double x)
{
double s, d;
if (x < 0)
abort ();
if (x == 0)
return 0;
s = x;
do
{
d = (s * s - x) / (2 * s);
s -= d;
}
while (d > .0001);
return s;
}