summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSøren Sandmann Pedersen <sandmann@daimi.au.dk>2009-08-29 14:31:11 -0400
committerSøren Sandmann Pedersen <sandmann@daimi.au.dk>2009-08-29 14:31:11 -0400
commit636993cbd190a067a364aaa86587c2bb4cd435f3 (patch)
treeee54902be5e122c8d6b827e4d24d51db633b43fa
parent5aef73cb7d023436f954906b84e61a58115a07f3 (diff)
Add hash-test.c. Some hash bug fixes.
-rw-r--r--Makefile.am5
-rw-r--r--hash-test.c32
-rw-r--r--hash.c8
-rw-r--r--libnul.h4
4 files changed, 42 insertions, 7 deletions
diff --git a/Makefile.am b/Makefile.am
index 2a917c7..abc5e53 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -27,7 +27,7 @@ libnul_la_LIBADD = @DEP_LIBS@ libffi/libffi_convenience.la
libnul_la_LDFLAGS = -no-undefined
-noinst_PROGRAMS = example dbw-example prefix-test
+noinst_PROGRAMS = example dbw-example prefix-test hash-test
example_SOURCES = example.c
example_LDADD = $(top_builddir)/libnul.la
@@ -38,6 +38,9 @@ dbw_example_LDADD = $(top_builddir)/libnul.la
prefix_test_SOURCES = prefix-test.c
prefix_test_LDADD = $(top_builddir)/libnul.la
+hash_test_SOURCES = hash-test.c
+hash_test_LDADD = $(top_builddir)/libnul.la
+
pkgconfigdir = $(libdir)/pkgconfig
pkgconfig_DATA = libnul.pc
diff --git a/hash-test.c b/hash-test.c
new file mode 100644
index 0000000..ae44d42
--- /dev/null
+++ b/hash-test.c
@@ -0,0 +1,32 @@
+#include "libnul.h"
+
+static size_t
+direct_hash (nul_const_ptr_t *ptr)
+{
+ return (size_t)ptr;
+}
+
+static nul_bool_t
+direct_equal (nul_const_ptr_t *ptr1, nul_const_ptr_t *ptr2)
+{
+ return ptr1 == ptr2;
+}
+
+int
+main ()
+{
+ nul_hash_t *hash = nul_hash_new (direct_hash, direct_equal, NULL, NULL);
+ int i;
+
+ for (i = 0; i < 1234; ++i)
+ {
+ nul_hash_insert (hash, (nul_ptr_t)i, (nul_ptr_t)i);
+ }
+
+ for (i = 0; i < 1234; ++i)
+ {
+ g_assert (nul_hash_has_key (hash, (nul_ptr_t)i));
+ }
+}
+
+
diff --git a/hash.c b/hash.c
index e7fb77a..d5c6291 100644
--- a/hash.c
+++ b/hash.c
@@ -120,7 +120,8 @@ rehash (nul_hash_t *hash)
{
hash_entry_t *entry = &(old_entries[t]);
- insert_internal (hash, entry->key, entry->value);
+ if (entry->value != hash->free_marker && entry->value != hash->dead_marker)
+ insert_internal (hash, entry->key, entry->value);
}
nul_array_free (old_entries);
@@ -210,9 +211,8 @@ need_rehash (nul_hash_t *hash)
int n_items = hash->n_items;
return
- (n_items * HIGH_OCCUPATION > table_size ||
- n_items * LOW_OCCUPATION < table_size) &&
- table_size > MIN_SIZE;
+ n_items * HIGH_OCCUPATION > table_size ||
+ (n_items * LOW_OCCUPATION < table_size && table_size > MIN_SIZE);
}
void
diff --git a/libnul.h b/libnul.h
index 3f01a01..c266be4 100644
--- a/libnul.h
+++ b/libnul.h
@@ -149,11 +149,11 @@ void nul_buffer_delete_tail (nul_buffer_t *queue,
* Hash tables
*/
typedef struct nul_hash_t nul_hash_t;
+typedef size_t (* nul_hash_func_t) (nul_const_ptr_t key);
+
typedef nul_bool_t (* nul_hash_equal_func_t) (nul_const_ptr_t key1,
nul_const_ptr_t key2);
-typedef uint32_t (* nul_hash_func_t) (nul_const_ptr_t key);
-
typedef void (* nul_free_func_t) (nul_ptr_t data);
nul_hash_t *nul_hash_new (nul_hash_func_t hash,