From 689b3f9b8fb68218515c05b3b7b13ff4e21c6c76 Mon Sep 17 00:00:00 2001 From: Justus Winter <4winter@informatik.uni-hamburg.de> Date: Thu, 15 May 2014 13:53:16 +0200 Subject: libihash: add hurd_ihash_get_load * libihash/ihash.c (hurd_ihash_add): Move the code computing the load factor of the hash table... * libihash/ihash.h (hurd_ihash_get_load): ... here, together with the comment describing the method and the rationale for chosing binary percent. --- libihash/ihash.c | 20 ++------------------ 1 file changed, 2 insertions(+), 18 deletions(-) (limited to 'libihash/ihash.c') diff --git a/libihash/ihash.c b/libihash/ihash.c index f20ba613..4d9cc18e 100644 --- a/libihash/ihash.c +++ b/libihash/ihash.c @@ -273,24 +273,8 @@ hurd_ihash_add (hurd_ihash_t ht, hurd_ihash_key_t key, hurd_ihash_value_t item) if (ht->size) { - /* Only fill the hash table up to its maximum load factor given - as "binary percent", where 128b% corresponds to 100%. As the - size is always a power of two, and 128 is also, the quotient - of both is also a power of two. Therefore, we can use bit - shifts to scale the number of items. - - load = nr_items * 128 / size - = nr_items * 2^{log2 (128) - log2 (size)} - = nr_items >> (log2 (size) - log2 (128)) - -- if size >= 128 - = nr_items << (log2 (128) - log2 (size)) - -- otherwise - */ - int d = __builtin_ctzl (ht->size) - 7; - unsigned int load = d >= 0 - ? ht->nr_items >> d - : ht->nr_items << -d; - if (load <= ht->max_load) + /* Only fill the hash table up to its maximum load factor. */ + if (hurd_ihash_get_load (ht) <= ht->max_load) if (add_one (ht, key, item)) return 0; } -- cgit v1.2.3