本文整理汇总了Java中net.byteseek.matcher.sequence.SequenceMatcher类的典型用法代码示例。如果您正苦于以下问题:Java SequenceMatcher类的具体用法?Java SequenceMatcher怎么用?Java SequenceMatcher使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
SequenceMatcher类属于net.byteseek.matcher.sequence包,在下文中一共展示了SequenceMatcher类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: find
import net.byteseek.matcher.sequence.SequenceMatcher; //导入依赖的package包/类
@Override
public List<Integer> find(String pattern, String text) {
Searcher<SequenceMatcher> searcher = algorithm.get(pattern);
byte[] stringBytes = text.getBytes(StandardCharsets.UTF_8);
ForwardSearchIterator<SequenceMatcher> searchIterator = new ForwardSearchIterator<SequenceMatcher>(searcher, stringBytes, 0);
List<Integer> indexes = new ArrayList<>();
long lastPosition = -1;
while (searchIterator.hasNext()) {
List<SearchResult<SequenceMatcher>> results = searchIterator.next();
for (SearchResult<SequenceMatcher> result : results) {
long pos = result.getMatchPosition();
if (pos >= lastPosition) {
indexes.add((int) pos);
lastPosition = pos + result.getMatchingObject().length();
}
}
}
return indexes;
}
示例2: find
import net.byteseek.matcher.sequence.SequenceMatcher; //导入依赖的package包/类
@Override
public List<Integer> find(String text) {
byte[] stringBytes = text.getBytes();
ForwardSearchIterator<SequenceMatcher> searchIterator = new ForwardSearchIterator<SequenceMatcher>(searcher, stringBytes, 0);
List<Integer> indexes = new ArrayList<>();
long lastPosition = -1;
while (searchIterator.hasNext()) {
List<SearchResult<SequenceMatcher>> results = searchIterator.next();
for (SearchResult<SequenceMatcher> result : results) {
long pos = result.getMatchPosition();
if (pos >= lastPosition) {
indexes.add((int) pos);
lastPosition = pos + result.getMatchingObject().length();
}
}
}
return indexes;
}
示例3: searchBackwards
import net.byteseek.matcher.sequence.SequenceMatcher; //导入依赖的package包/类
/**
* {@inheritDoc}
*/
@Override
public List<SearchResult<SequenceMatcher>> searchBackwards(final byte[] bytes,
final int fromPosition, final int toPosition) {
// Initialise:
final MultiSequenceMatcher matcher = sequences;
// Calculate safe bounds for the search:
final int lastPosition = toPosition > 0?
toPosition : 0;
final int firstPossiblePosition = bytes.length - sequences.getMinimumLength();
int searchPosition = fromPosition < firstPossiblePosition?
fromPosition : firstPossiblePosition;
// Search backwards:
while (searchPosition >= lastPosition) {
final Collection<SequenceMatcher> matches = matcher.allMatches(bytes, searchPosition);
if (!matches.isEmpty()) {
return SearchUtils.resultsAtPosition(searchPosition, matches);
}
searchPosition--;
}
return SearchUtils.noResults();
}
示例4: allMatches
import net.byteseek.matcher.sequence.SequenceMatcher; //导入依赖的package包/类
/**
* {@inheritDoc}
*/
@Override
public Collection<SequenceMatcher> allMatches(final byte[] bytes, final int matchPosition) {
List<SequenceMatcher> result = Collections.emptyList();
final int noOfBytes = bytes.length;
final int minimumLength = trie.getMinimumLength();
if (matchPosition >= minimumLength - 1 && matchPosition + minimumLength < noOfBytes) {
State<SequenceMatcher> state = trie.getInitialState();
int currentPosition = matchPosition;
while (state != null && currentPosition < noOfBytes) {
final byte currentByte = bytes[currentPosition++];
state = state.getNextState(currentByte);
if (state != null && state.isFinal()) {
final Collection<SequenceMatcher> matching = state.getAssociations();
if (result.isEmpty()) {
result = new ArrayList<SequenceMatcher>(matching.size() * 2);
}
result.addAll(matching);
}
}
}
return result;
}
示例5: allMatchesBackwards
import net.byteseek.matcher.sequence.SequenceMatcher; //导入依赖的package包/类
/**
* {@inheritDoc}
*/
@Override
public Collection<SequenceMatcher> allMatchesBackwards(final byte[] bytes,
final int matchPosition) {
List<SequenceMatcher> result = Collections.emptyList();
final int noOfBytes = bytes.length;
final int minimumLength = trie.getMinimumLength();
if (matchPosition >= minimumLength - 1 && matchPosition + minimumLength < noOfBytes) {
State<SequenceMatcher> state = trie.getInitialState();
int currentPosition = matchPosition;
while (state != null && currentPosition >= 0) {
final byte currentByte = bytes[currentPosition--];
state = state.getNextState(currentByte);
if (state != null && state.isFinal()) {
final Collection<SequenceMatcher> matching = state.getAssociations();
if (result.isEmpty()) {
result = new ArrayList<SequenceMatcher>(matching.size() * 2);
}
result.addAll(matching);
}
}
}
return result;
}
示例6: firstMatch
import net.byteseek.matcher.sequence.SequenceMatcher; //导入依赖的package包/类
/**
* {@inheritDoc}
*/
@Override
public SequenceMatcher firstMatch(final WindowReader reader, final long matchPosition)
throws IOException {
State<SequenceMatcher> state = trie.getInitialState();
long currentPosition = matchPosition;
Window window = reader.getWindow(matchPosition);
while (window != null) {
final int windowLength = window.length();
final byte[] array = window.getArray();
int windowPosition = reader.getWindowOffset(currentPosition);
while (windowPosition < windowLength) {
final byte currentByte = array[windowPosition++];
state = state.getNextState(currentByte);
if (state == null) {
return null;
}
if (state.isFinal()) {
return getFirstAssociation(state);
}
}
currentPosition += windowLength;
window = reader.getWindow(matchPosition);
}
return null;
}
示例7: firstMatchBackwards
import net.byteseek.matcher.sequence.SequenceMatcher; //导入依赖的package包/类
/**
* {@inheritDoc}
*/
@Override
public SequenceMatcher firstMatchBackwards(final WindowReader reader, final long matchPosition)
throws IOException {
State<SequenceMatcher> state = trie.getInitialState();
long currentPosition = matchPosition;
Window window = reader.getWindow(matchPosition);
while (window != null) {
final int windowLength = window.length();
final byte[] array = window.getArray();
int windowPosition = reader.getWindowOffset(currentPosition);
while (windowPosition >= 0) {
final byte currentByte = array[windowPosition--];
state = state.getNextState(currentByte);
if (state == null) {
return null;
}
if (state.isFinal()) {
return getFirstAssociation(state);
}
}
currentPosition -= windowLength;
window = reader.getWindow(matchPosition);
}
return null;
}
示例8: searchForwards
import net.byteseek.matcher.sequence.SequenceMatcher; //导入依赖的package包/类
/**
* {@inheritDoc}
*/
@Override
public List<SearchResult<SequenceMatcher>> searchForwards(final byte[] bytes, final int fromPosition, final int toPosition) {
// Initialise:
final SequenceMatcher sequence = matcher;
// Calculate safe bounds for the search:
final int lastPossiblePosition = bytes.length - sequence.length();
final int lastPosition = toPosition < lastPossiblePosition?
toPosition : lastPossiblePosition;
int searchPosition = fromPosition > 0?
fromPosition : 0;
// Search forwards
while (searchPosition <= lastPosition) {
if (sequence.matchesNoBoundsCheck(bytes, searchPosition)) {
return SearchUtils.singleResult(searchPosition, sequence);
}
searchPosition++;
}
return SearchUtils.noResults();
}
示例9: profileMultiSequence
import net.byteseek.matcher.sequence.SequenceMatcher; //导入依赖的package包/类
private void profileMultiSequence(int numberOfTimes, String... matcherArgs) throws IOException {
final List<SequenceMatcher> matchers = new ArrayList<SequenceMatcher>();
final StringBuilder description = new StringBuilder();
description.append('[');
boolean first = true;
for (String str : matcherArgs) {
matchers.add(new ByteSequenceMatcher(str));
if (!first)
description.append(',');
description.append('\'').append(str).append('\'');
first = false;
}
description.append(']');
lastResultCount = profileSearchers(description.toString(), numberOfTimes,
getMultiSequenceSearchers(getMultiSequenceMatcher(matchers)));
}
示例10: ListMultiSequenceMatcher
import net.byteseek.matcher.sequence.SequenceMatcher; //导入依赖的package包/类
/**
* Constructs a ListMultiSequenceMatcher from a list of byte arrays.
* <p>
* The byte arrays will be cloned when constructing {@link ByteSequenceMatcher}s
* from them to be used in this matcher. If the list of byte arrays is empty
* then a ListMultiSequenceMatcher is constructed which will not match anything.
*
* @param bytesToMatch A list of byte arrays from which to construct the
* ListMultiSequenceMatcher.
* @throws IllegalArgumentException if the list passed in is null, any of the
* byte arrays in the list is null, or any of the byte arrays in the
* list have a length of zero.
*/
public ListMultiSequenceMatcher(final List<byte[]> bytesToMatch) {
ArgUtils.checkNullObject(bytesToMatch, "bytesToMatch");
matchers = new ArrayList<SequenceMatcher>(bytesToMatch.size());
for (final byte[] bytes : bytesToMatch) {
ArgUtils.checkNullObject(bytes, "A byte array in the list of arrays.");
final SequenceMatcher sequence = new ByteSequenceMatcher(bytes);
matchers.add(sequence);
}
if (matchers.isEmpty()) {
minimumLength = 0;
maximumLength = 0;
} else {
int minLength = Integer.MAX_VALUE;
int maxLength = Integer.MIN_VALUE;
for (final SequenceMatcher matcher : matchers) {
final int length = matcher.length();
minLength = Math.min(minLength, length);
maxLength = Math.max(maxLength, length);
}
minimumLength = minLength;
maximumLength = maxLength;
}
}
示例11: allMatchesBackwards
import net.byteseek.matcher.sequence.SequenceMatcher; //导入依赖的package包/类
/**
* {@inheritDoc}
*/
@Override
public Collection<SequenceMatcher> allMatchesBackwards(final WindowReader reader,
final long matchPosition) throws IOException {
List<SequenceMatcher> result = Collections.emptyList();
final List<SequenceMatcher> localMatchers = matchers;
final long onePastMatchPosition = matchPosition + 1;
for (final SequenceMatcher sequence : localMatchers) {
if (sequence.matches(reader, onePastMatchPosition - sequence.length())) {
if (result.isEmpty()) {
result = new ArrayList<SequenceMatcher>(2);
}
result.add(sequence);
}
}
return result;
}
示例12: HashMultiSequenceMatcher
import net.byteseek.matcher.sequence.SequenceMatcher; //导入依赖的package包/类
/**
* Private constructor to prevent construction. This class is currently only
* a shell with no real implementation.
*
* @param matchers
*/
private HashMultiSequenceMatcher(final Collection<? extends SequenceMatcher> matchers) {
// Store matcher list:
ArgUtils.checkNullOrEmptyCollection(matchers, "matchers");
this.matchers = new ArrayList<SequenceMatcher>(matchers.size());
//Calculate min length, max length and the hash of each matcher:
hashTable = createHashTableFor(this.matchers);
int currentMin = Integer.MAX_VALUE;
int currentMax = Integer.MIN_VALUE;
for (final SequenceMatcher matcher : matchers) {
final int length = matcher.length();
if (length < currentMin) currentMin = length;
if (length > currentMax) currentMax = length;
addHashFor(matcher);
}
minimumLength = currentMin;
maximumLength = currentMax;
}
示例13: bytesAlignedRight
import net.byteseek.matcher.sequence.SequenceMatcher; //导入依赖的package包/类
/**
* Returns the set of bytes found by aligning all the {@link SequenceMatcher}s
* in a MultiSequenceMatcher to the right, and taking a position from the right
* hand side. Zero is the rightmost position.
*
* @param atPosition The position from the right hand side to find the superset of matching bytes.
* @param matcher The MultiSequenceMatcher to find the matching bytes from.
* @return A set of bytes containing the superset of all bytes matching at the position
* given in all the SequenceMatchers contained in the MultiSequenceMatcher, aligned right.
*/
public static Set<Byte> bytesAlignedRight(final int atPosition,
final MultiSequenceMatcher matcher) {
final Set<Byte> bytes = new HashSet<Byte>();
if (atPosition >= 0 && atPosition < matcher.getMaximumLength()) {
for (final SequenceMatcher sequence : matcher.getSequenceMatchers()) {
final int sequencePosition = sequence.length() - atPosition - 1;
if (sequencePosition >= 0) {
final ByteMatcher lastMatcher = sequence.getMatcherForPosition(sequencePosition);
final byte[] bytesForPosition = lastMatcher.getMatchingBytes();
ByteUtils.addAll(bytesForPosition, bytes);
}
}
}
return bytes;
}
示例14: create
import net.byteseek.matcher.sequence.SequenceMatcher; //导入依赖的package包/类
/**
* Calculates the safe shifts to use if searching backwards.
* A safe shift is either the length of the sequence plus one, if the
* byte does not appear in the {@link SequenceMatcher}, or
* the shortest distance it appears from the beginning of the matcher.
*/
@Override
public int[] create() {
// First set the default shift to the length of the sequence
// (negative if search direction is reversed)
final int[] shifts = new int[256];
final SequenceMatcher sequence = getMatcher();
final int numBytes = sequence.length();
Arrays.fill(shifts, numBytes + 1);
// Now set specific byte shifts for the bytes actually in
// the sequence itself. The shift is the distance of each character
// from the start of the sequence, where the first position equals 1.
// Each position can match more than one byte (e.g. if a byte class appears).
for (int sequenceByteIndex = numBytes - 1; sequenceByteIndex >= 0; sequenceByteIndex--) {
final ByteMatcher aMatcher = sequence.getMatcherForPosition(sequenceByteIndex);
final byte[] matchingBytes = aMatcher.getMatchingBytes();
final int distanceFromStart = sequenceByteIndex + 1;
for (final byte b : matchingBytes) {
shifts[b & 0xFF] = distanceFromStart;
}
}
return shifts;
}
示例15: preparePattern
import net.byteseek.matcher.sequence.SequenceMatcher; //导入依赖的package包/类
private Searcher<SequenceMatcher> preparePattern(Set<String> patterns) {
List<SequenceMatcher> matchers = patterns.stream()
.map(pattern -> new ByteSequenceMatcher(pattern))
.collect(toList());
MultiSequenceMatcher matcher = new ListMultiSequenceMatcher(matchers);
Searcher<SequenceMatcher> searcher = create(matcher);
searcher.prepareForwards();
return searcher;
}