Guest User

Untitled

a guest
Nov 19th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. /**
  2. * Boyer Moore Algorithm
  3. * @param cPattern
  4. * @param cText
  5. * @param bMatchFirst indicates to match the first occurence
  6. * @return
  7. */
  8. public long[] BoyerMoore(char []cPattern,char []cText,boolean bMatchFirst)
  9. {
  10. int iComparisons = 0, iMatchCount = 0, iNoOfInstructions = 0,iShift = 0;
  11. long lResult[] = new long[4];
  12. long lEndTime = 0, lStartTime = System.nanoTime();;
  13. int iPtLengtth = cPattern.length, iTxLength = cText.length;
  14. int i = iPtLengtth - 1, j = iPtLengtth - 1;
  15. if(cText.length > cPattern.length)
  16. {
  17.  
  18. do
  19. {
  20. iComparisons++;
  21. iNoOfInstructions++;
  22. if (cPattern[j] == cText[i])
  23. {
  24. iNoOfInstructions++;
  25. if (j == 0)
  26. {
  27. // Found a match
  28. iMatchCount++;
  29. iNoOfInstructions++;
  30. i = i + 1;
  31. iShift++;
  32. if(bMatchFirst)
  33. {
  34. iNoOfInstructions++;
  35. lEndTime=System.nanoTime();
  36. return CreateResultArray(iMatchCount,iComparisons,iNoOfInstructions,lStartTime,lEndTime);
  37. }
  38. }
  39. else
  40. {
  41. iNoOfInstructions++;
  42. i--;
  43. j--;
  44. }
  45. }
  46. else
  47. {
  48. iShift++;
  49. // lastIndexOf(char c) function is used as the last(c) function
  50. i = i + (iPtLengtth - Math.min(j, (1 + String.valueOf(cPattern).lastIndexOf(cText[i]))));
  51. j = iPtLengtth - 1;
  52. iNoOfInstructions++;
  53. }
  54. } while (i <= iTxLength - 1);
  55. lEndTime = System.nanoTime();
  56. return CreateResultArray(iMatchCount,iComparisons,iNoOfInstructions,lStartTime,lEndTime);
  57. }
  58. else
  59. {
  60. return CreateResultArray(iMatchCount,iComparisons,iNoOfInstructions,0,0);
  61. }
  62. }
  63.  
  64. private long[] CreateResultArray(int iMatchCount,int iComparisons,int iNoOfInstructions,long lStartTime,long lEndTime)
  65. {
  66. long lResult[]=new long[4];
  67. lResult[0]=iMatchCount;
  68. lResult[1]=iComparisons;
  69. lResult[2]=iNoOfInstructions;
  70. lResult[3]=lEndTime-lStartTime;
  71. return lResult;
  72.  
  73. }
Add Comment
Please, Sign In to add comment