Skip to content
Snippets Groups Projects
Commit 2df17f7f authored by Christian Wulf's avatar Christian Wulf
Browse files

improved performance of the CountingMap

parent b0fb800b
No related branches found
No related tags found
No related merge requests found
#FindBugs User Preferences #FindBugs User Preferences
#Thu Mar 19 12:32:13 CET 2015 #Thu Mar 19 17:05:30 CET 2015
detector_threshold=3 detector_threshold=3
effort=max effort=max
excludefilter0=.fbExcludeFilterFile|true excludefilter0=.fbExcludeFilterFile|true
......
...@@ -114,6 +114,12 @@ ...@@ -114,6 +114,12 @@
<version>1.0</version> <version>1.0</version>
<!-- <version>1.1-SNAPSHOT</version> --> <!-- <version>1.1-SNAPSHOT</version> -->
</dependency> </dependency>
<dependency>
<groupId>com.carrotsearch</groupId>
<artifactId>hppc</artifactId>
<version>0.6.0</version>
</dependency>
</dependencies> </dependencies>
<build> <build>
......
...@@ -15,12 +15,7 @@ ...@@ -15,12 +15,7 @@
*/ */
package teetime.stage; package teetime.stage;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import teetime.framework.AbstractConsumerStage; import teetime.framework.AbstractConsumerStage;
import teetime.framework.OutputPort;
import teetime.stage.util.CountingMap; import teetime.stage.util.CountingMap;
/** /**
...@@ -36,25 +31,15 @@ import teetime.stage.util.CountingMap; ...@@ -36,25 +31,15 @@ import teetime.stage.util.CountingMap;
*/ */
public final class CountingMapMerger<T> extends AbstractConsumerStage<CountingMap<T>> { public final class CountingMapMerger<T> extends AbstractConsumerStage<CountingMap<T>> {
private final CountingMap<T> result = new CountingMap<T>(); private final CountingMap<T> mergedResult = new CountingMap<T>();
private final OutputPort<Map<T, Integer>> port = createOutputPort();
@Override @Override
protected void execute(final CountingMap<T> element) { protected void execute(final CountingMap<T> element) {
Set<Map.Entry<T, Integer>> entries = element.entrySet(); mergedResult.add(element);
for (Entry<T, Integer> entry : entries) {
result.add(entry.getKey(), entry.getValue());
}
}
@Override
public void onTerminating() throws Exception {
port.send(result);
super.onTerminating();
} }
public CountingMap<T> getResult() { public CountingMap<T> getResult() {
return result; return mergedResult;
} }
} }
...@@ -15,25 +15,23 @@ ...@@ -15,25 +15,23 @@
*/ */
package teetime.stage.util; package teetime.stage.util;
import java.util.HashMap; import com.carrotsearch.hppc.ObjectIntMap;
import com.carrotsearch.hppc.ObjectIntOpenHashMap;
import com.carrotsearch.hppc.procedures.ObjectIntProcedure;
/** /**
* An implementation of HashMap which can be used to count the occurrence of different keys. * An implementation of a map which can be used to count the occurrence of different keys.
* This contains all methods of HashMap, but is enhanced with the {@link #add(T, Integer) add} and {@link #increment(T) increment} methods.
* *
* @since 1.1 * @since 1.1
* *
* @author Nelson Tavares de Sousa * @author Nelson Tavares de Sousa, Christian Wulf
* *
* @param <T> * @param <T>
* Key type to be count * Key type to be count
*/ */
public final class CountingMap<T> extends HashMap<T, Integer> { public final class CountingMap<T> {
/** private final ObjectIntMap<T> map = new ObjectIntOpenHashMap<T>();
* Generated serialVersionUID
*/
private static final long serialVersionUID = -8036971796701648200L;
/** /**
* Increments the value of key by one. * Increments the value of key by one.
...@@ -42,31 +40,37 @@ public final class CountingMap<T> extends HashMap<T, Integer> { ...@@ -42,31 +40,37 @@ public final class CountingMap<T> extends HashMap<T, Integer> {
* The key which sould be incremented * The key which sould be incremented
*/ */
public void increment(final T key) { public void increment(final T key) {
Integer count = super.get(key); map.addTo(key, 1);
if (null != count) {
count++;
super.put(key, count);
} else {
super.put(key, 1);
}
} }
/** /**
* Adds i to the value of key. * Adds i to the value of key.
* *
* @param key * @param key
* Key which is used to add i. * the key which is used to add i.
* @param i * @param value
* Integer value to be added. * the value to be added.
*/ */
public void add(final T key, final Integer i) { public void add(final T key, final int value) {
if (super.containsKey(key)) { map.addTo(key, value);
Integer j = super.get(key); }
j += i;
super.put(key, j); public void add(final CountingMap<T> otherMap) {
} else { final ObjectIntProcedure<? super T> procedure = new ObjectIntProcedure<T>() {
super.put(key, i); @Override
public void apply(final T key, final int value) {
map.addTo(key, value);
} }
};
otherMap.map.forEach(procedure);
}
public int get(final T key) {
return map.get(key);
}
public int size() {
return map.size();
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment