diff --git a/.settings/edu.umd.cs.findbugs.core.prefs b/.settings/edu.umd.cs.findbugs.core.prefs index 233d5c8cea4349a1b5cc17963d6484ba4084aa22..9327e5ac834f5c14f603ba2eac938978a7c05575 100644 --- a/.settings/edu.umd.cs.findbugs.core.prefs +++ b/.settings/edu.umd.cs.findbugs.core.prefs @@ -1,5 +1,5 @@ #FindBugs User Preferences -#Thu Mar 19 12:32:13 CET 2015 +#Thu Mar 19 17:05:30 CET 2015 detector_threshold=3 effort=max excludefilter0=.fbExcludeFilterFile|true diff --git a/pom.xml b/pom.xml index 312e29c9739b9d174f7b968a7c685dbc5ec6e50e..62f7bfc2234af57dd86cbd0394253d9c485303f3 100644 --- a/pom.xml +++ b/pom.xml @@ -112,7 +112,13 @@ <groupId>org.jctools</groupId> <artifactId>jctools-core</artifactId> <version>1.0</version> -<!-- <version>1.1-SNAPSHOT</version> --> + <!-- <version>1.1-SNAPSHOT</version> --> + </dependency> + + <dependency> + <groupId>com.carrotsearch</groupId> + <artifactId>hppc</artifactId> + <version>0.6.0</version> </dependency> </dependencies> diff --git a/src/main/java/teetime/stage/CountingMapMerger.java b/src/main/java/teetime/stage/CountingMapMerger.java index 22a5bb1b71451a844654e16711e1330f90b97357..06d7b735c56812dbd39df1ba642768cd4d459505 100644 --- a/src/main/java/teetime/stage/CountingMapMerger.java +++ b/src/main/java/teetime/stage/CountingMapMerger.java @@ -15,12 +15,7 @@ */ package teetime.stage; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Set; - import teetime.framework.AbstractConsumerStage; -import teetime.framework.OutputPort; import teetime.stage.util.CountingMap; /** @@ -36,25 +31,15 @@ import teetime.stage.util.CountingMap; */ public final class CountingMapMerger<T> extends AbstractConsumerStage<CountingMap<T>> { - private final CountingMap<T> result = new CountingMap<T>(); - private final OutputPort<Map<T, Integer>> port = createOutputPort(); + private final CountingMap<T> mergedResult = new CountingMap<T>(); @Override protected void execute(final CountingMap<T> element) { - Set<Map.Entry<T, Integer>> entries = element.entrySet(); - for (Entry<T, Integer> entry : entries) { - result.add(entry.getKey(), entry.getValue()); - } - } - - @Override - public void onTerminating() throws Exception { - port.send(result); - super.onTerminating(); + mergedResult.add(element); } public CountingMap<T> getResult() { - return result; + return mergedResult; } } diff --git a/src/main/java/teetime/stage/util/CountingMap.java b/src/main/java/teetime/stage/util/CountingMap.java index 5f491453edc6f3e9824930e77336676e14cd6ccb..52d8ab5a49a5487c17ebdc7cc928b14b21e348b2 100644 --- a/src/main/java/teetime/stage/util/CountingMap.java +++ b/src/main/java/teetime/stage/util/CountingMap.java @@ -15,25 +15,23 @@ */ 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. - * This contains all methods of HashMap, but is enhanced with the {@link #add(T, Integer) add} and {@link #increment(T) increment} methods. + * An implementation of a map which can be used to count the occurrence of different keys. * * @since 1.1 * - * @author Nelson Tavares de Sousa + * @author Nelson Tavares de Sousa, Christian Wulf * * @param <T> * Key type to be count */ -public final class CountingMap<T> extends HashMap<T, Integer> { +public final class CountingMap<T> { - /** - * Generated serialVersionUID - */ - private static final long serialVersionUID = -8036971796701648200L; + private final ObjectIntMap<T> map = new ObjectIntOpenHashMap<T>(); /** * Increments the value of key by one. @@ -42,31 +40,37 @@ public final class CountingMap<T> extends HashMap<T, Integer> { * The key which sould be incremented */ public void increment(final T key) { - Integer count = super.get(key); - if (null != count) { - count++; - super.put(key, count); - } else { - super.put(key, 1); - } + map.addTo(key, 1); } /** * Adds i to the value of key. * * @param key - * Key which is used to add i. - * @param i - * Integer value to be added. + * the key which is used to add i. + * @param value + * the value to be added. */ - public void add(final T key, final Integer i) { - if (super.containsKey(key)) { - Integer j = super.get(key); - j += i; - super.put(key, j); - } else { - super.put(key, i); - } + public void add(final T key, final int value) { + map.addTo(key, value); + } + + public void add(final CountingMap<T> otherMap) { + final ObjectIntProcedure<? super T> procedure = new ObjectIntProcedure<T>() { + @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(); } }