| | |
String Hash Function
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2009
Posts: 19
Reputation:
Solved Threads: 0
I am trying to optimize my hash function, for a separate chaining hash table that I have created from scratch. My hash function is as follows:
I was wondering if one could point me to either a better hash function or how I should go about modifying this function.
C++ Syntax (Toggle Plain Text)
int hash( const string &key, int tableSize) { int hashVal = 0; for(int i = 0; i<key.length(); i++) hashVal = 37*hashVal+key[i]; hashVal %= tableSize; if(hashVal<0) hashVal += tableSize; return hashVal; }
0
#2 Oct 21st, 2009
SAX was designed for hashing strings and is supposed to be really good for it:
Your algorithm is a common variation of a good hash that uses 33 instead of 37. It should work well in the general case.
C++ Syntax (Toggle Plain Text)
unsigned hash(string const& key, unsigned tableSize) { unsigned hashVal = 0; for (int x = 0; x < key.length(); ++x) { hashVal ^= (hashVal << 5) + (hashVal >> 2) + key[x]; } return hashVal % tableSize; }
-Tommy (For Great Justice!) Gunn
0
#4 Oct 21st, 2009
•
•
•
•
Thanks, but when I implemented your hash function it took nearly twice as long.
-Tommy (For Great Justice!) Gunn
•
•
Join Date: Sep 2009
Posts: 19
Reputation:
Solved Threads: 0
0
#7 Oct 21st, 2009
I was trying to play around with the FNV function but my program keeps seg faulting
static const unsigned InitialFNV = 2166136261;
static const unsigned FNVMultiple = 16777619;
// /* Fowler / Noll / Vo (FNV) Hash */
unsigned HashTable::hashFunc(const string &s)const
{
unsigned hash = InitialFNV;
for(unsigned i = 0; i < s.length(); i++)
{
hash = hash ^ (s[i]); /* xor the low 8 bits */
hash = hash * FNVMultiple; /* multiply by the magic number */
}
return hash;
}
static const unsigned InitialFNV = 2166136261;
static const unsigned FNVMultiple = 16777619;
// /* Fowler / Noll / Vo (FNV) Hash */
unsigned HashTable::hashFunc(const string &s)const
{
unsigned hash = InitialFNV;
for(unsigned i = 0; i < s.length(); i++)
{
hash = hash ^ (s[i]); /* xor the low 8 bits */
hash = hash * FNVMultiple; /* multiply by the magic number */
}
return hash;
}
•
•
Join Date: Sep 2009
Posts: 19
Reputation:
Solved Threads: 0
0
#8 Oct 21st, 2009
This was actually the original function I found online, but I was trying to modify it in order to get it to work with my code. However I am unfamiliar with size_t and the U at the end of the InitialFNV initialization.
So can someone explain the U and size_t please.
So can someone explain the U and size_t please.
C++ Syntax (Toggle Plain Text)
static const size_t InitialFNV = 2166136261U; static const size_t FNVMultiple = 16777619; /* Fowler / Noll / Vo (FNV) Hash */ size_t myhash(const string &s) { size_t hash = InitialFNV; for(size_t i = 0; i < s.length(); i++) { hash = hash ^ (s[i]); /* xor the low 8 bits */ hash = hash * FNVMultiple; /* multiply by the magic number */ } return hash; }
Last edited by killerqb; Oct 21st, 2009 at 1:43 pm.
1
#9 Oct 21st, 2009
The U means that it's an unsigned integer literal. size_t is an unsigned integer type that's guaranteed to be able to hold the size of an array. But not a std::string. If you're paranoid you should use std::string::size_type. Don't be.
Your program is not segfaulting because of the hash function. The error is elsewhere.
Your program is not segfaulting because of the hash function. The error is elsewhere.
All my posts may be redistributed under the GNU Free Documentation License.
![]() |
Similar Threads
- string array to function (C++)
- help with a hash function... (C++)
- hash function (C++)
- Help regarding PJW hash function (Java)
- Clarify returning string/pointer from function (C++)
- Simple Hash Function for Integer Arrays (C)
Other Threads in the C++ Forum
- Previous Thread: Efficient Way to Write a Bunch of Or's?
- Next Thread: Create Process
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count data database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






