# HG changeset patch # User Christian Wimmer # Date 1429744717 25200 # Node ID 814bd3dbc61544e74d6e2caabc96e1d8dd430a8f # Parent 71af9afd7ff67d91518449fe101b0db79dafb47e Manage null value manually to work around bug in older JDK versions diff -r 71af9afd7ff6 -r 814bd3dbc615 graal/com.oracle.graal.compiler.common/src/com/oracle/graal/compiler/common/util/FrequencyEncoder.java --- a/graal/com.oracle.graal.compiler.common/src/com/oracle/graal/compiler/common/util/FrequencyEncoder.java Wed Apr 22 13:05:36 2015 -0700 +++ b/graal/com.oracle.graal.compiler.common/src/com/oracle/graal/compiler/common/util/FrequencyEncoder.java Wed Apr 22 16:18:37 2015 -0700 @@ -46,6 +46,7 @@ } protected final Map> map; + protected boolean containsNull; /** * Creates an encoder that uses object identity. @@ -69,17 +70,17 @@ * Adds an object to the array. */ public void addObject(T object) { + if (object == null) { + containsNull = true; + return; + } + Entry entry = map.get(object); if (entry == null) { entry = new Entry<>(object); map.put(object, entry); } - if (object == null) { - /* null must get index 0, so sort it up. */ - entry.frequency = Integer.MAX_VALUE; - } else { - entry.frequency++; - } + entry.frequency++; } /** @@ -87,6 +88,10 @@ * {@link #addObject(Object) added} before. */ public int getIndex(Object object) { + if (object == null) { + assert containsNull; + return 0; + } Entry entry = map.get(object); assert entry != null && entry.index >= 0; return entry.index; @@ -96,7 +101,7 @@ * Returns the number of distinct objects that have been added, i.e., the length of the array. */ public int getLength() { - return map.size(); + return map.size() + (containsNull ? 1 : 0); } /** @@ -104,14 +109,21 @@ * correct length}. */ public T[] encodeAll(T[] allObjects) { - assert allObjects.length == map.size(); + assert allObjects.length == getLength(); List> sortedEntries = new ArrayList<>(map.values()); sortedEntries.sort((e1, e2) -> -Integer.compare(e1.frequency, e2.frequency)); + + int offset = 0; + if (containsNull) { + allObjects[0] = null; + offset = 1; + } for (int i = 0; i < sortedEntries.size(); i++) { Entry entry = sortedEntries.get(i); - entry.index = i; - allObjects[i] = entry.object; - assert entry.object != null || entry.index == 0; + int index = i + offset; + entry.index = index; + allObjects[index] = entry.object; + assert entry.object != null; } return allObjects; }