summaryrefslogtreecommitdiff
path: root/source/fitz/hash.c
diff options
context:
space:
mode:
authorTor Andersson <tor.andersson@artifex.com>2016-12-28 14:57:11 +0100
committerTor Andersson <tor.andersson@artifex.com>2017-01-17 17:15:59 +0100
commit9322cd236f2c74461ab5d20e0a399035051628ae (patch)
treec621f06eed60f5eb473afe315eb630cbca712825 /source/fitz/hash.c
parent2d8d711df455bf5cc5a7c7ba08c98867df3f33ef (diff)
downloadmupdf-9322cd236f2c74461ab5d20e0a399035051628ae.tar.xz
Add value destructor callback to fz_hash_table.
This allows us to prune the iteration functions. We also remove a few other unused functions.
Diffstat (limited to 'source/fitz/hash.c')
-rw-r--r--source/fitz/hash.c88
1 files changed, 20 insertions, 68 deletions
diff --git a/source/fitz/hash.c b/source/fitz/hash.c
index e16aefd1..7e4f49f1 100644
--- a/source/fitz/hash.c
+++ b/source/fitz/hash.c
@@ -23,6 +23,7 @@ struct fz_hash_table_s
int size;
int load;
int lock; /* -1 or the lock used to protect this hash table */
+ fz_hash_table_drop_fn drop_val;
fz_hash_entry *ents;
};
@@ -43,7 +44,7 @@ static unsigned hash(const unsigned char *s, int len)
}
fz_hash_table *
-fz_new_hash_table(fz_context *ctx, int initialsize, int keylen, int lock)
+fz_new_hash_table(fz_context *ctx, int initialsize, int keylen, int lock, fz_hash_table_drop_fn drop_val)
{
fz_hash_table *table;
@@ -54,6 +55,7 @@ fz_new_hash_table(fz_context *ctx, int initialsize, int keylen, int lock)
table->size = initialsize;
table->load = 0;
table->lock = lock;
+ table->drop_val = drop_val;
fz_try(ctx)
{
table->ents = fz_malloc_array(ctx, table->size, sizeof(fz_hash_entry));
@@ -69,42 +71,28 @@ fz_new_hash_table(fz_context *ctx, int initialsize, int keylen, int lock)
}
void
-fz_empty_hash(fz_context *ctx, fz_hash_table *table)
-{
- table->load = 0;
- memset(table->ents, 0, sizeof(fz_hash_entry) * table->size);
-}
-
-int
-fz_hash_len(fz_context *ctx, fz_hash_table *table)
-{
- return table->size;
-}
-
-void *
-fz_hash_get_key(fz_context *ctx, fz_hash_table *table, int idx)
-{
- return table->ents[idx].key;
-}
-
-void *
-fz_hash_get_val(fz_context *ctx, fz_hash_table *table, int idx)
-{
- return table->ents[idx].val;
-}
-
-void
-fz_drop_hash(fz_context *ctx, fz_hash_table *table)
+fz_drop_hash_table(fz_context *ctx, fz_hash_table *table)
{
if (!table)
return;
+ if (table->drop_val)
+ {
+ int i, n = table->size;
+ for (i = 0; i < n; ++i)
+ {
+ void *v = table->ents[i].val;
+ if (v)
+ table->drop_val(ctx, v);
+ }
+ }
+
fz_free(ctx, table->ents);
fz_free(ctx, table);
}
static void *
-do_hash_insert(fz_context *ctx, fz_hash_table *table, const void *key, void *val, unsigned *pos_ptr)
+do_hash_insert(fz_context *ctx, fz_hash_table *table, const void *key, void *val)
{
fz_hash_entry *ents;
unsigned size;
@@ -124,19 +112,13 @@ do_hash_insert(fz_context *ctx, fz_hash_table *table, const void *key, void *val
memcpy(ents[pos].key, key, table->keylen);
ents[pos].val = val;
table->load ++;
- if (pos_ptr)
- *pos_ptr = pos;
return NULL;
}
if (memcmp(key, ents[pos].key, table->keylen) == 0)
{
- /* This is legal, but should happen rarely in the non
- * pos_ptr case. */
- if (pos_ptr)
- *pos_ptr = pos;
- else
- fz_warn(ctx, "assert: overwrite hash slot");
+ /* This is legal, but should happen rarely. */
+ fz_warn(ctx, "assert: overwrite hash slot");
return ents[pos].val;
}
@@ -190,7 +172,7 @@ fz_resize_hash(fz_context *ctx, fz_hash_table *table, int newsize)
{
if (oldents[i].val)
{
- do_hash_insert(ctx, table, oldents[i].key, oldents[i].val, NULL);
+ do_hash_insert(ctx, table, oldents[i].key, oldents[i].val);
}
}
@@ -227,22 +209,8 @@ void *
fz_hash_insert(fz_context *ctx, fz_hash_table *table, const void *key, void *val)
{
if (table->load > table->size * 8 / 10)
- {
fz_resize_hash(ctx, table, table->size * 2);
- }
-
- return do_hash_insert(ctx, table, key, val, NULL);
-}
-
-void *
-fz_hash_insert_with_pos(fz_context *ctx, fz_hash_table *table, const void *key, void *val, unsigned *pos)
-{
- if (table->load > table->size * 8 / 10)
- {
- fz_resize_hash(ctx, table, table->size * 2);
- }
-
- return do_hash_insert(ctx, table, key, val, pos);
+ return do_hash_insert(ctx, table, key, val);
}
static void
@@ -312,22 +280,6 @@ fz_hash_remove(fz_context *ctx, fz_hash_table *table, const void *key)
}
void
-fz_hash_remove_fast(fz_context *ctx, fz_hash_table *table, const void *key, unsigned pos)
-{
- fz_hash_entry *ents = table->ents;
-
- if (ents[pos].val == NULL || memcmp(key, ents[pos].key, table->keylen) != 0)
- {
- /* The value isn't there, or the key didn't match! The table
- * must have been rebuilt (or the contents moved) in the
- * meantime. Do the removal the slow way. */
- fz_hash_remove(ctx, table, key);
- }
- else
- do_removal(ctx, table, key, pos);
-}
-
-void
fz_print_hash(fz_context *ctx, fz_output *out, fz_hash_table *table)
{
fz_print_hash_details(ctx, out, table, NULL, 0);