added 3dengfx into the repo, probably not the correct version for this
[summerhack] / src / 3dengfx / src / common / string_hash.cpp
1 // This code belongs to the public domain.
2
3 #include <string>
4 #include <string.h>
5
6 /*
7  * Hashing algorithm for strings from:
8  * Sedgewick's "Algorithms in C++, third edition" 
9  * parts 1-4, Chapter 14 (hashing) p.593
10  * 
11  * Modified to work on C++ string class objects by John Tsiombikas
12  */
13 unsigned int string_hash(const std::string &key, unsigned long size) {
14         int hash = 0, a = 31415, b = 27183;
15         char *str = new char[key.length() + 1];
16         strcpy(str, key.c_str());
17
18         char *sptr = str;
19         
20         while(*sptr) {
21                 hash = (a * hash + *sptr++) % size;
22                 a = a * b % (size - 1);
23         }
24         
25         delete [] str;
26         
27         return (unsigned int)(hash < 0 ? (hash + size) : hash);
28 }