diff options
-rw-r--r-- | Makefile.am | 5 | ||||
-rw-r--r-- | hash-test.c | 32 | ||||
-rw-r--r-- | hash.c | 8 | ||||
-rw-r--r-- | libnul.h | 4 |
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)); + } +} + + @@ -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 @@ -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, |