Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Boyer Moore Algorithm
- * @param cPattern
- * @param cText
- * @param bMatchFirst indicates to match the first occurence
- * @return
- */
- public long[] BoyerMoore(char []cPattern,char []cText,boolean bMatchFirst)
- {
- int iComparisons = 0, iMatchCount = 0, iNoOfInstructions = 0,iShift = 0;
- long lResult[] = new long[4];
- long lEndTime = 0, lStartTime = System.nanoTime();;
- int iPtLengtth = cPattern.length, iTxLength = cText.length;
- int i = iPtLengtth - 1, j = iPtLengtth - 1;
- if(cText.length > cPattern.length)
- {
- do
- {
- iComparisons++;
- iNoOfInstructions++;
- if (cPattern[j] == cText[i])
- {
- iNoOfInstructions++;
- if (j == 0)
- {
- // Found a match
- iMatchCount++;
- iNoOfInstructions++;
- i = i + 1;
- iShift++;
- if(bMatchFirst)
- {
- iNoOfInstructions++;
- lEndTime=System.nanoTime();
- return CreateResultArray(iMatchCount,iComparisons,iNoOfInstructions,lStartTime,lEndTime);
- }
- }
- else
- {
- iNoOfInstructions++;
- i--;
- j--;
- }
- }
- else
- {
- iShift++;
- // lastIndexOf(char c) function is used as the last(c) function
- i = i + (iPtLengtth - Math.min(j, (1 + String.valueOf(cPattern).lastIndexOf(cText[i]))));
- j = iPtLengtth - 1;
- iNoOfInstructions++;
- }
- } while (i <= iTxLength - 1);
- lEndTime = System.nanoTime();
- return CreateResultArray(iMatchCount,iComparisons,iNoOfInstructions,lStartTime,lEndTime);
- }
- else
- {
- return CreateResultArray(iMatchCount,iComparisons,iNoOfInstructions,0,0);
- }
- }
- private long[] CreateResultArray(int iMatchCount,int iComparisons,int iNoOfInstructions,long lStartTime,long lEndTime)
- {
- long lResult[]=new long[4];
- lResult[0]=iMatchCount;
- lResult[1]=iComparisons;
- lResult[2]=iNoOfInstructions;
- lResult[3]=lEndTime-lStartTime;
- return lResult;
- }
Add Comment
Please, Sign In to add comment