From 1ce6b69651c3300b0ac4d8f6578c5760ca472c9d Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Sun, 16 Sep 2012 13:37:44 -0500 Subject: [PATCH] Fix build warnings in FlexibleHashMap, reduce entry size --- .../v4/runtime/misc/FlexibleHashMap.java | 68 +++++++++++-------- 1 file changed, 38 insertions(+), 30 deletions(-) diff --git a/runtime/Java/src/org/antlr/v4/runtime/misc/FlexibleHashMap.java b/runtime/Java/src/org/antlr/v4/runtime/misc/FlexibleHashMap.java index f3bd5bf2a..657870aa7 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/misc/FlexibleHashMap.java +++ b/runtime/Java/src/org/antlr/v4/runtime/misc/FlexibleHashMap.java @@ -15,9 +15,9 @@ public class FlexibleHashMap implements EquivalenceMap { public static final int INITAL_BUCKET_CAPACITY = 8; public static final double LOAD_FACTOR = 0.75; - public class Entry { - K key; - V value; + public static class Entry { + public final K key; + public V value; public Entry(K key, V value) { this.key = key; this.value = value; } @@ -27,7 +27,7 @@ public class FlexibleHashMap implements EquivalenceMap { } } - protected LinkedList[] buckets; + protected LinkedList>[] buckets; /** How many elements in set */ protected int n = 0; @@ -42,13 +42,19 @@ public class FlexibleHashMap implements EquivalenceMap { } public FlexibleHashMap(int initialCapacity, int initialBucketCapacity) { - buckets = (LinkedList[])new LinkedList[initialCapacity]; + buckets = createEntryListArray(initialBucketCapacity); this.initialBucketCapacity = initialBucketCapacity; } + private static LinkedList>[] createEntryListArray(int length) { + @SuppressWarnings("unchecked") + LinkedList>[] result = (LinkedList>[])new LinkedList[length]; + return result; + } + @Override - public boolean equals(K a, K b) { - return a.equals(b); + public boolean equals(K keyA, K keyB) { + return keyA.equals(keyB); } @Override @@ -63,14 +69,15 @@ public class FlexibleHashMap implements EquivalenceMap { } @Override - public V get(Object o) { - K key = (K)o; + public V get(Object key) { + @SuppressWarnings("unchecked") + K typedKey = (K)key; if ( key==null ) return null; - int b = getBucket(key); - LinkedList bucket = buckets[b]; + int b = getBucket(typedKey); + LinkedList> bucket = buckets[b]; if ( bucket==null ) return null; // no bucket - for (Entry e : bucket) { - if ( equals(e.key, key) ) return e.value; // use special equals + for (Entry e : bucket) { + if ( equals(e.key, typedKey) ) return e.value; // use special equals } return null; } @@ -80,11 +87,11 @@ public class FlexibleHashMap implements EquivalenceMap { if ( key==null ) return null; if ( n > threshold ) expand(); int b = getBucket(key); - LinkedList bucket = buckets[b]; + LinkedList> bucket = buckets[b]; if ( bucket==null ) { - bucket = buckets[b] = new LinkedList(); + bucket = buckets[b] = new LinkedList>(); } - for (Entry e : bucket) { + for (Entry e : bucket) { if ( equals(e.key, key) ) { V prev = e.value; e.value = value; @@ -93,7 +100,7 @@ public class FlexibleHashMap implements EquivalenceMap { } } // not there - bucket.add(new Entry(key, value)); + bucket.add(new Entry(key, value)); n++; return null; } @@ -116,9 +123,9 @@ public class FlexibleHashMap implements EquivalenceMap { @Override public Collection values() { List a = new ArrayList(size()); - for (LinkedList bucket : buckets) { + for (LinkedList> bucket : buckets) { if ( bucket==null ) continue; - for (Entry e : bucket) { + for (Entry e : bucket) { a.add(e.value); } } @@ -143,9 +150,9 @@ public class FlexibleHashMap implements EquivalenceMap { @Override public int hashCode() { int h = 0; - for (LinkedList bucket : buckets) { + for (LinkedList> bucket : buckets) { if ( bucket==null ) continue; - for (Entry e : bucket) { + for (Entry e : bucket) { if ( e==null ) break; h += hashCode(e.key); } @@ -159,18 +166,18 @@ public class FlexibleHashMap implements EquivalenceMap { } protected void expand() { - LinkedList[] old = buckets; + LinkedList>[] old = buckets; currentPrime += 4; int newCapacity = buckets.length * 2; - LinkedList[] newTable = (LinkedList[])new LinkedList[newCapacity]; + LinkedList>[] newTable = createEntryListArray(newCapacity); buckets = newTable; threshold = (int)(newCapacity * LOAD_FACTOR); // System.out.println("new size="+newCapacity+", thres="+threshold); // rehash all existing entries int oldSize = size(); - for (LinkedList bucket : old) { + for (LinkedList> bucket : old) { if ( bucket==null ) continue; - for (Entry e : bucket) { + for (Entry e : bucket) { if ( e==null ) break; put(e.key, e.value); } @@ -190,19 +197,20 @@ public class FlexibleHashMap implements EquivalenceMap { @Override public void clear() { - buckets = (LinkedList[])new LinkedList[INITAL_CAPACITY]; + buckets = createEntryListArray(INITAL_CAPACITY); n = 0; } + @Override public String toString() { if ( size()==0 ) return "{}"; StringBuilder buf = new StringBuilder(); buf.append('{'); boolean first = true; - for (LinkedList bucket : buckets) { + for (LinkedList> bucket : buckets) { if ( bucket==null ) continue; - for (Entry e : bucket) { + for (Entry e : bucket) { if ( e==null ) break; if ( first ) first=false; else buf.append(", "); @@ -215,14 +223,14 @@ public class FlexibleHashMap implements EquivalenceMap { public String toTableString() { StringBuilder buf = new StringBuilder(); - for (LinkedList bucket : buckets) { + for (LinkedList> bucket : buckets) { if ( bucket==null ) { buf.append("null\n"); continue; } buf.append('['); boolean first = true; - for (Entry e : bucket) { + for (Entry e : bucket) { if ( first ) first=false; else buf.append(" "); if ( e==null ) buf.append("_");