MakeSkip.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #include <stdio.h>
  2. #include "bm.h"
  3. extern char *malloc();
  4. MakeSkip(Pattern,Skip1,Skip2,PatLen)
  5. char Pattern[];
  6. unsigned short int Skip1[], Skip2[];
  7. int PatLen;
  8. /* generate the skip tables for Boyer-Moore string search algorithm.
  9. * Skip1 is the skip depending on the character which failed to match
  10. * the pattern, and Skip2 is the skip depending on how far we got into
  11. * the pattern. Pattern is the search pattern and PatLen is strlen(Pattern) */
  12. {
  13. int *BackTrack; /* backtracking table for t when building skip2 */
  14. int c; /* general purpose constant */
  15. int j,k,t,tp; /* indices into Skip's and BackTrack */
  16. if (!(BackTrack = (int *) malloc(PatLen * (sizeof (int))))){
  17. fprintf(stderr,"bm: can't allocate space\n");
  18. exit(2);
  19. } /* if */
  20. for (c=0; c<=MAXCHAR; ++c)
  21. Skip1[c] = PatLen;
  22. for (k=0;k<PatLen;k++) {
  23. Skip1[Pattern[k]] = PatLen - k - 1;
  24. Skip2[k] = 2 * PatLen - k - 1;
  25. } /* for */
  26. for (j=PatLen - 1,t=PatLen;j >= 0; --j,--t) {
  27. BackTrack[j] = t;
  28. while (t<PatLen && Pattern[j] != Pattern[t]) {
  29. Skip2[t] = min(Skip2[t], PatLen - j - 1);
  30. t = BackTrack[t];
  31. } /* while */
  32. } /* for */
  33. for (k=0;k<=t;++k)
  34. Skip2[k] = min(Skip2[k],PatLen+t-k);
  35. tp=BackTrack[t];
  36. while(tp<PatLen) {
  37. while(t<PatLen) {
  38. Skip2[t] = min(Skip2[t],tp-t+PatLen);
  39. ++t;
  40. } /* while */
  41. tp = BackTrack[tp];
  42. } /* while */
  43. cfree(BackTrack);
  44. } /* MakeSkip */