java ByteArray
Seems like this class is mysteriously missing... not a big deal to implement, with all the String functionality.
Weird. Will go ahead and do it.
Weird. Will go ahead and do it.
public class ByteArray implements List<Byte>, Comparable<ByteArray>, CharSequence, Serializable { private static final long serialVersionUID = -3654555805387023818L; private ByteBuffer storage; ... /** * Rabin-Karp string search * {@see http://en.wikipedia.org/wiki/Rabin%E2%80%93Karp_string_search_algorithm} * @param subarray the one we are looking for * @return the position at which the subarray starts in current array */ public int indexOf(ByteArray subarray) { int n = subarray.size(); if (n == 0) { return 0; } int hash = subarray.hashCode(); int rollingHash = hashCode(0, n); int m = pow(n); for (int i = 0; i < size() - n; i++) { if (hash == rollingHash && subarrayEquals(subarray, i)) { return i; } rollingHash = ((rollingHash - m * get(i)) * HASH_PRIME) + get(i + n); } return -1; }