net.amygdalum.util.text.StringSet Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of stringsearchalgorithms Show documentation
Show all versions of stringsearchalgorithms Show documentation
Searching and Matching Strings with efficient algorithms:
- Knuth-Morris-Pratt
- Shift-And/Or
- Boyer-Moore-Horspool
- Sunday (QuickSearch)
- BNDM
- BOM
- Aho-Corasick
- Set-Horspool
- Wu-Manber
- Set-BOM
package net.amygdalum.util.text;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
public class StringSet {
private Set[] strings;
private int[] lengths;
public StringSet(List chars) {
this.lengths = CharUtils.lengths(chars);
this.strings = computeStrings(lengths, chars);
}
@SuppressWarnings("unchecked")
private static Set[] computeStrings(int[] lengths, List chars) {
int max = lengths[0];
int min = lengths[lengths.length - 1];
Set[] strings = new Set[max - min + 1];
for (int len : lengths) {
strings[max - len] = new LinkedHashSet<>();
}
for (char[] pattern : chars) {
int len = pattern.length;
Set set = strings[max - len];
set.add(new String(pattern));
}
return strings;
}
public int minLength() {
return lengths[lengths.length - 1];
}
public int maxLength() {
return lengths[0];
}
public int[] containedLengths() {
return lengths;
}
public boolean contains(char[] pattern) {
int max = maxLength();
int len = pattern.length;
Set set = strings[max - len];
if (set == null) {
return false;
} else {
return set.contains(new String(pattern));
}
}
}