Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

58
LINES

< > BotCompany Repo | #1013280 // BoyerMooreStringSearch

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (3826L/25K).

/**
 *  Finds the first occurrence of a pattern string
 *  in a text string.
 *  <p>
 *  This implementation uses the Boyer-Moore algorithm (with the bad-character
 *  rule, but not the strong good suffix rule).
 *  <p>
 *  For additional documentation,
 *  see <a href="https://algs4.cs.princeton.edu/53substring">Section 5.3</a> of
 *  <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne.
 *  Copyright © 2000–2017, Robert Sedgewick and Kevin Wayne. 
 *  Last updated: Fri Oct 20 12:50:46 EDT 2017.
 */
sclass BoyerMooreStringSearch {
  final static int R = 256;     // the radix
  final int[] right;     // the bad-character skip array
  final char[] pat;      // pattern

  *(S pat) {
    this.pat = toCharArray(pat);

    // position of rightmost occurrence of c in the pattern
    right = new int[R];
    for (int j = 0; j < this.pat.length; j++)
        right[this.pat[j] & (R-1)] = j+1;
  }

  /**
   * Returns the index of the first occurrrence of the pattern string
   * in the text string.
   *
   * @param  txt the text string
   * @return the index of the first occurrence of the pattern string
   *         in the text string; -1 if no such match
   */
  int search(S txt) {
    if (txt == null) ret -1;
    int m = pat.length;
    int diff = txt.length()-m;
    int skip;
    for (int i = 0; i <= diff; i += skip) {
      skip = 0;
      for (int j = m-1; j >= 0; j--) {
        int c = txt.charAt(i+j);
        if (pat[j] != c) {
          skip = Math.max(1, j - (right[c & (R-1)]-1));
          break;
        }
      }
      if (skip == 0) ret i;
    }
    ret -1;
  }
  
  bool containedIn(S txt) {
    ret search(txt) >= 0;
  }
}

Author comment

Began life as a copy of #1013279

download  show line numbers  debug dex  old transpilations   

Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1013280
Snippet name: BoyerMooreStringSearch
Eternal ID of this version: #1013280/14
Text MD5: 8777b5ffa28fc7662d008e425c030d24
Transpilation MD5: 3b3d5c653f76db94c6cc1af2bd5c7a32
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-05-23 14:13:14
Source code size: 1726 bytes / 58 lines
Pitched / IR pitched: No / No
Views / Downloads: 463 / 1119
Version history: 13 change(s)
Referenced in: #1020539 - BoyerMoore_chars - boyer-moore search on char arrays
#1028143 - BoyerMooreStringSearch_upper
#1034167 - Standard Classes + Interfaces (LIVE, continuation of #1003674)