This algorithm is a good choice for searching large, forward-only access streams for repeated search using pre-processed small to medium-sized patterns.
This is because in addition to the facts that it:
String#indexOf(java.lang.String, int) .
In particular, its higher average performance is biased toward larger search patterns, due to its ability to skip ahead further and with fewer tests under reverse pattern scan. But when searching forward-only access streams, overall performance considerations require the use a circular buffer of the same size as the search pattern to hold data from the searched stream as it is being compared in reverse order to the search pattern. Hence, Boyer-Moore requires at minimum twice the memory required by Knuth-Morris-Pratt to search for the same pattern and that factor has the greatest impact precisely on the same class of patterns (larger) for which it is most outperforms Knuth-Morris-Pratt.
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public static int[] | Returns: the table computed from the octet pattern.for which to compute the table. pattern)computes the table used to optimize octet pattern search |
| public static int[] | Returns: the table computed from the character pattern.for which to compute the table. pattern)computes the table used to optimize octet pattern search |
| public static int[] | Returns: the table computed from the String pattern.for which to compute the table. pattern)computes the table used to optimize octet pattern search |
| public static long | Returns: zero-based offset of first match; -1 ifinputStream == null
|| pattern == null or no match found, ; zero (0) if
pattern.length == 0.in which to search inputStream, final byte[] for which to search pattern, int[] computed from the pattern that optimizes the search.
If null, automatically computed. table)Searches the given octet stream for the given octet pattern returning the zero-based offset from the initial stream position at which the first match is detected. |
| public static long | Returns: zero-based offset of first match; -1 ifreader == null
|| pattern == null or no match found, ; zero (0) if
pattern.length == 0.in which to search reader, final char[] for which to search pattern, int[] computed from the pattern that optimizes the search
If null, automatically computed. table)Searches the given character stream for the given character pattern returning the zero-based offset from the initial stream position at which the first match is detected. |
| public static long | Returns: zero-based offset of first match; -1 ifreader == null
|| pattern == null or no match found, ; zero (0) if
pattern.length() == 0.in which to search reader, final String for which to search pattern, int[] computed from the pattern that optimizes the search
If null, automatically computed. table)Searches the given character stream for the given character pattern returning the zero-based offset from the initial stream position at which the first match is detected. |
| public static int | Returns: zero-based offset of first match; -1 ifsource == null
|| pattern == null or no match found, ; start if
pattern.length == 0 and start <= source.length.array in which to search source, final byte[] to be matched pattern, int[] computed from the pattern that optimizes the search
If null, automatically computed. table, final int position in source at which to start the search start)Searches the given octet string for the given octet pattern returning the zero-based offset from given start position at which the first match is detected. |
| public static int | Returns: zero-based offset of first match; -1 ifsource == null
|| pattern == null or no match found, ; start if
pattern.length == 0 and start <= source.length.array in which to search source, final char[] to be matched pattern, int[] computed from the pattern that optimizes the search
If null, automatically computed. table, final int position in source at which to start the search start)Searches the given character array for the given character pattern returning the zero-based offset from given start position at which the first match is detected. |
| public static int | Returns: zero-based offset of first match; -1 ifsource == null
|| pattern == null or no match found, ; start if
pattern.length == 0 and start <= source.length.array to be searched source, final String to be matched pattern, int[] computed from the pattern that optimizes the search table, final int position in source at which to start the search start)Searches the given String object for the given character pattern returning the zero-based offset from given start position at which the first match is detected. |
| KMPSearchAlgorithm | back to summary |
|---|---|
| private KMPSearchAlgorithm() | |
| computeTable | back to summary |
|---|---|
| public static int[] computeTable(final byte[] pattern) computes the table used to optimize octet pattern search
| |
| computeTable | back to summary |
|---|---|
| public static int[] computeTable(final char[] pattern) computes the table used to optimize octet pattern search
| |
| computeTable | back to summary |
|---|---|
| public static int[] computeTable(final String pattern) computes the table used to optimize octet pattern search
| |
| search | back to summary |
|---|---|
| public static long search(final InputStream inputStream, final byte[] pattern, int[] table) throws IOException Searches the given octet stream for the given octet pattern returning the zero-based offset from the initial stream position at which the first match is detected. Note that the signature includes a slot for the table so that searches for a pattern can be performed multiple times without incurring the overhead of computing the table each time.
| |
| search | back to summary |
|---|---|
| public static long search(final Reader reader, final char[] pattern, int[] table) throws IOException Searches the given character stream for the given character pattern returning the zero-based offset from the initial stream position at which the first match is detected. Note that the signature includes a slot for the table so that searches for a pattern can be performed multiple times without incurring the overhead of computing the table each time.
| |
| search | back to summary |
|---|---|
| public static long search(final Reader reader, final String pattern, int[] table) throws IOException Searches the given character stream for the given character pattern returning the zero-based offset from the initial stream position at which the first match is detected. Note that the signature includes a slot for the table so that searches for a pattern can be performed multiple times without incurring the overhead of computing the table each time.
| |
| search | back to summary |
|---|---|
| public static int search(final byte[] source, final byte[] pattern, int[] table, final int start) Searches the given octet string for the given octet pattern returning the zero-based offset from given start position at which the first match is detected. Note that the signature includes a slot for the table so that searches for a pattern can be performed multiple times without incurring the overhead of computing the table each time.
| |
| search | back to summary |
|---|---|
| public static int search(final char[] source, final char[] pattern, int[] table, final int start) Searches the given character array for the given character pattern returning the zero-based offset from given start position at which the first match is detected.
| |
| search | back to summary |
|---|---|
| public static int search(final String source, final String pattern, int[] table, final int start) Searches the given String object for the given character pattern returning the zero-based offset from given start position at which the first match is detected.
| |