X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?a=blobdiff_plain;f=src%2F3dengfx%2Fsrc%2Fcommon%2Fstring_hash.cpp;fp=src%2F3dengfx%2Fsrc%2Fcommon%2Fstring_hash.cpp;h=9146299bfe8de5b07527206be4864db37c4d1450;hb=6e23259dbabaeb1711a2a5ca25b9cb421f693759;hp=0000000000000000000000000000000000000000;hpb=fe068fa879814784c45e0cb2e65dac489e8f5594;p=summerhack diff --git a/src/3dengfx/src/common/string_hash.cpp b/src/3dengfx/src/common/string_hash.cpp new file mode 100644 index 0000000..9146299 --- /dev/null +++ b/src/3dengfx/src/common/string_hash.cpp @@ -0,0 +1,28 @@ +// This code belongs to the public domain. + +#include +#include + +/* + * 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); +}