Advantages of hash search:
When the length of the searched string is smaller than the length of the machine word, the string can be compared as long. Because the shift operation is quite fast (one clock cycle), it takes less time to execute the operation.
This search method is not universally available. (Big-Endian or little-Endian issues ). However, it is undeniable that the search speed is indeed fast.
// Search Algorithm for "csdn"
// The train of thought is: Compare "csdn" by integer. Shift the target string for each comparison.
Void dwordsearch (const char pstr, const char * Str)
{
Const int * pTARGET = (int *) STR;
Const int hash = * (int *) pstr;
// Number of occurrences
Size_t Index = 0;
Size_t index2 = 0;
Vector <size_t> poscollect;
// If it is not a multiple of integers, save the tail and the last Int.
Int Len = strlen (STR)-strlen (STR) % sizeof (INT );
Const char * end = STR + Len;
If (* end)
{
Cout <"tail" <Endl;
}
// Judge whether the start is equal or not
//
If (hash = pTARGET [Index])
{
Cout <"found" <Endl;
}
// Judge whether the end is equal or not
// Add it later.
For (Index = 0; index <Len/4-1; index ++)
{
For (index2 = 8; index2 <= 32; index2 + = 8)
{
// Shift left? Shift to the right? Big-endding and small-endding are different.
If (hash = (pTARGET [Index]> index2) | (pTARGET [index + 1] <(32-index2 ))))
{
Break;
}
}
If (index2 <= 32)
{
Poscollect. push_back (Index * 4 + index2/8 );
}
}
Cout <"Times found:" <poscollect. Size () <Endl;
For (vector <size_t>: const_iterator iter = poscollect. Begin (); iter! = Poscollect. End (); ITER ++)
{
Cout <* ITER <Endl;
}
System ("pause ");
}