summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChris Manton <cmanton@google.com>2014-09-19 09:08:32 -0700
committerAndre Eisenbach <eisenbach@google.com>2015-03-16 16:51:34 -0700
commit18b53d2e194a71e390020ddc5ffb10cce2d64ed3 (patch)
treeb2867874d701e04ae1ab811c1a14379046d50d18
parentc0e2f9927b8d60123b388c3d117e8f82c90d46e3 (diff)
downloadandroid_system_bt-18b53d2e194a71e390020ddc5ffb10cce2d64ed3.tar.gz
android_system_bt-18b53d2e194a71e390020ddc5ffb10cce2d64ed3.tar.bz2
android_system_bt-18b53d2e194a71e390020ddc5ffb10cce2d64ed3.zip
Add string hashing function using djb2
-rw-r--r--osi/include/hash_functions.h2
-rw-r--r--osi/src/hash_functions.c11
2 files changed, 13 insertions, 0 deletions
diff --git a/osi/include/hash_functions.h b/osi/include/hash_functions.h
index 012e15c9d..ad7d0953d 100644
--- a/osi/include/hash_functions.h
+++ b/osi/include/hash_functions.h
@@ -26,3 +26,5 @@ hash_index_t hash_function_integer(const void *key);
// Hashes a pointer based only on its address value
hash_index_t hash_function_pointer(const void *key);
+
+hash_index_t hash_function_string(const void *key);
diff --git a/osi/src/hash_functions.c b/osi/src/hash_functions.c
index fac2c41c5..f279bd456 100644
--- a/osi/src/hash_functions.c
+++ b/osi/src/hash_functions.c
@@ -16,6 +16,8 @@
*
******************************************************************************/
+#include <string.h>
+
#include "hash_functions.h"
hash_index_t hash_function_naive(const void *key) {
@@ -29,3 +31,12 @@ hash_index_t hash_function_integer(const void *key) {
hash_index_t hash_function_pointer(const void *key) {
return ((hash_index_t)key) * 2654435761;
}
+
+hash_index_t hash_function_string(const void *key) {
+ hash_index_t hash = 5381;
+ const char *name = (const char *)key;
+ size_t string_len = strlen(name);
+ for (size_t i = 0; i < string_len; ++i)
+ hash = ((hash << 5) + hash ) + name[i];
+ return hash;
+}