Recently has been doing string hash lookup, someone recommended the use of Gperf perfect hash, so to investigate the use of Gperf methods.
Gperf download Http://www.gnu.org/software/gperf/,ubuntu can be directly sudo apt-get install GPERF, the possible way is not up to date.
Gperf Document Http://www.gnu.org/software/gperf/manual/gperf.html#Functions
I mainly refer to the "use of gperf to achieve efficient C + + command line processing."
Gperf use:
Mainly the declarations and keyword parts, if simply find the existence of a string, you do not need the Declarations section,. gperf file directly save all the strings in the hash table. However, the hash table is usually the key and value pairs, such as in the declaration part of the Declaration of a struct, and then two percent after the keyword. The format is as follows:
struct KeyValue
{
const char * key;//The first field must be a const char *
int value; //can have more value
};
%
key1,1
key2,2
key3,3
Keyword Part if the first character is%, the string must be enclosed in double quotes, such as%key, which is "%key".
Then use the Gperf command to generate the HPP file:
Gperf-cgd-k key-l C + +-T xxx.gperf > PERFECTHASH.HPP
The generated header file has Perfect_hash classes, and the class name can be modified with the-Z option.
Class has a hash function, the default is hash (), can be modified through-h option.
The lookup function is In_word_set (), modified with-N.
-l Specifies the programming language.
If you have a declaration section, you must use the-t option.
-k Specifies the name of the keyword in struct.
The concrete principle does not make the detailed explanation, the Perfect hash should be quick, has not actually tested.
Problem:
Gperf just put the keywords in the. gperf file in the form of a structure array in the code file, eliminating the time to load the file, but feel that this approach is only suitable for small-scale hash lookups, such as C + + command line processing in the reference. For the hundreds of thousands of or millions of string index pairs I am dealing with, it is not appropriate to use GPERF for processing.
Do not know whether someone has handled a large string hash lookup, whether there is a better solution, please enlighten me, thank you.