added 3dengfx into the repo, probably not the correct version for this
[summerhack] / src / 3dengfx / src / common / string_hash.cpp
diff --git a/src/3dengfx/src/common/string_hash.cpp b/src/3dengfx/src/common/string_hash.cpp
new file mode 100644 (file)
index 0000000..9146299
--- /dev/null
@@ -0,0 +1,28 @@
+// This code belongs to the public domain.
+
+#include <string>
+#include <string.h>
+
+/*
+ * Hashing algorithm for strings from:
+ * Sedgewick's "Algorithms in C++, third edition" 
+ * parts 1-4, Chapter 14 (hashing) p.593
+ * 
+ * Modified to work on C++ string class objects by John Tsiombikas
+ */
+unsigned int string_hash(const std::string &key, unsigned long size) {
+       int hash = 0, a = 31415, b = 27183;
+       char *str = new char[key.length() + 1];
+       strcpy(str, key.c_str());
+
+       char *sptr = str;
+       
+       while(*sptr) {
+               hash = (a * hash + *sptr++) % size;
+               a = a * b % (size - 1);
+       }
+       
+       delete [] str;
+       
+       return (unsigned int)(hash < 0 ? (hash + size) : hash);
+}