Hash query string

Source: Internet
Author: User

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 ");

}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.