From: Martin Willi Date: Wed, 1 May 2013 10:13:28 +0000 (+0200) Subject: hashtable: add common hashtable hash/equals functions for pointer/string keys X-Git-Tag: 5.1.0rc1~25^2~37 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=437a6feb07eaf515c9384bb04aa1609ddb391a6d;p=thirdparty%2Fstrongswan.git hashtable: add common hashtable hash/equals functions for pointer/string keys --- diff --git a/src/libstrongswan/collections/hashtable.c b/src/libstrongswan/collections/hashtable.c index d181d8ec8d..1003aa0fa7 100644 --- a/src/libstrongswan/collections/hashtable.c +++ b/src/libstrongswan/collections/hashtable.c @@ -16,6 +16,8 @@ #include "hashtable.h" +#include + /** The maximum capacity of the hash table (MUST be a power of 2) */ #define MAX_CAPACITY (1 << 30) @@ -146,9 +148,40 @@ struct private_enumerator_t { * previous pair (used by remove_at) */ pair_t *prev; - }; +/* + * See header. + */ +u_int hashtable_hash_ptr(void *key) +{ + return chunk_hash(chunk_from_thing(key)); +} + +/* + * See header. + */ +u_int hashtable_hash_str(void *key) +{ + return chunk_hash(chunk_from_str((char*)key)); +} + +/* + * See header. + */ +bool hashtable_equals_ptr(void *key, void *other_key) +{ + return key == other_key; +} + +/* + * See header. + */ +bool hashtable_equals_str(void *key, void *other_key) +{ + return streq(key, other_key); +} + /** * This function returns the next-highest power of two for the given number. * The algorithm works by setting all bits on the right-hand side of the most @@ -441,4 +474,3 @@ hashtable_t *hashtable_create(hashtable_hash_t hash, hashtable_equals_t equals, return &this->public; } - diff --git a/src/libstrongswan/collections/hashtable.h b/src/libstrongswan/collections/hashtable.h index e38850ded2..520a86c901 100644 --- a/src/libstrongswan/collections/hashtable.h +++ b/src/libstrongswan/collections/hashtable.h @@ -33,6 +33,22 @@ typedef struct hashtable_t hashtable_t; */ typedef u_int (*hashtable_hash_t)(void *key); +/** + * Hashtable hash function calculation the hash solely based on the key pointer. + * + * @param key key to hash + * @return hash of key + */ +u_int hashtable_hash_ptr(void *key); + +/** + * Hashtable hash function calculation the hash for char* keys. + * + * @param key key to hash, a char* + * @return hash of key + */ +u_int hashtable_hash_str(void *key); + /** * Prototype for a function that compares the two keys for equality. * @@ -42,6 +58,24 @@ typedef u_int (*hashtable_hash_t)(void *key); */ typedef bool (*hashtable_equals_t)(void *key, void *other_key); +/** + * Hashtable equals function comparing pointers. + * + * @param key key to compare + * @param other_key other key to compare + * @return TRUE if key == other_key + */ +bool hashtable_equals_ptr(void *key, void *other_key); + +/** + * Hashtable equals function comparing char* keys. + * + * @param key key to compare + * @param other_key other key to compare + * @return TRUE if streq(key, other_key) + */ +bool hashtable_equals_str(void *key, void *other_key); + /** * Class implementing a hash table. * @@ -121,7 +155,6 @@ struct hashtable_t { * Destroys a hash table object. */ void (*destroy) (hashtable_t *this); - }; /**