summaryrefslogtreecommitdiff
path: root/libihash/ihash.h
diff options
context:
space:
mode:
authorJustus Winter <4winter@informatik.uni-hamburg.de>2015-11-21 16:27:40 +0100
committerJustus Winter <4winter@informatik.uni-hamburg.de>2015-11-29 23:57:56 +0100
commit2c4b1db9c9760205979d22b721c324cf215987da (patch)
tree69d48343c8f94621826d7334f6f38605cceca505 /libihash/ihash.h
parentf564e5f4a62fb8ca54695c722c7e04803df869ec (diff)
libihash: generalize the interface to support non-integer keys
* libihash/ihash.c (hash, compare): New functions that are used throughout libihash to hash and compare keys. (hurd_ihash_set_gki): New function. * libihash/ihash.h (hurd_ihash_fct_hash_t): New type for hash functions. (hurd_ihash_fct_cmp_t): New type for comparison functions. (struct hurd_ihash): New fields for hash and comparison functions. (HURD_IHASH_INITIALIZER_GKI): New static initializer. (hurd_ihash_set_gki): New prototype.
Diffstat (limited to 'libihash/ihash.h')
-rw-r--r--libihash/ihash.h36
1 files changed, 35 insertions, 1 deletions
diff --git a/libihash/ihash.h b/libihash/ihash.h
index fdfc3673..28fefe80 100644
--- a/libihash/ihash.h
+++ b/libihash/ihash.h
@@ -1,5 +1,5 @@
/* ihash.h - Integer keyed hash table interface.
- Copyright (C) 1995, 2003, 2004 Free Software Foundation, Inc.
+ Copyright (C) 1995, 2003, 2004, 2014, 2015 Free Software Foundation, Inc.
Written by Miles Bader <miles@gnu.org>.
Revised by Marcus Brinkmann <marcus@gnu.org>.
@@ -57,6 +57,20 @@ typedef uintptr_t hurd_ihash_key_t;
typedef hurd_ihash_value_t *hurd_ihash_locp_t;
+/* We support non-integer keys using the generalized key interface.
+
+ To use it, supply a pair of functions matching the following
+ specification, and use pointers to the key instead of the key
+ itself in all calls to libihash. */
+
+/* The type of a function computing a hash for the given key. */
+typedef hurd_ihash_key_t (*hurd_ihash_fct_hash_t) (const void *);
+
+/* The type of a function comparing two given keys. Return true if
+ both keys are equal. */
+typedef int (*hurd_ihash_fct_cmp_t) (const void *, const void *);
+
+
/* The type of the cleanup function, which is called for every value
removed from the hash table. */
typedef void (*hurd_ihash_cleanup_t) (hurd_ihash_value_t value, void *arg);
@@ -95,6 +109,10 @@ struct hurd_ihash
second argument. This does not happen if CLEANUP is NULL. */
hurd_ihash_cleanup_t cleanup;
void *cleanup_data;
+
+ /* User-supplied functions for the generalized key interface. */
+ hurd_ihash_fct_hash_t fct_hash;
+ hurd_ihash_fct_cmp_t fct_cmp;
};
typedef struct hurd_ihash *hurd_ihash_t;
@@ -118,6 +136,16 @@ typedef struct hurd_ihash *hurd_ihash_t;
.max_load = HURD_IHASH_MAX_LOAD_DEFAULT, \
.locp_offset = (locp_offs)}
+#define HURD_IHASH_INITIALIZER_GKI(locp_offs, f_clean, f_clean_data, \
+ f_hash, f_compare) \
+ { .nr_items = 0, .size = 0, \
+ .cleanup = (f_clean), \
+ .cleanup_data = (f_clean_data), \
+ .max_load = HURD_IHASH_MAX_LOAD_DEFAULT, \
+ .locp_offset = (locp_offs), \
+ .fct_hash = (f_hash), \
+ .fct_cmp = (f_compare)} \
+
/* Initialize the hash table at address HT. If LOCP_OFFSET is not
HURD_IHASH_NO_LOCP, then this is an offset (in bytes) from the
address of a hash value where a location pointer can be found. The
@@ -152,6 +180,12 @@ void hurd_ihash_free (hurd_ihash_t ht);
void hurd_ihash_set_cleanup (hurd_ihash_t ht, hurd_ihash_cleanup_t cleanup,
void *cleanup_data);
+/* Use the generalized key interface. Must be called before any item
+ is inserted into the table. */
+void hurd_ihash_set_gki (hurd_ihash_t ht,
+ hurd_ihash_fct_hash_t fct_hash,
+ hurd_ihash_fct_cmp_t fct_cmp);
+
/* Set the maximum load factor in binary percent to MAX_LOAD, which
should be between 64 and 128. The default is
HURD_IHASH_MAX_LOAD_DEFAULT. New elements are only added to the