All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.amygdalum.util.text.StringSet Maven / Gradle / Ivy

Go to download

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

There is a newer version: 0.4.4
Show newest version
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));
		}
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy