about summary refs log tree commit homepage
path: root/ext/tdb/djb.c
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2010-11-26 02:27:17 +0000
committerEric Wong <normalperson@yhbt.net>2010-12-01 09:45:32 +0000
commit58938c980f38a4581b4a0e8a780fffe7ac95bc93 (patch)
treeece822520cf8cbabe23f4559361afaf8346aa729 /ext/tdb/djb.c
downloadruby-tdb-58938c980f38a4581b4a0e8a780fffe7ac95bc93.tar.gz
Diffstat (limited to 'ext/tdb/djb.c')
-rw-r--r--ext/tdb/djb.c26
1 files changed, 26 insertions, 0 deletions
diff --git a/ext/tdb/djb.c b/ext/tdb/djb.c
new file mode 100644
index 0000000..83abe34
--- /dev/null
+++ b/ext/tdb/djb.c
@@ -0,0 +1,26 @@
+#include "rbtdb.h"
+
+unsigned int rbtdb_djb2(TDB_DATA *data)
+{
+        unsigned char *key = data->dptr;
+        size_t len = data->dsize;
+        unsigned int hash = 5381;
+        unsigned int i;
+
+        for (i = 0; i < len; ++i)
+                hash = ((hash << 5) + hash) + key[i]; /* (hash*33) + key[i] */
+
+        return hash;
+}
+unsigned int rbtdb_djb3(TDB_DATA *data)
+{
+        unsigned char *key = data->dptr;
+        size_t len = data->dsize;
+        unsigned int hash = 5381;
+        unsigned int i;
+
+        for (i = 0; i < len; ++i)
+                hash = ((hash << 5) + hash) ^ key[i]; /* (hash*33) ^ key[i] */
+
+        return hash;
+}