--- /dev/null 2016-01-22 18:25:55.206427233 -0800 +++ new/src/java.base/share/classes/java/util/regex/IntHashSet.java 2016-03-03 21:34:12.179727073 -0800 @@ -0,0 +1,100 @@ +package java.util.regex; + +import java.util.Arrays; + +/** + * A lightweight hashset implementation for positive 'int'. Not safe for + * concurrent access. + */ +class IntHashSet { + private int[] entries; + private int[] hashes; + private int pos = 0; + + public IntHashSet() { + this.entries = new int[16 << 1]; // initCapacity = 16; + this.hashes = new int[(16 / 2) | 1]; // odd -> fewer collisions + Arrays.fill(this.entries, -1); + Arrays.fill(this.hashes, -1); + } + + public boolean contains(int i) { + int h = hashes[i % hashes.length]; + while (h != -1) { + if (entries[h] == i) + return true; + h = entries[h + 1]; + } + return false; + } + + public void add(int i) { + int h0 = i % hashes.length; + int next = hashes[h0]; + // if invoker guarantees contains(i) checked before add(i) + // the following check is not needed. + int next0 = next; + while (next0 != -1) { + if (entries[next0 ] == i) + return; + next0 = entries[next0 + 1]; + } + hashes[h0] = pos; + entries[pos++] = i; + entries[pos++] = next; + if (pos == entries.length) + expand(); + } + + public void clear() { + Arrays.fill(this.entries, -1); + Arrays.fill(this.hashes, -1); + pos = 0; + } + + private void expand() { + int[] old = entries; + int[] es = new int[old.length << 1]; + int hlen = (old.length / 2) | 1; + int[] hs = new int[hlen]; + Arrays.fill(es, -1); + Arrays.fill(hs, -1); + for (int n = 0; n < pos;) { // re-hashing + int i = old[n]; + int hsh = i % hlen; + int next = hs[hsh]; + hs[hsh] = n; + es[n++] = i; + es[n++] = next; + } + this.entries = es; + this.hashes = hs; + } + + /* + public static void main(String[] args) throws Throwable { + java.util.Random r = new java.util.Random(); + int max = 10000; + for (int i = 0; i < 10000; i++) { + int[] data = new int[r.nextInt(max)]; + for (int j = 0; j < data.length; j++) { + data[j] = r.nextInt(max); + } + test(data, max); + } + } + + private static void test(int[] data, int max) throws Throwable { + java.util.HashSet hs = new java.util.HashSet<>(); + IntHashSet ihs = new IntHashSet(); + for (int i = 0; i < data.length; i++) { + hs.add(data[i]); + ihs.add(data[i]); + } + for (int i = 0; i < max; i++) { + if (hs.contains(i) != ihs.contains(i)) + throw new RuntimeException("failed!"); + } + } + */ +} \ No newline at end of file